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)
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(nmem, yaz_oid_recsyn_usmarc,
107 wrbuf_buf(wr), wrbuf_len(wr));
115 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
117 *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
119 match_element_next(&ptr, "enumeration", nmem, &(*volp)->enumeration);
120 match_element_next(&ptr, "chronology", nmem, &(*volp)->chronology);
121 match_element_next(&ptr, "enumAndChron", nmem, &(*volp)->enumAndChron);
125 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
130 for (i = 0; ptr; i++)
132 while (ptr && ptr->type != XML_ELEMENT_NODE)
136 if (!match_element(ptr, "volume"))
141 *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
143 for (i = 0; ptr; i++)
145 while (ptr && ptr->type != XML_ELEMENT_NODE)
149 if (!match_element(ptr, "volume"))
151 volume(ptr->children, (*volp) + i, nmem);
157 static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
159 *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
161 match_v_next(&ptr, "availableNow", nmem, &(*circp)->availableNow);
162 /* note the spelling of the ASN.1 member below */
163 match_element_next(&ptr, "availabilityDate", nmem,
164 &(*circp)->availablityDate);
165 match_element_next(&ptr, "availableThru", nmem, &(*circp)->availableThru);
166 match_element_next(&ptr, "restrictions", nmem, &(*circp)->restrictions);
167 match_element_next(&ptr, "itemId", nmem, &(*circp)->itemId);
168 match_v_next(&ptr, "renewable", nmem, &(*circp)->renewable);
169 match_v_next(&ptr, "onHold", nmem, &(*circp)->onHold);
170 match_element_next(&ptr, "enumAndChron", nmem, &(*circp)->enumAndChron);
171 match_element_next(&ptr, "midspine", nmem, &(*circp)->midspine);
172 match_element_next(&ptr, "temporaryLocation", nmem,
173 &(*circp)->temporaryLocation);
177 static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
183 for (i = 0; ptr; i++)
185 while (ptr && ptr->type != XML_ELEMENT_NODE)
189 if (!match_element(ptr, "circulation"))
194 *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
196 for (i = 0; ptr; i++)
198 while (ptr && ptr->type != XML_ELEMENT_NODE)
202 if (!match_element(ptr, "circulation"))
204 circulation(ptr->children, (*circp) + i, nmem);
210 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
212 Z_HoldingsAndCircData *h;
214 *r = (Z_HoldingsRecord *)
215 nmem_malloc(nmem, sizeof(**r));
216 (*r)->which = Z_HoldingsRecord_holdingsAndCirc;
217 h = (*r)->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
218 nmem_malloc(nmem, sizeof(*h));
220 match_element_next(&ptr, "typeOfRecord", nmem, &h->typeOfRecord);
221 match_element_next(&ptr, "encodingLevel", nmem, &h->encodingLevel);
222 match_element_next(&ptr, "format", nmem, &h->format);
223 match_element_next(&ptr, "receiptAcqStatus", nmem, &h->receiptAcqStatus);
224 match_element_next(&ptr, "generalRetention", nmem, &h->generalRetention);
225 match_element_next(&ptr, "completeness", nmem, &h->completeness);
226 match_element_next(&ptr, "dateOfReport", nmem, &h->dateOfReport);
227 match_element_next(&ptr, "nucCode", nmem, &h->nucCode);
228 match_element_next(&ptr, "localLocation", nmem, &h->localLocation);
229 match_element_next(&ptr, "shelvingLocation", nmem, &h->shelvingLocation);
230 match_element_next(&ptr, "callNumber", nmem, &h->callNumber);
231 match_element_next(&ptr, "shelvingData", nmem, &h->shelvingData);
232 match_element_next(&ptr, "copyNumber", nmem, &h->copyNumber);
233 match_element_next(&ptr, "publicNote", nmem, &h->publicNote);
234 match_element_next(&ptr, "reproductionNote", nmem, &h->reproductionNote);
235 match_element_next(&ptr, "termsUseRepro", nmem, &h->termsUseRepro);
236 match_element_next(&ptr, "enumAndChron", nmem, &h->enumAndChron);
240 while (ptr && ptr->type != XML_ELEMENT_NODE)
242 if (match_element(ptr, "volumes"))
244 volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
248 h->num_circulationData = 0;
249 h->circulationData = 0;
250 while (ptr && ptr->type != XML_ELEMENT_NODE)
252 if (match_element(ptr, "circulations"))
254 circulations(ptr->children, &h->circulationData,
255 &h->num_circulationData, nmem);
261 static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
263 yaz_iconv_t cd, NMEM nmem)
271 nmem = yaz_marc_get_nmem(mt);
272 if (!match_element(ptr, "opacRecord"))
275 while (ptr && ptr->type != XML_ELEMENT_NODE)
277 if (!match_element(ptr, "bibliographicRecord"))
279 if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem))
281 *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
282 opac->num_holdingsData = 0;
283 opac->holdingsData = 0;
284 opac->bibliographicRecord = ext;
287 while (ptr && ptr->type != XML_ELEMENT_NODE)
289 if (!match_element(ptr, "holdings"))
295 for (i = 0; ptr; i++)
297 while (ptr && ptr->type != XML_ELEMENT_NODE)
301 if (!match_element(ptr, "holding"))
305 opac->num_holdingsData = i;
306 opac->holdingsData = (Z_HoldingsRecord **)
307 nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
309 for (i = 0; ptr; i++)
311 while (ptr && ptr->type != XML_ELEMENT_NODE)
315 if (!match_element(ptr, "holding"))
317 if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
324 int yaz_xml_to_opac(yaz_marc_t mt, const char *buf_in, size_t size_in,
325 Z_OPACRecord **dst, yaz_iconv_t cd, NMEM nmem)
327 xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
331 r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem);
343 * c-file-style: "Stroustrup"
344 * indent-tabs-mode: nil
346 * vim: shiftwidth=4 tabstop=8 expandtab