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