added XML config file parsing, still need to read and parse XML attributes
authorMarc Cromme <marc@indexdata.dk>
Sun, 22 Jan 2006 00:05:51 +0000 (00:05 +0000)
committerMarc Cromme <marc@indexdata.dk>
Sun, 22 Jan 2006 00:05:51 +0000 (00:05 +0000)
also no action taken yet after XML parsing

src/filter_query_rewrite.cpp
src/filter_query_rewrite.hpp
src/test_filter_query_rewrite.cpp

index cffa7e3..159fbb9 100644 (file)
@@ -1,4 +1,4 @@
-/* $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%
@@ -25,12 +25,15 @@ namespace yp2 {
             //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)
 {
 }
@@ -44,11 +47,19 @@ void yf::QueryRewrite::process(yp2::Package &package) const
     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();
@@ -56,14 +67,14 @@ void yf::QueryRewrite::Rep::process(yp2::Package &package) const
     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
@@ -90,7 +101,7 @@ void yf::QueryRewrite::Rep::process(yp2::Package &package) const
 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;
     
@@ -104,8 +115,8 @@ void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
         //std::string expression("(<)|(>)|\\r");
         //std::string format("(?1&lt;)(?2&gt;)");
 
-        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());
 
@@ -128,9 +139,9 @@ void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
         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)
@@ -145,6 +156,63 @@ void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
 
 
 
+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;
index ac702de..34f5eba 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_query_rewrite.hpp,v 1.1 2006-01-19 12:18:09 marc Exp $
+/* $Id: filter_query_rewrite.hpp,v 1.2 2006-01-22 00:05:51 marc Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -21,6 +21,7 @@ namespace yp2 {
             QueryRewrite();
             ~QueryRewrite();
             void process(yp2::Package & package) const;
+            void configure(const xmlNode * ptr);
         };
     }
 }
index 0fe19a2..069d603 100644 (file)
@@ -1,4 +1,4 @@
-/* $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%
@@ -18,6 +18,7 @@
 #include <boost/test/auto_unit_test.hpp>
 
 using namespace boost::unit_test;
+using namespace yp2::util;
 
 class FilterBounceZ3950: public yp2::filter::Base {
 public:
@@ -26,6 +27,7 @@ public:
         if (package.session().is_closed())
         {
             std::cout << "Got Close.\n";
+            return;
         }
        
         Z_GDU *gdu = package.request().get();
@@ -36,20 +38,23 @@ public:
             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();
@@ -127,17 +132,8 @@ void check_query_rewrite_search(yp2::RouterChain &router,
         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);
     }
 }
@@ -177,6 +173,78 @@ BOOST_AUTO_UNIT_TEST( test_filter_query_rewrite2 )
     }
 }
 
+
+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