Decode SRU facet response
authorAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Sep 2013 18:56:04 +0000 (20:56 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Mon, 9 Sep 2013 18:56:04 +0000 (20:56 +0200)
src/sru-p.h
src/sru_facet.c
src/srw.c

index aa3f3bf..3c9856e 100644 (file)
@@ -45,6 +45,12 @@ void yaz_sru_facet_request(ODR, Z_FacetList **facetList, const char **limit);
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 
+int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val);
+int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, Odr_int **val);
+int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
+                       char **val, int *len);
+int match_element(xmlNodePtr ptr, const char *elem);
+
 xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val);
 
 void add_xsd_integer(xmlNodePtr ptr, const char *elem, const Odr_int *val);
index 093183c..69925b4 100644 (file)
@@ -129,6 +129,111 @@ void yaz_sru_facet_response(ODR o, Z_FacetList **facetList, xmlNodePtr n)
             }
         }
     }
+    else if (o->direction == ODR_DECODE)
+    {
+        Z_FacetList *fl = (Z_FacetList *) odr_malloc(o, sizeof(*fl));
+        xmlNode *p1;
+
+        fl->num = 0;
+        for (p1 = n->children; p1; p1 = p1->next)
+            if (match_element(p1, "facet"))
+                fl->num++;
+        if (fl->num > 0)
+        {
+            int i = 0;
+            *facetList = fl;
+            fl->elements = (Z_FacetField **)
+                odr_malloc(o, sizeof(*fl->elements) * fl->num);
+            for (p1 = n->children; p1; p1 = p1->next)
+                if (match_element(p1, "facet"))
+                {
+                    char *index_name = 0;
+                    xmlNode *p_terms = 0;
+                    xmlNode *p2 = p1->children;
+                    Z_FacetField *ff = (Z_FacetField *)
+                        odr_malloc(o, sizeof(*ff));
+                    fl->elements[i++] = ff;
+                    ff->attributes = 0;
+                    ff->num_terms = 0;
+                    ff->terms = 0;
+                    for (; p2; p2 = p2->next)
+                    {
+                        if (match_xsd_string(p2, "index", o, &index_name))
+                            ;
+                        else if (match_element(p2, "terms"))
+                            p_terms = p2;
+                    }
+                    if (index_name)
+                    {
+                        Z_AttributeList *al =
+                            (Z_AttributeList*) odr_malloc(o, sizeof(*al));
+                        Z_ComplexAttribute *ca =
+                            (Z_ComplexAttribute *) odr_malloc(o, sizeof(*ca));
+                        Z_AttributeElement *ae =
+                            (Z_AttributeElement *) odr_malloc(o, sizeof(*ae));
+                        al->num_attributes = 1;
+                        al->attributes = (Z_AttributeElement **)
+                            odr_malloc(o, sizeof(*al->attributes));
+                        al->attributes[0] = ae;
+                        ae->attributeSet = 0;
+                        ae->attributeType = odr_intdup(o, 1);
+                        ae->which = Z_AttributeValue_complex;
+                        ae->value.complex = ca;
+                        ca->num_semanticAction = 0;
+                        ca->semanticAction = 0;
+                        ca->num_list = 1;
+                        ca->list = (Z_StringOrNumeric **)
+                            odr_malloc(o, sizeof(*ca->list));
+                        ca->list[0] = (Z_StringOrNumeric *)
+                            odr_malloc(o, sizeof(**ca->list));
+                        ca->list[0]->which = Z_StringOrNumeric_string;
+                        ca->list[0]->u.string = index_name;
+                        ff->attributes = al;
+                    }
+                    if (p_terms)
+                    {
+                        xmlNode *p;
+                        int i = 0;
+                        for (p = p_terms->children; p; p = p->next)
+                        {
+                            if (match_element(p, "term"))
+                                ff->num_terms++;
+                        }
+                        if (ff->num_terms)
+                            ff->terms = (Z_FacetTerm **)
+                                odr_malloc(o,
+                                           sizeof(*ff->terms) * ff->num_terms);
+                        for (p = p_terms->children; p; p = p->next)
+                        {
+                            if (match_element(p, "term"))
+                            {
+                                char *cstr = 0;
+                                Odr_int *count = 0;
+                                xmlNode *p2 = p->children;
+                                for (; p2; p2 = p2->next)
+                                {
+                                    if (match_xsd_string(p2, "actualTerm", o,
+                                                         &cstr))
+                                        ;
+                                    else if (match_xsd_integer(p2, "count", o,
+                                                               &count))
+                                        ;
+                                }
+                                if (cstr && count)
+                                {
+                                    ff->terms[i++] =
+                                        facet_term_create_cstr(o, cstr, *count);
+                                }
+                            }
+                        }
+                        ff->num_terms = i;
+                        if (ff->num_terms == 0)
+                            ff->terms = 0;
+                    }
+                }
+
+        }
+    }
 }
 
 #endif
index 7f0cd29..20948b3 100644 (file)
--- a/src/srw.c
+++ b/src/srw.c
@@ -79,7 +79,7 @@ void add_xsd_integer(xmlNodePtr ptr, const char *elem,
     }
 }
 
-static int match_element(xmlNodePtr ptr, const char *elem)
+int match_element(xmlNodePtr ptr, const char *elem)
 {
     if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
     {
@@ -90,8 +90,8 @@ static int match_element(xmlNodePtr ptr, const char *elem)
 
 #define CHECK_TYPE 0
 
-static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
-                              char **val, int *len)
+int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
+                       char **val, int *len)
 {
 #if CHECK_TYPE
     struct _xmlAttr *attr;
@@ -127,8 +127,7 @@ static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
 }
 
 
-static int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o,
-                            char **val)
+int match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val)
 {
     return match_xsd_string_n(ptr, elem, o, val, 0);
 }
@@ -186,8 +185,7 @@ static int match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
     return match_xsd_XML_n2(ptr, elem, o, val, len, 0);
 }
 
-static int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
-                             Odr_int **val)
+int match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, Odr_int **val)
 {
 #if CHECK_TYPE
     struct _xmlAttr *attr;