WRBUF updates.
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.17 2007-03-20 07:27:51 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     int sk_node = 0;
35
36     r->num_sortkeys = r->num_metadata = 0;
37     // Allocate array of conf metadata and sortkey tructs, if necessary
38     for (n = node->children; n; n = n->next)
39         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
40                                                    n->name, "metadata"))
41         {
42             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
43             r->num_metadata++;
44             if (sortkey && strcmp((const char *) sortkey, "no"))
45                 r->num_sortkeys++;
46             xmlFree(sortkey);
47         }
48     if (r->num_metadata)
49         r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
50     else
51         r->metadata = 0;
52     if (r->num_sortkeys)
53         r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
54     else
55         r->sortkeys = 0;
56
57     for (n = node->children; n; n = n->next)
58     {
59         if (n->type != XML_ELEMENT_NODE)
60             continue;
61         if (!strcmp(n->name, (const char *) "metadata"))
62         {
63             struct conf_metadata *md = &r->metadata[md_node];
64             xmlChar *name = xmlGetProp(n, "name");
65             xmlChar *brief = xmlGetProp(n, "brief");
66             xmlChar *sortkey = xmlGetProp(n, "sortkey");
67             xmlChar *merge = xmlGetProp(n, "merge");
68             xmlChar *type = xmlGetProp(n, "type");
69             xmlChar *termlist = xmlGetProp(n, "termlist");
70             xmlChar *rank = xmlGetProp(n, "rank");
71
72             if (!name)
73             {
74                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
75                 return 0;
76             }
77             md->name = nmem_strdup(nmem, name);
78             if (brief)
79             {
80                 if (!strcmp(brief, "yes"))
81                     md->brief = 1;
82                 else if (strcmp(brief, "no"))
83                 {
84                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
85                     return 0;
86                 }
87             }
88             else
89                 md->brief = 0;
90
91             if (termlist)
92             {
93                 if (!strcmp(termlist, "yes"))
94                     md->termlist = 1;
95                 else if (strcmp(termlist, "no"))
96                 {
97                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
98                     return 0;
99                 }
100             }
101             else
102                 md->termlist = 0;
103
104             if (rank)
105                 md->rank = atoi(rank);
106             else
107                 md->rank = 0;
108
109             if (type)
110             {
111                 if (!strcmp(type, "generic"))
112                     md->type = Metadata_type_generic;
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 (merge)
125             {
126                 if (!strcmp(merge, "no"))
127                     md->merge = Metadata_merge_no;
128                 else if (!strcmp(merge, "unique"))
129                     md->merge = Metadata_merge_unique;
130                 else if (!strcmp(merge, "longest"))
131                     md->merge = Metadata_merge_longest;
132                 else if (!strcmp(merge, "range"))
133                     md->merge = Metadata_merge_range;
134                 else if (!strcmp(merge, "all"))
135                     md->merge = Metadata_merge_all;
136                 else
137                 {
138                     yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
139                     return 0;
140                 }
141             }
142             else
143                 md->merge = Metadata_merge_no;
144
145             if (sortkey && strcmp(sortkey, "no"))
146             {
147                 struct conf_sortkey *sk = &r->sortkeys[sk_node];
148                 if (md->merge == Metadata_merge_no)
149                 {
150                     yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
151                     return 0;
152                 }
153                 if (!strcmp(sortkey, "numeric"))
154                     sk->type = Metadata_sortkey_numeric;
155                 else if (!strcmp(sortkey, "skiparticle"))
156                     sk->type = Metadata_sortkey_skiparticle;
157                 else
158                 {
159                     yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
160                     return 0;
161                 }
162                 sk->name = md->name;
163                 md->sortkey_offset = sk_node;
164                 sk_node++;
165             }
166             else
167                 md->sortkey_offset = -1;
168
169             xmlFree(name);
170             xmlFree(brief);
171             xmlFree(sortkey);
172             xmlFree(merge);
173             xmlFree(termlist);
174             xmlFree(rank);
175             md_node++;
176         }
177         else
178         {
179             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
180             return 0;
181         }
182     }
183     return r;
184 }
185
186 static struct conf_server *parse_server(xmlNode *node)
187 {
188     xmlNode *n;
189     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
190
191     r->host = 0;
192     r->port = 0;
193     r->proxy_host = 0;
194     r->proxy_port = 0;
195     r->myurl = 0;
196     r->service = 0;
197     r->next = 0;
198
199     for (n = node->children; n; n = n->next)
200     {
201         if (n->type != XML_ELEMENT_NODE)
202             continue;
203         if (!strcmp(n->name, "listen"))
204         {
205             xmlChar *port = xmlGetProp(n, "port");
206             xmlChar *host = xmlGetProp(n, "host");
207             if (port)
208                 r->port = atoi(port);
209             if (host)
210                 r->host = nmem_strdup(nmem, host);
211             xmlFree(port);
212             xmlFree(host);
213         }
214         else if (!strcmp(n->name, "proxy"))
215         {
216             xmlChar *port = xmlGetProp(n, "port");
217             xmlChar *host = xmlGetProp(n, "host");
218             xmlChar *myurl = xmlGetProp(n, "myurl");
219             if (port)
220                 r->proxy_port = atoi(port);
221             if (host)
222                 r->proxy_host = nmem_strdup(nmem, host);
223             if (myurl)
224                 r->myurl = nmem_strdup(nmem, myurl);
225             else
226             {
227                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
228                 return 0;
229             }
230             xmlFree(port);
231             xmlFree(host);
232             xmlFree(myurl);
233         }
234         else if (!strcmp(n->name, "service"))
235         {
236             struct conf_service *s = parse_service(n);
237             if (!s)
238                 return 0;
239             r->service = s;
240         }
241         else
242         {
243             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
244             return 0;
245         }
246     }
247     return r;
248 }
249
250 static xsltStylesheet *load_stylesheet(const char *fname)
251 {
252     char path[256];
253     sprintf(path, "%s/%s", confdir, fname);
254     return xsltParseStylesheetFile(path);
255 }
256
257 static void setup_marc(struct conf_retrievalprofile *r)
258 {
259     yaz_iconv_t cm;
260     r->yaz_marc = yaz_marc_create();
261     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
262     {
263         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
264         return;
265     }
266     yaz_marc_iconv(r->yaz_marc, cm);
267 }
268
269 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
270 {
271     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
272     xmlNode *n;
273     struct conf_retrievalmap **rm = &r->maplist;
274
275     r->requestsyntax = 0;
276     r->native_syntax = Nativesyn_xml;
277     r->native_format = Nativeform_na;
278     r->native_encoding = 0;
279     r->native_mapto = Nativemapto_na;
280     r->yaz_marc = 0;
281     r->maplist = 0;
282     r->next = 0;
283
284     for (n = node->children; n; n = n->next)
285     {
286         if (n->type != XML_ELEMENT_NODE)
287             continue;
288         if (!strcmp(n->name, "requestsyntax"))
289         {
290             xmlChar *content = xmlNodeGetContent(n);
291             if (content)
292                 r->requestsyntax = nmem_strdup(nmem, content);
293         }
294         else if (!strcmp(n->name, "nativesyntax"))
295         {
296             xmlChar *name = xmlGetProp(n, "name");
297             xmlChar *format = xmlGetProp(n, "format");
298             xmlChar *encoding = xmlGetProp(n, "encoding");
299             xmlChar *mapto = xmlGetProp(n, "mapto");
300             if (!name)
301             {
302                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
303                 return 0;
304             }
305             if (encoding)
306                 r->native_encoding = encoding;
307             if (!strcmp(name, "iso2709"))
308             {
309                 r->native_syntax = Nativesyn_iso2709;
310                 // Set a few defaults, too
311                 r->native_format = Nativeform_marc21;
312                 r->native_mapto = Nativemapto_marcxml;
313                 if (!r->native_encoding)
314                     r->native_encoding = "marc-8";
315                 setup_marc(r);
316             }
317             else if (!strcmp(name, "xml"))
318                 r->native_syntax = Nativesyn_xml;
319             else
320             {
321                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
322                 return 0;
323             }
324             if (format)
325             {
326                 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
327                     r->native_format = Nativeform_marc21;
328                 else
329                 {
330                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
331                     return 0;
332                 }
333             }
334             if (mapto)
335             {
336                 if (!strcmp(mapto, "marcxml"))
337                     r->native_mapto = Nativemapto_marcxml;
338                 else if (!strcmp(mapto, "marcxchange"))
339                     r->native_mapto = Nativemapto_marcxchange;
340                 else
341                 {
342                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
343                     return 0;
344                 }
345             }
346             xmlFree(name);
347             xmlFree(format);
348             xmlFree(encoding);
349             xmlFree(mapto);
350         }
351         else if (!strcmp(n->name, "map"))
352         {
353             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
354             xmlChar *type = xmlGetProp(n, "type");
355             xmlChar *charset = xmlGetProp(n, "charset");
356             xmlChar *format = xmlGetProp(n, "format");
357             xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
358             memset(m, 0, sizeof(*m));
359             if (type)
360             {
361                 if (!strcmp(type, "xslt"))
362                     m->type = Map_xslt;
363                 else
364                 {
365                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
366                     return 0;
367                 }
368             }
369             if (charset)
370                 m->charset = nmem_strdup(nmem, charset);
371             if (format)
372                 m->format = nmem_strdup(nmem, format);
373             if (stylesheet)
374             {
375                 if (!(m->stylesheet = load_stylesheet(stylesheet)))
376                     return 0;
377             }
378             *rm = m;
379             rm = &m->next;
380             xmlFree(type);
381             xmlFree(charset);
382             xmlFree(format);
383             xmlFree(stylesheet);
384         }
385         else
386         {
387             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
388             return 0;
389         }
390     }
391
392     return r;
393 }
394
395 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
396 {
397     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
398     xmlChar *type = xmlGetProp(node, "type");
399     xmlChar *src = xmlGetProp(node, "src");
400
401     memset(r, 0, sizeof(*r));
402
403     if (type)
404     {
405         if (!strcmp(type, "local"))
406             r->type = Targetprofiles_local;
407         else
408         {
409             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
410             return 0;
411         }
412     }
413     else
414     {
415         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
416         return 0;
417     }
418
419     if (src)
420         r->src = nmem_strdup(nmem, src);
421     else
422     {
423         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
424         return 0;
425     }
426     xmlFree(type);
427     xmlFree(src);
428     return r;
429 }
430
431 static struct conf_config *parse_config(xmlNode *root)
432 {
433     xmlNode *n;
434     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
435     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
436
437     r->servers = 0;
438     r->queryprofiles = 0;
439     r->retrievalprofiles = 0;
440     r->targetprofiles = 0;
441
442     for (n = root->children; n; n = n->next)
443     {
444         if (n->type != XML_ELEMENT_NODE)
445             continue;
446         if (!strcmp(n->name, "server"))
447         {
448             struct conf_server *tmp = parse_server(n);
449             if (!tmp)
450                 return 0;
451             tmp->next = r->servers;
452             r->servers = tmp;
453         }
454         else if (!strcmp(n->name, "queryprofile"))
455         {
456         }
457         else if (!strcmp(n->name, "retrievalprofile"))
458         {
459             if (!(*rp = parse_retrievalprofile(n)))
460                 return 0;
461             rp = &(*rp)->next;
462         }
463         else if (!strcmp(n->name, "targetprofiles"))
464         {
465             // It would be fun to be able to fix this sometime
466             if (r->targetprofiles)
467             {
468                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
469                 return 0;
470             }
471             if (!(r->targetprofiles = parse_targetprofiles(n)))
472                 return 0;
473         }
474         else
475         {
476             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
477             return 0;
478         }
479     }
480     return r;
481 }
482
483 int read_config(const char *fname)
484 {
485     xmlDoc *doc = xmlParseFile(fname);
486     const char *p;
487
488     if (!nmem)  // Initialize
489     {
490         nmem = nmem_create();
491         xmlSubstituteEntitiesDefault(1);
492         xmlLoadExtDtdDefaultValue = 1;
493     }
494     if (!doc)
495     {
496         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
497         exit(1);
498     }
499     if ((p = strrchr(fname, '/')))
500     {
501         int len = p - fname;
502         strncpy(confdir, fname, len);
503         confdir[len] = '\0';
504     }
505     config = parse_config(xmlDocGetRootElement(doc));
506     xmlFreeDoc(doc);
507
508     if (config)
509         return 1;
510     else
511         return 0;
512 }
513
514
515 /*
516  * Local variables:
517  * c-basic-offset: 4
518  * indent-tabs-mode: nil
519  * End:
520  * vim: shiftwidth=4 tabstop=8 expandtab
521  */