1 /* $Id: filter_query_rewrite.cpp,v 1.11 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "package.hpp"
27 #include "xmlutil.hpp"
28 #include "filter_query_rewrite.hpp"
31 #include <yaz/xmlquery.h>
32 #include <yaz/diagbib1.h>
34 #include <libxslt/xsltutils.h>
35 #include <libxslt/transform.h>
37 namespace mp = metaproxy_1;
38 namespace yf = mp::filter;
40 namespace metaproxy_1 {
42 class QueryRewrite::Rep {
46 void process(mp::Package &package) const;
47 void configure(const xmlNode * ptr);
49 xsltStylesheetPtr m_stylesheet;
54 yf::QueryRewrite::Rep::Rep()
59 yf::QueryRewrite::Rep::~Rep()
62 xsltFreeStylesheet(m_stylesheet);
65 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
69 yf::QueryRewrite::~QueryRewrite()
70 { // must have a destructor because of boost::scoped_ptr
73 void yf::QueryRewrite::process(mp::Package &package) const
75 m_p->process(package);
78 void mp::filter::QueryRewrite::configure(const xmlNode *ptr)
83 void yf::QueryRewrite::Rep::process(mp::Package &package) const
85 Z_GDU *gdu = package.request().get();
87 if (gdu && gdu->which == Z_GDU_Z3950)
89 Z_APDU *apdu_req = gdu->u.z3950;
90 if (apdu_req->which == Z_APDU_searchRequest)
93 const char *addinfo = 0;
95 Z_SearchRequest *req = apdu_req->u.searchRequest;
97 xmlDocPtr doc_input = 0;
98 yaz_query2xml(req->query, &doc_input);
102 error_code = YAZ_BIB1_MALFORMED_QUERY;
103 addinfo = "converion from Query to XML failed";
109 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
113 error_code = YAZ_BIB1_MALFORMED_QUERY;
114 addinfo = "XSLT transform failed for query";
118 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
119 yaz_xml2query(root_element, &req->query, odr,
120 &error_code, &addinfo);
124 xmlFreeDoc(doc_input);
126 package.request() = gdu;
130 odr.create_searchResponse(apdu_req, error_code, addinfo);
131 package.response() = f_apdu;
139 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
141 for (ptr = ptr->children; ptr; ptr = ptr->next)
143 if (ptr->type != XML_ELEMENT_NODE)
146 if (mp::xml::check_element_mp(ptr, "xslt"))
150 throw mp::filter::FilterException
151 ("Only one xslt element allowed in query_rewrite filter");
154 std::string fname;// = mp::xml::get_text(ptr);
156 for (struct _xmlAttr *attr = ptr->properties;
157 attr; attr = attr->next)
159 mp::xml::check_attribute(attr, "", "stylesheet");
160 fname = mp::xml::get_text(attr);
163 if (0 == fname.size())
164 throw mp::filter::FilterException
165 ("Attribute <xslt stylesheet=\""
167 + "\"> needs XSLT stylesheet path content"
168 + " in query_rewrite filter");
170 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
173 throw mp::filter::FilterException
174 ("Failed to read XSLT stylesheet '"
176 + "' in query_rewrite filter");
181 throw mp::filter::FilterException
183 + std::string((const char *) ptr->name)
184 + " in query_rewrite filter");
189 static mp::filter::Base* filter_creator()
191 return new mp::filter::QueryRewrite;
195 struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
205 * indent-tabs-mode: nil
206 * c-file-style: "stroustrup"
208 * vim: shiftwidth=4 tabstop=8 expandtab