Reformat
[yaz-moved-to-github.git] / src / xmlquery.c
index db85df0..7a94d44 100644 (file)
@@ -1,11 +1,13 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2011 Index Data
  * See the file LICENSE for details.
  */
-
 /** \file xmlquery.c
     \brief Query / XML conversions
 */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdio.h>
 #include <string.h>
@@ -59,8 +61,8 @@ static int check_diagnostic(const xmlNode *ptr, ODR odr,
         return 0;
 }
 
-void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
-                                     xmlNodePtr parent)
+static void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
+                                            xmlNodePtr parent)
 {
     char formstr[30];
     const char *setname = 0;
@@ -117,8 +119,7 @@ void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
 }
 
 
-xmlNodePtr yaz_query2xml_term(const Z_Term *term,
-                             xmlNodePtr parent)
+static xmlNodePtr yaz_query2xml_term(const Z_Term *term, xmlNodePtr parent)
 {
     xmlNodePtr t = 0;
     xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "term", 0);
@@ -165,8 +166,8 @@ xmlNodePtr yaz_query2xml_term(const Z_Term *term,
     return node;
 }
 
-xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
-                            xmlNodePtr parent)
+static xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
+                                    xmlNodePtr parent)
 {
     xmlNodePtr node = xmlNewChild(parent, /* NS */ 0, BAD_CAST "apt", 0);
     int num_attributes = zapt->attributes->num_attributes;
@@ -179,7 +180,7 @@ xmlNodePtr yaz_query2xml_apt(const Z_AttributesPlusTerm *zapt,
 }
 
 
-void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
+static void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
 {
     const char *type = 0;
     switch(op->which)
@@ -239,8 +240,8 @@ void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
     }
 }
 
-xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
-                                     xmlNodePtr parent)
+static xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
+                                             xmlNodePtr parent)
 {
     if (zs->which == Z_RPNStructure_complex)
     {
@@ -265,7 +266,7 @@ xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
     return 0;
 }
 
-xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
+static xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
 {
     if (rpn->attributeSetId)
     {
@@ -278,17 +279,17 @@ xmlNodePtr yaz_query2xml_rpn(const Z_RPNQuery *rpn, xmlNodePtr parent)
     return yaz_query2xml_rpnstructure(rpn->RPNStructure, parent);
 }
 
-xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
+static xmlNodePtr yaz_query2xml_ccl(const Odr_oct *ccl, xmlNodePtr node)
 {
     return 0;
 }
 
-xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
+static xmlNodePtr yaz_query2xml_z3958(const Odr_oct *ccl, xmlNodePtr node)
 {
     return 0;
 }
 
-xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
+static xmlNodePtr yaz_query2xml_cql(const char *cql, xmlNodePtr node)
 {
     return 0;
 }
@@ -345,23 +346,23 @@ void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
     }
 }
 
-bool_t *boolVal(ODR odr, const char *str)
+static bool_t *boolVal(ODR odr, const char *str)
 {
     if (*str == '\0' || strchr("0fF", *str))
         return odr_booldup(odr, 0);
     return odr_booldup(odr, 1);
 }
 
-Odr_int *intVal(ODR odr, const char *str)
+static Odr_int *intVal(ODR odr, const char *str)
 {
     return odr_intdup(odr, atoi(str));
 }
 
-void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
-                            ODR odr, int *error_code, const char **addinfo)
+static void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
+                                   ODR odr,
+                                   int *error_code, const char **addinfo)
 {
-    const char *type = (const char *)
-        xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
+    xmlChar *type = xmlGetProp((xmlNodePtr) ptr, BAD_CAST "type");
     if (!type)
     {
         *error_code = 1;
@@ -369,78 +370,66 @@ void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
         return;
     }
     *op = (Z_Operator*) odr_malloc(odr, sizeof(Z_Operator));
-    if (!strcmp(type, "and"))
+    if (!xmlStrcmp(type, BAD_CAST "and"))
     {
         (*op)->which = Z_Operator_and;
         (*op)->u.op_and = odr_nullval();
     }
-    else if (!strcmp(type, "or"))
+    else if (!xmlStrcmp(type, BAD_CAST "or"))
     {
         (*op)->which = Z_Operator_or;
         (*op)->u.op_or = odr_nullval();
     }
-    else if (!strcmp(type, "not"))
+    else if (!xmlStrcmp(type, BAD_CAST "not"))
     {
         (*op)->which = Z_Operator_and_not;
         (*op)->u.and_not = odr_nullval();
     }
-    else if (!strcmp(type, "prox"))
+    else if (!xmlStrcmp(type, BAD_CAST "prox"))
     {
-        const char *atval;
-        Z_ProximityOperator *pop = (Z_ProximityOperator *) 
-            odr_malloc(odr, sizeof(Z_ProximityOperator));
-
+        struct _xmlAttr *attr;
+        Z_ProximityOperator *pop = (Z_ProximityOperator *)
+            odr_malloc(odr, sizeof(*pop));
         (*op)->which = Z_Operator_prox;
         (*op)->u.prox = pop;
+        /* default values */
+        pop->exclusion = 0;
+        pop->ordered = odr_booldup(odr, 1);
+        pop->relationType =
+            odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
+        pop->which = Z_ProximityOperator_known;
+        pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
+        pop->distance = odr_intdup(odr, 1);
 
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "exclusion");
-        if (atval)
-            pop->exclusion = boolVal(odr, atval);
-        else
-            pop->exclusion = 0;
-
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "distance");
-        if (atval)
-            pop->distance = intVal(odr, atval);
-        else
-            pop->distance = odr_intdup(odr, 1);
-
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "ordered");
-        if (atval)
-            pop->ordered = boolVal(odr, atval);
-        else
-            pop->ordered = odr_booldup(odr, 1);
-
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "relationType");
-        if (atval)
-            pop->relationType = intVal(odr, atval);
-        else
-            pop->relationType =
-                odr_intdup(odr, Z_ProximityOperator_Prox_lessThanOrEqual);
-
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "knownProximityUnit");
-        if (atval)
-        {
-            pop->which = Z_ProximityOperator_known;            
-            pop->u.known = intVal(odr, atval);
-        }
-        else
-        {
-            pop->which = Z_ProximityOperator_known;
-            pop->u.known = odr_intdup(odr, Z_ProxUnit_word);
-        }
-
-        atval = (const char *) xmlGetProp((xmlNodePtr) ptr,
-                                          BAD_CAST "privateProximityUnit");
-        if (atval)
+        for (attr = ptr->properties; attr; attr = attr->next)
         {
-            pop->which = Z_ProximityOperator_private;
-            pop->u.zprivate = intVal(odr, atval);
+            const char *value = (const char *) attr->children->content;
+            if (!xmlStrcmp(attr->name, BAD_CAST "type"))
+                ;
+            else if (!xmlStrcmp(attr->name, BAD_CAST "exclusion"))
+                pop->exclusion = boolVal(odr, value);
+            else if (!xmlStrcmp(attr->name, BAD_CAST "distance"))
+                pop->distance = intVal(odr, value);
+            else if (!xmlStrcmp(attr->name, BAD_CAST "ordered"))
+                pop->ordered = boolVal(odr, value);
+            else if (!xmlStrcmp(attr->name, BAD_CAST "relationType"))
+                pop->relationType = intVal(odr, value);
+            else if (!xmlStrcmp(attr->name, BAD_CAST "knownProximityUnit"))
+            {
+                pop->which = Z_ProximityOperator_known;
+                pop->u.known = intVal(odr, value);
+            }
+            else if (!xmlStrcmp(attr->name, BAD_CAST "privateProximityUnit"))
+            {
+                pop->which = Z_ProximityOperator_private;
+                pop->u.known = intVal(odr, value);
+            }
+            else
+            {
+                *error_code = 1;
+                *addinfo = "bad proximity attribute";
+                break;
+            }
         }
     }
     else
@@ -448,11 +437,13 @@ void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
         *error_code = 1;
         *addinfo = "bad operator type";
     }
+    xmlFree(type);
 }
 
-void yaz_xml2query_attribute_element(const xmlNode *ptr, 
-                                     Z_AttributeElement **elem, ODR odr,
-                                     int *error_code, const char **addinfo)
+static void yaz_xml2query_attribute_element(const xmlNode *ptr, 
+                                            Z_AttributeElement **elem, ODR odr,
+                                            int *error_code,
+                                            const char **addinfo)
 {
     int i;
     xmlChar *set = 0;
@@ -544,14 +535,13 @@ void yaz_xml2query_attribute_element(const xmlNode *ptr,
     }
 }
 
-char *strVal(const xmlNode *ptr_cdata, ODR odr)
+static char *strVal(const xmlNode *ptr_cdata, ODR odr)
 {
     return nmem_text_node_cdata(ptr_cdata, odr_getmem(odr));
 }
 
-void yaz_xml2query_term(const xmlNode *ptr,
-                       Z_Term **term, ODR odr,
-                       int *error_code, const char **addinfo)
+static void yaz_xml2query_term(const xmlNode *ptr, Z_Term **term, ODR odr,
+                               int *error_code, const char **addinfo)
 {
     xmlChar *type = 0;
     struct _xmlAttr *attr;
@@ -614,9 +604,9 @@ void yaz_xml2query_term(const xmlNode *ptr,
     }
 }
 
-void yaz_xml2query_apt(const xmlNode *ptr_apt,
-                       Z_AttributesPlusTerm **zapt, ODR odr,
-                       int *error_code, const char **addinfo)
+static void yaz_xml2query_apt(const xmlNode *ptr_apt,
+                              Z_AttributesPlusTerm **zapt, ODR odr,
+                              int *error_code, const char **addinfo)
 {
     const xmlNode *ptr = ptr_apt->children;
     int i, num_attr = 0;
@@ -681,8 +671,8 @@ void yaz_xml2query_apt(const xmlNode *ptr_apt,
     }
 }
 
-void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
-                        ODR odr, int *error_code, const char **addinfo)
+static void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
+                               ODR odr, int *error_code, const char **addinfo)
 {
     if (ptr->children)
     {
@@ -695,8 +685,9 @@ void yaz_xml2query_rset(const xmlNode *ptr, Z_ResultSetId **rset,
     }
 }
 
-void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
-                                ODR odr, int *error_code, const char **addinfo)
+static void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
+                                       ODR odr,
+                                       int *error_code, const char **addinfo)
 {
     while (ptr && ptr->type != XML_ELEMENT_NODE)
         ptr = ptr->next;
@@ -755,16 +746,19 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
     }
 }
 
-void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
-                   int *error_code, const char **addinfo)
+static void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
+                              int *error_code, const char **addinfo)
 {
-    const char *set = (const char *)
-        xmlGetProp((xmlNodePtr) ptr, BAD_CAST "set");
+    xmlChar *set = xmlGetProp((xmlNodePtr) ptr, BAD_CAST "set");
 
     *query = (Z_RPNQuery*) odr_malloc(odr, sizeof(Z_RPNQuery));
     if (set)
-        (*query)->attributeSetId = yaz_string_to_oid_odr(yaz_oid_std(),
-                                                         CLASS_ATTSET, set, odr);
+    {
+        (*query)->attributeSetId =
+            yaz_string_to_oid_odr(yaz_oid_std(),
+                                  CLASS_ATTSET, (const char *) set, odr);
+        xmlFree(set);
+    }
     else
         (*query)->attributeSetId = 0;
     yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,