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 match_v_next(xmlNode **ptr, const char *elem, NMEM nmem,
69 while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
70 (*ptr) = (*ptr)->next;
71 *val = nmem_booldup(nmem, 0);
72 if (*ptr && match_element(*ptr, elem))
74 struct _xmlAttr *attr = (*ptr)->properties;
77 for (; attr; attr = attr->next)
79 if (!strcmp((const char *) attr->name, "value"))
81 if (attr->children->type == XML_TEXT_NODE)
83 if (attr->children->content[0] == '0')
85 else if (attr->children->content[0] == '1')
97 static int bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
98 yaz_iconv_t cd, NMEM nmem, const Odr_oid *syntax)
101 if (yaz_marc_read_xml(mt, ptr) == 0)
103 WRBUF wr = wrbuf_alloc();
104 if (yaz_marc_write_iso2709(mt, wr) == 0)
106 *ext = z_ext_record_oid_nmem(
107 nmem, syntax ? syntax : yaz_oid_recsyn_usmarc,
108 wrbuf_buf(wr), wrbuf_len(wr));
116 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
118 *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
120 match_element_next(&ptr, "enumeration", nmem, &(*volp)->enumeration);
121 match_element_next(&ptr, "chronology", nmem, &(*volp)->chronology);
122 match_element_next(&ptr, "enumAndChron", nmem, &(*volp)->enumAndChron);
126 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
131 for (i = 0; ptr; i++)
133 while (ptr && ptr->type != XML_ELEMENT_NODE)
137 if (!match_element(ptr, "volume"))
142 *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
144 for (i = 0; ptr; i++)
146 while (ptr && ptr->type != XML_ELEMENT_NODE)
150 if (!match_element(ptr, "volume"))
152 volume(ptr->children, (*volp) + i, nmem);
158 static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
160 *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
162 match_v_next(&ptr, "availableNow", nmem, &(*circp)->availableNow);
163 /* note the spelling of the ASN.1 member below */
164 match_element_next(&ptr, "availabilityDate", nmem,
165 &(*circp)->availablityDate);
166 match_element_next(&ptr, "availableThru", nmem, &(*circp)->availableThru);
167 match_element_next(&ptr, "restrictions", nmem, &(*circp)->restrictions);
168 match_element_next(&ptr, "itemId", nmem, &(*circp)->itemId);
169 match_v_next(&ptr, "renewable", nmem, &(*circp)->renewable);
170 match_v_next(&ptr, "onHold", nmem, &(*circp)->onHold);
171 match_element_next(&ptr, "enumAndChron", nmem, &(*circp)->enumAndChron);
172 match_element_next(&ptr, "midspine", nmem, &(*circp)->midspine);
173 match_element_next(&ptr, "temporaryLocation", nmem,
174 &(*circp)->temporaryLocation);
178 static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
184 for (i = 0; ptr; i++)
186 while (ptr && ptr->type != XML_ELEMENT_NODE)
190 if (!match_element(ptr, "circulation"))
195 *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
197 for (i = 0; ptr; i++)
199 while (ptr && ptr->type != XML_ELEMENT_NODE)
203 if (!match_element(ptr, "circulation"))
205 circulation(ptr->children, (*circp) + i, nmem);
211 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
213 Z_HoldingsAndCircData *h;
215 *r = (Z_HoldingsRecord *)
216 nmem_malloc(nmem, sizeof(**r));
217 (*r)->which = Z_HoldingsRecord_holdingsAndCirc;
218 h = (*r)->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
219 nmem_malloc(nmem, sizeof(*h));
221 match_element_next(&ptr, "typeOfRecord", nmem, &h->typeOfRecord);
222 match_element_next(&ptr, "encodingLevel", nmem, &h->encodingLevel);
223 match_element_next(&ptr, "format", nmem, &h->format);
224 match_element_next(&ptr, "receiptAcqStatus", nmem, &h->receiptAcqStatus);
225 match_element_next(&ptr, "generalRetention", nmem, &h->generalRetention);
226 match_element_next(&ptr, "completeness", nmem, &h->completeness);
227 match_element_next(&ptr, "dateOfReport", nmem, &h->dateOfReport);
228 match_element_next(&ptr, "nucCode", nmem, &h->nucCode);
229 match_element_next(&ptr, "localLocation", nmem, &h->localLocation);
230 match_element_next(&ptr, "shelvingLocation", nmem, &h->shelvingLocation);
231 match_element_next(&ptr, "callNumber", nmem, &h->callNumber);
232 match_element_next(&ptr, "shelvingData", nmem, &h->shelvingData);
233 match_element_next(&ptr, "copyNumber", nmem, &h->copyNumber);
234 match_element_next(&ptr, "publicNote", nmem, &h->publicNote);
235 match_element_next(&ptr, "reproductionNote", nmem, &h->reproductionNote);
236 match_element_next(&ptr, "termsUseRepro", nmem, &h->termsUseRepro);
237 match_element_next(&ptr, "enumAndChron", nmem, &h->enumAndChron);
241 while (ptr && ptr->type != XML_ELEMENT_NODE)
243 if (match_element(ptr, "volumes"))
245 volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
249 h->num_circulationData = 0;
250 h->circulationData = 0;
251 while (ptr && ptr->type != XML_ELEMENT_NODE)
253 if (match_element(ptr, "circulations"))
255 circulations(ptr->children, &h->circulationData,
256 &h->num_circulationData, nmem);
262 static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
264 yaz_iconv_t cd, NMEM nmem,
265 const Odr_oid *syntax)
273 nmem = yaz_marc_get_nmem(mt);
274 if (!match_element(ptr, "opacRecord"))
277 while (ptr && ptr->type != XML_ELEMENT_NODE)
279 if (!match_element(ptr, "bibliographicRecord"))
281 if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem, syntax))
283 *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
284 opac->num_holdingsData = 0;
285 opac->holdingsData = 0;
286 opac->bibliographicRecord = ext;
289 while (ptr && ptr->type != XML_ELEMENT_NODE)
291 if (!match_element(ptr, "holdings"))
297 for (i = 0; ptr; i++)
299 while (ptr && ptr->type != XML_ELEMENT_NODE)
303 if (!match_element(ptr, "holding"))
307 opac->num_holdingsData = i;
308 opac->holdingsData = (Z_HoldingsRecord **)
309 nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
311 for (i = 0; ptr; i++)
313 while (ptr && ptr->type != XML_ELEMENT_NODE)
317 if (!match_element(ptr, "holding"))
319 if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
326 int yaz_xml_to_opac(yaz_marc_t mt, const char *buf_in, size_t size_in,
327 Z_OPACRecord **dst, yaz_iconv_t cd, NMEM nmem,
328 const Odr_oid *syntax)
330 xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
334 r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem,
347 * c-file-style: "Stroustrup"
348 * indent-tabs-mode: nil
350 * vim: shiftwidth=4 tabstop=8 expandtab