1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include <metaproxy/util.hpp>
22 #include <metaproxy/filter.hpp>
23 #include <metaproxy/package.hpp>
25 #include "filter_cql_to_rpn.hpp"
27 #include <yazpp/z-query.h>
29 #include <yazpp/cql2rpn.h>
31 #include <yaz/diagbib1.h>
35 namespace mp = metaproxy_1;
36 namespace yf = metaproxy_1::filter;
38 namespace metaproxy_1 {
40 class CQLtoRPN::Impl {
44 void process(metaproxy_1::Package & package);
45 void configure(const xmlNode * ptr);
47 yazpp_1::Yaz_cql2rpn m_cql2rpn;
53 // define Pimpl wrapper forwarding to Impl
55 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
59 yf::CQLtoRPN::~CQLtoRPN()
60 { // must have a destructor because of boost::scoped_ptr
63 void yf::CQLtoRPN::configure(const xmlNode *xmlnode, bool test_only,
66 m_p->configure(xmlnode);
69 void yf::CQLtoRPN::process(mp::Package &package) const
71 m_p->process(package);
75 // define Implementation stuff
77 yf::CQLtoRPN::Impl::Impl()
81 yf::CQLtoRPN::Impl::~Impl()
85 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode)
89 <filter type="cql_rpn">
90 <conversion file="pqf.properties"/>
95 for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
97 if (xmlnode->type != XML_ELEMENT_NODE)
99 if (!strcmp((const char *) xmlnode->name, "conversion"))
101 const struct _xmlAttr *attr;
102 for (attr = xmlnode->properties; attr; attr = attr->next)
104 if (!strcmp((const char *) attr->name, "file"))
105 fname = mp::xml::get_text(attr);
107 throw mp::filter::FilterException(
108 "Bad attribute " + std::string((const char *)
114 throw mp::filter::FilterException("Bad element "
115 + std::string((const char *)
119 if (fname.length() == 0)
121 throw mp::filter::FilterException("Missing conversion configuration "
122 "for filter cql_rpn");
126 if (!m_cql2rpn.parse_spec_file(fname.c_str(), &error))
128 throw mp::filter::FilterException("Bad or missing "
129 "CQL to RPN configuration "
134 void yf::CQLtoRPN::Impl::process(mp::Package &package)
136 Z_GDU *gdu = package.request().get();
138 if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
139 Z_APDU_searchRequest)
141 Z_APDU *apdu_req = gdu->u.z3950;
142 Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
143 if (sr->query && sr->query->which == Z_Query_type_104 &&
144 sr->query->u.type_104->which == Z_External_CQL)
147 Z_RPNQuery *rpnquery = 0;
150 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
156 odr.create_searchResponse(
158 YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
159 "cql_rpn: missing CQL to RPN configuration");
160 package.response() = f_apdu;
165 int error_code = yaz_diag_srw_to_bib1(r);
168 odr.create_searchResponse(apdu_req, error_code, addinfo);
169 package.response() = f_apdu;
175 sr->query->which = Z_Query_type_1;
176 sr->query->u.type_1 = rpnquery;
177 package.request() = gdu;
185 static mp::filter::Base* filter_creator()
187 return new mp::filter::CQLtoRPN;
191 struct metaproxy_1_filter_struct metaproxy_1_filter_cql_to_rpn = {
201 * c-file-style: "Stroustrup"
202 * indent-tabs-mode: nil
204 * vim: shiftwidth=4 tabstop=8 expandtab