*** empty log message ***
authorAdam Dickmeiss <adam@indexdata.dk>
Wed, 15 Mar 2006 14:55:17 +0000 (14:55 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Wed, 15 Mar 2006 14:55:17 +0000 (14:55 +0000)
configure.ac
etc/config5.xml [new file with mode: 0644]
etc/pqf2pqf.xsl [new file with mode: 0644]
src/factory_static.cpp
src/filter_query_rewrite.cpp
src/filter_query_rewrite.hpp

index 8a2173a..9d5223d 100644 (file)
@@ -110,10 +110,7 @@ if test "$xsltdir" != "no"; then
                 [Define to 1 if you have libxslt installed])
        else
                AC_MSG_RESULT(Not found)
-                if test "$xsltdir" = "default"; then
-                        AC_MSG_WARN([Libxslt development libraries not found.])
-                else
-                        AC_MSG_ERROR([libxslt development libraries not found.])                fi
+               AC_MSG_ERROR([libxslt required.])
        fi
 fi
 
diff --git a/etc/config5.xml b/etc/config5.xml
new file mode 100644 (file)
index 0000000..19a3f97
--- /dev/null
@@ -0,0 +1,26 @@
+<?xml version="1.0"?>
+<!-- $Id: config5.xml,v 1.1 2006-03-15 14:55:17 adam Exp $ -->
+<yp2 xmlns="http://indexdata.dk/yp2/config/1">
+  <start route="start"/>
+  <filters>
+    <filter id="frontend" type="frontend_net">
+      <port>@:9000</port>
+    </filter>
+  </filters>
+  <routes>  
+    <route id="start">
+      <filter refid="frontend"/>
+      <filter type="log">
+        <message>F</message>
+      </filter>
+      <filter type="query-rewrite">
+       <xslt>pqf2pqf.xsl</xslt>
+      </filter>
+      <filter type="log">
+        <message>B</message>
+      </filter>
+      <filter type="backend_test"/>
+    </route>
+  </routes>
+</yp2>
+
diff --git a/etc/pqf2pqf.xsl b/etc/pqf2pqf.xsl
new file mode 100644 (file)
index 0000000..021117b
--- /dev/null
@@ -0,0 +1,183 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+  version="1.0">
+
+  <xsl:output indent="yes" method="xml" version="1.0" encoding="UTF-8"/>
+
+  <!--
+./yaz-xmlquery -p '@and @attr 1=1016 @attr 4=2 @attr 6=3 the @attr 1=4 fish' > test.xml && xmllint -format test.xml && ./yaz-xmlquery -x test1.xml && xsltproc pqf2pqf.xsl test.xml |tee test2.xml && ./yaz-xmlquery -x test2.xml 
+
+./yaz-xmlquery -p '@not @attr 1=1016 @attr 4=2 @attr 6=3 @attr 7=1 @attr 8=4 fish @attr 1=4 fish' > test.xml && xmllint -format test.xml && ./yaz-xmlquery -x test.xml && xsltproc pqf2pqf.xsl test.xml |tee test2.xml && ./yaz-xmlquery -x test2.xml 
+  -->
+
+  <!-- disable default templates -->
+  <xsl:template match="text()"/>
+  <xsl:template match="node()"/>
+
+  <!-- identity stylesheet templates -->
+  <!-- these parse pqf-xml input recursively and make identity operations -->
+  <xsl:template match="/query">
+    <query>
+      <xsl:apply-templates/>
+    </query>
+  </xsl:template>
+
+  <xsl:template match="rpn">
+    <rpn>
+      <xsl:attribute name="set">
+        <xsl:value-of  select="@set"/>
+      </xsl:attribute>
+      <xsl:apply-templates/>
+    </rpn>
+  </xsl:template>
+
+  <xsl:template match="operator">
+    <operator>
+      <xsl:attribute name="type">
+        <xsl:value-of  select="@type"/>
+      </xsl:attribute>
+      <xsl:apply-templates/>
+    </operator>
+  </xsl:template>
+
+  <xsl:template match="apt">
+    <apt>
+      <!-- no re-ordering @attr's if you use the following -->
+      <!--
+      <xsl:apply-templates select="attr"/>
+      -->
+      <xsl:apply-templates select="attr[@type=1]"/>
+      <xsl:apply-templates select="attr[@type=2]"/>
+      <xsl:apply-templates select="attr[@type=4]"/>
+      <xsl:apply-templates select="attr[@type=5]"/>
+      <xsl:apply-templates select="attr[@type=6]"/>
+      <xsl:apply-templates select="attr[@type=7]"/>
+      <xsl:apply-templates select="attr[@type=8]"/>
+      <xsl:apply-templates select="attr[@type=9]"/>
+      <xsl:apply-templates select="term"/>
+    </apt>
+  </xsl:template>
+
+  <xsl:template match="attr">
+    <xsl:copy-of select="."/>
+  </xsl:template>
+
+  <xsl:template match="term">
+    <xsl:copy-of select="."/>
+  </xsl:template>
+
+
+  <!-- special rewrite templates
+       these are kicking in when special conditions apply -->
+
+
+  <!-- attribute rewrites --> 
+
+  <!-- remove all @attr 6=3 with bracket syntax -->
+  <!--
+  <xsl:template match="attr[@type=6][@value=3]">
+  </xsl:template>
+  -->
+
+  <!-- remove all @attr 6=4 with and syntax -->
+  <!--
+  <xsl:template match="attr[@type=6 and @value=4]">
+  </xsl:template>
+  -->
+
+  <!-- rewrite all @attr 4=2 to @attr 4=1 -->
+  <!--
+  <xsl:template match="attr[@type=4][@value=2]">
+    <attr type="4" value="1"/>
+  </xsl:template>
+  -->
+
+  <!-- rewrite all @attr 1=1016 to @attr 1=1016 @attr 6=2 -->
+  <!-- this will leave double @attr 6=? nodes, unless you remove all
+       @attr 6=? nodes in some other template -->
+  <!--
+  <xsl:template match="attr[@type=1 and @value=1016]">
+    <attr type="1" value="1016"/>
+    <attr type="6" value="2"/>
+  </xsl:template>
+  -->
+
+
+  <!-- rules depending on multiple attribute combinations -->
+  
+  <!-- whenever there is a <apt> containing an @attr 7 and an @attr 8,
+       rewrite these and drop all @attr 3 .
+       Notice that the selection rules can equally either be written 
+       'attr/@type=7' or 'attr[@type=8]' with no difference -->
+  <!--
+  <xsl:template match="apt[attr/@type=7 and attr[@type=8]]">
+    <apt>
+      <xsl:apply-templates select="attr[@type=1]"/>
+      <xsl:apply-templates select="attr[@type=2]"/>
+      <xsl:apply-templates select="attr[@type=4]"/>
+      <xsl:apply-templates select="attr[@type=5]"/>
+      <xsl:apply-templates select="attr[@type=6]"/>
+      <attr type="7" value="2"/>
+      <attr type="8" value="5"/>
+      <xsl:apply-templates select="attr[@type=9]"/>
+      <xsl:apply-templates select="term"/>
+    </apt>
+  </xsl:template>
+  -->
+
+  <!-- whenever there is an apt containing an @attr 7=1, an @attr 8=4, and
+       an @attr 1=? (of any value), let @attr 1=? pass unaltered, drop
+       @attr 3=? totally, and rewrite @attr 7=1 and @attr 8=4 .
+       Notice that this rule can equally be written either with 'and' 
+       connecting the attribute type and value, or with a double '[]'.-->
+  <!--
+  <xsl:template match="apt[attr[@type=7 and @value=1] 
+                       and attr[@type=8][@value=4] 
+                       and attr[@type=1]] ">
+    <apt>
+      <xsl:apply-templates select="attr[@type=1]"/>
+      <xsl:apply-templates select="attr[@type=2]"/>
+      <xsl:apply-templates select="attr[@type=4]"/>
+      <xsl:apply-templates select="attr[@type=5]"/>
+      <xsl:apply-templates select="attr[@type=6]"/>
+      <attr type="7" value="2"/>
+      <attr type="8" value="5"/>
+      <xsl:apply-templates select="attr[@type=9]"/>
+      <xsl:apply-templates select="term"/>
+   </apt>
+  </xsl:template>
+  -->
+
+
+  <!-- term rewrites -->
+
+  <!-- rewrite general term fish to squid -->
+  <!--
+  <xsl:template match="term[@type='general'][text()='fish']">
+    <term type="general">squid</term>
+  </xsl:template>
+  -->
+
+  <!-- operator rewrites -->
+
+  <!-- remove 'not' operator, use first <apt> only -->
+  <!-- 
+  <xsl:template match="operator[@type='not']">
+    <xsl:apply-templates select="apt[1]"/>
+  </xsl:template>
+  -->
+
+  <!-- nasty rewrite 'not' operator to 'and' operator -->
+  <!--
+  <xsl:template match="operator[@type='not']">
+    <operator>
+      <xsl:attribute name="type">
+        <xsl:value-of  select="'and'"/>
+      </xsl:attribute>
+      <xsl:apply-templates/>
+    </operator>
+  </xsl:template>
+  -->
+
+</xsl:stylesheet>
+
+
index 41bd521..b4e0aed 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: factory_static.cpp,v 1.7 2006-01-19 21:43:51 adam Exp $
+/* $Id: factory_static.cpp,v 1.8 2006-03-15 14:55:17 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -21,6 +21,7 @@
 #include "filter_http_file.hpp"
 #include "filter_log.hpp"
 #include "filter_multi.hpp"
+#include "filter_query_rewrite.hpp"
 #include "filter_session_shared.hpp"
 #include "filter_template.hpp"
 #include "filter_virt_db.hpp"
@@ -35,6 +36,7 @@ yp2::FactoryStatic::FactoryStatic()
         &yp2_filter_http_file,
         &yp2_filter_log,
         &yp2_filter_multi,
+        &yp2_filter_query_rewrite,
         &yp2_filter_session_shared,
         &yp2_filter_template,
         &yp2_filter_virt_db,
index 159fbb9..b030fac 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_query_rewrite.cpp,v 1.3 2006-01-22 00:05:51 marc Exp $
+/* $Id: filter_query_rewrite.cpp,v 1.4 2006-03-15 14:55:17 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
 #include "filter_query_rewrite.hpp"
 
 #include <yaz/zgdu.h>
+#include <yaz/xmlquery.h>
+#include <yaz/diagbib1.h>
+
+#include <libxslt/xsltutils.h>
+#include <libxslt/transform.h>
 
 namespace yf = yp2::filter;
 
 namespace yp2 {
     namespace filter {
         class QueryRewrite::Rep {
-            //friend class QueryRewrite;
         public:
+            Rep();
+            ~Rep();
             void process(yp2::Package &package) const;
             void configure(const xmlNode * ptr);
         private:
-            void rewriteRegex(Z_Query *query) const;
+            xsltStylesheetPtr m_stylesheet;
         };
     }
 }
 
-// Class QueryRewrite frowarding to class QueryRewrite::Rep
+yf::QueryRewrite::Rep::Rep()
+{
+    m_stylesheet = 0;
+}
+
+yf::QueryRewrite::Rep::~Rep()
+{
+    if (m_stylesheet)
+        xsltFreeStylesheet(m_stylesheet);
+}
 
 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
 {
@@ -52,165 +67,94 @@ 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";
-    }
-    
     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";         
-        //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)
+    if (gdu && gdu->which == Z_GDU_Z3950)
     {
-        //std::cout << "Got Z3950 Search PDU\n";   
-        Z_SearchRequest *req = gdu->u.z3950->u.searchRequest;
-
-        // applying regex query rewriting
-        rewriteRegex(req->query);
+        Z_APDU *apdu_req = gdu->u.z3950;
+        if (apdu_req->which == Z_APDU_searchRequest)
+        {
+            int error_code = 0;
+            const char *addinfo = 0;
+            yp2::odr odr;
+            Z_SearchRequest *req = apdu_req->u.searchRequest;
             
-        // fold new query structure into gdu package ..       
-        // yp2::util::pqf(odr, gdu->u.z3950, query_out);
-        // question: which odr structure to use in this call ??
-        // memory alignment has to be correct, this is a little tricky ...
-        // I'd rather like to alter the gdu and pack it back using:
-        package.request() = gdu;
-    } 
-    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;
-    } 
+            xmlDocPtr doc_input = 0;
+            yaz_query2xml(req->query, &doc_input);
+            
+            if (!doc_input)
+            {
+                error_code = YAZ_BIB1_MALFORMED_QUERY;
+                addinfo = "converion from Query to XML failed";
+            }
+            else
+            {
+                if (m_stylesheet)
+                {
+                    xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
+                                                            doc_input, 0);
+                    if (!doc_res)
+                    {
+                        error_code = YAZ_BIB1_MALFORMED_QUERY;
+                        addinfo = "XSLT transform failed for query";
+                    }
+                    else
+                    {
+                        const xmlNode *root_element = xmlDocGetRootElement(doc_res);
+                        yaz_xml2query(root_element, &req->query, odr,
+                                      &error_code, &addinfo);
+                        xmlFreeDoc(doc_res);
+                    }
+                }
+                xmlFreeDoc(doc_input);
+            }
+            package.request() = gdu;
+            if (error_code)
+            {
+                Z_APDU *f_apdu = 
+                    odr.create_searchResponse(apdu_req, error_code, addinfo);
+                package.response() = f_apdu;
+                return;
+            }
+        } 
+    }
     package.move();
 }
 
-
-void yf::QueryRewrite::Rep::rewriteRegex(Z_Query *query) const
+void yp2::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
 {
-    std::string query_in = yp2::util::zQueryToString(query);
-    //std::cout << "QUERY IN  '" << query_in << "'\n";
-
-    std::string query_out;
-    
-    boost::regex rgx;
-    try{
-        // make regular expression replacement here 
-        std::string expression("@attr 1=4");
-        std::string format("@attr 1=4 @attr 4=3");
-        //std::string expression("the");
-        //std::string format("else");
-        //std::string expression("(<)|(>)|\\r");
-        //std::string format("(?1&lt;)(?2&gt;)");
-
-        //std::cout << "EXPRESSION  '" << expression << "'\n";
-        //std::cout << "FORMAT      '" << format << "'\n";
-
-        rgx.assign(expression.c_str());
-
-        bool match(false);
-        bool search(false);
-
-        // other flags
-        // see http://www.boost.org/libs/regex/doc/match_flag_type.html
-        //boost::match_flag_type flags = boost::match_default;
-        // boost::format_default
-        // boost::format_perl
-        // boost::format_literal
-        // boost::format_all
-        // boost::format_no_copy
-        // boost::format_first_only
-
-        boost::match_flag_type flags 
-            = boost::match_default | boost::format_all;
-
-        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";
-
-    }
-    catch(boost::regex_error &e)
+    for (ptr = ptr->children; ptr; ptr = ptr->next)
     {
-        std::cout << "REGEX Error code=" << e.code() 
-                  << " position=" << e.position() << "\n";
-    }
-    
-    //std::cout << "QUERY OUT '" << query_out << "'\n";
-    // still need to fold this new rpn query string into Z_Query structure...
-}
-
-
-
-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";
+        if (ptr->type != XML_ELEMENT_NODE)
+            continue;
+        if (!strcmp((const char *) ptr->name, "xslt"))
+        {
+            if (m_stylesheet)
+            {
+                throw yp2::filter::FilterException
+                    ("Only one xslt element allowed in query_rewrite filter");
+            }
+
+            std::string fname = yp2::xml::get_text(ptr);
+            m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
+            if (!m_stylesheet)
+            {
+                throw yp2::filter::FilterException
+                    ("Failed to read stylesheet " 
+                     + fname
+                     + " in query_rewrite filter");
+            }
         }
-
-        // adding configuration
-        if (expr.size() && form.size()){
-            //std::cout << "adding regular expression\n";
+        else
+        {
+            throw yp2::filter::FilterException
+                ("Bad element " 
+                 + std::string((const char *) ptr->name)
+                 + " in query_rewrite filter");
         }
-
-        // 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()
@@ -226,11 +170,6 @@ extern "C" {
     };
 }
 
-extern "C" {
-    extern struct yp2_filter_struct yp2_filter_query_rewrite;
-}
-
-
 /*
  * Local variables:
  * c-basic-offset: 4
index 34f5eba..7fea8f7 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: filter_query_rewrite.hpp,v 1.2 2006-01-22 00:05:51 marc Exp $
+/* $Id: filter_query_rewrite.hpp,v 1.3 2006-03-15 14:55:17 adam Exp $
    Copyright (c) 2005, Index Data.
 
 %LICENSE%
@@ -27,7 +27,7 @@ namespace yp2 {
 }
 
 extern "C" {
-    extern struct yp2_filter_struct yp2_filter_queryRewrite;
+    extern struct yp2_filter_struct yp2_filter_query_rewrite;
 }
 
 #endif