1 /* $Id: filter_query_rewrite.cpp,v 1.10 2007-01-25 14:05:54 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
10 #include "package.hpp"
13 #include "xmlutil.hpp"
14 #include "filter_query_rewrite.hpp"
17 #include <yaz/xmlquery.h>
18 #include <yaz/diagbib1.h>
20 #include <libxslt/xsltutils.h>
21 #include <libxslt/transform.h>
23 namespace mp = metaproxy_1;
24 namespace yf = mp::filter;
26 namespace metaproxy_1 {
28 class QueryRewrite::Rep {
32 void process(mp::Package &package) const;
33 void configure(const xmlNode * ptr);
35 xsltStylesheetPtr m_stylesheet;
40 yf::QueryRewrite::Rep::Rep()
45 yf::QueryRewrite::Rep::~Rep()
48 xsltFreeStylesheet(m_stylesheet);
51 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
55 yf::QueryRewrite::~QueryRewrite()
56 { // must have a destructor because of boost::scoped_ptr
59 void yf::QueryRewrite::process(mp::Package &package) const
61 m_p->process(package);
64 void mp::filter::QueryRewrite::configure(const xmlNode *ptr)
69 void yf::QueryRewrite::Rep::process(mp::Package &package) const
71 Z_GDU *gdu = package.request().get();
73 if (gdu && gdu->which == Z_GDU_Z3950)
75 Z_APDU *apdu_req = gdu->u.z3950;
76 if (apdu_req->which == Z_APDU_searchRequest)
79 const char *addinfo = 0;
81 Z_SearchRequest *req = apdu_req->u.searchRequest;
83 xmlDocPtr doc_input = 0;
84 yaz_query2xml(req->query, &doc_input);
88 error_code = YAZ_BIB1_MALFORMED_QUERY;
89 addinfo = "converion from Query to XML failed";
95 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
99 error_code = YAZ_BIB1_MALFORMED_QUERY;
100 addinfo = "XSLT transform failed for query";
104 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
105 yaz_xml2query(root_element, &req->query, odr,
106 &error_code, &addinfo);
110 xmlFreeDoc(doc_input);
112 package.request() = gdu;
116 odr.create_searchResponse(apdu_req, error_code, addinfo);
117 package.response() = f_apdu;
125 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
127 for (ptr = ptr->children; ptr; ptr = ptr->next)
129 if (ptr->type != XML_ELEMENT_NODE)
132 if (mp::xml::check_element_mp(ptr, "xslt"))
136 throw mp::filter::FilterException
137 ("Only one xslt element allowed in query_rewrite filter");
140 std::string fname;// = mp::xml::get_text(ptr);
142 for (struct _xmlAttr *attr = ptr->properties;
143 attr; attr = attr->next)
145 mp::xml::check_attribute(attr, "", "stylesheet");
146 fname = mp::xml::get_text(attr);
149 if (0 == fname.size())
150 throw mp::filter::FilterException
151 ("Attribute <xslt stylesheet=\""
153 + "\"> needs XSLT stylesheet path content"
154 + " in query_rewrite filter");
156 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
159 throw mp::filter::FilterException
160 ("Failed to read XSLT stylesheet '"
162 + "' in query_rewrite filter");
167 throw mp::filter::FilterException
169 + std::string((const char *) ptr->name)
170 + " in query_rewrite filter");
175 static mp::filter::Base* filter_creator()
177 return new mp::filter::QueryRewrite;
181 struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
191 * indent-tabs-mode: nil
192 * c-file-style: "stroustrup"
194 * vim: shiftwidth=4 tabstop=8 expandtab