-/* $Id: filter_query_rewrite.cpp,v 1.2 2006-01-20 22:38:12 marc Exp $
+/* $Id: filter_query_rewrite.cpp,v 1.3 2006-01-22 00:05:51 marc Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
//friend class QueryRewrite;
public:
void process(yp2::Package &package) const;
+ void configure(const xmlNode * ptr);
private:
void rewriteRegex(Z_Query *query) const;
};
}
}
+// Class QueryRewrite frowarding to class QueryRewrite::Rep
+
yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
{
}
m_p->process(package);
}
+void yp2::filter::QueryRewrite::configure(const xmlNode *ptr)
+{
+ m_p->configure(ptr);
+}
+
+
+// Class QueryRewrite::Rep implementation
+
void yf::QueryRewrite::Rep::process(yp2::Package &package) const
{
if (package.session().is_closed())
{
- std::cout << "Got Close.\n";
+ //std::cout << "Got Close.\n";
}
Z_GDU *gdu = package.request().get();
if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
Z_APDU_initRequest)
{
- std::cout << "Got Z3950 Init PDU\n";
+ //std::cout << "Got Z3950 Init PDU\n";
//Z_InitRequest *req = gdu->u.z3950->u.initRequest;
//package.request() = gdu;
}
else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
Z_APDU_searchRequest)
{
- std::cout << "Got Z3950 Search PDU\n";
+ //std::cout << "Got Z3950 Search PDU\n";
Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
// applying regex query rewriting
void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
{
std::string query_in = yp2::util::zQueryToString(query);
- std::cout << "QUERY IN '" << query_in << "'\n";
+ //std::cout << "QUERY IN '" << query_in << "'\n";
std::string query_out;
//std::string expression("(<)|(>)|\\r");
//std::string format("(?1<)(?2>)");
- std::cout << "EXPRESSION '" << expression << "'\n";
- std::cout << "FORMAT '" << format << "'\n";
+ //std::cout << "EXPRESSION '" << expression << "'\n";
+ //std::cout << "FORMAT '" << format << "'\n";
rgx.assign(expression.c_str());
match = regex_match(query_in, rgx, flags);
search = regex_search(query_in, rgx, flags);
query_out = boost::regex_replace(query_in, rgx, format, flags);
- std::cout << "MATCH '" << match << "'\n";
- std::cout << "SEARCH '" << search << "'\n";
- std::cout << "QUERY OUT '" << query_out << "'\n";
+ //std::cout << "MATCH '" << match << "'\n";
+ //std::cout << "SEARCH '" << search << "'\n";
+ //std::cout << "QUERY OUT '" << query_out << "'\n";
}
catch(boost::regex_error &e)
+void yp2::filter::QueryRewrite::Rep::configure(const xmlNode *filter)
+{
+
+ //std::cout << "XML node '" << filter->name << "'\n";
+ yp2::xml::check_element_yp2(filter, "filter");
+
+ const xmlNode* regex
+ = yp2::xml::jump_to_children(filter, XML_ELEMENT_NODE);
+
+ while (regex){
+ //std::cout << "XML node '" << regex->name << "'\n";
+ yp2::xml::check_element_yp2(regex, "regex");
+
+ // parsing action
+// const xmlNode* action
+// = yp2::xml::jump_to_children(regex, XML_ATTRIBUTE_NODE);
+// if (action){
+// std::cout << "XML node '" << action->name << "' '";
+// std::cout << yp2::xml::get_text(action) << "'\n";
+// //yp2::xml::check_element_yp2(expression, "expression");
+// }
+
+ // parsing regex expression
+ std::string expr;
+ const xmlNode* expression
+ = yp2::xml::jump_to_children(regex, XML_ELEMENT_NODE);
+ if (expression){
+ yp2::xml::check_element_yp2(expression, "expression");
+ expr = yp2::xml::get_text(expression);
+ //std::cout << "XML node '" << expression->name << "' '";
+ //std::cout << yp2::xml::get_text(expression) << "'\n";
+ }
+
+ // parsing regex format
+ std::string form;
+ const xmlNode* format
+ = yp2::xml::jump_to_next(expression, XML_ELEMENT_NODE);
+ if (format){
+ yp2::xml::check_element_yp2(format, "format");
+ form = yp2::xml::get_text(format);
+ //std::cout << "XML node '" << format->name << "' '";
+ //std::cout << yp2::xml::get_text(format) << "'\n";
+ }
+
+ // adding configuration
+ if (expr.size() && form.size()){
+ //std::cout << "adding regular expression\n";
+ }
+
+ // moving forward to next regex
+ regex = yp2::xml::jump_to_next(regex, XML_ELEMENT_NODE);
+ }
+
+ // done parsing XML config
+
+}
+
static yp2::filter::Base* filter_creator()
{
return new yp2::filter::QueryRewrite;
-/* $Id: test_filter_query_rewrite.cpp,v 1.2 2006-01-20 22:38:12 marc Exp $
+/* $Id: test_filter_query_rewrite.cpp,v 1.3 2006-01-22 00:05:51 marc Exp $
Copyright (c) 2005, Index Data.
%LICENSE%
#include <boost/test/auto_unit_test.hpp>
using namespace boost::unit_test;
+using namespace yp2::util;
class FilterBounceZ3950: public yp2::filter::Base {
public:
if (package.session().is_closed())
{
std::cout << "Got Close.\n";
+ return;
}
Z_GDU *gdu = package.request().get();
std::cout << "Got Z3950 Init PDU\n";
//Z_InitRequest *req = gdu->u.z3950->u.initRequest;
//package.request() = gdu;
+ return;
}
- else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
- Z_APDU_searchRequest)
+ else if (gdu && gdu->which == Z_GDU_Z3950
+ && gdu->u.z3950->which == Z_APDU_searchRequest)
{
std::cout << "Got Z3950 Search PDU\n";
//Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
//package.request() = gdu;
+ return;
}
- else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
- Z_APDU_scanRequest)
+ else if (gdu && gdu->which == Z_GDU_Z3950
+ && gdu->u.z3950->which == Z_APDU_scanRequest)
{
std::cout << "Got Z3950 Scan PDU\n";
//Z_ScanRequest *req = gdu->u.z3950->u.scanRequest;
//package.request() = gdu;
+ return;
}
package.move();
BOOST_CHECK_EQUAL(z_gdu->u.z3950->which, Z_APDU_searchRequest);
// take query out of package again and check rewrite
- //std::string query_changed = yp2::util::apduToPqf(apdu);
std::string query_changed
- = yp2::util::zQueryToString(z_gdu->u.z3950->u.searchRequest->query);
-
- //BOOST_CHECK_EQUAL(query_in, query_expect);
-
- //std::cout << "'" << query_expect << "'\n";
- //std::cout << "'" << query_changed << "'\n";
-
- // need ugly whitespace here ..
- //BOOST_CHECK_EQUAL(query_expect, std::string(" ") + query_changed);
+ = zQueryToString(z_gdu->u.z3950->u.searchRequest->query);
BOOST_CHECK_EQUAL(query_expect, query_changed);
}
}
}
}
+
+BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite3 )
+{
+
+
+ try
+ {
+ yp2::RouterChain router;
+
+
+ std::string xmlconf =
+ "<?xml version='1.0'?>\n"
+ "<filter xmlns='http://indexdata.dk/yp2/config/1'\n"
+ " id='qrw1' type='query_rewrite'>\n"
+ " <regex action='all'>\n"
+ " <expression>@attrset XYZ</expression>\n"
+ " <format>@attrset Bib-1</format>\n"
+ " </regex>\n"
+ " <regex action='search'>\n"
+ " <expression>@attr 1=4</expression>\n"
+ " <format>@attr 1=4 @attr 4=2</format>\n"
+ " </regex>\n"
+ " <regex action='search'>\n"
+ " <expression>fish</expression>\n"
+ " <format>cat</format>\n"
+ " </regex>\n"
+ " <regex action='scan'>\n"
+ " <expression>@attr 1=4</expression>\n"
+ " <format>@attr 1=5 @attr 4=1</format>\n"
+ " </regex>\n"
+ " <regex action='scan'>\n"
+ " <expression>fish</expression>\n"
+ " <format>mouse</format>\n"
+ " </regex>\n"
+ "</filter>\n"
+ ;
+
+ //std::cout << xmlconf << std::endl;
+
+ // reading and parsing XML conf
+ xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), xmlconf.size());
+ BOOST_CHECK(doc);
+ xmlNode *root_element = xmlDocGetRootElement(doc);
+
+ // creating and configuring filter
+ yp2::filter::QueryRewrite f_query_rewrite;
+ f_query_rewrite.configure(root_element);
+
+ // remeber to free XML DOM
+ xmlFreeDoc(doc);
+
+ // add only filter to router
+ router.append(f_query_rewrite);
+
+ // start testing
+ check_query_rewrite_init(router);
+ check_query_rewrite_search(router,
+ "@attrset Bib-1 @attr 1=4 the",
+ "@attrset Bib-1 @attr 1=4 the");
+
+ }
+
+ catch (std::exception &e) {
+ std::cout << e.what() << "\n";
+ BOOST_CHECK (false);
+ }
+
+ catch ( ... ) {
+ BOOST_CHECK (false);
+ }
+}
+
/*
* Local variables:
* c-basic-offset: 4