GFS: conversions between OPAC and XML
[yaz-moved-to-github.git] / src / xml_to_opac.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file xml_to_opac.c
7  * \brief Implements XML to OPAC conversion
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16
17 #include <yaz/proto.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/wrbuf.h>
20 #include <yaz/oid_db.h>
21
22 #if YAZ_HAVE_XML2
23 #include <libxml/parser.h>
24 #include <libxml/tree.h>
25
26 static int match_element(xmlNode *ptr, const char *elem)
27 {
28     if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem))
29     {
30         return 1;
31     }
32     return 0;
33 }
34
35 static int match_xsd_string_n(xmlNodePtr ptr, const char *elem, NMEM nmem,
36                               char **val, int *len)
37 {
38     if (!match_element(ptr, elem))
39         return 0;
40     ptr = ptr->children;
41     if (!ptr || ptr->type != XML_TEXT_NODE)
42     {
43         *val = "";
44         return 1;
45     }
46     *val = nmem_strdup(nmem, (const char *) ptr->content);
47     if (len)
48         *len = xmlStrlen(ptr->content);
49     return 1;
50 }
51
52 static int match_element_next(xmlNode **ptr, const char *elem, NMEM nmem,
53                               char **val)
54 {
55     while (*ptr && (*ptr)->type != XML_ELEMENT_NODE)
56         (*ptr) = (*ptr)->next;
57     if (*ptr && match_xsd_string_n(*ptr, elem, nmem, val, 0))
58     {
59         *ptr = (*ptr)->next;
60         return 1;
61     }
62     *val = 0;
63     return 0;
64 }
65
66 static int match_v_next(xmlNode **ptr, const char *elem, NMEM nmem,
67                         Odr_bool **val)
68 {
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))
73     {
74         struct _xmlAttr *attr = (*ptr)->properties;
75
76         *ptr = (*ptr)->next;
77         for (; attr; attr = attr->next)
78         {
79             if (!strcmp((const char *) attr->name, "value"))
80             {
81                 if (attr->children->type == XML_TEXT_NODE)
82                 {
83                     if (attr->children->content[0] == '0')
84                         return 1;
85                     else if (attr->children->content[0] == '1')
86                     {
87                         **val = 1;
88                         return 1;
89                     }
90                 }
91             }
92         }
93     }
94     return 0;
95 }
96
97 static int bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
98                                yaz_iconv_t cd, NMEM nmem)
99 {
100     int ret = 0;
101     if (yaz_marc_read_xml(mt, ptr) == 0)
102     {
103         WRBUF wr = wrbuf_alloc();
104         if (yaz_marc_write_iso2709(mt, wr) == 0)
105         {
106             *ext = z_ext_record_oid_nmem(nmem, yaz_oid_recsyn_usmarc,
107                                          wrbuf_buf(wr), wrbuf_len(wr));
108             ret = 1;
109         }
110         wrbuf_destroy(wr);
111     }
112     return ret;
113 }
114
115 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
116 {
117     *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
118
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);
122     return 1;
123 }
124
125 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
126 {
127     int i;
128     xmlNode *ptr0 = ptr;
129
130     for (i = 0; ptr; i++)
131     {
132         while (ptr && ptr->type != XML_ELEMENT_NODE)
133             ptr = ptr->next;
134         if (!ptr)
135             break;
136         if (!match_element(ptr, "volume"))
137             return 0;
138         ptr = ptr->next;
139     }
140     *num = i;
141     *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
142     ptr = ptr0;
143     for (i = 0; ptr; i++)
144     {
145         while (ptr && ptr->type != XML_ELEMENT_NODE)
146             ptr = ptr->next;
147         if (!ptr)
148             break;
149         if (!match_element(ptr, "volume"))
150             return 0;
151         volume(ptr->children, (*volp) + i, nmem);
152         ptr = ptr->next;
153     }
154     return 1;
155 }
156
157 static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
158 {
159     *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
160
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);
174     return 1;
175 }
176
177 static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
178                         int *num, NMEM nmem)
179 {
180     int i;
181     xmlNode *ptr0 = ptr;
182
183     for (i = 0; ptr; i++)
184     {
185         while (ptr && ptr->type != XML_ELEMENT_NODE)
186             ptr = ptr->next;
187         if (!ptr)
188             break;
189         if (!match_element(ptr, "circulation"))
190             return 0;
191         ptr = ptr->next;
192     }
193     *num = i;
194     *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
195     ptr = ptr0;
196     for (i = 0; ptr; i++)
197     {
198         while (ptr && ptr->type != XML_ELEMENT_NODE)
199             ptr = ptr->next;
200         if (!ptr)
201             break;
202         if (!match_element(ptr, "circulation"))
203             return 0;
204         circulation(ptr->children, (*circp) + i, nmem);
205         ptr = ptr->next;
206     }
207     return 1;
208 }
209
210 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
211 {
212     Z_HoldingsAndCircData *h;
213
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));
219
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);
237
238     h->num_volumes = 0;
239     h->volumes = 0;
240     while (ptr && ptr->type != XML_ELEMENT_NODE)
241         ptr = ptr->next;
242     if (match_element(ptr, "volumes"))
243     {
244         volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
245         ptr = ptr->next;
246     }
247
248     h->num_circulationData = 0;
249     h->circulationData = 0;
250     while (ptr && ptr->type != XML_ELEMENT_NODE)
251         ptr = ptr->next;
252     if (match_element(ptr, "circulations"))
253     {
254         circulations(ptr->children, &h->circulationData,
255                      &h->num_circulationData, nmem);
256         ptr = ptr->next;
257     }
258     return 1;
259 }
260
261 static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
262                                Z_OPACRecord **dst,
263                                yaz_iconv_t cd, NMEM nmem)
264 {
265     int i;
266     Z_External *ext = 0;
267     Z_OPACRecord *opac;
268     xmlNode *ptr0;
269
270     if (!nmem)
271         nmem = yaz_marc_get_nmem(mt);
272     if (!match_element(ptr, "opacRecord"))
273         return 0;
274     ptr = ptr->children;
275     while (ptr && ptr->type != XML_ELEMENT_NODE)
276         ptr = ptr->next;
277     if (!match_element(ptr, "bibliographicRecord"))
278         return 0;
279     if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem))
280         return 0;
281     *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
282     opac->num_holdingsData = 0;
283     opac->holdingsData = 0;
284     opac->bibliographicRecord = ext;
285
286     ptr = ptr->next;
287     while (ptr && ptr->type != XML_ELEMENT_NODE)
288         ptr = ptr->next;
289     if (!match_element(ptr, "holdings"))
290         return 0;
291
292     ptr = ptr->children;
293     ptr0 = ptr;
294
295     for (i = 0; ptr; i++)
296     {
297         while (ptr && ptr->type != XML_ELEMENT_NODE)
298             ptr = ptr->next;
299         if (!ptr)
300             break;
301         if (!match_element(ptr, "holding"))
302             return 0;
303         ptr = ptr->next;
304     }
305     opac->num_holdingsData = i;
306     opac->holdingsData = (Z_HoldingsRecord **)
307         nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
308     ptr = ptr0;
309     for (i = 0; ptr; i++)
310     {
311         while (ptr && ptr->type != XML_ELEMENT_NODE)
312             ptr = ptr->next;
313         if (!ptr)
314             break;
315         if (!match_element(ptr, "holding"))
316             return 0;
317         if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
318             return 0;
319         ptr = ptr->next;
320     }
321     return 1;
322 }
323
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)
326 {
327     xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
328     int r = 0;
329     if (doc)
330     {
331         r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem);
332         xmlFreeDoc(doc);
333     }
334     return r;
335 }
336
337
338 #endif
339
340 /*
341  * Local variables:
342  * c-basic-offset: 4
343  * c-file-style: "Stroustrup"
344  * indent-tabs-mode: nil
345  * End:
346  * vim: shiftwidth=4 tabstop=8 expandtab
347  */
348