20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
28 #include <yaz/xmlquery.h>
29 #include <yaz/diagbib1.h>
30 #include <yaz/query-charset.h>
31 #include <yaz/tpath.h>
33 #include <libxslt/xsltutils.h>
34 #include <libxslt/transform.h>
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
39 namespace metaproxy_1 {
45 void process(mp::Package &package)
const;
46 void configure(
const xmlNode * ptr,
bool test_only,
56 yf::QueryRewrite::Rep::Rep() : m_stylesheet(0), charset_from(
"UTF-8")
60 yf::QueryRewrite::Rep::~Rep()
63 xsltFreeStylesheet(m_stylesheet);
77 m_p->configure(ptr, test_only, path);
82 m_p->process(package);
87 Z_GDU *gdu = package.request().get();
89 if (gdu && gdu->which == Z_GDU_Z3950)
91 Z_APDU *apdu_req = gdu->u.z3950;
92 if (apdu_req->which == Z_APDU_searchRequest)
95 const char *addinfo = 0;
97 Z_SearchRequest *req = apdu_req->u.searchRequest;
101 xmlDocPtr doc_input = 0;
102 yaz_query2xml(req->query, &doc_input);
106 error_code = YAZ_BIB1_MALFORMED_QUERY;
107 addinfo =
"converion from Query to XML failed";
111 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
115 error_code = YAZ_BIB1_MALFORMED_QUERY;
116 addinfo =
"XSLT transform failed for query";
120 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
121 yaz_xml2query(root_element, &req->query, odr,
122 &error_code, &addinfo);
125 xmlFreeDoc(doc_input);
128 if (!error_code && charset_to.length() && charset_from.length() &&
129 (req->query->which == Z_Query_type_1
130 || req->query->which == Z_Query_type_101))
132 yaz_iconv_t cd = yaz_iconv_open(charset_to.c_str(),
133 charset_from.c_str());
136 int r = yaz_query_charset_convert_rpnquery_check(
137 req->query->u.type_1, odr, cd);
141 error_code = YAZ_BIB1_MALFORMED_QUERY;
142 addinfo =
"could not convert query to target charset";
149 odr.create_searchResponse(apdu_req, error_code, addinfo);
150 package.response() = f_apdu;
153 package.request() = gdu;
160 bool test_only,
const char *path)
162 for (ptr = ptr->children; ptr; ptr = ptr->next)
164 if (ptr->type != XML_ELEMENT_NODE)
167 if (mp::xml::is_element_mp(ptr,
"xslt"))
171 throw mp::filter::FilterException
172 (
"Only one xslt element allowed in query_rewrite filter");
177 for (
struct _xmlAttr *attr = ptr->properties;
178 attr; attr = attr->next)
180 mp::xml::check_attribute(attr,
"",
"stylesheet");
181 fname = mp::xml::get_text(attr);
184 if (0 == fname.size())
185 throw mp::filter::FilterException
186 (
"Attribute <xslt stylesheet=\""
188 +
"\"> needs XSLT stylesheet path content"
189 +
" in query_rewrite filter");
192 char *cp = yaz_filepath_resolve(fname.c_str(), path, 0, fullpath);
195 throw mp::filter::FilterException(
"Cannot read XSLT " + fname);
198 m_stylesheet = xsltParseStylesheetFile(BAD_CAST cp);
201 throw mp::filter::FilterException
202 (
"Failed to read XSLT stylesheet '"
204 +
"' in query_rewrite filter");
207 else if (mp::xml::is_element_mp(ptr,
"charset"))
209 for (
struct _xmlAttr *attr = ptr->properties;
210 attr; attr = attr->next)
212 if (!strcmp((
const char *) attr->name,
"from"))
214 charset_from = mp::xml::get_text(attr);
216 else if (!strcmp((
const char *) attr->name,
"to"))
218 charset_to = mp::xml::get_text(attr);
221 throw mp::filter::FilterException
222 (
"Invalid attribute inside charset inside "
223 "query_rewrite filter");
228 throw mp::filter::FilterException
230 + std::string((
const char *) ptr->name)
231 +
" in query_rewrite filter");