Fixed bug in Generic Frontend Server that could cause a server to stop
[yaz-moved-to-github.git] / src / xmlquery.c
index 446dec6..62ebd7f 100644 (file)
@@ -1,20 +1,19 @@
 /*
- * Copyright (C) 1995-2005, Index Data ApS
+ * Copyright (C) 1995-2006, Index Data ApS
  * All rights reserved.
  *
- * $Id: xmlquery.c,v 1.4 2006-02-19 18:44:23 adam Exp $
+ * $Id: xmlquery.c,v 1.10 2006-07-06 10:17:54 adam Exp $
  */
 
-/**
- * \file querytostr.c
- * \brief Query / XML conversions
- */
+/** \file xmlquery.c
+    \brief Query / XML conversions
+*/
 
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
@@ -207,7 +206,7 @@ xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
     {
         Z_Complex *zc = zs->u.complex;
 
-        xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "binary", 0);
+        xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "operator", 0);
         if (zc->roperator)
             yaz_query2xml_operator(zc->roperator, node);
         yaz_query2xml_rpnstructure(zc->s1, node);
@@ -249,20 +248,18 @@ xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
     return 0;
 }
 
-void yaz_rpnquery2xml(const Z_RPNQuery *rpn, void *docp_void)
+void yaz_rpnquery2xml(const Z_RPNQuery *rpn, xmlDocPtr *docp)
 {
     Z_Query query;
 
     query.which = Z_Query_type_1;
     query.u.type_1 = (Z_RPNQuery *) rpn;
-    yaz_query2xml(&query, docp_void);
+    yaz_query2xml(&query, docp);
 }
 
-void yaz_query2xml(const Z_Query *q, void *docp_void)
+void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
 {
-    xmlDocPtr *docp = (xmlDocPtr *) docp_void;
-    xmlNodePtr top_node, child_node = 0;
-    const char *type = 0;
+    xmlNodePtr top_node, q_node = 0, child_node = 0;
 
     assert(q);
     assert(docp);
@@ -273,31 +270,28 @@ void yaz_query2xml(const Z_Query *q, void *docp_void)
     {
     case Z_Query_type_1: 
     case Z_Query_type_101:
-       type = "rpn";
-       child_node = yaz_query2xml_rpn(q->u.type_1, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
+       child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
         break;
     case Z_Query_type_2:
-       type = "ccl";
-       child_node = yaz_query2xml_ccl(q->u.type_2, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "ccl", 0);
+       child_node = yaz_query2xml_ccl(q->u.type_2, q_node);
         break;
     case Z_Query_type_100:
-       type = "z39.58";
-       child_node = yaz_query2xml_z3958(q->u.type_100, top_node);
+        q_node = xmlNewChild(top_node, 0, BAD_CAST "z39.58", 0);
+       child_node = yaz_query2xml_z3958(q->u.type_100, q_node);
         break;
     case Z_Query_type_104:
         if (q->u.type_104->which == Z_External_CQL)
        {
-           type = "cql";
-           child_node = yaz_query2xml_cql(q->u.type_104->u.cql, top_node);
+            q_node = xmlNewChild(top_node, 0, BAD_CAST "cql", 0);
+           child_node = yaz_query2xml_cql(q->u.type_104->u.cql, q_node);
        }
     }
-    if (child_node && type)
+    if (child_node && q_node)
     {
        *docp = xmlNewDoc(BAD_CAST "1.0");
        xmlDocSetRootElement(*docp, top_node); /* make it top node in doc */
-
-       /* set type attribute now */
-       xmlNewProp(top_node, BAD_CAST "type", BAD_CAST type);
     }
     else
     {
@@ -505,19 +499,7 @@ void yaz_xml2query_attribute_element(const xmlNode *ptr,
 
 char *strVal(const xmlNode *ptr_cdata, ODR odr)
 {
-    char *cdata;
-    int len = 0;
-    const xmlNode *ptr;
-
-    for (ptr = ptr_cdata; ptr; ptr = ptr->next)
-        if (ptr->type == XML_TEXT_NODE)
-            len += xmlStrlen(ptr->content);
-    cdata = (char *) odr_malloc(odr, len+1);
-    *cdata = '\0';
-    for (ptr = ptr_cdata; ptr; ptr = ptr->next)
-        if (ptr->type == XML_TEXT_NODE)
-            strcat(cdata, (const char *) ptr->content);
-    return cdata;
+    return nmem_text_node_cdata(ptr_cdata, odr->mem);
 }
 
 void yaz_xml2query_term(const xmlNode *ptr,
@@ -672,11 +654,11 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
     if (!ptr || ptr->type != XML_ELEMENT_NODE)
     {
         *error_code = 1;
-        *addinfo = "missing rpn structure node";
+        *addinfo = "missing rpn operator, rset, apt node";
         return;
     }
     *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
-    if (!xmlStrcmp(ptr->name, BAD_CAST "binary"))
+    if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
     {
         Z_Complex *zc = odr_malloc(odr, sizeof(Z_Complex));
         
@@ -741,8 +723,18 @@ static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
     if (ptr && ptr->type == XML_ELEMENT_NODE && 
         !xmlStrcmp(ptr->name, BAD_CAST "query"))
     {
-        const char *type = (const char *)
-            xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
+        const char *type;
+        ptr = ptr->children;
+        while (ptr && ptr->type != XML_ELEMENT_NODE)
+            ptr = ptr->next;
+        if (!ptr || ptr->type != XML_ELEMENT_NODE)
+        {
+            *error_code = 1;
+            *addinfo = "missing query content";
+            return;
+        }
+        type = (const char *) ptr->name;
+
         *query = (Z_Query*) odr_malloc(odr, sizeof(Z_Query));
         if (!type || !strcmp(type, "rpn"))
         {
@@ -781,10 +773,10 @@ static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
 void yaz_xml2query(const void *xmlnodep, Z_Query **query, ODR odr,
                    int *error_code, const char **addinfo)
 {
-    return yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
+    yaz_xml2query_(xmlnodep, query, odr, error_code, addinfo);
 }
 
-/* HAVE_XML2 */
+/* YAZ_HAVE_XML2 */
 #endif
 
 /*