Further work on yaz_xml_to_opac
[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 bibliographicRecord(yaz_marc_t mt, xmlNode *ptr, Z_External **ext,
67                                yaz_iconv_t cd, NMEM nmem)
68 {
69     int ret = 0;
70     if (yaz_marc_read_xml(mt, ptr) == 0)
71     {
72         WRBUF wr = wrbuf_alloc();
73         if (yaz_marc_write_iso2709(mt, wr) == 0)
74         {
75             *ext = z_ext_record_oid_nmem(nmem, yaz_oid_recsyn_usmarc,
76                                          wrbuf_buf(wr), wrbuf_len(wr));
77             ret = 1;
78         }
79         wrbuf_destroy(wr);
80     }
81     return ret;
82 }
83
84 static int volume(xmlNode *ptr, Z_Volume **volp, NMEM nmem)
85 {
86     *volp = (Z_Volume *) nmem_malloc(nmem, sizeof(Z_Volume));
87
88     match_element_next(&ptr, "enumeration", nmem, &(*volp)->enumeration);
89     match_element_next(&ptr, "chronology", nmem, &(*volp)->chronology);
90     match_element_next(&ptr, "enumAndChron", nmem, &(*volp)->enumAndChron);
91     return 1;
92 }
93
94 static int volumes(xmlNode *ptr, Z_Volume ***volp, int *num, NMEM nmem)
95 {
96     int i;
97     xmlNode *ptr0 = ptr;
98
99     for (i = 0; ptr; i++)
100     {
101         while (ptr && ptr->type != XML_ELEMENT_NODE)
102             ptr = ptr->next;
103         if (!match_element(ptr, "volume"))
104             return 0;
105         ptr = ptr->next;
106     }
107     *num = i;
108     *volp = (Z_Volume **) nmem_malloc(nmem, sizeof(**volp) * i);
109     ptr = ptr0;
110     for (i = 0; ptr; i++)
111     {
112         while (ptr && ptr->type != XML_ELEMENT_NODE)
113             ptr = ptr->next;
114         if (!match_element(ptr, "volume"))
115             return 0;
116         volume(ptr->children, (*volp) + i, nmem);
117         ptr = ptr->next;
118     }
119     return 1;
120 }
121
122 static void circulations(xmlNode *ptr, Z_CircRecord ***circp,
123                          int *num, NMEM nmem)
124 {
125
126 }
127
128 static int holdingsRecord(xmlNode *ptr, Z_HoldingsRecord **r, NMEM nmem)
129 {
130     Z_HoldingsAndCircData *h;
131
132     *r = (Z_HoldingsRecord *)
133         nmem_malloc(nmem, sizeof(**r));
134     (*r)->which = Z_HoldingsRecord_holdingsAndCirc;
135     h = (*r)->u.holdingsAndCirc = (Z_HoldingsAndCircData *)
136         nmem_malloc(nmem, sizeof(*h));
137
138     match_element_next(&ptr, "typeOfRecord", nmem, &h->typeOfRecord);
139     match_element_next(&ptr, "encodingLevel", nmem, &h->encodingLevel);
140     match_element_next(&ptr, "format", nmem, &h->format);
141     match_element_next(&ptr, "receiptAcqStatus", nmem, &h->receiptAcqStatus);
142     match_element_next(&ptr, "generalRetention", nmem, &h->generalRetention);
143     match_element_next(&ptr, "completeness", nmem, &h->completeness);
144     match_element_next(&ptr, "dateOfReport", nmem, &h->dateOfReport);
145     match_element_next(&ptr, "nucCode", nmem, &h->nucCode);
146     match_element_next(&ptr, "localLocation", nmem, &h->localLocation);
147     match_element_next(&ptr, "shelvingLocation", nmem, &h->shelvingLocation);
148     match_element_next(&ptr, "callNumber", nmem, &h->callNumber);
149     match_element_next(&ptr, "shelvingData", nmem, &h->shelvingData);
150     match_element_next(&ptr, "copyNumber", nmem, &h->copyNumber);
151     match_element_next(&ptr, "publicNote", nmem, &h->publicNote);
152     match_element_next(&ptr, "reproductionNote", nmem, &h->reproductionNote);
153     match_element_next(&ptr, "termsUseRepro", nmem, &h->termsUseRepro);
154     match_element_next(&ptr, "enumAndChron", nmem, &h->enumAndChron);
155
156     h->num_volumes = 0;
157     h->volumes = 0;
158     while (ptr && ptr->type != XML_ELEMENT_NODE)
159         ptr = ptr->next;
160     if (match_element(ptr, "volumes"))
161     {
162         volumes(ptr->children, &h->volumes, &h->num_volumes, nmem);
163         ptr = ptr->next;
164     }
165
166     h->num_circulationData = 0;
167     h->circulationData = 0;
168     while (ptr && ptr->type != XML_ELEMENT_NODE)
169         ptr = ptr->next;
170     if (match_element(ptr, "circulations"))
171     {
172         circulations(ptr->children, &h->circulationData,
173                      &h->num_circulationData, nmem);
174         ptr = ptr->next;
175     }
176     return 1;
177 }
178
179 int yaz_xml_to_opac(yaz_marc_t mt, xmlNode *ptr, Z_OPACRecord **dst,
180                     yaz_iconv_t cd, NMEM nmem)
181 {
182     int i;
183     Z_External *ext = 0;
184     Z_OPACRecord *opac;
185     xmlNode *ptr0;
186
187     if (!nmem)
188         nmem = yaz_marc_get_nmem(mt);
189     if (!match_element(ptr, "opacRecord"))
190         return 0;
191     ptr = ptr->children;
192     while (ptr && ptr->type != XML_ELEMENT_NODE)
193         ptr = ptr->next;
194     if (!match_element(ptr, "bibliographicRecord"))
195         return 0;
196     if (!bibliographicRecord(mt, ptr->children, &ext, cd, nmem))
197         return 0;
198     *dst = opac = (Z_OPACRecord *) nmem_malloc(nmem, sizeof(*opac));
199     opac->num_holdingsData = 0;
200     opac->holdingsData = 0;
201     opac->bibliographicRecord = ext;
202
203     while (ptr && ptr->type != XML_ELEMENT_NODE)
204         ptr = ptr->next;
205     if (!match_element(ptr, "holdings"))
206         return 0;
207
208     ptr = ptr->children;
209     ptr0 = ptr;
210
211     for (i = 0; ptr; i++)
212     {
213         while (ptr && ptr->type != XML_ELEMENT_NODE)
214             ptr = ptr->next;
215         if (!ptr)
216             break;
217         if (!match_element(ptr, "holding"))
218             return 0;
219         ptr = ptr->next;
220     }
221     opac->num_holdingsData = i;
222     opac->holdingsData = (Z_HoldingsRecord **)
223         nmem_malloc(nmem, sizeof(*opac->holdingsData) * i);
224     ptr = ptr0;
225     for (i = 0; ptr; i++)
226     {
227         while (ptr && ptr->type != XML_ELEMENT_NODE)
228             ptr = ptr->next;
229         if (!ptr)
230             break;
231         if (!match_element(ptr, "holding"))
232             return 0;
233         if (!holdingsRecord(ptr->children, opac->holdingsData + i, nmem))
234             return 0;
235         ptr = ptr->next;
236     }
237     return 1;
238 }
239 #endif
240
241 /*
242  * Local variables:
243  * c-basic-offset: 4
244  * c-file-style: "Stroustrup"
245  * indent-tabs-mode: nil
246  * End:
247  * vim: shiftwidth=4 tabstop=8 expandtab
248  */
249