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);
106 std::stringstream ss;
110 } catch ( ZorbaException &e) {
111 std::string msg = e.what();
112 yaz_log(YLOG_WARN, "XQuery execute failure: %s", msg.c_str());
117 void yf::XQuery::process(Package &package) const
119 Z_GDU *gdu_req = package.request().get();
120 Z_PresentRequest *pr_req = 0;
121 Z_SearchRequest *sr_req = 0;
123 const char *input_schema = 0;
124 Odr_oid *input_syntax = 0;
126 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
127 gdu_req->u.z3950->which == Z_APDU_presentRequest)
129 pr_req = gdu_req->u.z3950->u.presentRequest;
132 mp_util::record_composition_to_esn(pr_req->recordComposition);
133 input_syntax = pr_req->preferredRecordSyntax;
135 else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
136 gdu_req->u.z3950->which == Z_APDU_searchRequest)
138 sr_req = gdu_req->u.z3950->u.searchRequest;
140 input_syntax = sr_req->preferredRecordSyntax;
142 // we don't know how many hits we're going to get and therefore
143 // the effective element set name.. Therefore we can only allow
144 // two cases.. Both equal or absent.. If not, we'll just have to
145 // disable the piggyback!
146 if (sr_req->smallSetElementSetNames
148 sr_req->mediumSetElementSetNames
150 sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
152 sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
154 !strcmp(sr_req->smallSetElementSetNames->u.generic,
155 sr_req->mediumSetElementSetNames->u.generic))
157 input_schema = sr_req->smallSetElementSetNames->u.generic;
159 else if (!sr_req->smallSetElementSetNames &&
160 !sr_req->mediumSetElementSetNames)
161 ; // input_schema is 0 already
164 // disable piggyback (perhaps it was disabled already)
165 *sr_req->smallSetUpperBound = 0;
166 *sr_req->largeSetLowerBound = 0;
167 *sr_req->mediumSetPresentNumber = 0;
171 // we can handle it in record_transform.
179 mp::odr odr_en(ODR_ENCODE);
181 const char *backend_schema = 0;
182 const Odr_oid *backend_syntax = 0;
184 if (input_schema && !strcmp(input_schema, "bibframe") &&
185 (!input_syntax || !oid_oidcmp(input_syntax, yaz_oid_recsyn_xml)))
187 backend_schema = "marcxml";
188 backend_syntax = yaz_oid_recsyn_xml;
199 sr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
201 sr_req->preferredRecordSyntax = 0;
204 sr_req->smallSetElementSetNames
205 = (Z_ElementSetNames *)
206 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
207 sr_req->smallSetElementSetNames->which = Z_ElementSetNames_generic;
208 sr_req->smallSetElementSetNames->u.generic
209 = odr_strdup(odr_en, backend_schema);
210 sr_req->mediumSetElementSetNames = sr_req->smallSetElementSetNames;
214 sr_req->smallSetElementSetNames = 0;
215 sr_req->mediumSetElementSetNames = 0;
221 pr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
223 pr_req->preferredRecordSyntax = 0;
227 pr_req->recordComposition
228 = (Z_RecordComposition *)
229 odr_malloc(odr_en, sizeof(Z_RecordComposition));
230 pr_req->recordComposition->which
231 = Z_RecordComp_simple;
232 pr_req->recordComposition->u.simple
233 = (Z_ElementSetNames *)
234 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
235 pr_req->recordComposition->u.simple->which = Z_ElementSetNames_generic;
236 pr_req->recordComposition->u.simple->u.generic
237 = odr_strdup(odr_en, backend_schema);
240 pr_req->recordComposition = 0;
244 Z_GDU *gdu_res = package.response().get();
246 // see if we have a records list to patch!
247 Z_NamePlusRecordList *records = 0;
248 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
249 gdu_res->u.z3950->which == Z_APDU_presentResponse)
251 Z_PresentResponse * pr_res = gdu_res->u.z3950->u.presentResponse;
254 && pr_res->numberOfRecordsReturned
255 && *(pr_res->numberOfRecordsReturned) > 0
257 && pr_res->records->which == Z_Records_DBOSD)
259 records = pr_res->records->u.databaseOrSurDiagnostics;
262 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
263 gdu_res->u.z3950->which == Z_APDU_searchResponse)
265 Z_SearchResponse *sr_res = gdu_res->u.z3950->u.searchResponse;
268 && sr_res->numberOfRecordsReturned
269 && *(sr_res->numberOfRecordsReturned) > 0
271 && sr_res->records->which == Z_Records_DBOSD)
273 records = sr_res->records->u.databaseOrSurDiagnostics;
279 for (i = 0; i < records->num_records; i++)
281 Z_NamePlusRecord *npr = records->records[i];
282 if (npr->which == Z_NamePlusRecord_databaseRecord)
284 const char *details = 0;
285 Z_External *r = npr->u.databaseRecord;
287 if (r->which == Z_External_octet &&
288 !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
291 if (convert_one_record(
292 r->u.octet_aligned->buf, r->u.octet_aligned->len,
295 npr->u.databaseRecord =
296 z_ext_record_oid(odr_en, yaz_oid_recsyn_xml,
303 package.response() = gdu_res;
307 void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
310 for (ptr = ptr->children; ptr; ptr = ptr->next)
312 if (ptr->type != XML_ELEMENT_NODE)
314 if (!strcmp((const char *) ptr->name, "setVariable"))
318 struct _xmlAttr *attr;
319 for (attr = ptr->properties; attr; attr = attr->next)
320 if (!strcmp((const char *) attr->name, "name"))
321 name = mp::xml::get_text(attr->children);
322 else if (!strcmp((const char *) attr->name, "value"))
323 value = mp::xml::get_text(attr->children);
325 throw mp::filter::FilterException(
326 "Bad attribute " + std::string((const char *)
328 if (name.length() > 0)
329 zorba_variables[name] = value;
331 else if (!strcmp((const char *) ptr->name, "script"))
334 struct _xmlAttr *attr;
335 for (attr = ptr->properties; attr; attr = attr->next)
336 if (!strcmp((const char *) attr->name, "value"))
337 value = mp::xml::get_text(attr->children);
339 throw mp::filter::FilterException(
340 "Bad attribute " + std::string((const char *)
342 zorba_script = value;
344 else if (!strcmp((const char *) ptr->name, "record"))
347 struct _xmlAttr *attr;
348 for (attr = ptr->properties; attr; attr = attr->next)
349 if (!strcmp((const char *) attr->name, "value"))
350 value = mp::xml::get_text(attr->children);
352 throw mp::filter::FilterException(
353 "Bad attribute " + std::string((const char *)
355 zorba_record_variable = value;
359 throw mp::filter::FilterException("Bad element "
360 + std::string((const char *)
364 if (zorba_script.length() == 0)
365 throw mp::filter::FilterException("Missing element script");
366 if (zorba_record_variable.length() == 0)
367 throw mp::filter::FilterException("Missing element record");
370 void* lStore = StoreManager::getStore();
371 lZorba = Zorba::getInstance(lStore);
373 lQuery = lZorba->createQuery();
376 size_t t = zorba_script.find_last_of('/');
377 if (t != std::string::npos)
378 lQuery->setFileName(zorba_script.substr(0, t + 1));
379 std::unique_ptr<std::istream> qfile(
380 new std::ifstream(zorba_script.c_str()));
381 Zorba_CompilerHints lHints;
382 lQuery->compile(*qfile, lHints);
383 } catch ( ZorbaException &e) {
384 std::string msg = "XQuery compile failure: ";
386 throw mp::filter::FilterException(msg);
391 static yf::Base* filter_creator()
393 return new mp::filter::XQuery;
397 struct metaproxy_1_filter_struct metaproxy_1_filter_xquery = {
408 * c-file-style: "Stroustrup"
409 * indent-tabs-mode: nil
411 * vim: shiftwidth=4 tabstop=8 expandtab