Code updates which makes things compile as C++. Mostly type casts were
[yaz-moved-to-github.git] / src / marc_read_xml.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: marc_read_xml.c,v 1.3 2007-05-06 20:12:20 adam Exp $
6  */
7
8 /**
9  * \file marc_read_xml.c
10  * \brief Implements reading of MARC as XML
11  */
12
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #ifdef WIN32
18 #include <windows.h>
19 #endif
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <yaz/marcdisp.h>
25 #include <yaz/wrbuf.h>
26 #include <yaz/yaz-util.h>
27 #include <yaz/nmem_xml.h>
28
29 #if YAZ_HAVE_XML2
30 #include <libxml/tree.h>
31 #endif
32
33 #if YAZ_HAVE_XML2
34 int yaz_marc_read_xml_subfields(yaz_marc_t mt, const xmlNode *ptr)
35 {
36     NMEM nmem = yaz_marc_get_nmem(mt);
37     for (; ptr; ptr = ptr->next)
38     {
39         if (ptr->type == XML_ELEMENT_NODE)
40         {
41             if (!strcmp((const char *) ptr->name, "subfield"))
42             {
43                 size_t ctrl_data_len = 0;
44                 char *ctrl_data_buf = 0;
45                 const xmlNode *p = 0, *ptr_code = 0;
46                 struct _xmlAttr *attr;
47                 for (attr = ptr->properties; attr; attr = attr->next)
48                     if (!strcmp((const char *)attr->name, "code"))
49                         ptr_code = attr->children;
50                     else
51                     {
52                         yaz_marc_cprintf(
53                             mt, "Bad attribute '%.80s' for 'subfield'",
54                             attr->name);
55                         return -1;
56                     }
57                 if (!ptr_code)
58                 {
59                     yaz_marc_cprintf(
60                         mt, "Missing attribute 'code' for 'subfield'" );
61                     return -1;
62                 }
63                 if (ptr_code->type == XML_TEXT_NODE)
64                 {
65                     ctrl_data_len = 
66                         strlen((const char *)ptr_code->content);
67                 }
68                 else
69                 {
70                     yaz_marc_cprintf(
71                         mt, "Missing value for 'code' in 'subfield'" );
72                     return -1;
73                 }
74                 for (p = ptr->children; p ; p = p->next)
75                     if (p->type == XML_TEXT_NODE)
76                         ctrl_data_len += strlen((const char *)p->content);
77                 ctrl_data_buf = (char *) nmem_malloc(nmem, ctrl_data_len+1);
78                 strcpy(ctrl_data_buf, (const char *)ptr_code->content);
79                 for (p = ptr->children; p ; p = p->next)
80                     if (p->type == XML_TEXT_NODE)
81                         strcat(ctrl_data_buf, (const char *)p->content);
82                 yaz_marc_add_subfield(mt, ctrl_data_buf, ctrl_data_len);
83             }
84             else
85             {
86                 yaz_marc_cprintf(
87                     mt, "Expected element 'subfield', got '%.80s'", ptr->name);
88                 return -1;
89             }
90         }
91     }
92     return 0;
93 }
94
95 static int yaz_marc_read_xml_leader(yaz_marc_t mt, const xmlNode **ptr_p)
96 {
97     int indicator_length;
98     int identifier_length;
99     int base_address;
100     int length_data_entry;
101     int length_starting;
102     int length_implementation;
103     const char *leader = 0;
104     const xmlNode *ptr = *ptr_p;
105
106     for(; ptr; ptr = ptr->next)
107         if (ptr->type == XML_ELEMENT_NODE)
108         {
109             if (!strcmp((const char *) ptr->name, "leader"))
110             {
111                 xmlNode *p = ptr->children;
112                 for(; p; p = p->next)
113                     if (p->type == XML_TEXT_NODE)
114                         leader = (const char *) p->content;
115                 break;
116             }
117             else
118             {
119                 yaz_marc_cprintf(
120                     mt, "Expected element 'leader', got '%.80s'", ptr->name);
121                 return -1;
122             }
123         }
124     if (!leader)
125     {
126         yaz_marc_cprintf(mt, "Missing element 'leader'");
127         return -1;
128     }
129     if (strlen(leader) != 24)
130     {
131         yaz_marc_cprintf(mt, "Bad length %d of leader data."
132                          " Must have length of 24 characters", strlen(leader));
133         return -1;
134     }
135     yaz_marc_set_leader(mt, leader,
136                         &indicator_length,
137                         &identifier_length,
138                         &base_address,
139                         &length_data_entry,
140                         &length_starting,
141                         &length_implementation);
142     *ptr_p = ptr;
143     return 0;
144 }
145
146 static int yaz_marc_read_xml_fields(yaz_marc_t mt, const xmlNode *ptr)
147 {
148     for(; ptr; ptr = ptr->next)
149         if (ptr->type == XML_ELEMENT_NODE)
150         {
151             if (!strcmp((const char *) ptr->name, "controlfield"))
152             {
153                 const xmlNode *ptr_tag = 0;
154                 struct _xmlAttr *attr;
155                 for (attr = ptr->properties; attr; attr = attr->next)
156                     if (!strcmp((const char *)attr->name, "tag"))
157                         ptr_tag = attr->children;
158                     else
159                     {
160                         yaz_marc_cprintf(
161                             mt, "Bad attribute '%.80s' for 'controlfield'",
162                             attr->name);
163                         return -1;
164                     }
165                 if (!ptr_tag)
166                 {
167                     yaz_marc_cprintf(
168                         mt, "Missing attribute 'tag' for 'controlfield'" );
169                     return -1;
170                 }
171                 yaz_marc_add_controlfield_xml(mt, ptr_tag, ptr->children);
172             }
173             else if (!strcmp((const char *) ptr->name, "datafield"))
174             {
175                 char indstr[11]; /* 0(unused), 1,....9, + zero term */
176                 const xmlNode *ptr_tag = 0;
177                 struct _xmlAttr *attr;
178                 int i;
179                 for (i = 0; i<11; i++)
180                     indstr[i] = '\0';
181                 for (attr = ptr->properties; attr; attr = attr->next)
182                     if (!strcmp((const char *)attr->name, "tag"))
183                         ptr_tag = attr->children;
184                     else if (strlen((const char *)attr->name) == 4 &&
185                              !memcmp(attr->name, "ind", 3))
186                     {
187                         int no = atoi((const char *)attr->name+3);
188                         if (attr->children
189                             && attr->children->type == XML_TEXT_NODE)
190                             indstr[no] = attr->children->content[0];
191                     }
192                     else
193                     {
194                         yaz_marc_cprintf(
195                             mt, "Bad attribute '%.80s' for 'datafield'",
196                             attr->name);
197                         return -1;
198                     }
199                 if (!ptr_tag)
200                 {
201                     yaz_marc_cprintf(
202                         mt, "Missing attribute 'tag' for 'datafield'" );
203                     return -1;
204                 }
205                 /* note that indstr[0] is unused so we use indstr[1..] */
206                 yaz_marc_add_datafield_xml(mt, ptr_tag,
207                                            indstr+1, strlen(indstr+1));
208                 
209                 if (yaz_marc_read_xml_subfields(mt, ptr->children))
210                     return -1;
211             }
212             else
213             {
214                 yaz_marc_cprintf(mt,
215                                  "Expected element controlfield or datafield,"
216                                  " got %.80s", ptr->name);
217                 return -1;
218             }
219         }
220     return 0;
221 }
222 #endif
223
224 int yaz_marc_read_xml(yaz_marc_t mt, const xmlNode *ptr)
225 {
226 #if YAZ_HAVE_XML2
227     for(; ptr; ptr = ptr->next)
228         if (ptr->type == XML_ELEMENT_NODE)
229         {
230             if (!strcmp((const char *) ptr->name, "record"))
231                 break;
232             else
233             {
234                 yaz_marc_cprintf(
235                     mt, "Unknown element '%.80s' in MARC XML reader",
236                     ptr->name);
237                 return -1;
238             }
239         }
240     if (!ptr)
241     {
242         yaz_marc_cprintf(mt, "Missing element 'record' in MARC XML record");
243         return -1;
244     }
245     /* ptr points to record node now */
246     ptr = ptr->children;
247     if (yaz_marc_read_xml_leader(mt, &ptr))
248         return -1;
249     return yaz_marc_read_xml_fields(mt, ptr->next);
250 #else
251     return -1;
252 #endif
253 }
254
255
256 /*
257  * Local variables:
258  * c-basic-offset: 4
259  * indent-tabs-mode: nil
260  * End:
261  * vim: shiftwidth=4 tabstop=8 expandtab
262  */
263