Fix bug #814: pazpar2 does not compile on Debian woody. Use xmlParseFile
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.11 2007-01-14 17:34:31 adam Exp $ */
2
3 #include <string.h>
4
5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
7 #include <libxslt/xslt.h>
8 #include <libxslt/transform.h>
9 #include <libxslt/xsltutils.h>
10
11 #if HAVE_CONFIG_H
12 #include <cconfig.h>
13 #endif
14
15 #include <yaz/yaz-util.h>
16 #include <yaz/nmem.h>
17
18 #define CONFIG_NOEXTERNS
19 #include "config.h"
20
21 static NMEM nmem = 0;
22 static char confdir[256] = ".";
23
24 struct conf_config *config = 0;
25
26 /* Code to parse configuration file */
27 /* ==================================================== */
28
29 static struct conf_service *parse_service(xmlNode *node)
30 {
31     xmlNode *n;
32     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
33     int md_node = 0;
34
35     r->num_sortkeys = r->num_metadata = 0;
36     // Allocate array of conf metadata structs, if necessary
37     for (n = node->children; n; n = n->next)
38         if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, "metadata"))
39         {
40             xmlChar *sortkey = xmlGetProp(n, "sortkey");
41             r->num_metadata++;
42             if (sortkey && strcmp(sortkey, "no"))
43                 r->num_sortkeys++;
44             xmlFree(sortkey);
45         }
46     if (r->num_metadata)
47         r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
48     else
49         r->metadata = 0;
50     if (r->num_sortkeys)
51         r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
52     else
53         r->sortkeys = 0;
54
55     for (n = node->children; n; n = n->next)
56     {
57         if (n->type != XML_ELEMENT_NODE)
58             continue;
59         if (!strcmp(n->name, "metadata"))
60         {
61             struct conf_metadata *md = &r->metadata[md_node];
62             xmlChar *name = xmlGetProp(n, "name");
63             xmlChar *brief = xmlGetProp(n, "brief");
64             xmlChar *sortkey = xmlGetProp(n, "sortkey");
65             xmlChar *merge = xmlGetProp(n, "merge");
66             xmlChar *type = xmlGetProp(n, "type");
67             xmlChar *termlist = xmlGetProp(n, "termlist");
68             xmlChar *rank = xmlGetProp(n, "rank");
69
70             if (!name)
71             {
72                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
73                 return 0;
74             }
75             md->name = nmem_strdup(nmem, name);
76             if (brief)
77             {
78                 if (!strcmp(brief, "yes"))
79                     md->brief = 1;
80                 else if (strcmp(brief, "no"))
81                 {
82                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
83                     return 0;
84                 }
85             }
86             else
87                 md->brief = 0;
88
89             if (termlist)
90             {
91                 if (!strcmp(termlist, "yes"))
92                     md->termlist = 1;
93                 else if (strcmp(termlist, "no"))
94                 {
95                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
96                     return 0;
97                 }
98             }
99             else
100                 md->termlist = 0;
101
102             if (rank)
103                 md->rank = atoi(rank);
104             else
105                 md->rank = 0;
106
107             if (type)
108             {
109                 if (!strcmp(type, "generic"))
110                     md->type = Metadata_type_generic;
111                 else if (!strcmp(type, "integer"))
112                     md->type = Metadata_type_integer;
113                 else if (!strcmp(type, "year"))
114                     md->type = Metadata_type_year;
115                 else
116                 {
117                     yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
118                     return 0;
119                 }
120             }
121             else
122                 md->type = Metadata_type_generic;
123
124             if (sortkey)
125             {
126                 if (!strcmp(sortkey, "no"))
127                     md->sortkey = Metadata_sortkey_no;
128                 else if (!strcmp(sortkey, "numeric"))
129                     md->sortkey = Metadata_sortkey_numeric;
130                 else if (!strcmp(sortkey, "range"))
131                     md->sortkey = Metadata_sortkey_range;
132                 else if (!strcmp(sortkey, "skiparticle"))
133                     md->sortkey = Metadata_sortkey_skiparticle;
134                 else
135                 {
136                     yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
137                     return 0;
138                 }
139             }
140             else
141                 md->sortkey = Metadata_sortkey_no;
142
143             if (merge)
144             {
145                 if (!strcmp(merge, "no"))
146                     md->merge = Metadata_merge_no;
147                 else if (!strcmp(merge, "unique"))
148                     md->merge = Metadata_merge_unique;
149                 else if (!strcmp(merge, "longest"))
150                     md->merge = Metadata_merge_longest;
151                 else if (!strcmp(merge, "range"))
152                     md->merge = Metadata_merge_range;
153                 else if (!strcmp(merge, "all"))
154                     md->merge = Metadata_merge_all;
155                 else
156                 {
157                     yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
158                     return 0;
159                 }
160             }
161             else
162                 md->merge = Metadata_merge_no;
163
164             xmlFree(name);
165             xmlFree(brief);
166             xmlFree(sortkey);
167             xmlFree(merge);
168             xmlFree(termlist);
169             xmlFree(rank);
170             md_node++;
171         }
172         else
173         {
174             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
175             return 0;
176         }
177     }
178     return r;
179 }
180
181 static struct conf_server *parse_server(xmlNode *node)
182 {
183     xmlNode *n;
184     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
185
186     r->host = 0;
187     r->port = 0;
188     r->proxy_host = 0;
189     r->proxy_port = 0;
190     r->service = 0;
191     r->next = 0;
192
193     for (n = node->children; n; n = n->next)
194     {
195         if (n->type != XML_ELEMENT_NODE)
196             continue;
197         if (!strcmp(n->name, "listen"))
198         {
199             xmlChar *port = xmlGetProp(n, "port");
200             xmlChar *host = xmlGetProp(n, "host");
201             if (port)
202                 r->port = atoi(port);
203             if (host)
204                 r->host = nmem_strdup(nmem, host);
205             xmlFree(port);
206             xmlFree(host);
207         }
208         else if (!strcmp(n->name, "proxy"))
209         {
210             xmlChar *port = xmlGetProp(n, "port");
211             xmlChar *host = xmlGetProp(n, "host");
212             if (port)
213                 r->proxy_port = atoi(port);
214             if (host)
215                 r->proxy_host = nmem_strdup(nmem, host);
216             xmlFree(port);
217             xmlFree(host);
218         }
219         else if (!strcmp(n->name, "service"))
220         {
221             struct conf_service *s = parse_service(n);
222             if (!s)
223                 return 0;
224             r->service = s;
225         }
226         else
227         {
228             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
229             return 0;
230         }
231     }
232     return r;
233 }
234
235 static xsltStylesheet *load_stylesheet(const char *fname)
236 {
237     char path[256];
238     sprintf(path, "%s/%s", confdir, fname);
239     return xsltParseStylesheetFile(path);
240 }
241
242 static void setup_marc(struct conf_retrievalprofile *r)
243 {
244     yaz_iconv_t cm;
245     r->yaz_marc = yaz_marc_create();
246     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
247     {
248         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
249         return;
250     }
251     yaz_marc_iconv(r->yaz_marc, cm);
252 }
253
254 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
255 {
256     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
257     xmlNode *n;
258     struct conf_retrievalmap **rm = &r->maplist;
259
260     r->requestsyntax = 0;
261     r->native_syntax = Nativesyn_xml;
262     r->native_format = Nativeform_na;
263     r->native_encoding = 0;
264     r->native_mapto = Nativemapto_na;
265     r->yaz_marc = 0;
266     r->maplist = 0;
267     r->next = 0;
268
269     for (n = node->children; n; n = n->next)
270     {
271         if (n->type != XML_ELEMENT_NODE)
272             continue;
273         if (!strcmp(n->name, "requestsyntax"))
274         {
275             xmlChar *content = xmlNodeGetContent(n);
276             if (content)
277                 r->requestsyntax = nmem_strdup(nmem, content);
278         }
279         else if (!strcmp(n->name, "nativesyntax"))
280         {
281             xmlChar *name = xmlGetProp(n, "name");
282             xmlChar *format = xmlGetProp(n, "format");
283             xmlChar *encoding = xmlGetProp(n, "encoding");
284             xmlChar *mapto = xmlGetProp(n, "mapto");
285             if (!name)
286             {
287                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
288                 return 0;
289             }
290             if (!strcmp(name, "iso2709"))
291             {
292                 r->native_syntax = Nativesyn_iso2709;
293                 // Set a few defaults, too
294                 r->native_format = Nativeform_marc21;
295                 r->native_mapto = Nativemapto_marcxml;
296                 r->native_encoding = "marc-8";
297                 setup_marc(r);
298             }
299             else if (!strcmp(name, "xml"))
300                 r->native_syntax = Nativesyn_xml;
301             else
302             {
303                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
304                 return 0;
305             }
306             if (format)
307             {
308                 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
309                     r->native_format = Nativeform_marc21;
310                 else
311                 {
312                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
313                     return 0;
314                 }
315             }
316             if (encoding)
317                 r->native_encoding = encoding;
318             if (mapto)
319             {
320                 if (!strcmp(mapto, "marcxml"))
321                     r->native_mapto = Nativemapto_marcxml;
322                 else if (!strcmp(mapto, "marcxchange"))
323                     r->native_mapto = Nativemapto_marcxchange;
324                 else
325                 {
326                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
327                     return 0;
328                 }
329             }
330             xmlFree(name);
331             xmlFree(format);
332             xmlFree(encoding);
333             xmlFree(mapto);
334         }
335         else if (!strcmp(n->name, "map"))
336         {
337             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
338             xmlChar *type = xmlGetProp(n, "type");
339             xmlChar *charset = xmlGetProp(n, "charset");
340             xmlChar *format = xmlGetProp(n, "format");
341             xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
342             memset(m, 0, sizeof(*m));
343             if (type)
344             {
345                 if (!strcmp(type, "xslt"))
346                     m->type = Map_xslt;
347                 else
348                 {
349                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
350                     return 0;
351                 }
352             }
353             if (charset)
354                 m->charset = nmem_strdup(nmem, charset);
355             if (format)
356                 m->format = nmem_strdup(nmem, format);
357             if (stylesheet)
358             {
359                 if (!(m->stylesheet = load_stylesheet(stylesheet)))
360                     return 0;
361             }
362             *rm = m;
363             rm = &m->next;
364             xmlFree(type);
365             xmlFree(charset);
366             xmlFree(format);
367             xmlFree(stylesheet);
368         }
369         else
370         {
371             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
372             return 0;
373         }
374     }
375
376     return r;
377 }
378
379 static struct conf_config *parse_config(xmlNode *root)
380 {
381     xmlNode *n;
382     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
383     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
384
385     r->servers = 0;
386     r->queryprofiles = 0;
387     r->retrievalprofiles = 0;
388
389     for (n = root->children; n; n = n->next)
390     {
391         if (n->type != XML_ELEMENT_NODE)
392             continue;
393         if (!strcmp(n->name, "server"))
394         {
395             struct conf_server *tmp = parse_server(n);
396             if (!tmp)
397                 return 0;
398             tmp->next = r->servers;
399             r->servers = tmp;
400         }
401         else if (!strcmp(n->name, "queryprofile"))
402         {
403         }
404         else if (!strcmp(n->name, "retrievalprofile"))
405         {
406             if (!(*rp = parse_retrievalprofile(n)))
407                 return 0;
408             rp = &(*rp)->next;
409         }
410         else
411         {
412             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
413             return 0;
414         }
415     }
416     return r;
417 }
418
419 int read_config(const char *fname)
420 {
421     xmlDoc *doc = xmlParseFile(fname);
422     const char *p;
423
424     if (!nmem)  // Initialize
425     {
426         nmem = nmem_create();
427         xmlSubstituteEntitiesDefault(1);
428         xmlLoadExtDtdDefaultValue = 1;
429     }
430     if (!doc)
431     {
432         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
433         exit(1);
434     }
435     if ((p = strrchr(fname, '/')))
436     {
437         int len = p - fname;
438         strncpy(confdir, fname, len);
439         confdir[len] = '\0';
440     }
441     config = parse_config(xmlDocGetRootElement(doc));
442     xmlFreeDoc(doc);
443
444     if (config)
445         return 1;
446     else
447         return 0;
448 }
449
450
451 /*
452  * Local variables:
453  * c-basic-offset: 4
454  * indent-tabs-mode: nil
455  * End:
456  * vim: shiftwidth=4 tabstop=8 expandtab
457  */