97e99e5bebe201c287e3341c60bf42b257df377c
[yaz-moved-to-github.git] / src / marc_read_xml.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 /**
7  * \file marc_read_xml.c
8  * \brief Implements reading of MARC as XML
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #ifdef WIN32
16 #include <windows.h>
17 #endif
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <yaz/marcdisp.h>
22 #include <yaz/wrbuf.h>
23 #include <yaz/yaz-util.h>
24 #include <yaz/nmem_xml.h>
25
26 #if YAZ_HAVE_XML2
27 #include <libxml/tree.h>
28 #endif
29
30 #if YAZ_HAVE_XML2
31 int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
32 {
33     NMEM nmem = yaz_marc_get_nmem(mt);
34     for (; ptr; ptr = ptr->next)
35     {
36         if (ptr->type == XML_ELEMENT_NODE)
37         {
38             if (!strcmp((const char *) ptr->name, "subfield"))
39             {
40                 size_t ctrl_data_len = 0;
41                 char *ctrl_data_buf = 0;
42                 const xmlNode *p = 0, *ptr_code = 0;
43                 struct _xmlAttr *attr;
44                 for (attr = ptr->properties; attr; attr = attr->next)
45                     if (!strcmp((const char *)attr->name, "code"))
46                         ptr_code = attr->children;
47                     else
48                     {
49                         yaz_marc_cprintf(
50                             mt, "Bad attribute '%.80s' for 'subfield'",
51                             attr->name);
52                         return -1;
53                     }
54                 if (!ptr_code)
55                 {
56                     yaz_marc_cprintf(
57                         mt, "Missing attribute 'code' for 'subfield'" );
58                     return -1;
59                 }
60                 if (ptr_code->type == XML_TEXT_NODE)
61                 {
62                     ctrl_data_len =
63                         strlen((const char *)ptr_code->content);
64                 }
65                 else
66                 {
67                     yaz_marc_cprintf(
68                         mt, "Missing value for 'code' in 'subfield'" );
69                     return -1;
70                 }
71                 for (p = ptr->children; p ; p = p->next)
72                     if (p->type == XML_TEXT_NODE)
73                         ctrl_data_len += strlen((const char *)p->content);
74                 ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
75                 strcpy(ctrl_data_buf, (const char *)ptr_code->content);
76                 for (p = ptr->children; p ; p = p->next)
77                     if (p->type == XML_TEXT_NODE)
78                         strcat(ctrl_data_buf, (const char *)p->content);
79                 yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
80             }
81             else
82             {
83                 yaz_marc_cprintf(
84                     mt, "Expected element 'subfield', got '%.80s'", ptr->name);
85                 return -1;
86             }
87         }
88     }
89     return 0;
90 }
91
92 const char *tag_value_extract(const char *name, char tag_buffer[5])
93 {
94     size_t length = strlen(name);
95     if (length == 3)
96     {
97         strcpy(tag_buffer, name);
98         return tag_buffer;
99     }
100     return 0;
101 }
102
103 // Given a xmlNode ptr,  extract a value from either a element name or from a given attribute
104 char *element_attribute_value_extract(const xmlNode *ptr,
105                                       const char *attribute_name,
106                                       NMEM nmem)
107 {
108     const char *name = (const char *) ptr->name;
109     size_t length = strlen(name);
110     xmlAttr *attr;
111     if (length > 1 )
112         return nmem_strdup(nmem, name+1);
113     // TODO Extract from attribute where matches attribute_name
114     for (attr = ptr->properties; attr; attr = attr->next)
115         if (!strcmp((const char *)attr->name, attribute_name))
116             return nmem_text_node_cdata(attr->children, nmem);
117     return 0;
118 }
119
120
121 int yaz_marc_read_turbo_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
122 {
123     for (; ptr; ptr = ptr->next)
124     {
125         if (ptr->type == XML_ELEMENT_NODE)
126         {
127             if (!strncmp((const char *) ptr->name, "s", 1))
128             {
129                 NMEM nmem = yaz_marc_get_nmem(mt);
130                 xmlNode *p;
131                 size_t ctrl_data_len = 0;
132                 char *ctrl_data_buf = 0;
133                 const char *tag_value = element_attribute_value_extract(ptr, "code", nmem);
134                 if (!tag_value)
135                 {
136                     yaz_marc_cprintf(
137                         mt, "Missing 'code' value for 'subfield'" );
138                     return -1;
139                 }
140
141                 ctrl_data_len = strlen((const char *) tag_value);
142                 // Extract (length) from CDATA
143                 for (p = ptr->children; p ; p = p->next)
144                     if (p->type == XML_TEXT_NODE)
145                         ctrl_data_len += strlen((const char *)p->content);
146                 // Allocate memory for code value (1 character (can be multi-byte) and data
147                 ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
148                 // Build a string with "<Code><data>"
149                 strcpy(ctrl_data_buf, (const char *) tag_value);
150                 for (p = ptr->children; p ; p = p->next)
151                     if (p->type == XML_TEXT_NODE)
152                         strcat(ctrl_data_buf, (const char *)p->content);
153                 yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
154             }
155             else
156             {
157                 yaz_marc_cprintf(
158                     mt, "Expected element 'subfield', got '%.80s'", ptr->name);
159                 return -1;
160             }
161         }
162     }
163     return 0;
164 }
165
166
167 static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p,
168                                     int *indicator_length)
169 {
170     int identifier_length;
171     int base_address;
172     int length_data_entry;
173     int length_starting;
174     int length_implementation;
175     const char *leader = 0;
176     const xmlNode *ptr = *ptr_p;
177
178     for(; ptr; ptr = ptr->next)
179         if (ptr->type == XML_ELEMENT_NODE)
180         {
181             if ( !strcmp( (const char *) ptr->name, "leader") ||
182                  (!strncmp((const char *) ptr->name, "l", 1) ))
183             {
184                 xmlNode *p = ptr->children;
185                 for(; p; p = p->next)
186                     if (p->type == XML_TEXT_NODE)
187                         leader = (const char *) p->content;
188             }
189             break;
190         }
191     if (!leader)
192     {
193         yaz_marc_cprintf(mt, "Missing leader. Inserting fake leader");
194         leader = "00000nam a22000000a 4500";
195     }
196     if (strlen(leader) != 24)
197     {
198         yaz_marc_cprintf(mt, "Bad length %d of leader data."
199                          " Must have length of 24 characters", strlen(leader));
200         return -1;
201     }
202     yaz_marc_set_leader(mt, leader,
203                         indicator_length,
204                         &identifier_length,
205                         &base_address,
206                         &length_data_entry,
207                         &length_starting,
208                         &length_implementation);
209     *ptr_p = ptr;
210     return 0;
211 }
212
213 static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr,
214                                     int indicator_length)
215 {
216     for(; ptr; ptr = ptr->next)
217         if (ptr->type == XML_ELEMENT_NODE)
218         {
219             if (!strcmp( (const char *) ptr->name, "controlfield"))
220             {
221                 const xmlNode *ptr_tag = 0;
222                 struct _xmlAttr *attr;
223                 for (attr = ptr->properties; attr; attr = attr->next)
224                     if (!strcmp((const char *)attr->name, "tag"))
225                         ptr_tag = attr->children;
226                     else
227                     {
228                         yaz_marc_cprintf(
229                             mt, "Bad attribute '%.80s' for 'controlfield'",
230                             attr->name);
231                         return -1;
232                     }
233                 if (!ptr_tag)
234                 {
235                     yaz_marc_cprintf(
236                         mt, "Missing attribute 'tag' for 'controlfield'" );
237                     return -1;
238                 }
239                 yaz_marc_add_controlfield_xml(mt, ptr_tag, ptr->children);
240             }
241             else if (!strcmp((const char *) ptr->name, "datafield"))
242             {
243                 char indstr[11]; /* 0(unused), 1,....9, + zero term */
244                 const xmlNode *ptr_tag = 0;
245                 struct _xmlAttr *attr;
246                 int i;
247                 for (i = 0; i < indicator_length; i++)
248                     indstr[i] = ' ';
249                 indstr[i] = '\0';
250                 for (attr = ptr->properties; attr; attr = attr->next)
251                     if (!strcmp((const char *)attr->name, "tag"))
252                         ptr_tag = attr->children;
253                     else if (strlen((const char *)attr->name) == 4 &&
254                              !memcmp(attr->name, "ind", 3))
255                     {
256                         int no = atoi((const char *)attr->name + 3);
257                         if (attr->children &&
258                             attr->children->type == XML_TEXT_NODE &&
259                             no <= indicator_length && no > 0 &&
260                             attr->children->content[0])
261                         {
262                             indstr[no - 1] = attr->children->content[0];
263                         }
264                         else
265                         {
266                             yaz_marc_cprintf(
267                                 mt, "Bad attribute '%.80s' for 'datafield'",
268                                 attr->name);
269                         }
270                     }
271                     else
272                     {
273                         yaz_marc_cprintf(
274                             mt, "Bad attribute '%.80s' for 'datafield'",
275                             attr->name);
276                     }
277                 if (!ptr_tag)
278                 {
279                     yaz_marc_cprintf(
280                         mt, "Missing attribute 'tag' for 'datafield'" );
281                     return -1;
282                 }
283                 yaz_marc_add_datafield_xml(mt, ptr_tag,
284                                            indstr, indicator_length);
285                 if (yaz_marc_read_xml_subfields(mt, ptr->children))
286                     return -1;
287             }
288             else
289             {
290                 yaz_marc_cprintf(mt,
291                                  "Expected element controlfield or datafield,"
292                                  " got %.80s", ptr->name);
293                 return -1;
294             }
295         }
296     return 0;
297 }
298
299
300 static int yaz_marc_read_turbo_xml_fields(yaz_marc_t mt, const xmlNode *ptr,
301                                           int indicator_length)
302 {
303     for(; ptr; ptr = ptr->next)
304         if (ptr->type == XML_ELEMENT_NODE)
305         {
306             if (!strncmp( (const char *) ptr->name, "c", 1))
307             {
308                 NMEM nmem = yaz_marc_get_nmem(mt);
309                 char *tag_value = element_attribute_value_extract(ptr, "tag", nmem);
310                 if (!tag_value)
311                 {
312                     yaz_marc_cprintf(
313                         mt, "Missing attribute 'tag' for 'controlfield'" );
314                     return -1;
315                 }
316                 yaz_marc_add_controlfield_xml2(mt, tag_value, ptr->children);
317             }
318             else if (!strncmp((const char *) ptr->name, "d",1))
319             {
320                 struct _xmlAttr *attr;
321                 NMEM nmem = yaz_marc_get_nmem(mt);
322                 char *tag_value;
323                 char *indstr = nmem_malloc(nmem, indicator_length + 1);
324                 int i = 0;
325                 for (i = 0; i < indicator_length; i++)
326                     indstr[i] = ' ';
327                 indstr[i] = '\0';
328                 tag_value = element_attribute_value_extract(ptr, "tag", nmem);
329                 if (!tag_value)
330                 {
331                     yaz_marc_cprintf(
332                         mt, "Missing attribute 'tag' for 'datafield'" );
333                     return -1;
334                 }
335                 for (attr = ptr->properties; attr; attr = attr->next)
336                     if (strlen((const char *)attr->name) == 2 &&
337                         attr->name[0] == 'i')
338                     {
339                         //extract indicator attribute from i#="Y" pattern
340                         int no = atoi((const char *)attr->name + 1);
341                         if (attr->children &&
342                             attr->children->type == XML_TEXT_NODE &&
343                             no <= indicator_length && no > 0 &&
344                             attr->children->content[0])
345                         {
346                             indstr[no - 1] = attr->children->content[0];
347                         }
348                         else
349                         {
350                             yaz_marc_cprintf(
351                                 mt, "Bad attribute '%.80s' for 'd'",attr->name);
352                         }
353                     }
354                     else
355                     {
356                         yaz_marc_cprintf(
357                             mt, "Bad attribute '%.80s' for 'd'", attr->name);
358                     }
359                 yaz_marc_add_datafield_xml2(mt, tag_value, indstr);
360                 if (yaz_marc_read_turbo_xml_subfields(mt, ptr->children /*, indstr */))
361                     return -1;
362             }
363             else
364             {
365                 yaz_marc_cprintf(mt,
366                                  "Expected element controlfield or datafield,"
367                                  " got %.80s", ptr->name);
368                 return -1;
369             }
370         }
371     return 0;
372 }
373
374
375 #endif
376
377 #if YAZ_HAVE_XML2
378 int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
379 {
380     int indicator_length = 0;
381     int format = 0;
382     yaz_marc_reset(mt);
383
384     for(; ptr; ptr = ptr->next)
385         if (ptr->type == XML_ELEMENT_NODE)
386         {
387             if (!strcmp((const char *) ptr->name, "record"))
388             {
389                 format = YAZ_MARC_MARCXML;
390                 break;
391             }
392             else if (!strcmp((const char *) ptr->name, "r"))
393             {
394                 format = YAZ_MARC_TURBOMARC;
395                 break;
396             }
397             else
398             {
399                 yaz_marc_cprintf(
400                     mt, "Unknown element '%.80s' in MARC XML reader",
401                     ptr->name);
402                 return -1;
403             }
404         }
405     if (!ptr)
406     {
407         yaz_marc_cprintf(mt, "Missing element 'record' in MARC XML record");
408         return -1;
409     }
410     /* ptr points to record node now */
411     ptr = ptr->children;
412     if (yaz_marc_read_xml_leader(mt, &ptr, &indicator_length))
413         return -1;
414
415     switch (format)
416     {
417     case YAZ_MARC_MARCXML:
418         return yaz_marc_read_xml_fields(mt, ptr->next, indicator_length);
419     case YAZ_MARC_TURBOMARC:
420         return yaz_marc_read_turbo_xml_fields(mt, ptr->next, indicator_length);
421     }
422     return -1;
423 }
424 #endif
425
426
427 /*
428  * Local variables:
429  * c-basic-offset: 4
430  * c-file-style: "Stroustrup"
431  * indent-tabs-mode: nil
432  * End:
433  * vim: shiftwidth=4 tabstop=8 expandtab
434  */
435