Update header about CQL grammar
[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, const Odr_oid *syntax)
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(
107                 nmem, syntax ? syntax : yaz_oid_recsyn_usmarc,
108                 wrbuf_buf(wr), wrbuf_len(wr));
109             ret = 1;
110         }
111         wrbuf_destroy(wr);
112     }
113     return ret;
114 }
115
116 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
117 {
118     *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
119
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);
123     return 1;
124 }
125
126 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
127 {
128     int i;
129     xmlNode *ptr0 = ptr;
130
131     for (i = 0; ptr; i++)
132     {
133         while (ptr && ptr->type != XML_ELEMENT_NODE)
134             ptr = ptr->next;
135         if (!ptr)
136             break;
137         if (!match_element(ptr, "volume"))
138             return 0;
139         ptr = ptr->next;
140     }
141     *num = i;
142     *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
143     ptr = ptr0;
144     for (i = 0; ptr; i++)
145     {
146         while (ptr && ptr->type != XML_ELEMENT_NODE)
147             ptr = ptr->next;
148         if (!ptr)
149             break;
150         if (!match_element(ptr, "volume"))
151             return 0;
152         volume(ptr->children, (*volp) + i, nmem);
153         ptr = ptr->next;
154     }
155     return 1;
156 }
157
158 static int circulation(xmlNode *ptr, Z_CircRecord **circp, NMEM nmem)
159 {
160     *circp = (Z_CircRecord *) nmem_malloc(nmem, sizeof(Z_CircRecord));
161
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);
175     return 1;
176 }
177
178 static int circulations(xmlNode *ptr, Z_CircRecord ***circp,
179                         int *num, NMEM nmem)
180 {
181     int i;
182     xmlNode *ptr0 = ptr;
183
184     for (i = 0; ptr; i++)
185     {
186         while (ptr && ptr->type != XML_ELEMENT_NODE)
187             ptr = ptr->next;
188         if (!ptr)
189             break;
190         if (!match_element(ptr, "circulation"))
191             return 0;
192         ptr = ptr->next;
193     }
194     *num = i;
195     *circp = (Z_CircRecord **) nmem_malloc(nmem, sizeof(**circp) * i);
196     ptr = ptr0;
197     for (i = 0; ptr; i++)
198     {
199         while (ptr && ptr->type != XML_ELEMENT_NODE)
200             ptr = ptr->next;
201         if (!ptr)
202             break;
203         if (!match_element(ptr, "circulation"))
204             return 0;
205         circulation(ptr->children, (*circp) + i, nmem);
206         ptr = ptr->next;
207     }
208     return 1;
209 }
210
211 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
212 {
213     Z_HoldingsAndCircData *h;
214
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));
220
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);
238
239     h->num_volumes = 0;
240     h->volumes = 0;
241     while (ptr && ptr->type != XML_ELEMENT_NODE)
242         ptr = ptr->next;
243     if (match_element(ptr, "volumes"))
244     {
245         volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
246         ptr = ptr->next;
247     }
248
249     h->num_circulationData = 0;
250     h->circulationData = 0;
251     while (ptr && ptr->type != XML_ELEMENT_NODE)
252         ptr = ptr->next;
253     if (match_element(ptr, "circulations"))
254     {
255         circulations(ptr->children, &h->circulationData,
256                      &h->num_circulationData, nmem);
257         ptr = ptr->next;
258     }
259     return 1;
260 }
261
262 static int yaz_xml_to_opac_ptr(yaz_marc_t mt, xmlNode *ptr,
263                                Z_OPACRecord **dst,
264                                yaz_iconv_t cd, NMEM nmem,
265                                const Odr_oid *syntax)
266 {
267     int i;
268     Z_External *ext = 0;
269     Z_OPACRecord *opac;
270     xmlNode *ptr0;
271
272     if (!nmem)
273         nmem = yaz_marc_get_nmem(mt);
274     if (!match_element(ptr, "opacRecord"))
275         return 0;
276     ptr = ptr->children;
277     while (ptr && ptr->type != XML_ELEMENT_NODE)
278         ptr = ptr->next;
279     if (!match_element(ptr, "bibliographicRecord"))
280         return 0;
281     if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem, syntax))
282         return 0;
283     *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
284     opac->num_holdingsData = 0;
285     opac->holdingsData = 0;
286     opac->bibliographicRecord = ext;
287
288     ptr = ptr->next;
289     while (ptr && ptr->type != XML_ELEMENT_NODE)
290         ptr = ptr->next;
291     if (!match_element(ptr, "holdings"))
292         return 0;
293
294     ptr = ptr->children;
295     ptr0 = ptr;
296
297     for (i = 0; ptr; i++)
298     {
299         while (ptr && ptr->type != XML_ELEMENT_NODE)
300             ptr = ptr->next;
301         if (!ptr)
302             break;
303         if (!match_element(ptr, "holding"))
304             return 0;
305         ptr = ptr->next;
306     }
307     opac->num_holdingsData = i;
308     opac->holdingsData = (Z_HoldingsRecord **)
309         nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
310     ptr = ptr0;
311     for (i = 0; ptr; i++)
312     {
313         while (ptr && ptr->type != XML_ELEMENT_NODE)
314             ptr = ptr->next;
315         if (!ptr)
316             break;
317         if (!match_element(ptr, "holding"))
318             return 0;
319         if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
320             return 0;
321         ptr = ptr->next;
322     }
323     return 1;
324 }
325
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)
329 {
330     xmlDocPtr doc = xmlParseMemory(buf_in, size_in);
331     int r = 0;
332     if (doc)
333     {
334         r = yaz_xml_to_opac_ptr(mt, xmlDocGetRootElement(doc), dst, cd, nmem,
335                                 syntax);
336         xmlFreeDoc(doc);
337     }
338     return r;
339 }
340
341
342 #endif
343
344 /*
345  * Local variables:
346  * c-basic-offset: 4
347  * c-file-style: "Stroustrup"
348  * indent-tabs-mode: nil
349  * End:
350  * vim: shiftwidth=4 tabstop=8 expandtab
351  */
352