From: Adam Dickmeiss Date: Mon, 17 Dec 2007 20:59:30 +0000 (+0000) Subject: Support read/write MARCXML collections. X-Git-Tag: YAZ.3.0.18~5 X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=commitdiff_plain;h=3eb8be5cd44ecdbd68ec3ca8a54ec86691ff9a4d Support read/write MARCXML collections. --- diff --git a/include/yaz/marcdisp.h b/include/yaz/marcdisp.h index dd92044..75820ce 100644 --- a/include/yaz/marcdisp.h +++ b/include/yaz/marcdisp.h @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* $Id: marcdisp.h,v 1.29 2007-12-16 11:08:50 adam Exp $ */ +/* $Id: marcdisp.h,v 1.30 2007-12-17 20:59:30 adam Exp $ */ /** * \file marcdisp.h @@ -367,6 +367,19 @@ YAZ_EXPORT void yaz_display_OPAC(WRBUF wrbuf, Z_OPACRecord *r, int flags); YAZ_EXPORT void yaz_opac_decode_wrbuf(yaz_marc_t mt, Z_OPACRecord *r, WRBUF wrbuf); +/** \brief flushes records + \param mt handle + \param wrbuf WRBUF for output + \retval 0 OK + \retval -1 ERROR +*/ +YAZ_EXPORT int yaz_marc_write_trailer(yaz_marc_t mt, WRBUF wr); + +/** \brief enables record collection output + \param mt handle +*/ +YAZ_EXPORT void yaz_marc_enable_collection(yaz_marc_t mt); + YAZ_END_CDECL #endif diff --git a/src/marc_read_xml.c b/src/marc_read_xml.c index caf5a55..8f4ffd6 100644 --- a/src/marc_read_xml.c +++ b/src/marc_read_xml.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: marc_read_xml.c,v 1.3 2007-05-06 20:12:20 adam Exp $ + * $Id: marc_read_xml.c,v 1.4 2007-12-17 20:59:30 adam Exp $ */ /** @@ -224,6 +224,8 @@ static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr) int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr) { #if YAZ_HAVE_XML2 + yaz_marc_reset(mt); + for(; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE) { diff --git a/src/marcdisp.c b/src/marcdisp.c index 0dd8aaa..e1c6b03 100644 --- a/src/marcdisp.c +++ b/src/marcdisp.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: marcdisp.c,v 1.51 2007-09-20 17:22:45 adam Exp $ + * $Id: marcdisp.c,v 1.52 2007-12-17 20:59:30 adam Exp $ */ /** @@ -33,6 +33,12 @@ #include #endif +enum yaz_collection_state { + no_collection, + collection_first, + collection_second +}; + /** \brief node types for yaz_marc_node */ enum YAZ_MARC_NODE_TYPE { @@ -85,6 +91,7 @@ struct yaz_marc_t_ { int xml; int debug; int write_using_libxml2; + enum yaz_collection_state enable_collection; yaz_iconv_t iconv_cd; char subfield_str[8]; char endline_str[8]; @@ -100,6 +107,7 @@ yaz_marc_t yaz_marc_create(void) mt->xml = YAZ_MARC_LINE; mt->debug = 0; mt->write_using_libxml2 = 0; + mt->enable_collection = no_collection; mt->m_wr = wrbuf_alloc(); mt->iconv_cd = 0; mt->leader_spec = 0; @@ -496,6 +504,28 @@ int yaz_marc_write_line(yaz_marc_t mt, WRBUF wr) return 0; } +int yaz_marc_write_trailer(yaz_marc_t mt, WRBUF wr) +{ + if (mt->enable_collection == collection_second) + { + switch(mt->xml) + { + case YAZ_MARC_MARCXML: + wrbuf_printf(wr, "\n"); + break; + case YAZ_MARC_XCHANGE: + wrbuf_printf(wr, "\n"); + break; + } + } + return 0; +} + +void yaz_marc_enable_collection(yaz_marc_t mt) +{ + mt->enable_collection = collection_first; +} + int yaz_marc_write_mode(yaz_marc_t mt, WRBUF wr) { switch(mt->xml) @@ -541,8 +571,18 @@ static int yaz_marc_write_marcxml_ns1(yaz_marc_t mt, WRBUF wr, return -1; if (!atoi_n_check(leader+11, 1, &identifier_length)) return -1; - - wrbuf_printf(wr, "enable_collection != no_collection) + { + if (mt->enable_collection == collection_first) + wrbuf_printf(wr, "\n", ns); + mt->enable_collection = collection_second; + wrbuf_printf(wr, " + + 00988nam0a32003011 450 @@ -91,3 +92,4 @@ 1970-1979 + diff --git a/test/marc2.xml b/test/marc2.xml index dd0bc13..b665385 100644 --- a/test/marc2.xml +++ b/test/marc2.xml @@ -1,4 +1,5 @@ - + + 01116nam0a32002171 450 @@ -98,3 +99,4 @@ 1 girl + diff --git a/test/marc3.xml b/test/marc3.xml index baab879..2784431 100644 --- a/test/marc3.xml +++ b/test/marc3.xml @@ -1,4 +1,5 @@ - + + 00914naa a2200337 450 @@ -118,3 +119,4 @@ a00001508 + diff --git a/test/marc4.xml b/test/marc4.xml index 6587da9..57fa9ca 100644 --- a/test/marc4.xml +++ b/test/marc4.xml @@ -1,4 +1,5 @@ - + + @@ -7,3 +8,4 @@ + diff --git a/test/marc5.xml b/test/marc5.xml index 54323ee..56fbdcc 100644 --- a/test/marc5.xml +++ b/test/marc5.xml @@ -1,4 +1,5 @@ - + + 00492nam a22001455a 4500 000277485 20051026111436.0 @@ -29,3 +30,4 @@ Cryptography. + diff --git a/test/marc6.xml b/test/marc6.xml index a78dca2..f4799f4 100644 --- a/test/marc6.xml +++ b/test/marc6.xml @@ -1,4 +1,5 @@ - + + 00366nam a22001698a 4500 11224466 DLC @@ -30,3 +31,4 @@ p. cm. + diff --git a/test/marc7.xml b/test/marc7.xml index bc6922d..17927db 100644 --- a/test/marc7.xml +++ b/test/marc7.xml @@ -1,4 +1,5 @@ - + + 03114cam a2200349 i 4500 77123332 DLC @@ -84,3 +85,4 @@ Standard PC Keyboard: 1234567890-= !@#$%^&*()_+ qwertyuiop[]\ QWERTYUIOP{}| asdfghjkl;' ASDFGHJKL:" zxcvbnm,./ ZXCVBNM<>? + diff --git a/test/marc8.xml b/test/marc8.xml index bb73af0..edb674e 100644 --- a/test/marc8.xml +++ b/test/marc8.xml @@ -1,4 +1,5 @@ - + + 02647nam^a2200469^^^4500 UCD-002592301 20061209034435.0 @@ -137,3 +138,4 @@ 002592301 + diff --git a/test/marc9.xml b/test/marc9.xml index d7ed2a5..d8b83d7 100644 --- a/test/marc9.xml +++ b/test/marc9.xml @@ -1,4 +1,5 @@ - + + 02075cas a22005055a 4500 2005336282 DLC @@ -153,3 +154,4 @@ LC Cairo Office [we 45] + diff --git a/util/marcdump.c b/util/marcdump.c index e65aed5..2dc36ef 100644 --- a/util/marcdump.c +++ b/util/marcdump.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: marcdump.c,v 1.53 2007-09-23 07:40:13 adam Exp $ + * $Id: marcdump.c,v 1.54 2007-12-17 20:59:32 adam Exp $ */ #define _FILE_OFFSET_BITS 64 @@ -14,7 +14,7 @@ #if YAZ_HAVE_XML2 #include #include - +#include #include #include @@ -93,34 +93,54 @@ static void marcdump_read_line(yaz_marc_t mt, const char *fname) fputs(wrbuf_cstr(wrbuf), stdout); wrbuf_destroy(wrbuf); } + { + WRBUF wrbuf = wrbuf_alloc(); + yaz_marc_write_trailer(mt, wrbuf); + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_destroy(wrbuf); + } fclose(inf); } #if YAZ_HAVE_XML2 static void marcdump_read_xml(yaz_marc_t mt, const char *fname) { - xmlNodePtr ptr; - xmlDocPtr doc = xmlParseFile(fname); - if (!doc) - return; + xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */, + 0 /* options */); - ptr = xmlDocGetRootElement(doc); - if (ptr) + if (reader) { - int r; + int ret; WRBUF wrbuf = wrbuf_alloc(); - r = yaz_marc_read_xml(mt, ptr); - if (r) - fprintf(stderr, "yaz_marc_read_xml failed\n"); - else + while ((ret = xmlTextReaderRead(reader)) == 1) { - yaz_marc_write_mode(mt, wrbuf); - - fputs(wrbuf_cstr(wrbuf), stdout); + int type = xmlTextReaderNodeType(reader); + if (type == XML_READER_TYPE_ELEMENT) + { + const char *name = (const char *) + xmlTextReaderConstName(reader); + if (!strcmp(name, "record")) + { + xmlNodePtr ptr = xmlTextReaderExpand(reader); + + int r = yaz_marc_read_xml(mt, ptr); + if (r) + fprintf(stderr, "yaz_marc_read_xml failed\n"); + else + { + yaz_marc_write_mode(mt, wrbuf); + + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_rewind(wrbuf); + } + } + } } + yaz_marc_write_trailer(mt, wrbuf); + fputs(wrbuf_cstr(wrbuf), stdout); wrbuf_destroy(wrbuf); + xmlFreeTextReader(reader); } - xmlFreeDoc(doc); } #endif @@ -152,6 +172,7 @@ static void dump(const char *fname, const char *from, const char *to, yaz_marc_iconv(mt, cd); } yaz_marc_xml(mt, output_format); + yaz_marc_enable_collection(mt); yaz_marc_write_using_libxml2(mt, write_using_libxml2); yaz_marc_debug(mt, verbose); @@ -307,6 +328,12 @@ static void dump(const char *fname, const char *from, const char *to, fprintf (cfile, "};\n"); fclose(inf); } + { + WRBUF wrbuf = wrbuf_alloc(); + yaz_marc_write_trailer(mt, wrbuf); + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_destroy(wrbuf); + } if (cd) yaz_iconv_close(cd); yaz_marc_destroy(mt);