1 /* $Id: config.c,v 1.20 2007-03-31 20:55:19 marc Exp $ */
6 #include <libxml/parser.h>
7 #include <libxml/tree.h>
8 #include <libxslt/xslt.h>
9 #include <libxslt/transform.h>
10 #include <libxslt/xsltutils.h>
16 #include <yaz/yaz-util.h>
19 #define CONFIG_NOEXTERNS
23 static char confdir[256] = ".";
25 struct conf_config *config = 0;
27 /* Code to parse configuration file */
28 /* ==================================================== */
30 static struct conf_service *parse_service(xmlNode *node)
33 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
37 r->num_sortkeys = r->num_metadata = 0;
38 // Allocate array of conf metadata and sortkey tructs, if necessary
39 for (n = node->children; n; n = n->next)
40 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
43 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
45 if (sortkey && strcmp((const char *) sortkey, "no"))
50 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
54 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
58 for (n = node->children; n; n = n->next)
60 if (n->type != XML_ELEMENT_NODE)
62 if (!strcmp((const char *) n->name, (const char *) "metadata"))
64 struct conf_metadata *md = &r->metadata[md_node];
65 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
66 xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
67 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
68 xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
69 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
70 xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
71 xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
75 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
78 md->name = nmem_strdup(nmem, (const char *) name);
81 if (!strcmp((const char *) brief, "yes"))
83 else if (strcmp((const char *) brief, "no"))
85 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
94 if (!strcmp((const char *) termlist, "yes"))
96 else if (strcmp((const char *) termlist, "no"))
98 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
106 md->rank = atoi((const char *) rank);
112 if (!strcmp((const char *) type, "generic"))
113 md->type = Metadata_type_generic;
114 else if (!strcmp((const char *) type, "year"))
115 md->type = Metadata_type_year;
118 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
123 md->type = Metadata_type_generic;
127 if (!strcmp((const char *) merge, "no"))
128 md->merge = Metadata_merge_no;
129 else if (!strcmp((const char *) merge, "unique"))
130 md->merge = Metadata_merge_unique;
131 else if (!strcmp((const char *) merge, "longest"))
132 md->merge = Metadata_merge_longest;
133 else if (!strcmp((const char *) merge, "range"))
134 md->merge = Metadata_merge_range;
135 else if (!strcmp((const char *) merge, "all"))
136 md->merge = Metadata_merge_all;
139 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
144 md->merge = Metadata_merge_no;
146 if (sortkey && strcmp((const char *) sortkey, "no"))
148 struct conf_sortkey *sk = &r->sortkeys[sk_node];
149 if (md->merge == Metadata_merge_no)
151 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
154 if (!strcmp((const char *) sortkey, "numeric"))
155 sk->type = Metadata_sortkey_numeric;
156 else if (!strcmp((const char *) sortkey, "skiparticle"))
157 sk->type = Metadata_sortkey_skiparticle;
160 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
164 md->sortkey_offset = sk_node;
168 md->sortkey_offset = -1;
180 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
187 static char *parse_settings(xmlNode *node)
189 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
193 r = nmem_strdup(nmem, (const char *) src);
196 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
203 static struct conf_server *parse_server(xmlNode *node)
206 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
219 for (n = node->children; n; n = n->next)
221 if (n->type != XML_ELEMENT_NODE)
223 if (!strcmp((const char *) n->name, "listen"))
225 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
226 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
228 r->port = atoi((const char *) port);
230 r->host = nmem_strdup(nmem, (const char *) host);
231 else { // get hostname from system
234 if (0 == gethostname(h, len)){
236 r->host = nmem_strdup(nmem, h);
238 yaz_log(YLOG_WARN, "Could not get host name");
243 else if (!strcmp((const char *) n->name, "proxy"))
245 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
246 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
247 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
249 r->proxy_port = atoi((const char *) port);
251 r->proxy_host = nmem_strdup(nmem, (const char *) host);
253 r->myurl = nmem_strdup(nmem, (const char *) myurl);
257 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
265 else if (!strcmp((const char *) n->name, "zproxy"))
270 port = xmlGetProp(n, (xmlChar *) "port");
271 host = xmlGetProp(n, (xmlChar *) "host");
274 r->zproxy_port = atoi((const char *) port);
276 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
281 else if (!strcmp((const char *) n->name, "settings"))
285 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
288 if (!(r->settings = parse_settings(n)))
291 else if (!strcmp((const char *) n->name, "service"))
293 struct conf_service *s = parse_service(n);
300 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
307 static xsltStylesheet *load_stylesheet(const char *fname)
310 sprintf(path, "%s/%s", confdir, fname);
311 return xsltParseStylesheetFile((xmlChar *) path);
314 static void setup_marc(struct conf_retrievalprofile *r)
317 r->yaz_marc = yaz_marc_create();
318 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
320 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
323 yaz_marc_iconv(r->yaz_marc, cm);
326 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
328 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
330 struct conf_retrievalmap **rm = &r->maplist;
332 r->requestsyntax = 0;
333 r->native_syntax = Nativesyn_xml;
334 r->native_format = Nativeform_na;
335 r->native_encoding = 0;
336 r->native_mapto = Nativemapto_na;
341 for (n = node->children; n; n = n->next)
343 if (n->type != XML_ELEMENT_NODE)
345 if (!strcmp((const char *) n->name, "requestsyntax"))
347 xmlChar *content = xmlNodeGetContent(n);
349 r->requestsyntax = nmem_strdup(nmem, (const char *) content);
351 else if (!strcmp((const char *) n->name, "nativesyntax"))
353 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
354 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
355 xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
356 xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
359 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
363 r->native_encoding = (char *) encoding;
364 if (!strcmp((const char *) name, "iso2709"))
366 r->native_syntax = Nativesyn_iso2709;
367 // Set a few defaults, too
368 r->native_format = Nativeform_marc21;
369 r->native_mapto = Nativemapto_marcxml;
370 if (!r->native_encoding)
371 r->native_encoding = "marc-8";
374 else if (!strcmp((const char *) name, "xml"))
375 r->native_syntax = Nativesyn_xml;
378 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
383 if (!strcmp((const char *) format, "marc21")
384 || !strcmp((const char *) format, "usmarc"))
385 r->native_format = Nativeform_marc21;
388 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
394 if (!strcmp((const char *) mapto, "marcxml"))
395 r->native_mapto = Nativemapto_marcxml;
396 else if (!strcmp((const char *)mapto, "marcxchange"))
397 r->native_mapto = Nativemapto_marcxchange;
400 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
409 else if (!strcmp((const char *) n->name, "map"))
411 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
412 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
413 xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
414 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
415 xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
416 memset(m, 0, sizeof(*m));
419 if (!strcmp((const char *) type, "xslt"))
423 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
428 m->charset = nmem_strdup(nmem, (const char *) charset);
430 m->format = nmem_strdup(nmem, (const char *) format);
433 if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
445 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
453 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
455 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
456 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
457 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
459 memset(r, 0, sizeof(*r));
463 if (!strcmp((const char *) type, "local"))
464 r->type = Targetprofiles_local;
467 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
473 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
478 r->src = nmem_strdup(nmem, (const char *) src);
481 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
489 static struct conf_config *parse_config(xmlNode *root)
492 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
493 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
496 r->retrievalprofiles = 0;
497 r->targetprofiles = 0;
499 for (n = root->children; n; n = n->next)
501 if (n->type != XML_ELEMENT_NODE)
503 if (!strcmp((const char *) n->name, "server"))
505 struct conf_server *tmp = parse_server(n);
508 tmp->next = r->servers;
511 else if (!strcmp((const char *) n->name, "retrievalprofile"))
513 if (!(*rp = parse_retrievalprofile(n)))
517 else if (!strcmp((const char *) n->name, "targetprofiles"))
519 // It would be fun to be able to fix this sometime
520 if (r->targetprofiles)
522 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
525 if (!(r->targetprofiles = parse_targetprofiles(n)))
530 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
537 int read_config(const char *fname)
539 xmlDoc *doc = xmlParseFile(fname);
542 if (!nmem) // Initialize
544 nmem = nmem_create();
545 xmlSubstituteEntitiesDefault(1);
546 xmlLoadExtDtdDefaultValue = 1;
550 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
553 if ((p = strrchr(fname, '/')))
556 strncpy(confdir, fname, len);
559 config = parse_config(xmlDocGetRootElement(doc));
572 * indent-tabs-mode: nil
574 * vim: shiftwidth=4 tabstop=8 expandtab