Using yaz_xml_get_prop
authorAdam Dickmeiss <adam@indexdata.dk>
Fri, 8 May 2015 13:07:26 +0000 (15:07 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Fri, 8 May 2015 13:07:26 +0000 (15:07 +0200)
src/charsets.c
src/session.c
src/settings.c
src/settings.h

index 61b6f19..b36d7e2 100644 (file)
@@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/wrbuf.h>
 #include <yaz/log.h>
 #include <yaz/yaz-version.h>
+#include <yaz/xml_get.h>
 #include <ctype.h>
 #include <assert.h>
 #include <string.h>
@@ -166,11 +167,10 @@ int pp2_charset_fact_add(pp2_charset_fact_t pft,
 }
 
 int pp2_charset_fact_define(pp2_charset_fact_t pft,
-                            xmlNode *xml_node, const char *default_id)
+                            xmlNode *xml_node, const char *id)
 {
     int r;
     pp2_charset_t pct;
-    xmlChar *id = 0;
 
     assert(xml_node);
 
@@ -190,20 +190,17 @@ int pp2_charset_fact_define(pp2_charset_fact_t pft,
     pct = pp2_charset_create_xml(xml_node);
     if (!pct)
         return -1;
-    if (!default_id)
+    if (!id)
     {
-        id = xmlGetProp(xml_node, (xmlChar*) "id");
+        id = yaz_xml_get_prop(xml_node, "id");
         if (!id)
         {
             yaz_log(YLOG_WARN, "Missing id for icu_chain");
             pp2_charset_destroy(pct);
             return -1;
         }
-        default_id = (const char *) id;
     }
-    r = pp2_charset_fact_add(pft, pct, default_id);
-    if (id)
-        xmlFree(id);
+    r = pp2_charset_fact_add(pft, pct, id);
     return r;
 }
 
index 56af317..9e238a6 100644 (file)
@@ -56,6 +56,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/querytowrbuf.h>
 #include <yaz/oid_db.h>
 #include <yaz/snprintf.h>
+#include <yaz/xml_get.h>
 
 #define USE_TIMING 0
 #if USE_TIMING
@@ -1571,7 +1572,7 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
             continue;
         if (!strcmp((const char *) n->name, "metadata"))
         {
-            xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
+            const char *type = yaz_xml_get_prop(n, "type");
             if (type == NULL) {
                 yaz_log(YLOG_FATAL, "Missing type attribute on metadata element. Skipping!");
             }
@@ -1590,7 +1591,6 @@ static int get_mergekey_from_doc(xmlDoc *doc, xmlNode *root, const char *name,
                 if (value)
                     xmlFree(value);
             }
-            xmlFree(type);
         }
     }
     return no_found;
@@ -1603,7 +1603,7 @@ static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
 {
     char *mergekey_norm = 0;
     WRBUF norm_wr = wrbuf_alloc();
-    xmlChar *mergekey;
+    const char *mergekey;
 
     if (session_mergekey)
     {
@@ -1615,10 +1615,9 @@ static const char *get_mergekey(xmlDoc *doc, xmlNode *root,
         for (i = 0; i < num; i++)
             get_mergekey_from_doc(doc, root, values[i], service, norm_wr);
     }
-    else if ((mergekey = xmlGetProp(root, (xmlChar *) "mergekey")))
+    else if ((mergekey = yaz_xml_get_prop(root, "mergekey")))
     {
-        mergekey_norm_wr(service->charsets, norm_wr, (const char *) mergekey);
-        xmlFree(mergekey);
+        mergekey_norm_wr(service->charsets, norm_wr, mergekey);
     }
     else
     {
@@ -1685,7 +1684,7 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
             continue;
         if (!strcmp((const char *) n->name, "metadata"))
         {
-            xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
+            const char *type = yaz_xml_get_prop(n, "type");
             if (type)
             {
                 size_t len;
@@ -1713,7 +1712,6 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
                     }
                     xmlFree(value);
                 }
-                xmlFree(type);
             }
         }
     }
@@ -1721,6 +1719,8 @@ static int check_record_filter(xmlNode *root, struct session_database *sdb)
 }
 
 static int ingest_to_cluster(struct client *cl,
+                             WRBUF wrbuf_disp,
+                             WRBUF wrbuf_norm,
                              xmlDoc *xdoc,
                              xmlNode *root,
                              int record_no,
@@ -1733,6 +1733,7 @@ static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
 {
     int ret = 0;
     struct session *se = client_get_session(cl);
+    WRBUF wrbuf_disp, wrbuf_norm;
 
     if (!check_record_filter(root, sdb))
     {
@@ -1741,11 +1742,15 @@ static int ingest_sub_record(struct client *cl, xmlDoc *xdoc, xmlNode *root,
                     record_no, sdb->database->id);
         return 0;
     }
+    wrbuf_disp = wrbuf_alloc();
+    wrbuf_norm = wrbuf_alloc();
     session_enter(se, "ingest_sub_record");
     if (client_get_session(cl) == se && se->relevance)
-        ret = ingest_to_cluster(cl, xdoc, root, record_no, mergekeys);
+        ret = ingest_to_cluster(cl, wrbuf_disp, wrbuf_norm,
+                                xdoc, root, record_no, mergekeys);
     session_leave(se, "ingest_sub_record");
-
+    wrbuf_destroy(wrbuf_norm);
+    wrbuf_destroy(wrbuf_disp);
     return ret;
 }
 
@@ -2026,14 +2031,14 @@ static int check_limit_local(struct client *cl,
 }
 
 static int ingest_to_cluster(struct client *cl,
+                             WRBUF wrbuf_disp,
+                             WRBUF wrbuf_norm,
                              xmlDoc *xdoc,
                              xmlNode *root,
                              int record_no,
                              struct record_metadata_attr *merge_keys)
 {
     xmlNode *n;
-    xmlChar *type = 0;
-    xmlChar *value = 0;
     struct session *se = client_get_session(cl);
     struct conf_service *service = se->service;
     int term_factor = 1;
@@ -2050,12 +2055,6 @@ static int ingest_to_cluster(struct client *cl,
 
     for (n = root->children; n; n = n->next)
     {
-        if (type)
-            xmlFree(type);
-        if (value)
-            xmlFree(value);
-        type = value = 0;
-
         if (n->type != XML_ELEMENT_NODE)
             continue;
         if (!strcmp((const char *) n->name, "metadata"))
@@ -2064,20 +2063,26 @@ static int ingest_to_cluster(struct client *cl,
             struct record_metadata **wheretoput = 0;
             struct record_metadata *rec_md = 0;
             int md_field_id = -1;
+            xmlChar *value0;
+            const char *type = yaz_xml_get_prop(n, "type");
 
-            type = xmlGetProp(n, (xmlChar *) "type");
-            value = xmlNodeListGetString(xdoc, n->children, 1);
             if (!type)
                 continue;
-            if (!value || !*value)
+            wrbuf_rewind(wrbuf_disp);
+            value0 = xmlNodeListGetString(xdoc, n->children, 1);
+            if (!value0 || !*value0)
             {
-                xmlChar *empty = xmlGetProp(n, (xmlChar *) "empty");
+                const char *empty = yaz_xml_get_prop(n, "empty");
                 if (!empty)
                     continue;
-                if (value)
-                    xmlFree(value);
-                value = empty;
+                wrbuf_puts(wrbuf_disp, (const char *) empty);
             }
+            else
+            {
+                wrbuf_puts(wrbuf_disp, (const char *) value0);
+            }
+            if (value0)
+                xmlFree(value0);
             md_field_id
                 = conf_service_metadata_field_id(service, (const char *) type);
             if (md_field_id < 0)
@@ -2094,12 +2099,12 @@ static int ingest_to_cluster(struct client *cl,
             ser_md = &service->metadata[md_field_id];
 
             // non-merged metadata
-            rec_md = record_metadata_init(se->nmem, (const char *) value,
+            rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
                                           ser_md->type, n->properties);
             if (!rec_md)
             {
                 session_log(se, YLOG_WARN, "bad metadata data '%s' "
-                            "for element '%s'", value, type);
+                            "for element '%s'", wrbuf_cstr(wrbuf_disp), type);
                 continue;
             }
 
@@ -2107,7 +2112,7 @@ static int ingest_to_cluster(struct client *cl,
             {
                 WRBUF w = wrbuf_alloc();
                 if (relevance_snippet(se->relevance,
-                                      (char*) value, ser_md->name, w))
+                                      wrbuf_cstr(wrbuf_disp), ser_md->name, w))
                     rec_md->data.text.snippet = nmem_strdup(se->nmem,
                                                             wrbuf_cstr(w));
                 wrbuf_destroy(w);
@@ -2123,20 +2128,12 @@ static int ingest_to_cluster(struct client *cl,
 
     if (check_limit_local(cl, record, record_no))
     {
-        if (type)
-            xmlFree(type);
-        if (value)
-            xmlFree(value);
         return -2;
     }
     cluster = reclist_insert(se->reclist, se->relevance, service, record,
                              merge_keys, &se->total_merged);
     if (!cluster)
     {
-        if (type)
-            xmlFree(type);
-        if (value)
-            xmlFree(value);
         return 0; // complete match with existing record
     }
 
@@ -2174,12 +2171,6 @@ static int ingest_to_cluster(struct client *cl,
     // now parsing XML record and adding data to cluster or record metadata
     for (n = root->children; n; n = n->next)
     {
-        if (type)
-            xmlFree(type);
-        if (value)
-            xmlFree(value);
-        type = value = 0;
-
         if (n->type != XML_ELEMENT_NODE)
             continue;
         if (!strcmp((const char *) n->name, "metadata"))
@@ -2191,12 +2182,13 @@ static int ingest_to_cluster(struct client *cl,
             int md_field_id = -1;
             int sk_field_id = -1;
             const char *rank = 0;
-            xmlChar *xml_rank = 0;
-
-            type = xmlGetProp(n, (xmlChar *) "type");
-            value = xmlNodeListGetString(xdoc, n->children, 1);
+            const char *xml_rank = 0;
+            const char *type = 0;
+            xmlChar *value0;
 
-            if (!type || !value || !*value)
+            wrbuf_rewind(wrbuf_disp);
+            type = yaz_xml_get_prop(n, "type");
+            if (!type)
                 continue;
 
             md_field_id
@@ -2212,8 +2204,19 @@ static int ingest_to_cluster(struct client *cl,
                 ser_sk = &service->sortkeys[sk_field_id];
             }
 
+            value0 = xmlNodeListGetString(xdoc, n->children, 1);
+            if (!value0 || !*value0)
+            {
+                if (value0)
+                    xmlFree(value0);
+                continue;
+            }
+            wrbuf_puts(wrbuf_disp, (const char *) value0);
+            xmlFree(value0);
+
+
             // merged metadata
-            rec_md = record_metadata_init(se->nmem, (const char *) value,
+            rec_md = record_metadata_init(se->nmem, wrbuf_cstr(wrbuf_disp),
                                           ser_md->type, 0);
 
             // see if the field was not in cluster already (from beginning)
@@ -2240,7 +2243,7 @@ static int ingest_to_cluster(struct client *cl,
             }
             else
             {
-                xml_rank = xmlGetProp(n, (xmlChar *) "rank");
+                xml_rank = yaz_xml_get_prop(n, "rank");
                 rank = xml_rank ? (const char *) xml_rank : ser_md->rank;
             }
 
@@ -2340,7 +2343,8 @@ static int ingest_to_cluster(struct client *cl,
             if (rank)
             {
                 relevance_countwords(se->relevance, cluster,
-                                     (char *) value, rank, ser_md->name);
+                                     wrbuf_cstr(wrbuf_disp),
+                                     rank, ser_md->name);
             }
             // construct facets ... unless the client already has reported them
             if (ser_md->termlist && !client_has_facet(cl, (char *) type))
@@ -2358,15 +2362,8 @@ static int ingest_to_cluster(struct client *cl,
                     }
                 }
                 else
-                    add_facet(se, (char *) type, (char *) value, term_factor);
+                    add_facet(se, type, wrbuf_cstr(wrbuf_disp), term_factor);
             }
-
-            // cleaning up
-            if (xml_rank)
-                xmlFree(xml_rank);
-            xmlFree(type);
-            xmlFree(value);
-            type = value = 0;
         }
         else
         {
@@ -2376,11 +2373,6 @@ static int ingest_to_cluster(struct client *cl,
             se->number_of_warnings_unknown_elements++;
         }
     }
-    if (type)
-        xmlFree(type);
-    if (value)
-        xmlFree(value);
-
     nmem_destroy(ingest_nmem);
     xfree(metadata0);
     relevance_donerecord(se->relevance, cluster);
index ae92345..1c52b30 100644 (file)
@@ -32,6 +32,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <stdio.h>
 #include <sys/types.h>
 #include <yaz/dirent.h>
+#include <yaz/xml_get.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 
@@ -176,11 +177,10 @@ int settings_read_node_x(xmlNode *n,
                                      struct setting *set))
 {
     int ret_val = 0; /* success */
-    char *namea = (char *) xmlGetProp(n, (xmlChar *) "name");
-    char *targeta = (char *) xmlGetProp(n, (xmlChar *) "target");
-    char *valuea = (char *) xmlGetProp(n, (xmlChar *) "value");
-    char *usera = (char *) xmlGetProp(n, (xmlChar *) "user");
-    char *precedencea = (char *) xmlGetProp(n, (xmlChar *) "precedence");
+    const char *namea = yaz_xml_get_prop(n, "name");
+    const char *targeta = yaz_xml_get_prop(n, "target");
+    const char *valuea = yaz_xml_get_prop(n, "value");
+    const char *precedencea = yaz_xml_get_prop(n, "precedence");
 
     for (n = n->children; n; n = n->next)
     {
@@ -190,11 +190,10 @@ int settings_read_node_x(xmlNode *n,
         {
             xmlNode *root = n->children;
             struct setting set;
-            char *name = (char *) xmlGetProp(n, (xmlChar *) "name");
-            char *target = (char *) xmlGetProp(n, (xmlChar *) "target");
-            char *value = (char *) xmlGetProp(n, (xmlChar *) "value");
-            char *user = (char *) xmlGetProp(n, (xmlChar *) "user");
-            char *precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
+            const char *name = yaz_xml_get_prop(n, "name");
+            const char *target = yaz_xml_get_prop(n, "target");
+            const char *value = yaz_xml_get_prop(n, "value");
+            const char *precedence = yaz_xml_get_prop(n, "precedence");
             xmlChar *buf_out = 0;
 
             set.next = 0;
@@ -231,7 +230,7 @@ int settings_read_node_x(xmlNode *n,
                     xmlDocSetRootElement(doc, xmlCopyNode(root, 1));
                     xmlDocDumpMemory(doc, &buf_out, &len_out);
                     /* xmlDocDumpMemory 0-terminates */
-                    set.value = (char *) buf_out;
+                    set.value = (const char *) buf_out;
                     xmlFreeDoc(doc);
                 }
             }
@@ -248,11 +247,6 @@ int settings_read_node_x(xmlNode *n,
                 ret_val = -1;
             }
             xmlFree(buf_out);
-            xmlFree(name);
-            xmlFree(precedence);
-            xmlFree(value);
-            xmlFree(user);
-            xmlFree(target);
         }
         else
         {
@@ -261,11 +255,6 @@ int settings_read_node_x(xmlNode *n,
             ret_val = -1;
         }
     }
-    xmlFree(namea);
-    xmlFree(precedencea);
-    xmlFree(valuea);
-    xmlFree(usera);
-    xmlFree(targeta);
     return ret_val;
 }
 
index cd01937..6378a8a 100644 (file)
@@ -65,9 +65,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 struct setting
 {
     int precedence;
-    char *target;
-    char *name;
-    char *value;
+    const char *target;
+    const char *name;
+    const char *value;
     struct setting *next;
 };