1 /* $Id: filter_query_rewrite.cpp,v 1.4 2006-03-15 14:55:17 adam Exp $
2 Copyright (c) 2005, Index Data.
10 #include "package.hpp"
12 //#include <boost/thread/mutex.hpp>
13 #include <boost/regex.hpp>
16 #include "filter_query_rewrite.hpp"
19 #include <yaz/xmlquery.h>
20 #include <yaz/diagbib1.h>
22 #include <libxslt/xsltutils.h>
23 #include <libxslt/transform.h>
25 namespace yf = yp2::filter;
29 class QueryRewrite::Rep {
33 void process(yp2::Package &package) const;
34 void configure(const xmlNode * ptr);
36 xsltStylesheetPtr m_stylesheet;
41 yf::QueryRewrite::Rep::Rep()
46 yf::QueryRewrite::Rep::~Rep()
49 xsltFreeStylesheet(m_stylesheet);
52 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
56 yf::QueryRewrite::~QueryRewrite()
57 { // must have a destructor because of boost::scoped_ptr
60 void yf::QueryRewrite::process(yp2::Package &package) const
62 m_p->process(package);
65 void yp2::filter::QueryRewrite::configure(const xmlNode *ptr)
70 void yf::QueryRewrite::Rep::process(yp2::Package &package) const
72 Z_GDU *gdu = package.request().get();
74 if (gdu && gdu->which == Z_GDU_Z3950)
76 Z_APDU *apdu_req = gdu->u.z3950;
77 if (apdu_req->which == Z_APDU_searchRequest)
80 const char *addinfo = 0;
82 Z_SearchRequest *req = apdu_req->u.searchRequest;
84 xmlDocPtr doc_input = 0;
85 yaz_query2xml(req->query, &doc_input);
89 error_code = YAZ_BIB1_MALFORMED_QUERY;
90 addinfo = "converion from Query to XML failed";
96 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
100 error_code = YAZ_BIB1_MALFORMED_QUERY;
101 addinfo = "XSLT transform failed for query";
105 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
106 yaz_xml2query(root_element, &req->query, odr,
107 &error_code, &addinfo);
111 xmlFreeDoc(doc_input);
113 package.request() = gdu;
117 odr.create_searchResponse(apdu_req, error_code, addinfo);
118 package.response() = f_apdu;
126 void yp2::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
128 for (ptr = ptr->children; ptr; ptr = ptr->next)
130 if (ptr->type != XML_ELEMENT_NODE)
132 if (!strcmp((const char *) ptr->name, "xslt"))
136 throw yp2::filter::FilterException
137 ("Only one xslt element allowed in query_rewrite filter");
140 std::string fname = yp2::xml::get_text(ptr);
141 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
144 throw yp2::filter::FilterException
145 ("Failed to read stylesheet "
147 + " in query_rewrite filter");
152 throw yp2::filter::FilterException
154 + std::string((const char *) ptr->name)
155 + " in query_rewrite filter");
160 static yp2::filter::Base* filter_creator()
162 return new yp2::filter::QueryRewrite;
166 struct yp2_filter_struct yp2_filter_query_rewrite = {
176 * indent-tabs-mode: nil
177 * c-file-style: "stroustrup"
179 * vim: shiftwidth=4 tabstop=8 expandtab