New yaz_xml_get_prop utility YAZ-839
[yaz-moved-to-github.git] / src / xmlquery.c
index 21849c6..4728de6 100644 (file)
@@ -1,11 +1,13 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 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>
 #include <yaz/logrpn.h>
 #include <yaz/xmlquery.h>
 #include <yaz/nmem_xml.h>
+#include <yaz/xml_get.h>
 #include <yaz/oid_db.h>
 
-void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
-                                     xmlNodePtr parent)
+static int check_diagnostic(const xmlNode *ptr, ODR odr,
+                            int *error_code, const char **addinfo)
+{
+    if (ptr && ptr->type == XML_ELEMENT_NODE &&
+        !xmlStrcmp(ptr->name, BAD_CAST "diagnostic"))
+    {
+        struct _xmlAttr *attr;
+        const char *code_str = 0;
+        const char *addinfo_str = 0;
+        for (attr = ptr->properties; attr; attr = attr->next)
+        {
+            if (!xmlStrcmp(attr->name, BAD_CAST "code") &&
+                attr->children && attr->children->type == XML_TEXT_NODE)
+                code_str = (const char *) attr->children->content;
+            else if (!xmlStrcmp(attr->name, BAD_CAST "addinfo") &&
+                     attr->children && attr->children->type == XML_TEXT_NODE)
+                addinfo_str = (const char *) attr->children->content;
+            else
+            {
+                *error_code = 1;
+                *addinfo = "bad attribute for diagnostic element";
+                return 1;
+            }
+        }
+        if (!code_str)
+        {
+            *error_code = 1;
+            *addinfo = "missing @code for diagnostic element";
+            return 1;
+        }
+        *error_code = atoi(code_str);
+        if (addinfo_str)
+            *addinfo = odr_strdup(odr, addinfo_str);
+        return 1;
+    }
+    else
+        return 0;
+}
+
+static void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
+                                            xmlNodePtr parent)
 {
     char formstr[30];
     const char *setname = 0;
     char oid_name_str[OID_STR_MAX];
-    
+
     if (element->attributeSet)
     {
         setname = yaz_oid_to_string_buf(element->attributeSet,
@@ -53,17 +95,17 @@ void yaz_query2xml_attribute_element(const Z_AttributeElement *element,
         for (i = 0; i<element->value.complex->num_list; i++)
         {
             xmlNodePtr node = xmlNewChild(parent, 0, BAD_CAST "attr", 0);
-            
+
             if (setname)
                 xmlNewProp(node, BAD_CAST "set", BAD_CAST setname);
-            
+
             sprintf(formstr, ODR_INT_PRINTF, *element->attributeType);
             xmlNewProp(node, BAD_CAST "type", BAD_CAST formstr);
-            
+
             if (element->value.complex->list[i]->which ==
                 Z_StringOrNumeric_string)
             {
-                xmlNewProp(node, BAD_CAST "value", BAD_CAST 
+                xmlNewProp(node, BAD_CAST "value", BAD_CAST
                            element->value.complex->list[i]->u.string);
             }
             else if (element->value.complex->list[i]->which ==
@@ -78,8 +120,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);
@@ -95,7 +136,7 @@ xmlNodePtr yaz_query2xml_term(const Z_Term *term,
     case Z_Term_numeric:
         type = "numeric";
        sprintf(formstr, ODR_INT_PRINTF, *term->u.numeric);
-       t = xmlNewText(BAD_CAST formstr);       
+       t = xmlNewText(BAD_CAST formstr);
         break;
     case Z_Term_characterString:
         type = "string";
@@ -126,8 +167,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;
@@ -140,7 +181,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)
@@ -161,11 +202,11 @@ void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
         return;
     }
     xmlNewProp(node, BAD_CAST "type", BAD_CAST type);
-    
+
     if (op->which == Z_Operator_prox)
     {
         char formstr[30];
-        
+
         if (op->u.prox->exclusion)
         {
             if (*op->u.prox->exclusion)
@@ -178,12 +219,12 @@ void yaz_query2xml_operator(Z_Operator *op, xmlNodePtr node)
 
         if (*op->u.prox->ordered)
             xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "true");
-        else 
+        else
             xmlNewProp(node, BAD_CAST "ordered", BAD_CAST "false");
-       
+
         sprintf(formstr, ODR_INT_PRINTF, *op->u.prox->relationType);
         xmlNewProp(node, BAD_CAST "relationType", BAD_CAST formstr);
-        
+
         switch(op->u.prox->which)
         {
         case Z_ProximityOperator_known:
@@ -200,8 +241,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)
     {
@@ -220,13 +261,13 @@ xmlNodePtr yaz_query2xml_rpnstructure(const Z_RPNStructure *zs,
             return yaz_query2xml_apt(zs->u.simple->u.attributesPlusTerm,
                                     parent);
         else if (zs->u.simple->which == Z_Operand_resultSetId)
-            return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset", 
+            return xmlNewChild(parent, /* NS */ 0, BAD_CAST "rset",
                                BAD_CAST zs->u.simple->u.resultSetId);
     }
     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)
     {
@@ -239,17 +280,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;
 }
@@ -274,7 +315,7 @@ void yaz_query2xml(const Z_Query *q, xmlDocPtr *docp)
 
     switch (q->which)
     {
-    case Z_Query_type_1: 
+    case Z_Query_type_1:
     case Z_Query_type_101:
         q_node = xmlNewChild(top_node, 0, BAD_CAST "rpn", 0);
        child_node = yaz_query2xml_rpn(q->u.type_1, q_node);
@@ -306,23 +347,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_t *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");
+    const char *type = yaz_xml_get_prop((xmlNodePtr) ptr, "type");
     if (!type)
     {
         *error_code = 1;
@@ -347,61 +388,49 @@ void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
     }
     else if (!strcmp(type, "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
@@ -411,9 +440,10 @@ void yaz_xml2query_operator(const xmlNode *ptr, Z_Operator **op,
     }
 }
 
-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;
@@ -454,7 +484,7 @@ void yaz_xml2query_attribute_element(const xmlNode *ptr,
         *addinfo = "missing value attribute for att content";
         return;
     }
-        
+
     *elem = (Z_AttributeElement *) odr_malloc(odr, sizeof(**elem));
     if (set)
         (*elem)->attributeSet = yaz_string_to_oid_odr(yaz_oid_std(),
@@ -496,7 +526,7 @@ void yaz_xml2query_attribute_element(const xmlNode *ptr,
             }
         }
         (*elem)->value.complex->num_semanticAction = 0;
-        (*elem)->value.complex->semanticAction = 0;        
+        (*elem)->value.complex->semanticAction = 0;
     }
     else
     {   /* good'ld numeric value */
@@ -505,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;
@@ -536,7 +565,7 @@ void yaz_xml2query_term(const xmlNode *ptr,
     {
         (*term)->which = Z_Term_general;
         (*term)->u.general =
-            odr_create_Odr_oct(odr, (unsigned char *)cdata, strlen(cdata));
+            odr_create_Odr_oct(odr, cdata, strlen(cdata));
     }
     else if (!xmlStrcmp(type, BAD_CAST "numeric"))
     {
@@ -575,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;
@@ -604,7 +633,7 @@ void yaz_xml2query_apt(const xmlNode *ptr_apt,
     (*zapt)->attributes->attributes = (Z_AttributeElement **)
         odr_malloc(odr, sizeof(Z_AttributeElement*) * num_attr);
 
-    i = 0;    
+    i = 0;
     ptr = ptr_apt->children;
     for (; ptr; ptr = ptr->next)
         if (ptr->type == XML_ELEMENT_NODE)
@@ -619,10 +648,13 @@ void yaz_xml2query_apt(const xmlNode *ptr_apt,
             else
                 break;
         }
+    if (check_diagnostic(ptr, odr, error_code, addinfo))
+        return;
+
     if (ptr && ptr->type == XML_ELEMENT_NODE)
     {
         if (!xmlStrcmp(ptr->name, BAD_CAST "term"))
-        {        
+        {
             /* deal with term */
             yaz_xml2query_term(ptr, &(*zapt)->term, odr, error_code, addinfo);
         }
@@ -639,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)
     {
@@ -653,26 +685,30 @@ 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;
-    
+
     if (!ptr || ptr->type != XML_ELEMENT_NODE)
     {
         *error_code = 1;
         *addinfo = "missing rpn operator, rset, apt node";
         return;
     }
+    if (check_diagnostic(ptr, odr, error_code, addinfo))
+        return;
+
     *zs = (Z_RPNStructure *) odr_malloc(odr, sizeof(Z_RPNStructure));
     if (!xmlStrcmp(ptr->name, BAD_CAST "operator"))
     {
         Z_Complex *zc = (Z_Complex *) odr_malloc(odr, sizeof(Z_Complex));
-        
+
         (*zs)->which = Z_RPNStructure_complex;
         (*zs)->u.complex = zc;
-        
+
         yaz_xml2query_operator(ptr, &zc->roperator, odr, error_code, addinfo);
 
         ptr = ptr->children;
@@ -685,7 +721,7 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
             ptr = ptr->next;
         yaz_xml2query_rpnstructure(ptr, &zc->s2, odr, error_code, addinfo);
     }
-    else 
+    else
     {
         Z_Operand *s = (Z_Operand *) odr_malloc(odr, sizeof(Z_Operand));
         (*zs)->which = Z_RPNStructure_simple;
@@ -698,7 +734,7 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
         }
         else if (!xmlStrcmp(ptr->name, BAD_CAST "rset"))
         {
-            s->which = Z_Operand_resultSetId; 
+            s->which = Z_Operand_resultSetId;
             yaz_xml2query_rset(ptr, &s->u.resultSetId,
                                odr, error_code, addinfo);
         }
@@ -706,20 +742,22 @@ void yaz_xml2query_rpnstructure(const xmlNode *ptr, Z_RPNStructure **zs,
         {
             *error_code = 1;
             *addinfo = "bad element: expected binary, apt or rset";
-        }        
+        }
     }
 }
 
-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");
+    const char  *set = yaz_xml_get_prop((xmlNodePtr) ptr, "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, set, odr);
+    }
     else
         (*query)->attributeSetId = 0;
     yaz_xml2query_rpnstructure(ptr->children, &(*query)->RPNStructure,
@@ -729,7 +767,9 @@ void yaz_xml2query_rpn(const xmlNode *ptr, Z_RPNQuery **query, ODR odr,
 static void yaz_xml2query_(const xmlNode *ptr, Z_Query **query, ODR odr,
                            int *error_code, const char **addinfo)
 {
-    if (ptr && ptr->type == XML_ELEMENT_NODE && 
+    if (check_diagnostic(ptr, odr, error_code, addinfo))
+        return;
+    if (ptr && ptr->type == XML_ELEMENT_NODE &&
         !xmlStrcmp(ptr->name, BAD_CAST "query"))
     {
         const char *type;