Remove timeout member from COMSTACK
[yaz-moved-to-github.git] / src / marc_read_xml.c
index 2ed1df1..7356ca2 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2009 Index Data
+ * Copyright (C) 1995-2013 Index Data
  * See the file LICENSE for details.
  */
 
@@ -18,7 +18,6 @@
 
 #include <stdio.h>
 #include <string.h>
-#include <ctype.h>
 #include <yaz/marcdisp.h>
 #include <yaz/wrbuf.h>
 #include <yaz/yaz-util.h>
@@ -60,7 +59,7 @@ int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
                 }
                 if (ptr_code->type == XML_TEXT_NODE)
                 {
-                    ctrl_data_len = 
+                    ctrl_data_len =
                         strlen((const char *)ptr_code->content);
                 }
                 else
@@ -90,9 +89,84 @@ int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
     return 0;
 }
 
-static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
+const char *tag_value_extract(const char *name, char tag_buffer[5])
+{
+    size_t length = strlen(name);
+    if (length == 3)
+    {
+        strcpy(tag_buffer, name);
+        return tag_buffer;
+    }
+    return 0;
+}
+
+// Given a xmlNode ptr,  extract a value from either a element name or from a given attribute
+char *element_attribute_value_extract(const xmlNode *ptr,
+                                      const char *attribute_name,
+                                      NMEM nmem)
+{
+    const char *name = (const char *) ptr->name;
+    size_t length = strlen(name);
+    xmlAttr *attr;
+    if (length > 1 )
+        return nmem_strdup(nmem, name+1);
+    // TODO Extract from attribute where matches attribute_name
+    for (attr = ptr->properties; attr; attr = attr->next)
+        if (!strcmp((const char *)attr->name, attribute_name))
+            return nmem_text_node_cdata(attr->children, nmem);
+    return 0;
+}
+
+
+int yaz_marc_read_turbo_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
+{
+    for (; ptr; ptr = ptr->next)
+    {
+        if (ptr->type == XML_ELEMENT_NODE)
+        {
+            if (!strncmp((const char *) ptr->name, "s", 1))
+            {
+                NMEM nmem = yaz_marc_get_nmem(mt);
+                xmlNode *p;
+               size_t ctrl_data_len = 0;
+                char *ctrl_data_buf = 0;
+                const char *tag_value = element_attribute_value_extract(ptr, "code", nmem);
+                if (!tag_value)
+                {
+                    yaz_marc_cprintf(
+                        mt, "Missing 'code' value for 'subfield'" );
+                    return -1;
+                }
+
+                ctrl_data_len = strlen((const char *) tag_value);
+                // Extract (length) from CDATA
+                for (p = ptr->children; p ; p = p->next)
+                    if (p->type == XML_TEXT_NODE)
+                        ctrl_data_len += strlen((const char *)p->content);
+                // Allocate memory for code value (1 character (can be multi-byte) and data
+                ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
+                // Build a string with "<Code><data>"
+                strcpy(ctrl_data_buf, (const char *) tag_value);
+                for (p = ptr->children; p ; p = p->next)
+                    if (p->type == XML_TEXT_NODE)
+                        strcat(ctrl_data_buf, (const char *)p->content);
+                yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
+            }
+            else
+            {
+                yaz_marc_cprintf(
+                    mt, "Expected element 'subfield', got '%.80s'", ptr->name);
+                return -1;
+            }
+        }
+    }
+    return 0;
+}
+
+
+static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p,
+                                    int *indicator_length)
 {
-    int indicator_length;
     int identifier_length;
     int base_address;
     int length_data_entry;
@@ -104,24 +178,21 @@ static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
     for(; ptr; ptr = ptr->next)
         if (ptr->type == XML_ELEMENT_NODE)
         {
-            if (!strcmp((const char *) ptr->name, "leader"))
+            if ( !strcmp( (const char *) ptr->name, "leader") ||
+                 (!strncmp((const char *) ptr->name, "l", 1) ))
             {
                 xmlNode *p = ptr->children;
                 for(; p; p = p->next)
                     if (p->type == XML_TEXT_NODE)
                         leader = (const char *) p->content;
-                break;
-            }
-            else
-            {
-                yaz_marc_cprintf(
-                    mt, "Expected element 'leader', got '%.80s'", ptr->name);
+                ptr = ptr->next;
             }
+            break;
         }
     if (!leader)
     {
-        yaz_marc_cprintf(mt, "Missing element 'leader'");
-        return -1;
+        yaz_marc_cprintf(mt, "Missing leader. Inserting fake leader");
+        leader = "00000nam a22000000a 4500";
     }
     if (strlen(leader) != 24)
     {
@@ -130,7 +201,7 @@ static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
         return -1;
     }
     yaz_marc_set_leader(mt, leader,
-                        &indicator_length,
+                        indicator_length,
                         &identifier_length,
                         &base_address,
                         &length_data_entry,
@@ -140,12 +211,13 @@ static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
     return 0;
 }
 
-static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
+static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr,
+                                    int indicator_length)
 {
     for(; ptr; ptr = ptr->next)
         if (ptr->type == XML_ELEMENT_NODE)
         {
-            if (!strcmp((const char *) ptr->name, "controlfield"))
+            if (!strcmp( (const char *) ptr->name, "controlfield"))
             {
                 const xmlNode *ptr_tag = 0;
                 struct _xmlAttr *attr;
@@ -173,18 +245,29 @@ static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
                 const xmlNode *ptr_tag = 0;
                 struct _xmlAttr *attr;
                 int i;
-                for (i = 0; i<11; i++)
-                    indstr[i] = '\0';
+                for (i = 0; i < indicator_length; i++)
+                    indstr[i] = ' ';
+                indstr[i] = '\0';
                 for (attr = ptr->properties; attr; attr = attr->next)
                     if (!strcmp((const char *)attr->name, "tag"))
                         ptr_tag = attr->children;
                     else if (strlen((const char *)attr->name) == 4 &&
                              !memcmp(attr->name, "ind", 3))
                     {
-                        int no = atoi((const char *)attr->name+3);
-                        if (attr->children
-                            && attr->children->type == XML_TEXT_NODE)
-                            indstr[no] = attr->children->content[0];
+                        int no = atoi((const char *)attr->name + 3);
+                        if (attr->children &&
+                            attr->children->type == XML_TEXT_NODE &&
+                            no <= indicator_length && no > 0 &&
+                            attr->children->content[0])
+                        {
+                            indstr[no - 1] = attr->children->content[0];
+                        }
+                        else
+                        {
+                            yaz_marc_cprintf(
+                                mt, "Bad attribute '%.80s' for 'datafield'",
+                                attr->name);
+                        }
                     }
                     else
                     {
@@ -198,10 +281,8 @@ static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
                         mt, "Missing attribute 'tag' for 'datafield'" );
                     return -1;
                 }
-                /* note that indstr[0] is unused so we use indstr[1..] */
                 yaz_marc_add_datafield_xml(mt, ptr_tag,
-                                           indstr+1, strlen(indstr+1));
-                
+                                           indstr, indicator_length);
                 if (yaz_marc_read_xml_subfields(mt, ptr->children))
                     return -1;
             }
@@ -215,18 +296,105 @@ static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
         }
     return 0;
 }
+
+
+static int yaz_marc_read_turbo_xml_fields(yaz_marc_t mt, const xmlNode *ptr,
+                                          int indicator_length)
+{
+    for(; ptr; ptr = ptr->next)
+        if (ptr->type == XML_ELEMENT_NODE)
+        {
+            if (!strncmp( (const char *) ptr->name, "c", 1))
+            {
+                NMEM nmem = yaz_marc_get_nmem(mt);
+                char *tag_value = element_attribute_value_extract(ptr, "tag", nmem);
+                if (!tag_value)
+                {
+                    yaz_marc_cprintf(
+                        mt, "Missing attribute 'tag' for 'controlfield'" );
+                    return -1;
+                }
+                yaz_marc_add_controlfield_xml2(mt, tag_value, ptr->children);
+            }
+            else if (!strncmp((const char *) ptr->name, "d",1))
+            {
+                struct _xmlAttr *attr;
+                NMEM nmem = yaz_marc_get_nmem(mt);
+                char *tag_value;
+                char *indstr = nmem_malloc(nmem, indicator_length + 1);
+                int i = 0;
+                for (i = 0; i < indicator_length; i++)
+                    indstr[i] = ' ';
+                indstr[i] = '\0';
+                tag_value = element_attribute_value_extract(ptr, "tag", nmem);
+                if (!tag_value)
+                {
+                    yaz_marc_cprintf(
+                        mt, "Missing attribute 'tag' for 'datafield'" );
+                    return -1;
+                }
+                for (attr = ptr->properties; attr; attr = attr->next)
+                    if (strlen((const char *)attr->name) == 2 &&
+                        attr->name[0] == 'i')
+                    {
+                       //extract indicator attribute from i#="Y" pattern
+                        int no = atoi((const char *)attr->name + 1);
+                        if (attr->children &&
+                            attr->children->type == XML_TEXT_NODE &&
+                            no <= indicator_length && no > 0 &&
+                            attr->children->content[0])
+                        {
+                            indstr[no - 1] = attr->children->content[0];
+                        }
+                        else
+                        {
+                            yaz_marc_cprintf(
+                                mt, "Bad attribute '%.80s' for 'd'",attr->name);
+                        }
+                    }
+                    else
+                    {
+                        yaz_marc_cprintf(
+                            mt, "Bad attribute '%.80s' for 'd'", attr->name);
+                    }
+                yaz_marc_add_datafield_xml2(mt, tag_value, indstr);
+                if (yaz_marc_read_turbo_xml_subfields(mt, ptr->children /*, indstr */))
+                    return -1;
+            }
+            else
+            {
+                yaz_marc_cprintf(mt,
+                                 "Expected element controlfield or datafield,"
+                                 " got %.80s", ptr->name);
+                return -1;
+            }
+        }
+    return 0;
+}
+
+
 #endif
 
 #if YAZ_HAVE_XML2
 int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
 {
+    int indicator_length = 0;
+    int format = 0;
     yaz_marc_reset(mt);
 
     for(; ptr; ptr = ptr->next)
         if (ptr->type == XML_ELEMENT_NODE)
         {
             if (!strcmp((const char *) ptr->name, "record"))
+            {
+                format = YAZ_MARC_MARCXML;
+                break;
+            }
+            else if (!strcmp((const char *) ptr->name, "r"))
+            {
+                format = YAZ_MARC_TURBOMARC;
                 break;
+            }
             else
             {
                 yaz_marc_cprintf(
@@ -242,9 +410,17 @@ int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
     }
     /* ptr points to record node now */
     ptr = ptr->children;
-    if (yaz_marc_read_xml_leader(mt, &ptr))
+    if (yaz_marc_read_xml_leader(mt, &ptr, &indicator_length))
         return -1;
-    return yaz_marc_read_xml_fields(mt, ptr->next);
+
+    switch (format)
+    {
+    case YAZ_MARC_MARCXML:
+        return yaz_marc_read_xml_fields(mt, ptr, indicator_length);
+    case YAZ_MARC_TURBOMARC:
+        return yaz_marc_read_turbo_xml_fields(mt, ptr, indicator_length);
+    }
+    return -1;
 }
 #endif
 
@@ -252,6 +428,7 @@ int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab