Complete XML to OPAC conversion
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 29 Jan 2013 13:46:21 +0000 (14:46 +0100)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 29 Jan 2013 13:46:21 +0000 (14:46 +0100)
include/yaz/marcdisp.h
src/xml_to_opac.c
test/test_record_conv.c

index 3c98b5b..752de41 100644 (file)
@@ -441,14 +441,16 @@ YAZ_EXPORT void yaz_opac_decode_wrbuf2(yaz_marc_t mt, Z_OPACRecord *r,
 #if YAZ_HAVE_XML2
 /** \brief Converts XML to OPAC
     \param mt marc handle 
-    \param src XML root node (presumably opacRecord node)
+    \param buf_in XML buffer
+    \param size_in size of XML buffer
     \param dst Z39.50 OPAC result - allocated by NMEM on marc handle
     \param cd iconv handle for the OPAC content (not ISO2709 part)
     \param nmem memory for OPACRecord (if NULL, mt NMEM memory is used)
     \retval 1 conversion OK
     \retval 0 conversion NOT OK
     \*/
-YAZ_EXPORT int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *src,
+YAZ_EXPORT int yaz_xml_to_opac(yaz_marc_t mt,
+                               const char *buf_in, size_t size_in,
                                Z_OPACRecord **dst, yaz_iconv_t cd,
                                NMEM nmem);
 #endif
index 004f308..e6735c9 100644 (file)
@@ -63,6 +63,37 @@ static int match_element_next(xmlNode **ptr, const char *elem, NMEM nmem,
     return 0;
 }
 
+static int match_v_next(xmlNode **ptr, const char *elem, NMEM nmem,
+                        Odr_bool **val)
+{
+    while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
+        (*ptr) = (*ptr)->next;
+    *val = nmem_booldup(nmem, 0);
+    if (*ptr && match_element(*ptr, elem))
+    {
+        struct _xmlAttr *attr = (*ptr)->properties;
+
+        *ptr = (*ptr)->next;
+        for (; attr; attr = attr->next)
+        {
+            if (!strcmp((const char *) attr->name, "value"))
+            {
+                if (attr->children->type == XML_TEXT_NODE)
+                {
+                    if (attr->children->content[0] == '0')
+                        return 1;
+                    else if (attr->children->content[0] == '1')
+                    {
+                        **val = 1;
+                        return 1;
+                    }
+                }
+            }
+        }
+    }
+    return 0;
+}
+
 static int bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
                                yaz_iconv_t cd, NMEM nmem)
 {
@@ -100,6 +131,8 @@ static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
     {
         while (ptr && ptr->type != XML_ELEMENT_NODE)
             ptr = ptr->next;
+        if (!ptr)
+            break;
         if (!match_element(ptr, "volume"))
             return 0;
         ptr = ptr->next;
@@ -111,6 +144,8 @@ static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
     {
         while (ptr && ptr->type != XML_ELEMENT_NODE)
             ptr = ptr->next;
+        if (!ptr)
+            break;
         if (!match_element(ptr, "volume"))
             return 0;
         volume(ptr->children, (*volp) + i, nmem);
@@ -119,10 +154,57 @@ static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
     return 1;
 }
 
-static void circulations(xmlNode *ptr, Z_CircRecord ***circp,
-                         int *num, NMEM nmem)
+static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
 {
+    *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
 
+    match_v_next(&ptr, "availableNow", nmem, &(*circp)->availableNow);
+    /* note the spelling of the ASN.1 member below */
+    match_element_next(&ptr,     "availabilityDate", nmem,
+                       &(*circp)->availablityDate);
+    match_element_next(&ptr, "availableThru", nmem, &(*circp)->availableThru);
+    match_element_next(&ptr, "restrictions", nmem, &(*circp)->restrictions);
+    match_element_next(&ptr, "itemId", nmem, &(*circp)->itemId);
+    match_v_next(&ptr, "renewable", nmem, &(*circp)->renewable);
+    match_v_next(&ptr, "onHold", nmem, &(*circp)->onHold);
+    match_element_next(&ptr, "enumAndChron", nmem, &(*circp)->enumAndChron);
+    match_element_next(&ptr, "midspine", nmem, &(*circp)->midspine);
+    match_element_next(&ptr, "temporaryLocation", nmem,
+                       &(*circp)->temporaryLocation);
+    return 1;
+}
+
+static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
+                        int *num, NMEM nmem)
+{
+    int i;
+    xmlNode *ptr0 = ptr;
+
+    for (i = 0; ptr; i++)
+    {
+        while (ptr && ptr->type != XML_ELEMENT_NODE)
+            ptr = ptr->next;
+        if (!ptr)
+            break;
+        if (!match_element(ptr, "circulation"))
+            return 0;
+        ptr = ptr->next;
+    }
+    *num = i;
+    *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
+    ptr = ptr0;
+    for (i = 0; ptr; i++)
+    {
+        while (ptr && ptr->type != XML_ELEMENT_NODE)
+            ptr = ptr->next;
+        if (!ptr)
+            break;
+        if (!match_element(ptr, "circulation"))
+            return 0;
+        circulation(ptr->children, (*circp) + i, nmem);
+        ptr = ptr->next;
+    }
+    return 1;
 }
 
 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
@@ -176,8 +258,9 @@ static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
     return 1;
 }
 
-int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *ptr, Z_OPACRecord **dst,
-                    yaz_iconv_t cd, NMEM nmem)
+static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
+                               Z_OPACRecord **dst,
+                               yaz_iconv_t cd, NMEM nmem)
 {
     int i;
     Z_External *ext = 0;
@@ -200,6 +283,7 @@ int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *ptr, Z_OPACRecord **dst,
     opac->holdingsData = 0;
     opac->bibliographicRecord = ext;
 
+    ptr = ptr->next;
     while (ptr && ptr->type != XML_ELEMENT_NODE)
         ptr = ptr->next;
     if (!match_element(ptr, "holdings"))
@@ -236,6 +320,21 @@ int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *ptr, Z_OPACRecord **dst,
     }
     return 1;
 }
+
+int yaz_xml_to_opac(yaz_marc_t mt, const char *buf_in, size_t size_in,
+                    Z_OPACRecord **dst, yaz_iconv_t cd, NMEM nmem)
+{
+    xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
+    int r = 0;
+    if (doc)
+    {
+        r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem);
+        xmlFreeDoc(doc);
+    }
+    return r;
+}
+
+
 #endif
 
 /*
index 1e1d2b0..3cb5bfd 100644 (file)
@@ -363,7 +363,7 @@ static void tst_convert3(void)
         "  <leader>00077nam a22000498a 4500</leader>\n"
         "  <controlfield tag=\"001\">   11224466 </controlfield>\n"
         "  <datafield tag=\"010\" ind1=\" \" ind2=\" \">\n"
-        "    <subfield code=\"a\">k" "\xc3" "\xb8" /* oslash in UTF_8 */ 
+        "    <subfield code=\"a\">k" "\xc3" "\xb8" /* oslash in UTF_8 */
         "benhavn</subfield>\n"
         "  </datafield>\n"
         "</record>\n"
@@ -379,9 +379,22 @@ static void tst_convert3(void)
         "  <callNumber>MLCM 89/00602 (N)</callNumber>\n"
         "  <shelvingData>FT MEADE</shelvingData>\n"
         "  <copyNumber>Copy 1</copyNumber>\n"
+        "  <volumes>\n"
+        "   <volume>\n"
+        "    <enumeration>1</enumeration>\n"
+        "    <chronology>2</chronology>\n"
+        "    <enumAndChron>3</enumAndChron>\n"
+        "   </volume>\n"
+        "   <volume>\n"
+        "    <enumeration>1</enumeration>\n"
+        "    <chronology>2</chronology>\n"
+        "    <enumAndChron>3</enumAndChron>\n"
+        "   </volume>\n"
+        "  </volumes>\n"
         "  <circulations>\n"
         "   <circulation>\n"
         "    <availableNow value=\"1\"/>\n"
+        "    <availabilityDate>20130129</availabilityDate>\n"
         "    <itemId>1226176</itemId>\n"
         "    <renewable value=\"0\"/>\n"
         "    <onHold value=\"0\"/>\n"
@@ -425,15 +438,27 @@ static void tst_convert3(void)
     h->reproductionNote = 0;
     h->termsUseRepro = 0;
     h->enumAndChron = 0;
-    h->num_volumes = 0;
+    h->num_volumes = 2;
     h->volumes = 0;
+
+    h->volumes = (Z_Volume **)
+        nmem_malloc(nmem, 2 * sizeof(Z_Volume *));
+
+    h->volumes[0] = (Z_Volume *)
+        nmem_malloc(nmem, sizeof(Z_Volume));
+    h->volumes[1] = h->volumes[0];
+
+    h->volumes[0]->enumeration = nmem_strdup(nmem, "1");
+    h->volumes[0]->chronology = nmem_strdup(nmem, "2");
+    h->volumes[0]->enumAndChron = nmem_strdup(nmem, "3");
+
     h->num_circulationData = 1;
     h->circulationData = (Z_CircRecord **)
         nmem_malloc(nmem, 1 * sizeof(Z_CircRecord *));
     circ = h->circulationData[0] = (Z_CircRecord *)
         nmem_malloc(nmem, sizeof(Z_CircRecord));
     circ->availableNow = nmem_booldup(nmem, 1);
-    circ->availablityDate = 0;
+    circ->availablityDate = nmem_strdup(nmem, "20130129");
     circ->availableThru = 0;
     circ->restrictions = 0;
     circ->itemId = nmem_strdup(nmem, "1226176");
@@ -475,6 +500,44 @@ static void tst_convert3(void)
         yaz_record_conv_destroy(p);
         wrbuf_destroy(output_record);
     }
+    {
+        Z_OPACRecord *opac = 0;
+        yaz_marc_t mt =  yaz_marc_create();
+        ret = yaz_xml_to_opac(mt, opacxml_rec, strlen(opacxml_rec),
+                              &opac, 0 /* iconv */, nmem);
+        YAZ_CHECK(ret);
+        YAZ_CHECK(opac);
+
+        if (opac)
+        {
+            WRBUF output_record = wrbuf_alloc();
+            char *p;
+
+            yaz_marc_xml(mt, YAZ_MARC_MARCXML);
+            yaz_opac_decode_wrbuf(mt, opac, output_record);
+
+            /* change MARC size to 00077 from 00078, due to
+               encoding of the aring (two bytes in UTF-8) */
+            p = strstr(wrbuf_buf(output_record), "00078");
+            YAZ_CHECK(p);
+            if (p)
+                p[4] = '7';
+
+            ret = strcmp(wrbuf_cstr(output_record), opacxml_rec);
+            YAZ_CHECK(ret == 0);
+            if (ret)
+            {
+                printf("got-output_record len=%ld: %s\n",
+                       (long) wrbuf_len(output_record),
+                       wrbuf_cstr(output_record));
+                printf("output_expect_record len=%ld %s\n",
+                       (long) strlen(opacxml_rec),
+                       opacxml_rec);
+            }
+            wrbuf_destroy(output_record);
+        }
+        yaz_marc_destroy(mt);
+    }
     nmem_destroy(nmem);
 }