1 /* $Id: config.c,v 1.14 2007-02-05 16:15:41 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(n->name, "metadata"))
41 xmlChar *sortkey = xmlGetProp(n, "sortkey");
43 if (sortkey && strcmp(sortkey, "no"))
48 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
52 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
56 for (n = node->children; n; n = n->next)
58 if (n->type != XML_ELEMENT_NODE)
60 if (!strcmp(n->name, "metadata"))
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");
73 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
76 md->name = nmem_strdup(nmem, name);
79 if (!strcmp(brief, "yes"))
81 else if (strcmp(brief, "no"))
83 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
92 if (!strcmp(termlist, "yes"))
94 else if (strcmp(termlist, "no"))
96 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
104 md->rank = atoi(rank);
110 if (!strcmp(type, "generic"))
111 md->type = Metadata_type_generic;
112 else if (!strcmp(type, "year"))
113 md->type = Metadata_type_year;
116 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
121 md->type = Metadata_type_generic;
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;
137 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
142 md->merge = Metadata_merge_no;
144 if (sortkey && strcmp(sortkey, "no"))
146 struct conf_sortkey *sk = &r->sortkeys[sk_node];
147 if (md->merge == Metadata_merge_no)
149 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
152 if (!strcmp(sortkey, "numeric"))
153 sk->type = Metadata_sortkey_numeric;
154 else if (!strcmp(sortkey, "skiparticle"))
155 sk->type = Metadata_sortkey_skiparticle;
158 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
162 md->sortkey_offset = sk_node;
166 md->sortkey_offset = -1;
178 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
185 static struct conf_server *parse_server(xmlNode *node)
188 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
198 for (n = node->children; n; n = n->next)
200 if (n->type != XML_ELEMENT_NODE)
202 if (!strcmp(n->name, "listen"))
204 xmlChar *port = xmlGetProp(n, "port");
205 xmlChar *host = xmlGetProp(n, "host");
207 r->port = atoi(port);
209 r->host = nmem_strdup(nmem, host);
213 else if (!strcmp(n->name, "proxy"))
215 xmlChar *port = xmlGetProp(n, "port");
216 xmlChar *host = xmlGetProp(n, "host");
217 xmlChar *myurl = xmlGetProp(n, "myurl");
219 r->proxy_port = atoi(port);
221 r->proxy_host = nmem_strdup(nmem, host);
223 r->myurl = nmem_strdup(nmem, myurl);
226 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
233 else if (!strcmp(n->name, "service"))
235 struct conf_service *s = parse_service(n);
242 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
249 static xsltStylesheet *load_stylesheet(const char *fname)
252 sprintf(path, "%s/%s", confdir, fname);
253 return xsltParseStylesheetFile(path);
256 static void setup_marc(struct conf_retrievalprofile *r)
259 r->yaz_marc = yaz_marc_create();
260 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
262 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
265 yaz_marc_iconv(r->yaz_marc, cm);
268 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
270 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
272 struct conf_retrievalmap **rm = &r->maplist;
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;
283 for (n = node->children; n; n = n->next)
285 if (n->type != XML_ELEMENT_NODE)
287 if (!strcmp(n->name, "requestsyntax"))
289 xmlChar *content = xmlNodeGetContent(n);
291 r->requestsyntax = nmem_strdup(nmem, content);
293 else if (!strcmp(n->name, "nativesyntax"))
295 xmlChar *name = xmlGetProp(n, "name");
296 xmlChar *format = xmlGetProp(n, "format");
297 xmlChar *encoding = xmlGetProp(n, "encoding");
298 xmlChar *mapto = xmlGetProp(n, "mapto");
301 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
305 r->native_encoding = encoding;
306 if (!strcmp(name, "iso2709"))
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";
316 else if (!strcmp(name, "xml"))
317 r->native_syntax = Nativesyn_xml;
320 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
325 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
326 r->native_format = Nativeform_marc21;
329 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
335 if (!strcmp(mapto, "marcxml"))
336 r->native_mapto = Nativemapto_marcxml;
337 else if (!strcmp(mapto, "marcxchange"))
338 r->native_mapto = Nativemapto_marcxchange;
341 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
350 else if (!strcmp(n->name, "map"))
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));
360 if (!strcmp(type, "xslt"))
364 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
369 m->charset = nmem_strdup(nmem, charset);
371 m->format = nmem_strdup(nmem, format);
374 if (!(m->stylesheet = load_stylesheet(stylesheet)))
386 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
394 static struct conf_config *parse_config(xmlNode *root)
397 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
398 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
401 r->queryprofiles = 0;
402 r->retrievalprofiles = 0;
404 for (n = root->children; n; n = n->next)
406 if (n->type != XML_ELEMENT_NODE)
408 if (!strcmp(n->name, "server"))
410 struct conf_server *tmp = parse_server(n);
413 tmp->next = r->servers;
416 else if (!strcmp(n->name, "queryprofile"))
419 else if (!strcmp(n->name, "retrievalprofile"))
421 if (!(*rp = parse_retrievalprofile(n)))
427 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
434 int read_config(const char *fname)
436 xmlDoc *doc = xmlParseFile(fname);
439 if (!nmem) // Initialize
441 nmem = nmem_create();
442 xmlSubstituteEntitiesDefault(1);
443 xmlLoadExtDtdDefaultValue = 1;
447 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
450 if ((p = strrchr(fname, '/')))
453 strncpy(confdir, fname, len);
456 config = parse_config(xmlDocGetRootElement(doc));
469 * indent-tabs-mode: nil
471 * vim: shiftwidth=4 tabstop=8 expandtab