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)
79 void yf::QueryRewrite::Rep::process(mp::Package &package) const
81 Z_GDU *gdu = package.request().get();
83 if (gdu && gdu->which == Z_GDU_Z3950)
85 Z_APDU *apdu_req = gdu->u.z3950;
86 if (apdu_req->which == Z_APDU_searchRequest)
89 const char *addinfo = 0;
91 Z_SearchRequest *req = apdu_req->u.searchRequest;
93 xmlDocPtr doc_input = 0;
94 yaz_query2xml(req->query, &doc_input);
98 error_code = YAZ_BIB1_MALFORMED_QUERY;
99 addinfo = "converion from Query to XML failed";
105 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
109 error_code = YAZ_BIB1_MALFORMED_QUERY;
110 addinfo = "XSLT transform failed for query";
114 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
115 yaz_xml2query(root_element, &req->query, odr,
116 &error_code, &addinfo);
120 xmlFreeDoc(doc_input);
122 package.request() = gdu;
126 odr.create_searchResponse(apdu_req, error_code, addinfo);
127 package.response() = f_apdu;
135 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
137 for (ptr = ptr->children; ptr; ptr = ptr->next)
139 if (ptr->type != XML_ELEMENT_NODE)
142 if (mp::xml::check_element_mp(ptr, "xslt"))
146 throw mp::filter::FilterException
147 ("Only one xslt element allowed in query_rewrite filter");
150 std::string fname;// = mp::xml::get_text(ptr);
152 for (struct _xmlAttr *attr = ptr->properties;
153 attr; attr = attr->next)
155 mp::xml::check_attribute(attr, "", "stylesheet");
156 fname = mp::xml::get_text(attr);
159 if (0 == fname.size())
160 throw mp::filter::FilterException
161 ("Attribute <xslt stylesheet=\""
163 + "\"> needs XSLT stylesheet path content"
164 + " in query_rewrite filter");
166 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
169 throw mp::filter::FilterException
170 ("Failed to read XSLT stylesheet '"
172 + "' in query_rewrite filter");
177 throw mp::filter::FilterException
179 + std::string((const char *) ptr->name)
180 + " in query_rewrite filter");
185 static mp::filter::Base* filter_creator()
187 return new mp::filter::QueryRewrite;
191 struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
201 * c-file-style: "Stroustrup"
202 * indent-tabs-mode: nil
204 * vim: shiftwidth=4 tabstop=8 expandtab