1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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>
25 static int match_element_next(xmlNode **ptr, const char *elem, NMEM nmem,
28 while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
29 (*ptr) = (*ptr)->next;
30 if (yaz_match_xsd_string_n_nmem(*ptr, elem, nmem, val, 0))
39 static int match_v_next(xmlNode **ptr, const char *elem, NMEM nmem,
42 while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
43 (*ptr) = (*ptr)->next;
44 *val = nmem_booldup(nmem, 0);
45 if (yaz_match_xsd_element(*ptr, elem))
47 struct _xmlAttr *attr = (*ptr)->properties;
50 for (; attr; attr = attr->next)
52 if (!strcmp((const char *) attr->name, "value"))
54 if (attr->children->type == XML_TEXT_NODE)
56 if (attr->children->content[0] == '0')
58 else if (attr->children->content[0] == '1')
70 static int bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
71 yaz_iconv_t cd, NMEM nmem, const Odr_oid *syntax)
74 if (yaz_marc_read_xml(mt, ptr) == 0)
76 WRBUF wr = wrbuf_alloc();
77 if (yaz_marc_write_iso2709(mt, wr) == 0)
79 *ext = z_ext_record_oid_nmem(
80 nmem, syntax ? syntax : yaz_oid_recsyn_usmarc,
81 wrbuf_buf(wr), wrbuf_len(wr));
89 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
91 *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
93 match_element_next(&ptr, "enumeration", nmem, &(*volp)->enumeration);
94 match_element_next(&ptr, "chronology", nmem, &(*volp)->chronology);
95 match_element_next(&ptr, "enumAndChron", nmem, &(*volp)->enumAndChron);
99 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
104 for (i = 0; ptr; i++)
106 while (ptr && ptr->type != XML_ELEMENT_NODE)
110 if (!yaz_match_xsd_element(ptr, "volume"))
115 *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
117 for (i = 0; ptr; i++)
119 while (ptr && ptr->type != XML_ELEMENT_NODE)
123 if (!yaz_match_xsd_element(ptr, "volume"))
125 volume(ptr->children, (*volp) + i, nmem);
131 static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
133 *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
135 match_v_next(&ptr, "availableNow", nmem, &(*circp)->availableNow);
136 /* note the spelling of the ASN.1 member below */
137 match_element_next(&ptr, "availabilityDate", nmem,
138 &(*circp)->availablityDate);
139 match_element_next(&ptr, "availableThru", nmem, &(*circp)->availableThru);
140 match_element_next(&ptr, "restrictions", nmem, &(*circp)->restrictions);
141 match_element_next(&ptr, "itemId", nmem, &(*circp)->itemId);
142 match_v_next(&ptr, "renewable", nmem, &(*circp)->renewable);
143 match_v_next(&ptr, "onHold", nmem, &(*circp)->onHold);
144 match_element_next(&ptr, "enumAndChron", nmem, &(*circp)->enumAndChron);
145 match_element_next(&ptr, "midspine", nmem, &(*circp)->midspine);
146 match_element_next(&ptr, "temporaryLocation", nmem,
147 &(*circp)->temporaryLocation);
151 static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
157 for (i = 0; ptr; i++)
159 while (ptr && ptr->type != XML_ELEMENT_NODE)
163 if (!yaz_match_xsd_element(ptr, "circulation"))
168 *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
170 for (i = 0; ptr; i++)
172 while (ptr && ptr->type != XML_ELEMENT_NODE)
176 if (!yaz_match_xsd_element(ptr, "circulation"))
178 circulation(ptr->children, (*circp) + i, nmem);
184 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
186 Z_HoldingsAndCircData *h;
188 *r = (Z_HoldingsRecord *)
189 nmem_malloc(nmem, sizeof(**r));
190 (*r)->which = Z_HoldingsRecord_holdingsAndCirc;
191 h = (*r)->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
192 nmem_malloc(nmem, sizeof(*h));
194 match_element_next(&ptr, "typeOfRecord", nmem, &h->typeOfRecord);
195 match_element_next(&ptr, "encodingLevel", nmem, &h->encodingLevel);
196 match_element_next(&ptr, "format", nmem, &h->format);
197 match_element_next(&ptr, "receiptAcqStatus", nmem, &h->receiptAcqStatus);
198 match_element_next(&ptr, "generalRetention", nmem, &h->generalRetention);
199 match_element_next(&ptr, "completeness", nmem, &h->completeness);
200 match_element_next(&ptr, "dateOfReport", nmem, &h->dateOfReport);
201 match_element_next(&ptr, "nucCode", nmem, &h->nucCode);
202 match_element_next(&ptr, "localLocation", nmem, &h->localLocation);
203 match_element_next(&ptr, "shelvingLocation", nmem, &h->shelvingLocation);
204 match_element_next(&ptr, "callNumber", nmem, &h->callNumber);
205 match_element_next(&ptr, "shelvingData", nmem, &h->shelvingData);
206 match_element_next(&ptr, "copyNumber", nmem, &h->copyNumber);
207 match_element_next(&ptr, "publicNote", nmem, &h->publicNote);
208 match_element_next(&ptr, "reproductionNote", nmem, &h->reproductionNote);
209 match_element_next(&ptr, "termsUseRepro", nmem, &h->termsUseRepro);
210 match_element_next(&ptr, "enumAndChron", nmem, &h->enumAndChron);
214 while (ptr && ptr->type != XML_ELEMENT_NODE)
216 if (yaz_match_xsd_element(ptr, "volumes"))
218 volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
222 h->num_circulationData = 0;
223 h->circulationData = 0;
224 while (ptr && ptr->type != XML_ELEMENT_NODE)
226 if (yaz_match_xsd_element(ptr, "circulations"))
228 circulations(ptr->children, &h->circulationData,
229 &h->num_circulationData, nmem);
235 static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
237 yaz_iconv_t cd, NMEM nmem,
238 const Odr_oid *syntax)
246 nmem = yaz_marc_get_nmem(mt);
247 if (!yaz_match_xsd_element(ptr, "opacRecord"))
250 while (ptr && ptr->type != XML_ELEMENT_NODE)
252 if (!yaz_match_xsd_element(ptr, "bibliographicRecord"))
254 if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem, syntax))
256 *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
257 opac->num_holdingsData = 0;
258 opac->holdingsData = 0;
259 opac->bibliographicRecord = ext;
262 while (ptr && ptr->type != XML_ELEMENT_NODE)
264 if (!yaz_match_xsd_element(ptr, "holdings"))
270 for (i = 0; ptr; i++)
272 while (ptr && ptr->type != XML_ELEMENT_NODE)
276 if (!yaz_match_xsd_element(ptr, "holding"))
280 opac->num_holdingsData = i;
281 opac->holdingsData = (Z_HoldingsRecord **)
282 nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
284 for (i = 0; ptr; i++)
286 while (ptr && ptr->type != XML_ELEMENT_NODE)
290 if (!yaz_match_xsd_element(ptr, "holding"))
292 if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
299 int yaz_xml_to_opac(yaz_marc_t mt, const char *buf_in, size_t size_in,
300 Z_OPACRecord **dst, yaz_iconv_t cd, NMEM nmem,
301 const Odr_oid *syntax)
303 xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
307 r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem,
320 * c-file-style: "Stroustrup"
321 * indent-tabs-mode: nil
323 * vim: shiftwidth=4 tabstop=8 expandtab