1 /* This file is part of mp-xquery
2 Copyright (C) Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <metaproxy/package.hpp>
20 #include <metaproxy/util.hpp>
22 #include <yaz/oid_db.h>
29 #include <zorba/zorba.h>
30 #include <zorba/store_manager.h>
31 #include <zorba/serializer.h>
32 #include <zorba/singleton_item_sequence.h>
33 #include <zorba/zorba_exception.h>
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
38 namespace mp_util = metaproxy_1::util;
40 using namespace zorba;
42 namespace metaproxy_1 {
44 class XQuery : public Base {
48 void process(metaproxy_1::Package & package) const;
49 void configure(const xmlNode * ptr, bool test_only,
52 void stop(int signo) const;
54 bool convert_one_record(const char *input_buf,
56 std::string &result) const;
57 std::map<std::string, std::string> zorba_variables;
58 std::string zorba_filename;
59 std::string zorba_script;
60 std::string zorba_record_variable;
78 void yf::XQuery::start() const
82 void yf::XQuery::stop(int signo) const
86 bool yf::XQuery::convert_one_record(const char *input_buf,
88 std::string &result) const
90 XQuery_t tQuery = lQuery->clone();
92 zorba::DynamicContext* lDynamicContext = tQuery->getDynamicContext();
95 std::map<std::string, std::string>::const_iterator it;
96 for (it = zorba_variables.begin(); it != zorba_variables.end(); it++)
98 lItem = lZorba->getItemFactory()->createString(it->second);
99 lDynamicContext->setVariable(it->first, lItem);
101 std::string rec_content = "raw:" + std::string(input_buf, input_len);
102 lItem = lZorba->getItemFactory()->createString(rec_content);
103 lDynamicContext->setVariable(zorba_record_variable, lItem);
105 std::stringstream ss;
111 void yf::XQuery::process(Package &package) const
113 Z_GDU *gdu_req = package.request().get();
114 Z_PresentRequest *pr_req = 0;
115 Z_SearchRequest *sr_req = 0;
117 const char *input_schema = 0;
118 Odr_oid *input_syntax = 0;
120 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
121 gdu_req->u.z3950->which == Z_APDU_presentRequest)
123 pr_req = gdu_req->u.z3950->u.presentRequest;
126 mp_util::record_composition_to_esn(pr_req->recordComposition);
127 input_syntax = pr_req->preferredRecordSyntax;
129 else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
130 gdu_req->u.z3950->which == Z_APDU_searchRequest)
132 sr_req = gdu_req->u.z3950->u.searchRequest;
134 input_syntax = sr_req->preferredRecordSyntax;
136 // we don't know how many hits we're going to get and therefore
137 // the effective element set name.. Therefore we can only allow
138 // two cases.. Both equal or absent.. If not, we'll just have to
139 // disable the piggyback!
140 if (sr_req->smallSetElementSetNames
142 sr_req->mediumSetElementSetNames
144 sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
146 sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
148 !strcmp(sr_req->smallSetElementSetNames->u.generic,
149 sr_req->mediumSetElementSetNames->u.generic))
151 input_schema = sr_req->smallSetElementSetNames->u.generic;
153 else if (!sr_req->smallSetElementSetNames &&
154 !sr_req->mediumSetElementSetNames)
155 ; // input_schema is 0 already
158 // disable piggyback (perhaps it was disabled already)
159 *sr_req->smallSetUpperBound = 0;
160 *sr_req->largeSetLowerBound = 0;
161 *sr_req->mediumSetPresentNumber = 0;
165 // we can handle it in record_transform.
173 mp::odr odr_en(ODR_ENCODE);
175 const char *backend_schema = 0;
176 const Odr_oid *backend_syntax = 0;
178 if (input_schema && !strcmp(input_schema, "bibframe") &&
179 (!input_syntax || !oid_oidcmp(input_syntax, yaz_oid_recsyn_xml)))
181 backend_schema = "marcxml";
182 backend_syntax = yaz_oid_recsyn_xml;
193 sr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
195 sr_req->preferredRecordSyntax = 0;
198 sr_req->smallSetElementSetNames
199 = (Z_ElementSetNames *)
200 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
201 sr_req->smallSetElementSetNames->which = Z_ElementSetNames_generic;
202 sr_req->smallSetElementSetNames->u.generic
203 = odr_strdup(odr_en, backend_schema);
204 sr_req->mediumSetElementSetNames = sr_req->smallSetElementSetNames;
208 sr_req->smallSetElementSetNames = 0;
209 sr_req->mediumSetElementSetNames = 0;
215 pr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
217 pr_req->preferredRecordSyntax = 0;
221 pr_req->recordComposition
222 = (Z_RecordComposition *)
223 odr_malloc(odr_en, sizeof(Z_RecordComposition));
224 pr_req->recordComposition->which
225 = Z_RecordComp_simple;
226 pr_req->recordComposition->u.simple
227 = (Z_ElementSetNames *)
228 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
229 pr_req->recordComposition->u.simple->which = Z_ElementSetNames_generic;
230 pr_req->recordComposition->u.simple->u.generic
231 = odr_strdup(odr_en, backend_schema);
234 pr_req->recordComposition = 0;
238 Z_GDU *gdu_res = package.response().get();
240 // see if we have a records list to patch!
241 Z_NamePlusRecordList *records = 0;
242 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
243 gdu_res->u.z3950->which == Z_APDU_presentResponse)
245 Z_PresentResponse * pr_res = gdu_res->u.z3950->u.presentResponse;
248 && pr_res->numberOfRecordsReturned
249 && *(pr_res->numberOfRecordsReturned) > 0
251 && pr_res->records->which == Z_Records_DBOSD)
253 records = pr_res->records->u.databaseOrSurDiagnostics;
256 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
257 gdu_res->u.z3950->which == Z_APDU_searchResponse)
259 Z_SearchResponse *sr_res = gdu_res->u.z3950->u.searchResponse;
262 && sr_res->numberOfRecordsReturned
263 && *(sr_res->numberOfRecordsReturned) > 0
265 && sr_res->records->which == Z_Records_DBOSD)
267 records = sr_res->records->u.databaseOrSurDiagnostics;
273 for (i = 0; i < records->num_records; i++)
275 Z_NamePlusRecord *npr = records->records[i];
276 if (npr->which == Z_NamePlusRecord_databaseRecord)
278 const char *details = 0;
279 Z_External *r = npr->u.databaseRecord;
281 if (r->which == Z_External_octet &&
282 !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
285 if (convert_one_record(
286 r->u.octet_aligned->buf, r->u.octet_aligned->len,
289 npr->u.databaseRecord =
290 z_ext_record_oid(odr_en, yaz_oid_recsyn_xml,
297 package.response() = gdu_res;
301 void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
304 for (ptr = ptr->children; ptr; ptr = ptr->next)
306 if (ptr->type != XML_ELEMENT_NODE)
308 if (!strcmp((const char *) ptr->name, "setVariable"))
312 struct _xmlAttr *attr;
313 for (attr = ptr->properties; attr; attr = attr->next)
314 if (!strcmp((const char *) attr->name, "name"))
315 name = mp::xml::get_text(attr->children);
316 else if (!strcmp((const char *) attr->name, "value"))
317 value = mp::xml::get_text(attr->children);
319 throw mp::filter::FilterException(
320 "Bad attribute " + std::string((const char *)
322 if (name.length() > 0)
323 zorba_variables[name] = value;
325 else if (!strcmp((const char *) ptr->name, "filename"))
328 struct _xmlAttr *attr;
329 for (attr = ptr->properties; attr; attr = attr->next)
330 if (!strcmp((const char *) attr->name, "value"))
331 value = mp::xml::get_text(attr->children);
333 throw mp::filter::FilterException(
334 "Bad attribute " + std::string((const char *)
336 zorba_filename = value;
338 else if (!strcmp((const char *) ptr->name, "script"))
341 struct _xmlAttr *attr;
342 for (attr = ptr->properties; attr; attr = attr->next)
343 if (!strcmp((const char *) attr->name, "value"))
344 value = mp::xml::get_text(attr->children);
346 throw mp::filter::FilterException(
347 "Bad attribute " + std::string((const char *)
349 zorba_script = value;
351 else if (!strcmp((const char *) ptr->name, "record"))
354 struct _xmlAttr *attr;
355 for (attr = ptr->properties; attr; attr = attr->next)
356 if (!strcmp((const char *) attr->name, "value"))
357 value = mp::xml::get_text(attr->children);
359 throw mp::filter::FilterException(
360 "Bad attribute " + std::string((const char *)
362 zorba_record_variable = value;
366 throw mp::filter::FilterException("Bad element "
367 + std::string((const char *)
371 if (zorba_script.length() == 0)
372 throw mp::filter::FilterException("Missing element script");
373 if (zorba_record_variable.length() == 0)
374 throw mp::filter::FilterException("Missing element record");
375 if (zorba_filename.length() == 0)
376 throw mp::filter::FilterException("Missing element filename");
379 void* lStore = StoreManager::getStore();
380 lZorba = Zorba::getInstance(lStore);
382 lQuery = lZorba->createQuery();
384 lQuery->setFileName(zorba_filename);
386 std::unique_ptr<std::istream> qfile;
387 qfile.reset(new std::ifstream(zorba_script.c_str()));
389 Zorba_CompilerHints lHints;
390 lQuery->compile(*qfile, lHints);
394 static yf::Base* filter_creator()
396 return new mp::filter::XQuery;
400 struct metaproxy_1_filter_struct metaproxy_1_filter_xquery = {
411 * c-file-style: "Stroustrup"
412 * indent-tabs-mode: nil
414 * vim: shiftwidth=4 tabstop=8 expandtab