1 /* $Id: config.c,v 1.18 2007-03-27 11:25:57 marc 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 struct conf_server *parse_server(xmlNode *node)
189 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
201 for (n = node->children; n; n = n->next)
203 if (n->type != XML_ELEMENT_NODE)
205 if (!strcmp((const char *) n->name, "listen"))
207 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
208 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
210 r->port = atoi((const char *) port);
212 r->host = nmem_strdup(nmem, (const char *) host);
216 else if (!strcmp((const char *) n->name, "proxy"))
218 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
219 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
220 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
222 r->proxy_port = atoi((const char *) port);
224 r->proxy_host = nmem_strdup(nmem, (const char *) host);
226 r->myurl = nmem_strdup(nmem, (const char *) myurl);
229 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
236 else if (!strcmp((const char *) n->name, "zproxy"))
241 port = xmlGetProp(n, (xmlChar *) "port");
242 host = xmlGetProp(n, (xmlChar *) "host");
245 r->zproxy_port = atoi((const char *) port);
247 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
252 else if (!strcmp((const char *) n->name, "service"))
254 struct conf_service *s = parse_service(n);
261 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
268 static xsltStylesheet *load_stylesheet(const char *fname)
271 sprintf(path, "%s/%s", confdir, fname);
272 return xsltParseStylesheetFile((xmlChar *) path);
275 static void setup_marc(struct conf_retrievalprofile *r)
278 r->yaz_marc = yaz_marc_create();
279 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
281 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
284 yaz_marc_iconv(r->yaz_marc, cm);
287 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
289 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
291 struct conf_retrievalmap **rm = &r->maplist;
293 r->requestsyntax = 0;
294 r->native_syntax = Nativesyn_xml;
295 r->native_format = Nativeform_na;
296 r->native_encoding = 0;
297 r->native_mapto = Nativemapto_na;
302 for (n = node->children; n; n = n->next)
304 if (n->type != XML_ELEMENT_NODE)
306 if (!strcmp((const char *) n->name, "requestsyntax"))
308 xmlChar *content = xmlNodeGetContent(n);
310 r->requestsyntax = nmem_strdup(nmem, (const char *) content);
312 else if (!strcmp((const char *) n->name, "nativesyntax"))
314 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
315 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
316 xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
317 xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
320 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
324 r->native_encoding = (char *) encoding;
325 if (!strcmp((const char *) name, "iso2709"))
327 r->native_syntax = Nativesyn_iso2709;
328 // Set a few defaults, too
329 r->native_format = Nativeform_marc21;
330 r->native_mapto = Nativemapto_marcxml;
331 if (!r->native_encoding)
332 r->native_encoding = "marc-8";
335 else if (!strcmp((const char *) name, "xml"))
336 r->native_syntax = Nativesyn_xml;
339 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
344 if (!strcmp((const char *) format, "marc21")
345 || !strcmp((const char *) format, "usmarc"))
346 r->native_format = Nativeform_marc21;
349 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
355 if (!strcmp((const char *) mapto, "marcxml"))
356 r->native_mapto = Nativemapto_marcxml;
357 else if (!strcmp((const char *)mapto, "marcxchange"))
358 r->native_mapto = Nativemapto_marcxchange;
361 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
370 else if (!strcmp((const char *) n->name, "map"))
372 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
373 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
374 xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
375 xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
376 xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
377 memset(m, 0, sizeof(*m));
380 if (!strcmp((const char *) type, "xslt"))
384 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
389 m->charset = nmem_strdup(nmem, (const char *) charset);
391 m->format = nmem_strdup(nmem, (const char *) format);
394 if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
406 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
414 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
416 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
417 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
418 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
420 memset(r, 0, sizeof(*r));
424 if (!strcmp((const char *) type, "local"))
425 r->type = Targetprofiles_local;
428 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
434 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
439 r->src = nmem_strdup(nmem, (const char *) src);
442 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
450 static struct conf_config *parse_config(xmlNode *root)
453 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
454 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
457 r->queryprofiles = 0;
458 r->retrievalprofiles = 0;
459 r->targetprofiles = 0;
461 for (n = root->children; n; n = n->next)
463 if (n->type != XML_ELEMENT_NODE)
465 if (!strcmp((const char *) n->name, "server"))
467 struct conf_server *tmp = parse_server(n);
470 tmp->next = r->servers;
473 else if (!strcmp((const char *) n->name, "queryprofile"))
476 else if (!strcmp((const char *) n->name, "retrievalprofile"))
478 if (!(*rp = parse_retrievalprofile(n)))
482 else if (!strcmp((const char *) n->name, "targetprofiles"))
484 // It would be fun to be able to fix this sometime
485 if (r->targetprofiles)
487 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
490 if (!(r->targetprofiles = parse_targetprofiles(n)))
495 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
502 int read_config(const char *fname)
504 xmlDoc *doc = xmlParseFile(fname);
507 if (!nmem) // Initialize
509 nmem = nmem_create();
510 xmlSubstituteEntitiesDefault(1);
511 xmlLoadExtDtdDefaultValue = 1;
515 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
518 if ((p = strrchr(fname, '/')))
521 strncpy(confdir, fname, len);
524 config = parse_config(xmlDocGetRootElement(doc));
537 * indent-tabs-mode: nil
539 * vim: shiftwidth=4 tabstop=8 expandtab