1 /* $Id: filter_query_rewrite.cpp,v 1.2 2006-01-20 22:38:12 marc Exp $
2 Copyright (c) 2005, Index Data.
10 #include "package.hpp"
12 //#include <boost/thread/mutex.hpp>
13 #include <boost/regex.hpp>
16 #include "filter_query_rewrite.hpp"
20 namespace yf = yp2::filter;
24 class QueryRewrite::Rep {
25 //friend class QueryRewrite;
27 void process(yp2::Package &package) const;
29 void rewriteRegex(Z_Query *query) const;
34 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
38 yf::QueryRewrite::~QueryRewrite()
39 { // must have a destructor because of boost::scoped_ptr
42 void yf::QueryRewrite::process(yp2::Package &package) const
44 m_p->process(package);
47 void yf::QueryRewrite::Rep::process(yp2::Package &package) const
49 if (package.session().is_closed())
51 std::cout << "Got Close.\n";
54 Z_GDU *gdu = package.request().get();
56 if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
59 std::cout << "Got Z3950 Init PDU\n";
60 //Z_InitRequest *req = gdu->u.z3950->u.initRequest;
61 //package.request() = gdu;
63 else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
66 std::cout << "Got Z3950 Search PDU\n";
67 Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
69 // applying regex query rewriting
70 rewriteRegex(req->query);
72 // fold new query structure into gdu package ..
73 // yp2::util::pqf(odr, gdu->u.z3950, query_out);
74 // question: which odr structure to use in this call ??
75 // memory alignment has to be correct, this is a little tricky ...
76 // I'd rather like to alter the gdu and pack it back using:
77 package.request() = gdu;
79 else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
82 std::cout << "Got Z3950 Scan PDU\n";
83 //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest;
84 //package.request() = gdu;
90 void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
92 std::string query_in = yp2::util::zQueryToString(query);
93 std::cout << "QUERY IN '" << query_in << "'\n";
95 std::string query_out;
99 // make regular expression replacement here
100 std::string expression("@attr 1=4");
101 std::string format("@attr 1=4 @attr 4=3");
102 //std::string expression("the");
103 //std::string format("else");
104 //std::string expression("(<)|(>)|\\r");
105 //std::string format("(?1<)(?2>)");
107 std::cout << "EXPRESSION '" << expression << "'\n";
108 std::cout << "FORMAT '" << format << "'\n";
110 rgx.assign(expression.c_str());
116 // see http://www.boost.org/libs/regex/doc/match_flag_type.html
117 //boost::match_flag_type flags = boost::match_default;
118 // boost::format_default
119 // boost::format_perl
120 // boost::format_literal
122 // boost::format_no_copy
123 // boost::format_first_only
125 boost::match_flag_type flags
126 = boost::match_default | boost::format_all;
128 match = regex_match(query_in, rgx, flags);
129 search = regex_search(query_in, rgx, flags);
130 query_out = boost::regex_replace(query_in, rgx, format, flags);
131 std::cout << "MATCH '" << match << "'\n";
132 std::cout << "SEARCH '" << search << "'\n";
133 std::cout << "QUERY OUT '" << query_out << "'\n";
136 catch(boost::regex_error &e)
138 std::cout << "REGEX Error code=" << e.code()
139 << " position=" << e.position() << "\n";
142 //std::cout << "QUERY OUT '" << query_out << "'\n";
143 // still need to fold this new rpn query string into Z_Query structure...
148 static yp2::filter::Base* filter_creator()
150 return new yp2::filter::QueryRewrite;
154 struct yp2_filter_struct yp2_filter_query_rewrite = {
162 extern struct yp2_filter_struct yp2_filter_query_rewrite;
169 * indent-tabs-mode: nil
170 * c-file-style: "stroustrup"
172 * vim: shiftwidth=4 tabstop=8 expandtab