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>
24 namespace mp = metaproxy_1;
25 namespace yf = mp::filter;
26 namespace mp_util = metaproxy_1::util;
29 namespace metaproxy_1 {
31 class XQuery : public Base {
35 void process(metaproxy_1::Package & package) const;
36 void configure(const xmlNode * ptr, bool test_only,
39 void stop(int signo) const;
41 std::map<std::string, std::string> zorba_variables;
42 std::string zorba_filename;
43 std::string zorba_script;
44 std::string zorba_record_variable;
59 void yf::XQuery::start() const
63 void yf::XQuery::stop(int signo) const
67 void yf::XQuery::process(Package &package) const
69 Z_GDU *gdu_req = package.request().get();
70 Z_PresentRequest *pr_req = 0;
71 Z_SearchRequest *sr_req = 0;
73 const char *input_schema = 0;
74 Odr_oid *input_syntax = 0;
76 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
77 gdu_req->u.z3950->which == Z_APDU_presentRequest)
79 pr_req = gdu_req->u.z3950->u.presentRequest;
82 mp_util::record_composition_to_esn(pr_req->recordComposition);
83 input_syntax = pr_req->preferredRecordSyntax;
85 else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
86 gdu_req->u.z3950->which == Z_APDU_searchRequest)
88 sr_req = gdu_req->u.z3950->u.searchRequest;
90 input_syntax = sr_req->preferredRecordSyntax;
92 // we don't know how many hits we're going to get and therefore
93 // the effective element set name.. Therefore we can only allow
94 // two cases.. Both equal or absent.. If not, we'll just have to
95 // disable the piggyback!
96 if (sr_req->smallSetElementSetNames
98 sr_req->mediumSetElementSetNames
100 sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
102 sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
104 !strcmp(sr_req->smallSetElementSetNames->u.generic,
105 sr_req->mediumSetElementSetNames->u.generic))
107 input_schema = sr_req->smallSetElementSetNames->u.generic;
109 else if (!sr_req->smallSetElementSetNames &&
110 !sr_req->mediumSetElementSetNames)
111 ; // input_schema is 0 already
114 // disable piggyback (perhaps it was disabled already)
115 *sr_req->smallSetUpperBound = 0;
116 *sr_req->largeSetLowerBound = 0;
117 *sr_req->mediumSetPresentNumber = 0;
121 // we can handle it in record_transform.
131 void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
134 for (ptr = ptr->children; ptr; ptr = ptr->next)
136 if (ptr->type != XML_ELEMENT_NODE)
138 if (!strcmp((const char *) ptr->name, "setVariable"))
142 struct _xmlAttr *attr;
143 for (attr = ptr->properties; attr; attr = attr->next)
144 if (!strcmp((const char *) attr->name, "name"))
145 name = mp::xml::get_text(attr->children);
146 else if (!strcmp((const char *) attr->name, "value"))
147 value = mp::xml::get_text(attr->children);
149 throw mp::filter::FilterException(
150 "Bad attribute " + std::string((const char *)
152 if (name.length() > 0)
153 zorba_variables[name] = value;
155 else if (!strcmp((const char *) ptr->name, "filename"))
158 struct _xmlAttr *attr;
159 for (attr = ptr->properties; attr; attr = attr->next)
160 if (!strcmp((const char *) attr->name, "value"))
161 value = mp::xml::get_text(attr->children);
163 throw mp::filter::FilterException(
164 "Bad attribute " + std::string((const char *)
166 zorba_filename = value;
168 else if (!strcmp((const char *) ptr->name, "script"))
171 struct _xmlAttr *attr;
172 for (attr = ptr->properties; attr; attr = attr->next)
173 if (!strcmp((const char *) attr->name, "value"))
174 value = mp::xml::get_text(attr->children);
176 throw mp::filter::FilterException(
177 "Bad attribute " + std::string((const char *)
179 zorba_script = value;
181 else if (!strcmp((const char *) ptr->name, "record"))
184 struct _xmlAttr *attr;
185 for (attr = ptr->properties; attr; attr = attr->next)
186 if (!strcmp((const char *) attr->name, "value"))
187 value = mp::xml::get_text(attr->children);
189 throw mp::filter::FilterException(
190 "Bad attribute " + std::string((const char *)
192 zorba_record_variable = value;
196 throw mp::filter::FilterException("Bad element "
197 + std::string((const char *)
201 if (zorba_script.length() == 0)
202 throw mp::filter::FilterException("Missing element script");
203 if (zorba_record_variable.length() == 0)
204 throw mp::filter::FilterException("Missing element record");
205 if (zorba_filename.length() == 0)
206 throw mp::filter::FilterException("Missing element filename");
209 static yf::Base* filter_creator()
211 return new mp::filter::XQuery;
215 struct metaproxy_1_filter_struct metaproxy_1_filter_xquery = {
226 * c-file-style: "Stroustrup"
227 * indent-tabs-mode: nil
229 * vim: shiftwidth=4 tabstop=8 expandtab