throwing out 9 unused header files and many lines of out-commented
[metaproxy-moved-to-github.git] / src / filter_cql_to_rpn.cpp
1 /* $Id: filter_cql_to_rpn.cpp,v 1.4 2007-01-16 08:49:05 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8 #include "util.hpp"
9
10 #include "filter.hpp"
11 #include "package.hpp"
12
13 #include "filter_cql_to_rpn.hpp"
14
15
16 #include <yaz/log.h>
17 #include <yaz/cql.h>
18 #include <yaz/zgdu.h>
19 #include <yaz/otherinfo.h>
20 #include <yaz/diagbib1.h>
21 #include <yaz/srw.h>
22 #include <yazpp/z-query.h>
23 #include <yazpp/cql2rpn.h>
24
25
26 namespace mp = metaproxy_1;
27 namespace yf = metaproxy_1::filter;
28
29 namespace metaproxy_1 {
30     namespace filter {
31         class CQLtoRPN::Impl {
32         public:
33             Impl();
34             ~Impl();
35             void process(metaproxy_1::Package & package);
36             void configure(const xmlNode * ptr);
37         private:
38             yazpp_1::Yaz_cql2rpn m_cql2rpn;
39         };
40     }
41 }
42
43
44 // define Pimpl wrapper forwarding to Impl
45  
46 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
47 {
48 }
49
50 yf::CQLtoRPN::~CQLtoRPN()
51 {  // must have a destructor because of boost::scoped_ptr
52 }
53
54 void yf::CQLtoRPN::configure(const xmlNode *xmlnode)
55 {
56     m_p->configure(xmlnode);
57 }
58
59 void yf::CQLtoRPN::process(mp::Package &package) const
60 {
61     m_p->process(package);
62 }
63
64
65 // define Implementation stuff
66
67 yf::CQLtoRPN::Impl::Impl()
68 {
69 }
70
71 yf::CQLtoRPN::Impl::~Impl()
72
73 }
74
75 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode)
76 {
77
78     /*
79       <filter type="cql_rpn">
80       <conversion file="pqf.properties"/>
81       </filter>
82     */
83     
84     std::string fname;
85     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
86     {
87         if (xmlnode->type != XML_ELEMENT_NODE)
88             continue;
89         if (!strcmp((const char *) xmlnode->name, "conversion"))
90         {
91             const struct _xmlAttr *attr;
92             for (attr = xmlnode->properties; attr; attr = attr->next)
93             {
94                 if (!strcmp((const char *) attr->name, "file"))
95                     fname = mp::xml::get_text(attr);
96                 else
97                     throw mp::filter::FilterException(
98                         "Bad attribute " + std::string((const char *)
99                                                        attr->name));
100             }
101         }
102         else
103         {
104             throw mp::filter::FilterException("Bad element " 
105                                                + std::string((const char *)
106                                                              xmlnode->name));
107         }
108     }
109     if (fname.length() == 0)
110     {
111         throw mp::filter::FilterException("Missing conversion spec for "
112                                           "filter cql_rpn");
113     }
114
115     int error = 0;
116     if (!m_cql2rpn.parse_spec_file(fname.c_str(), &error))
117     {
118         throw mp::filter::FilterException("Bad or missing CQL to RPN spec "
119                                           + fname);
120     }
121 }
122
123 void yf::CQLtoRPN::Impl::process(mp::Package &package)
124 {
125     Z_GDU *gdu = package.request().get();
126
127     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
128         Z_APDU_searchRequest)
129     {
130         Z_APDU *apdu_req = gdu->u.z3950;
131         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
132         if (sr->query && sr->query->which == Z_Query_type_104 &&
133             sr->query->u.type_104->which == Z_External_CQL)
134         {
135             char *addinfo = 0;
136             Z_RPNQuery *rpnquery = 0;
137             mp::odr odr;
138             
139             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
140                                                  &rpnquery, odr,
141                                                  &addinfo);
142             if (r == -3)
143             {
144                 yaz_log(YLOG_LOG, "No CQL to RPN table");
145                 Z_APDU *f_apdu = 
146                     odr.create_searchResponse(
147                         apdu_req, 
148                         YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
149                         "Missing CQL to RPN spec");
150                 package.response() = f_apdu;
151                 return;
152             }
153             else if (r)
154             {
155                 int error_code = yaz_diag_srw_to_bib1(r);
156
157                 yaz_log(YLOG_LOG, "CQL Conversion error %d", r);
158                 Z_APDU *f_apdu = 
159                     odr.create_searchResponse(apdu_req, error_code, addinfo);
160                 package.response() = f_apdu;
161                 return;
162             }
163             else
164             {   // conversion OK
165                 
166                 sr->query->which = Z_Query_type_1;
167                 sr->query->u.type_1 = rpnquery;
168                 package.request() = gdu;
169             }
170         }
171     }
172     package.move();
173 }
174
175
176 static mp::filter::Base* filter_creator()
177 {
178     return new mp::filter::CQLtoRPN;
179 }
180
181 extern "C" {
182     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_to_rpn = {
183         0,
184         "cql_rpn",
185         filter_creator
186     };
187 }
188
189 /*
190  * Local variables:
191  * c-basic-offset: 4
192  * indent-tabs-mode: nil
193  * c-file-style: "stroustrup"
194  * End:
195  * vim: shiftwidth=4 tabstop=8 expandtab
196  */