1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
24 #include "filter_query_rewrite.hpp"
27 #include <yaz/xmlquery.h>
28 #include <yaz/diagbib1.h>
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/transform.h>
33 namespace mp = metaproxy_1;
34 namespace yf = mp::filter;
36 namespace metaproxy_1 {
38 class QueryRewrite::Rep {
42 void process(mp::Package &package) const;
43 void configure(const xmlNode * ptr);
45 xsltStylesheetPtr m_stylesheet;
50 yf::QueryRewrite::Rep::Rep()
55 yf::QueryRewrite::Rep::~Rep()
58 xsltFreeStylesheet(m_stylesheet);
61 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
65 yf::QueryRewrite::~QueryRewrite()
66 { // must have a destructor because of boost::scoped_ptr
69 void yf::QueryRewrite::process(mp::Package &package) const
71 m_p->process(package);
74 void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only,
80 void yf::QueryRewrite::Rep::process(mp::Package &package) const
82 Z_GDU *gdu = package.request().get();
84 if (gdu && gdu->which == Z_GDU_Z3950)
86 Z_APDU *apdu_req = gdu->u.z3950;
87 if (apdu_req->which == Z_APDU_searchRequest)
90 const char *addinfo = 0;
92 Z_SearchRequest *req = apdu_req->u.searchRequest;
94 xmlDocPtr doc_input = 0;
95 yaz_query2xml(req->query, &doc_input);
99 error_code = YAZ_BIB1_MALFORMED_QUERY;
100 addinfo = "converion from Query to XML failed";
106 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
110 error_code = YAZ_BIB1_MALFORMED_QUERY;
111 addinfo = "XSLT transform failed for query";
115 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
116 yaz_xml2query(root_element, &req->query, odr,
117 &error_code, &addinfo);
121 xmlFreeDoc(doc_input);
123 package.request() = gdu;
127 odr.create_searchResponse(apdu_req, error_code, addinfo);
128 package.response() = f_apdu;
136 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
138 for (ptr = ptr->children; ptr; ptr = ptr->next)
140 if (ptr->type != XML_ELEMENT_NODE)
143 if (mp::xml::check_element_mp(ptr, "xslt"))
147 throw mp::filter::FilterException
148 ("Only one xslt element allowed in query_rewrite filter");
151 std::string fname;// = mp::xml::get_text(ptr);
153 for (struct _xmlAttr *attr = ptr->properties;
154 attr; attr = attr->next)
156 mp::xml::check_attribute(attr, "", "stylesheet");
157 fname = mp::xml::get_text(attr);
160 if (0 == fname.size())
161 throw mp::filter::FilterException
162 ("Attribute <xslt stylesheet=\""
164 + "\"> needs XSLT stylesheet path content"
165 + " in query_rewrite filter");
167 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
170 throw mp::filter::FilterException
171 ("Failed to read XSLT stylesheet '"
173 + "' in query_rewrite filter");
178 throw mp::filter::FilterException
180 + std::string((const char *) ptr->name)
181 + " in query_rewrite filter");
186 static mp::filter::Base* filter_creator()
188 return new mp::filter::QueryRewrite;
192 struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
202 * c-file-style: "Stroustrup"
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab