1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements XML to OPAC conversion
17 #include <yaz/proto.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/wrbuf.h>
20 #include <yaz/oid_db.h>
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
26 static int match_element(xmlNode *ptr, const char *elem)
28 if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
35 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, NMEM nmem,
38 if (!match_element(ptr, elem))
41 if (!ptr || ptr->type != XML_TEXT_NODE)
46 *val = nmem_strdup(nmem, (const char *) ptr->content);
48 *len = xmlStrlen(ptr->content);
52 static int match_element_next(xmlNode **ptr, const char *elem, NMEM nmem,
55 while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
56 (*ptr) = (*ptr)->next;
57 if (*ptr && match_xsd_string_n(*ptr, elem, nmem, val, 0))
66 static int bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
67 yaz_iconv_t cd, NMEM nmem)
70 if (yaz_marc_read_xml(mt, ptr) == 0)
72 WRBUF wr = wrbuf_alloc();
73 if (yaz_marc_write_iso2709(mt, wr) == 0)
75 *ext = z_ext_record_oid_nmem(nmem, yaz_oid_recsyn_usmarc,
76 wrbuf_buf(wr), wrbuf_len(wr));
84 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
86 *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
88 match_element_next(&ptr, "enumeration", nmem, &(*volp)->enumeration);
89 match_element_next(&ptr, "chronology", nmem, &(*volp)->chronology);
90 match_element_next(&ptr, "enumAndChron", nmem, &(*volp)->enumAndChron);
94 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
101 while (ptr && ptr->type != XML_ELEMENT_NODE)
103 if (!match_element(ptr, "volume"))
108 *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
110 for (i = 0; ptr; i++)
112 while (ptr && ptr->type != XML_ELEMENT_NODE)
114 if (!match_element(ptr, "volume"))
116 volume(ptr->children, (*volp) + i, nmem);
122 static void circulations(xmlNode *ptr, Z_CircRecord ***circp,
128 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
130 Z_HoldingsAndCircData *h;
132 *r = (Z_HoldingsRecord *)
133 nmem_malloc(nmem, sizeof(**r));
134 (*r)->which = Z_HoldingsRecord_holdingsAndCirc;
135 h = (*r)->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
136 nmem_malloc(nmem, sizeof(*h));
138 match_element_next(&ptr, "typeOfRecord", nmem, &h->typeOfRecord);
139 match_element_next(&ptr, "encodingLevel", nmem, &h->encodingLevel);
140 match_element_next(&ptr, "format", nmem, &h->format);
141 match_element_next(&ptr, "receiptAcqStatus", nmem, &h->receiptAcqStatus);
142 match_element_next(&ptr, "generalRetention", nmem, &h->generalRetention);
143 match_element_next(&ptr, "completeness", nmem, &h->completeness);
144 match_element_next(&ptr, "dateOfReport", nmem, &h->dateOfReport);
145 match_element_next(&ptr, "nucCode", nmem, &h->nucCode);
146 match_element_next(&ptr, "localLocation", nmem, &h->localLocation);
147 match_element_next(&ptr, "shelvingLocation", nmem, &h->shelvingLocation);
148 match_element_next(&ptr, "callNumber", nmem, &h->callNumber);
149 match_element_next(&ptr, "shelvingData", nmem, &h->shelvingData);
150 match_element_next(&ptr, "copyNumber", nmem, &h->copyNumber);
151 match_element_next(&ptr, "publicNote", nmem, &h->publicNote);
152 match_element_next(&ptr, "reproductionNote", nmem, &h->reproductionNote);
153 match_element_next(&ptr, "termsUseRepro", nmem, &h->termsUseRepro);
154 match_element_next(&ptr, "enumAndChron", nmem, &h->enumAndChron);
158 while (ptr && ptr->type != XML_ELEMENT_NODE)
160 if (match_element(ptr, "volumes"))
162 volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
166 h->num_circulationData = 0;
167 h->circulationData = 0;
168 while (ptr && ptr->type != XML_ELEMENT_NODE)
170 if (match_element(ptr, "circulations"))
172 circulations(ptr->children, &h->circulationData,
173 &h->num_circulationData, nmem);
179 int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *ptr, Z_OPACRecord **dst,
180 yaz_iconv_t cd, NMEM nmem)
188 nmem = yaz_marc_get_nmem(mt);
189 if (!match_element(ptr, "opacRecord"))
192 while (ptr && ptr->type != XML_ELEMENT_NODE)
194 if (!match_element(ptr, "bibliographicRecord"))
196 if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem))
198 *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
199 opac->num_holdingsData = 0;
200 opac->holdingsData = 0;
201 opac->bibliographicRecord = ext;
203 while (ptr && ptr->type != XML_ELEMENT_NODE)
205 if (!match_element(ptr, "holdings"))
211 for (i = 0; ptr; i++)
213 while (ptr && ptr->type != XML_ELEMENT_NODE)
217 if (!match_element(ptr, "holding"))
221 opac->num_holdingsData = i;
222 opac->holdingsData = (Z_HoldingsRecord **)
223 nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
225 for (i = 0; ptr; i++)
227 while (ptr && ptr->type != XML_ELEMENT_NODE)
231 if (!match_element(ptr, "holding"))
233 if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
244 * c-file-style: "Stroustrup"
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab