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