1 /* $Id: config.c,v 1.19 2007-03-30 02:45:07 quinn Exp $ */
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>
15 #include <yaz/yaz-util.h>
18 #define CONFIG_NOEXTERNS
22 static char confdir[256] = ".";
24 struct conf_config *config = 0;
26 /* Code to parse configuration file */
27 /* ==================================================== */
29 static struct conf_service *parse_service(xmlNode *node)
32 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
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 *)
42 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
44 if (sortkey && strcmp((const char *) sortkey, "no"))
49 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
53 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
57 for (n = node->children; n; n = n->next)
59 if (n->type != XML_ELEMENT_NODE)
61 if (!strcmp((const char *) n->name, (const char *) "metadata"))
63 struct conf_metadata *md = &r->metadata[md_node];
64 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
65 xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
66 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
67 xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
68 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
69 xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
70 xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
74 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
77 md->name = nmem_strdup(nmem, (const char *) name);
80 if (!strcmp((const char *) brief, "yes"))
82 else if (strcmp((const char *) brief, "no"))
84 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
93 if (!strcmp((const char *) termlist, "yes"))
95 else if (strcmp((const char *) termlist, "no"))
97 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
105 md->rank = atoi((const char *) rank);
111 if (!strcmp((const char *) type, "generic"))
112 md->type = Metadata_type_generic;
113 else if (!strcmp((const char *) type, "year"))
114 md->type = Metadata_type_year;
117 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
122 md->type = Metadata_type_generic;
126 if (!strcmp((const char *) merge, "no"))
127 md->merge = Metadata_merge_no;
128 else if (!strcmp((const char *) merge, "unique"))
129 md->merge = Metadata_merge_unique;
130 else if (!strcmp((const char *) merge, "longest"))
131 md->merge = Metadata_merge_longest;
132 else if (!strcmp((const char *) merge, "range"))
133 md->merge = Metadata_merge_range;
134 else if (!strcmp((const char *) merge, "all"))
135 md->merge = Metadata_merge_all;
138 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
143 md->merge = Metadata_merge_no;
145 if (sortkey && strcmp((const char *) sortkey, "no"))
147 struct conf_sortkey *sk = &r->sortkeys[sk_node];
148 if (md->merge == Metadata_merge_no)
150 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
153 if (!strcmp((const char *) sortkey, "numeric"))
154 sk->type = Metadata_sortkey_numeric;
155 else if (!strcmp((const char *) sortkey, "skiparticle"))
156 sk->type = Metadata_sortkey_skiparticle;
159 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
163 md->sortkey_offset = sk_node;
167 md->sortkey_offset = -1;
179 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
186 static char *parse_settings(xmlNode *node)
188 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
192 r = nmem_strdup(nmem, (const char *) src);
195 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
202 static struct conf_server *parse_server(xmlNode *node)
205 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
218 for (n = node->children; n; n = n->next)
220 if (n->type != XML_ELEMENT_NODE)
222 if (!strcmp((const char *) n->name, "listen"))
224 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
225 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
227 r->port = atoi((const char *) port);
229 r->host = nmem_strdup(nmem, (const char *) host);
233 else if (!strcmp((const char *) n->name, "proxy"))
235 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
236 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
237 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
239 r->proxy_port = atoi((const char *) port);
241 r->proxy_host = nmem_strdup(nmem, (const char *) host);
243 r->myurl = nmem_strdup(nmem, (const char *) myurl);
247 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
255 else if (!strcmp((const char *) n->name, "zproxy"))
260 port = xmlGetProp(n, (xmlChar *) "port");
261 host = xmlGetProp(n, (xmlChar *) "host");
264 r->zproxy_port = atoi((const char *) port);
266 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
271 else if (!strcmp((const char *) n->name, "settings"))
275 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
278 if (!(r->settings = parse_settings(n)))
281 else if (!strcmp((const char *) n->name, "service"))
283 struct conf_service *s = parse_service(n);
290 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
297 static xsltStylesheet *load_stylesheet(const char *fname)
300 sprintf(path, "%s/%s", confdir, fname);
301 return xsltParseStylesheetFile((xmlChar *) path);
304 static void setup_marc(struct conf_retrievalprofile *r)
307 r->yaz_marc = yaz_marc_create();
308 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
310 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
313 yaz_marc_iconv(r->yaz_marc, cm);
316 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
318 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
320 struct conf_retrievalmap **rm = &r->maplist;
322 r->requestsyntax = 0;
323 r->native_syntax = Nativesyn_xml;
324 r->native_format = Nativeform_na;
325 r->native_encoding = 0;
326 r->native_mapto = Nativemapto_na;
331 for (n = node->children; n; n = n->next)
333 if (n->type != XML_ELEMENT_NODE)
335 if (!strcmp((const char *) n->name, "requestsyntax"))
337 xmlChar *content = xmlNodeGetContent(n);
339 r->requestsyntax = nmem_strdup(nmem, (const char *) content);
341 else if (!strcmp((const char *) n->name, "nativesyntax"))
343 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
344 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
345 xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
346 xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
349 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
353 r->native_encoding = (char *) encoding;
354 if (!strcmp((const char *) name, "iso2709"))
356 r->native_syntax = Nativesyn_iso2709;
357 // Set a few defaults, too
358 r->native_format = Nativeform_marc21;
359 r->native_mapto = Nativemapto_marcxml;
360 if (!r->native_encoding)
361 r->native_encoding = "marc-8";
364 else if (!strcmp((const char *) name, "xml"))
365 r->native_syntax = Nativesyn_xml;
368 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
373 if (!strcmp((const char *) format, "marc21")
374 || !strcmp((const char *) format, "usmarc"))
375 r->native_format = Nativeform_marc21;
378 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
384 if (!strcmp((const char *) mapto, "marcxml"))
385 r->native_mapto = Nativemapto_marcxml;
386 else if (!strcmp((const char *)mapto, "marcxchange"))
387 r->native_mapto = Nativemapto_marcxchange;
390 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
399 else if (!strcmp((const char *) n->name, "map"))
401 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
402 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
403 xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
404 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
405 xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
406 memset(m, 0, sizeof(*m));
409 if (!strcmp((const char *) type, "xslt"))
413 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
418 m->charset = nmem_strdup(nmem, (const char *) charset);
420 m->format = nmem_strdup(nmem, (const char *) format);
423 if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
435 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
443 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
445 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
446 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
447 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
449 memset(r, 0, sizeof(*r));
453 if (!strcmp((const char *) type, "local"))
454 r->type = Targetprofiles_local;
457 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
463 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
468 r->src = nmem_strdup(nmem, (const char *) src);
471 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
479 static struct conf_config *parse_config(xmlNode *root)
482 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
483 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
486 r->retrievalprofiles = 0;
487 r->targetprofiles = 0;
489 for (n = root->children; n; n = n->next)
491 if (n->type != XML_ELEMENT_NODE)
493 if (!strcmp((const char *) n->name, "server"))
495 struct conf_server *tmp = parse_server(n);
498 tmp->next = r->servers;
501 else if (!strcmp((const char *) n->name, "retrievalprofile"))
503 if (!(*rp = parse_retrievalprofile(n)))
507 else if (!strcmp((const char *) n->name, "targetprofiles"))
509 // It would be fun to be able to fix this sometime
510 if (r->targetprofiles)
512 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
515 if (!(r->targetprofiles = parse_targetprofiles(n)))
520 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
527 int read_config(const char *fname)
529 xmlDoc *doc = xmlParseFile(fname);
532 if (!nmem) // Initialize
534 nmem = nmem_create();
535 xmlSubstituteEntitiesDefault(1);
536 xmlLoadExtDtdDefaultValue = 1;
540 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
543 if ((p = strrchr(fname, '/')))
546 strncpy(confdir, fname, len);
549 config = parse_config(xmlDocGetRootElement(doc));
562 * indent-tabs-mode: nil
564 * vim: shiftwidth=4 tabstop=8 expandtab