zoomtst3: print event name
[yaz-moved-to-github.git] / src / cclxmlconfig.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /** \file cclxmlconfig.c
7     \brief XML configuration for CCL
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13
14 #include <yaz/ccl_xml.h>
15
16 #if YAZ_HAVE_XML2
17
18 static int ccl_xml_config_combqual(WRBUF wrbuf,
19                                    const xmlNode *ptr,
20                                    const char **addinfo)
21 {
22     struct _xmlAttr *attr;
23     const char *name = 0;
24     for (attr = ptr->properties; attr; attr = attr->next)
25     {
26         if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
27             attr->children && attr->children->type == XML_TEXT_NODE)
28             name = (const char *) attr->children->content;
29         else
30         {
31             *addinfo = "bad attribute for 'attr'. "
32                 "Expecting 'type', 'value', or 'attrset'";
33             return 1;
34         }
35     }
36     if (!name)
37     {
38         *addinfo = "missing attribute for 'name' for element 'qual'";
39         return 1;
40     }
41     wrbuf_printf(wrbuf, "%s", name);
42     return 0;
43
44
45 }
46
47 static int ccl_xml_config_attr(const char *default_set,
48                                WRBUF wrbuf,
49                                const xmlNode *ptr,
50                                const char **addinfo)
51 {
52     struct _xmlAttr *attr;
53     const char *type = 0;
54     const char *value = 0;
55     const char *attrset = default_set;
56     for (attr = ptr->properties; attr; attr = attr->next)
57     {
58         if (!xmlStrcmp(attr->name, BAD_CAST "type") &&
59             attr->children && attr->children->type == XML_TEXT_NODE)
60             type = (const char *) attr->children->content;
61         else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
62             attr->children && attr->children->type == XML_TEXT_NODE)
63             value = (const char *) attr->children->content;
64         else if (!xmlStrcmp(attr->name, BAD_CAST "attrset") &&
65             attr->children && attr->children->type == XML_TEXT_NODE)
66             attrset = (const char *) attr->children->content;
67         else
68         {
69             *addinfo = "bad attribute for 'attr'. "
70                 "Expecting 'type', 'value', or 'attrset'";
71             return 1;
72         }
73     }
74     if (!type)
75     {
76         *addinfo = "missing attribute for 'type' for element 'attr'";
77         return 1;
78     }
79     if (!value)
80     {
81         *addinfo = "missing attribute for 'value' for element 'attr'";
82         return 1;
83     }
84     if (attrset)
85         wrbuf_printf(wrbuf, "%s,%s=%s", attrset, type, value);
86     else
87         wrbuf_printf(wrbuf, "%s=%s", type, value);
88     return 0;
89 }
90
91 static int ccl_xml_config_qual(CCL_bibset bibset, const char *default_set,
92                                WRBUF wrbuf, 
93                                const xmlNode *ptr,
94                                const char **addinfo)
95 {
96     struct _xmlAttr *attr;
97     const char *name = 0;
98     const xmlNode *a_ptr = ptr->children;
99     for (attr = ptr->properties; attr; attr = attr->next)
100     {
101         if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
102             attr->children && attr->children->type == XML_TEXT_NODE)
103             name = (const char *) attr->children->content;
104         else
105         {
106             *addinfo = "bad attribute for 'qual'. Expecting 'name' only";
107             return 1;
108         }
109     }
110     if (!name)
111     {
112         *addinfo = "missing attribute 'name' for 'qual' element";
113         return 1;
114     }
115     for (; a_ptr; a_ptr = a_ptr->next)
116     {
117         if (a_ptr->type == XML_ELEMENT_NODE)
118         {
119             if (!xmlStrcmp(a_ptr->name, BAD_CAST "attr"))
120             {
121                 int r = ccl_xml_config_attr(default_set, wrbuf,
122                                             a_ptr, addinfo);
123                 if (r)
124                     return r;
125                 wrbuf_printf(wrbuf, " ");
126             }
127             else if (!xmlStrcmp(a_ptr->name, BAD_CAST "qual"))
128             {
129                 int r = ccl_xml_config_combqual(wrbuf, a_ptr, addinfo);
130                 if (r)
131                     return r;
132                 wrbuf_printf(wrbuf, " ");
133             }
134             else
135             {
136                 *addinfo = "bad element: expecting 'attr'";
137                 return 1;
138             }
139         }
140     }
141     ccl_qual_fitem(bibset, wrbuf_cstr(wrbuf), name);
142     return 0;
143 }
144
145 int ccl_xml_config_directive(CCL_bibset bibset, const xmlNode *ptr,
146                              const char **addinfo)
147 {
148     struct _xmlAttr *attr;
149     const char *name = 0;
150     const char *value = 0;
151     for (attr = ptr->properties; attr; attr = attr->next)
152     {
153         if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
154             attr->children && attr->children->type == XML_TEXT_NODE)
155             name = (const char *) attr->children->content;
156         else if (!xmlStrcmp(attr->name, BAD_CAST "value") &&
157             attr->children && attr->children->type == XML_TEXT_NODE)
158             value = (const char *) attr->children->content;
159         else
160         {
161             *addinfo = "bad attribute for 'diretive'. "
162                 "Expecting 'name' or 'value'";
163             return 1;
164         }
165     }
166     if (!name)
167     {
168         *addinfo = "missing attribute 'name' for 'directive' element";
169         return 1;
170     }
171     if (!value)
172     {
173         *addinfo = "missing attribute 'name' for 'value' element";
174         return 1;
175     }
176     ccl_qual_add_special(bibset, name, value);
177     return 0;
178 }
179
180 int ccl_xml_config(CCL_bibset bibset, const xmlNode *ptr, const char **addinfo)
181 {
182     if (ptr && ptr->type == XML_ELEMENT_NODE && 
183         !xmlStrcmp(ptr->name, BAD_CAST "cclmap"))
184     {
185         const xmlNode *c_ptr;
186         const char *set = 0;
187         struct _xmlAttr *attr;
188         for (attr = ptr->properties; attr; attr = attr->next)
189         {
190             if (!xmlStrcmp(attr->name, BAD_CAST "defaultattrset") &&
191                 attr->children && attr->children->type == XML_TEXT_NODE)
192                 set = (const char *) attr->children->content;
193             else
194             {
195                 *addinfo = "bad attribute for 'cclmap'. "
196                     "expecting 'defaultattrset'";
197                 return 1;
198             }
199         }
200         for (c_ptr = ptr->children; c_ptr; c_ptr = c_ptr->next)
201         {
202             if (c_ptr->type == XML_ELEMENT_NODE)
203             {
204                 if (!xmlStrcmp(c_ptr->name, BAD_CAST "qual"))
205                 {
206                     WRBUF wrbuf = wrbuf_alloc();
207                     int r = ccl_xml_config_qual(bibset, set,
208                                                 wrbuf, c_ptr, addinfo);
209                     wrbuf_destroy(wrbuf);
210                     if (r)
211                         return r;
212                 }
213                 else if (!xmlStrcmp(c_ptr->name, BAD_CAST "directive"))
214                 {
215                     int r = ccl_xml_config_directive(bibset, c_ptr, addinfo);
216                     if (r)
217                         return r;
218                 }
219                 else
220                 {
221                     *addinfo = "bad element for 'cclmap'. "
222                         "expecting 'directive' or 'qual'";
223                     return 1;
224                 }
225             }
226         }
227     }
228     return 0;
229 }
230 #endif
231
232 /*
233  * Local variables:
234  * c-basic-offset: 4
235  * c-file-style: "Stroustrup"
236  * indent-tabs-mode: nil
237  * End:
238  * vim: shiftwidth=4 tabstop=8 expandtab
239  */
240