For Libxml2 and friends, YAZ defines YAZ_HAVE_{XML2,XSLT,EXSLT) in
[yaz-moved-to-github.git] / src / marcdisp.c
index 846c38f..2b06b2a 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: marcdisp.c,v 1.27 2006-04-20 19:47:01 adam Exp $
+ * $Id: marcdisp.c,v 1.31 2006-07-06 10:17:53 adam Exp $
  */
 
 /**
@@ -26,7 +26,7 @@
 #include <yaz/wrbuf.h>
 #include <yaz/yaz-util.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #include <libxml/tree.h>
 #endif
@@ -132,25 +132,6 @@ void yaz_marc_add_comment(yaz_marc_t mt, char *comment)
     n->u.comment = nmem_strdup(mt->nmem, comment);
 }
 
-#if HAVE_XML2
-static char *yaz_marc_get_xml_text(const xmlNode *ptr_cdata, NMEM nmem)
-{
-    char *cdata;
-    int len = 0;
-    const xmlNode *ptr;
-
-    for (ptr = ptr_cdata; ptr; ptr = ptr->next)
-        if (ptr->type == XML_TEXT_NODE)
-            len += xmlStrlen(ptr->content);
-    cdata = (char *) nmem_malloc(nmem, len+1);
-    *cdata = '\0';
-    for (ptr = ptr_cdata; ptr; ptr = ptr->next)
-        if (ptr->type == XML_TEXT_NODE)
-            strcat(cdata, (const char *) ptr->content);
-    return cdata;
-}
-#endif
-
 void yaz_marc_cprintf(yaz_marc_t mt, const char *fmt, ...)
 {
     va_list ap;
@@ -200,14 +181,14 @@ void yaz_marc_add_controlfield(yaz_marc_t mt, const char *tag,
     }
 }
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 void yaz_marc_add_controlfield_xml(yaz_marc_t mt, const xmlNode *ptr_tag,
                                    const xmlNode *ptr_data)
 {
     struct yaz_marc_node *n = yaz_marc_add_node(mt);
     n->which = YAZ_MARC_CONTROLFIELD;
-    n->u.controlfield.tag = yaz_marc_get_xml_text(ptr_tag, mt->nmem);
-    n->u.controlfield.data = yaz_marc_get_xml_text(ptr_data, mt->nmem);
+    n->u.controlfield.tag = nmem_text_node_cdata(ptr_tag, mt->nmem);
+    n->u.controlfield.data = nmem_text_node_cdata(ptr_data, mt->nmem);
 }
 #endif
 
@@ -225,13 +206,13 @@ void yaz_marc_add_datafield(yaz_marc_t mt, const char *tag,
     mt->subfield_pp = &n->u.datafield.subfields;
 }
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 void yaz_marc_add_datafield_xml(yaz_marc_t mt, const xmlNode *ptr_tag,
                                 const char *indicator, size_t indicator_len)
 {
     struct yaz_marc_node *n = yaz_marc_add_node(mt);
     n->which = YAZ_MARC_DATAFIELD;
-    n->u.datafield.tag = yaz_marc_get_xml_text(ptr_tag, mt->nmem);
+    n->u.datafield.tag = nmem_text_node_cdata(ptr_tag, mt->nmem);
     n->u.datafield.indicator =
         nmem_strdupn(mt->nmem, indicator, indicator_len);
     n->u.datafield.subfields = 0;
@@ -370,16 +351,6 @@ void yaz_marc_endline_str(yaz_marc_t mt, const char *s)
     mt->endline_str[sizeof(mt->endline_str)-1] = '\0';
 }
 
-static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len, WRBUF wr)
-{
-    if (mt->xml == YAZ_MARC_ISO2709)
-        wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
-    else if (mt->xml == YAZ_MARC_LINE)
-        wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
-    else
-        wrbuf_iconv_write_cdata(wr, mt->iconv_cd, buf, len);
-}
-
 /* try to guess how many bytes the identifier really is! */
 static size_t cdata_one_character(yaz_marc_t mt, const char *buf)
 {
@@ -484,15 +455,24 @@ int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr)
     case YAZ_MARC_MARCXML:
         return yaz_marc_write_marcxml(mt, wr);
     case YAZ_MARC_XCHANGE:
-        return yaz_marc_write_marcxchange(mt, wr);
+        return yaz_marc_write_marcxchange(mt, wr, 0, 0); /* no format, type */
     case YAZ_MARC_ISO2709:
         return yaz_marc_write_iso2709(mt, wr);
     }
     return -1;
 }
 
+/** \brief common MARC XML/Xchange writer
+    \param mt handle
+    \param wr WRBUF output
+    \param ns XMLNS for the elements
+    \param format record format (e.g. "MARC21")
+    \param type record type (e.g. "Bibliographic")
+*/
 static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
-                                     const char *ns)
+                                     const char *ns, 
+                                     const char *format,
+                                     const char *type)
 {
     struct yaz_marc_node *n;
     int identifier_length;
@@ -510,7 +490,12 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
     if (!atoi_n_check(leader+11, 1, &identifier_length))
         return -1;
 
-    wrbuf_printf(wr, "<record xmlns=\"%s\">\n", ns);
+    wrbuf_printf(wr, "<record xmlns=\"%s\"", ns);
+    if (format)
+        wrbuf_printf(wr, " format=\"%.80s\"", format);
+    if (type)
+        wrbuf_printf(wr, " type=\"%.80s\"", type);
+    wrbuf_printf(wr, ">\n");
     for (n = mt->nodes; n; n = n->next)
     {
         struct yaz_marc_subfield *s;
@@ -563,7 +548,9 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
             wrbuf_printf(wr, "</controlfield>\n");
             break;
         case YAZ_MARC_COMMENT:
-            wrbuf_printf(wr, "<!-- %s -->\n", n->u.comment);
+            wrbuf_printf(wr, "<!-- ");
+            wrbuf_puts(wr, n->u.comment);
+            wrbuf_printf(wr, " -->\n");
             break;
         case YAZ_MARC_LEADER:
             wrbuf_printf(wr, "  <leader>");
@@ -580,13 +567,17 @@ static int yaz_marc_write_marcxml_ns(yaz_marc_t mt, WRBUF wr,
 int yaz_marc_write_marcxml(yaz_marc_t mt, WRBUF wr)
 {
     yaz_marc_modify_leader(mt, 9, "a");
-    return yaz_marc_write_marcxml_ns(mt, wr, "http://www.loc.gov/MARC21/slim");
+    return yaz_marc_write_marcxml_ns(mt, wr, "http://www.loc.gov/MARC21/slim",
+                                     0, 0);
 }
 
-int yaz_marc_write_marcxchange(yaz_marc_t mt, WRBUF wr)
+int yaz_marc_write_marcxchange(yaz_marc_t mt, WRBUF wr,
+                               const char *format,
+                               const char *type)
 {
     return yaz_marc_write_marcxml_ns(mt, wr,
-                                     "http://www.bs.dk/standards/MarcXchange");
+                                     "http://www.bs.dk/standards/MarcXchange",
+                                     0, 0);
 }
 
 int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
@@ -681,11 +672,15 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
             wrbuf_printf(wr, "%.*s", indicator_length,
                          n->u.datafield.indicator);
             for (s = n->u.datafield.subfields; s; s = s->next)
-                wrbuf_printf(wr, "%c%s", ISO2709_IDFS, s->code_data);
+            {
+                wrbuf_printf(wr, "%c", ISO2709_IDFS);
+                wrbuf_puts(wr, s->code_data);
+            }
             wrbuf_printf(wr, "%c", ISO2709_FS);
             break;
         case YAZ_MARC_CONTROLFIELD:
-            wrbuf_printf(wr, "%s%c", n->u.controlfield.data, ISO2709_FS);
+            wrbuf_puts(wr, n->u.controlfield.data);
+            wrbuf_printf(wr, "%c", ISO2709_FS);
             break;
         case YAZ_MARC_COMMENT:
             break;
@@ -697,7 +692,7 @@ int yaz_marc_write_iso2709(yaz_marc_t mt, WRBUF wr)
     return 0;
 }
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
 {
     for (; ptr; ptr = ptr->next)