Fix: import header so we dont return a int and seg.fault on that.
[yaz-moved-to-github.git] / src / srw.c
index a8a9f3e..bd3c577 100644 (file)
--- a/src/srw.c
+++ b/src/srw.c
@@ -1,18 +1,26 @@
 /* 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 srw.c
  * \brief Implements SRW/SRU package encoding and decoding
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
 
 #include <yaz/srw.h>
+#include <yaz/wrbuf.h>
 #if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #include <assert.h>
 
+#include "sru-p.h"
+
 static void add_XML_n(xmlNodePtr ptr, const char *elem, char *val, int len,
                       xmlNsPtr ns_ptr)
 {
@@ -61,7 +69,7 @@ xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val)
 }
 
 static void add_xsd_integer(xmlNodePtr ptr, const char *elem,
-                            const odr_int_t *val)
+                            const Odr_int *val)
 {
     if (val)
     {
@@ -166,7 +174,7 @@ static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
 }
                      
 static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
-                             odr_int_t **val)
+                             Odr_int **val)
 {
 #if CHECK_TYPE
     struct _xmlAttr *attr;
@@ -192,7 +200,7 @@ static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
     ptr = ptr->children;
     if (!ptr || ptr->type != XML_TEXT_NODE)
         return 0;
-    *val = odr_intdup(o, atoi((const char *) ptr->content));
+    *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
     return 1;
 }
 
@@ -439,11 +447,91 @@ static int yaz_srw_versions(ODR o, xmlNodePtr pptr,
     return 0;
 }
 
+Z_FacetTerm *yaz_sru_proxy_get_facet_term_count(ODR odr, xmlNodePtr node) {
+
+    int freq;
+    xmlNodePtr child;
+    WRBUF wrbuf = wrbuf_alloc();
+    const char *freq_string = yaz_element_attribute_value_get(node, "facetvalue", "est_representation");
+    Z_Term *term;
+    if (freq_string)
+        freq =  atoi(freq_string);
+    else
+        freq = -1;
+
+    for (child = node->children; child ; child = child->next) {
+        if (child->type == XML_TEXT_NODE)
+        wrbuf_puts(wrbuf, (const char *) child->content);
+    }
+    term = term_create(odr, wrbuf_cstr(wrbuf));
+    yaz_log(YLOG_DEBUG, "sru-proxy facet: %s %d", wrbuf_cstr(wrbuf), freq);
+    wrbuf_destroy(wrbuf);
+    return facet_term_create(odr, term, freq);
+};
+
+static Z_FacetField *yaz_sru_proxy_decode_facet_field(ODR odr, xmlNodePtr ptr) {
+    Z_AttributeList *list;
+    Z_FacetField *facet_field;
+    int num_terms = 0;
+    int index = 0;
+    xmlNodePtr node;
+    // USE attribute
+    const char* name = yaz_element_attribute_value_get(ptr, "facet", "code");
+    yaz_log(YLOG_DEBUG, "sru-proxy facet type: %s", name);
+
+    list = yaz_use_atttribute_create(odr, name);
+    for (node = ptr->children; node; node = node->next) {
+        if (match_element(node, "facetvalue"))
+            num_terms++;
+    }
+    facet_field = facet_field_create(odr, list, num_terms);
+    index = 0;
+    for (node = ptr->children; node; node = node->next) {
+        if (match_element(node, "facetvalue")) {
+            facet_field_term_set(odr, facet_field, yaz_sru_proxy_get_facet_term_count(odr, node), index);
+        index++;
+        }
+    }
+    return facet_field;
+}
+
+static int yaz_sru_proxy_decode_facets(ODR o, xmlNodePtr root, Z_FacetList **facetList)
+{
+    xmlNodePtr ptr;
+
+    for (ptr = root->children; ptr; ptr = ptr->next)
+    {
+        if (match_element(ptr, "facets"))
+        {
+            xmlNodePtr node;
+            Z_FacetList *facet_list;
+            int num_facets = 0;
+            for (node = ptr->children; node; node= node->next)
+            {
+                if (node->type == XML_ELEMENT_NODE)
+                    num_facets++;
+            }
+            facet_list = facet_list_create(o, num_facets);
+            num_facets = 0;
+            for (node = ptr->children; node; node= node->next)
+            {
+                if (match_element(node, "facet")) {
+                    facet_list_field_set(o, facet_list, yaz_sru_proxy_decode_facet_field(o, node), num_facets);
+                    num_facets++;
+                }
+            }
+            *facetList = facet_list;
+            break;
+        }
+    }
+    return 0;
+}
+
+
 
 static int yaz_srw_decode_diagnostics(ODR o, xmlNodePtr pptr,
                                       Z_SRW_diagnostic **recs, int *num,
                                       void *client_data, const char *ns)
-    
 {
     int i;
     xmlNodePtr ptr;
@@ -734,12 +822,17 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
             res->diagnostics = 0;
             res->num_diagnostics = 0;
             res->nextRecordPosition = 0;
+            res->facetList = 0;
 
             for (; ptr; ptr = ptr->next)
             {
                 if (match_xsd_string(ptr, "version", o,
                                      &(*p)->srw_version))
                     ;
+                else if (match_xsd_XML_n(ptr, "extraResponseData", o, 
+                                         &(*p)->extraResponseData_buf,
+                                         &(*p)->extraResponseData_len))
+                    ;
                 else if (match_xsd_integer(ptr, "numberOfRecords", o, 
                                       &res->numberOfRecords))
                     ;
@@ -760,6 +853,8 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
                     yaz_srw_diagnostics(o, ptr, &res->diagnostics,
                                         &res->num_diagnostics,
                                         client_data, ns);
+                else if (match_element(ptr, "facet_analysis"))
+                    yaz_sru_proxy_decode_facets(o, ptr, &res->facetList);
             }
         }
         else if (!xmlStrcmp(method->name, BAD_CAST "explainRequest"))
@@ -778,6 +873,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
                 if (match_xsd_string(ptr, "version", o,
                                            &(*p)->srw_version))
                     ;
+                else if (match_xsd_XML_n(ptr, "extraResponseData", o, 
+                                         &(*p)->extraResponseData_buf,
+                                         &(*p)->extraResponseData_len))
+                    ;
                 else if (match_xsd_string(ptr, "stylesheet", o,
                                           &req->stylesheet))
                     ;
@@ -809,6 +908,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
                 if (match_xsd_string(ptr, "version", o,
                                            &(*p)->srw_version))
                     ;
+                else if (match_xsd_XML_n(ptr, "extraResponseData", o, 
+                                         &(*p)->extraResponseData_buf,
+                                         &(*p)->extraResponseData_len))
+                    ;
                 else if (match_element(ptr, "record"))
                     yaz_srw_record(o, ptr, &res->record, &res->extra_record,
                                    client_data, ns);
@@ -839,6 +942,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
                 if (match_xsd_string(ptr, "version", o,
                                      &(*p)->srw_version))
                     ;
+                else if (match_xsd_XML_n(ptr, "extraResponseData", o, 
+                                         &(*p)->extraResponseData_buf,
+                                         &(*p)->extraResponseData_len))
+                    ;
                 else if (match_xsd_string(ptr, "scanClause", o,
                                      &req->scanClause.cql))
                     ;
@@ -879,6 +986,10 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
                 if (match_xsd_string(ptr, "version", o,
                                      &(*p)->srw_version))
                     ;
+                else if (match_xsd_XML_n(ptr, "extraResponseData", o, 
+                                         &(*p)->extraResponseData_buf,
+                                         &(*p)->extraResponseData_len))
+                    ;
                 else if (match_element(ptr, "terms"))
                     yaz_srw_terms(o, ptr, &res->terms,
                                   &res->num_terms, client_data,
@@ -902,12 +1013,12 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
     {
         Z_SRW_PDU **p = handler_data;
         xmlNsPtr ns_srw;
+        xmlNodePtr ptr = 0;
         
         if ((*p)->which == Z_SRW_searchRetrieve_request)
         {
             Z_SRW_searchRetrieveRequest *req = (*p)->u.request;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0,
-                                         BAD_CAST "searchRetrieveRequest", 0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveRequest", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -948,8 +1059,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         else if ((*p)->which == Z_SRW_searchRetrieve_response)
         {
             Z_SRW_searchRetrieveResponse *res = (*p)->u.response;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0,
-                                         BAD_CAST "searchRetrieveResponse", 0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "searchRetrieveResponse", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -978,8 +1088,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         else if ((*p)->which == Z_SRW_explain_request)
         {
             Z_SRW_explainRequest *req = (*p)->u.explain_request;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest",
-                                         0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "explainRequest", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -991,8 +1100,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         else if ((*p)->which == Z_SRW_explain_response)
         {
             Z_SRW_explainResponse *res = (*p)->u.explain_response;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse",
-                                         0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "explainResponse", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -1014,7 +1122,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         else if ((*p)->which == Z_SRW_scan_request)
         {
             Z_SRW_scanRequest *req = (*p)->u.scan_request;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "scanRequest", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -1036,7 +1144,7 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         else if ((*p)->which == Z_SRW_scan_response)
         {
             Z_SRW_scanResponse *res = (*p)->u.scan_response;
-            xmlNodePtr ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
+            ptr = xmlNewChild(pptr, 0, BAD_CAST "scanResponse", 0);
             ns_srw = xmlNewNs(ptr, BAD_CAST ns, BAD_CAST "zs");
             xmlSetNs(ptr, ns_srw);
 
@@ -1058,6 +1166,11 @@ int yaz_srw_codec(ODR o, void * vptr, Z_SRW_PDU **handler_data,
         }
         else
             return -1;
+        if (ptr && (*p)->extraResponseData_len)
+            add_XML_n(ptr, "extraResponseData", 
+                      (*p)->extraResponseData_buf, 
+                      (*p)->extraResponseData_len, ns_srw);
+            
 
     }
     return 0;