1 /* $Id: filter_query_rewrite.cpp,v 1.5 2006-03-16 09:38:33 adam Exp $
2 Copyright (c) 2005, Index Data.
10 #include "package.hpp"
13 #include "filter_query_rewrite.hpp"
16 #include <yaz/xmlquery.h>
17 #include <yaz/diagbib1.h>
19 #include <libxslt/xsltutils.h>
20 #include <libxslt/transform.h>
22 namespace yf = yp2::filter;
26 class QueryRewrite::Rep {
30 void process(yp2::Package &package) const;
31 void configure(const xmlNode * ptr);
33 xsltStylesheetPtr m_stylesheet;
38 yf::QueryRewrite::Rep::Rep()
43 yf::QueryRewrite::Rep::~Rep()
46 xsltFreeStylesheet(m_stylesheet);
49 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
53 yf::QueryRewrite::~QueryRewrite()
54 { // must have a destructor because of boost::scoped_ptr
57 void yf::QueryRewrite::process(yp2::Package &package) const
59 m_p->process(package);
62 void yp2::filter::QueryRewrite::configure(const xmlNode *ptr)
67 void yf::QueryRewrite::Rep::process(yp2::Package &package) const
69 Z_GDU *gdu = package.request().get();
71 if (gdu && gdu->which == Z_GDU_Z3950)
73 Z_APDU *apdu_req = gdu->u.z3950;
74 if (apdu_req->which == Z_APDU_searchRequest)
77 const char *addinfo = 0;
79 Z_SearchRequest *req = apdu_req->u.searchRequest;
81 xmlDocPtr doc_input = 0;
82 yaz_query2xml(req->query, &doc_input);
86 error_code = YAZ_BIB1_MALFORMED_QUERY;
87 addinfo = "converion from Query to XML failed";
93 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
97 error_code = YAZ_BIB1_MALFORMED_QUERY;
98 addinfo = "XSLT transform failed for query";
102 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
103 yaz_xml2query(root_element, &req->query, odr,
104 &error_code, &addinfo);
108 xmlFreeDoc(doc_input);
110 package.request() = gdu;
114 odr.create_searchResponse(apdu_req, error_code, addinfo);
115 package.response() = f_apdu;
123 void yp2::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
125 for (ptr = ptr->children; ptr; ptr = ptr->next)
127 if (ptr->type != XML_ELEMENT_NODE)
129 if (!strcmp((const char *) ptr->name, "xslt"))
133 throw yp2::filter::FilterException
134 ("Only one xslt element allowed in query_rewrite filter");
137 std::string fname = yp2::xml::get_text(ptr);
138 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
141 throw yp2::filter::FilterException
142 ("Failed to read stylesheet "
144 + " in query_rewrite filter");
149 throw yp2::filter::FilterException
151 + std::string((const char *) ptr->name)
152 + " in query_rewrite filter");
157 static yp2::filter::Base* filter_creator()
159 return new yp2::filter::QueryRewrite;
163 struct yp2_filter_struct yp2_filter_query_rewrite = {
173 * indent-tabs-mode: nil
174 * c-file-style: "stroustrup"
176 * vim: shiftwidth=4 tabstop=8 expandtab