1 /* $Id: config.c,v 1.11 2007-01-14 17:34:31 adam 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));
35 r->num_sortkeys = r->num_metadata = 0;
36 // Allocate array of conf metadata structs, if necessary
37 for (n = node->children; n; n = n->next)
38 if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, "metadata"))
40 xmlChar *sortkey = xmlGetProp(n, "sortkey");
42 if (sortkey && strcmp(sortkey, "no"))
47 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
51 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
55 for (n = node->children; n; n = n->next)
57 if (n->type != XML_ELEMENT_NODE)
59 if (!strcmp(n->name, "metadata"))
61 struct conf_metadata *md = &r->metadata[md_node];
62 xmlChar *name = xmlGetProp(n, "name");
63 xmlChar *brief = xmlGetProp(n, "brief");
64 xmlChar *sortkey = xmlGetProp(n, "sortkey");
65 xmlChar *merge = xmlGetProp(n, "merge");
66 xmlChar *type = xmlGetProp(n, "type");
67 xmlChar *termlist = xmlGetProp(n, "termlist");
68 xmlChar *rank = xmlGetProp(n, "rank");
72 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
75 md->name = nmem_strdup(nmem, name);
78 if (!strcmp(brief, "yes"))
80 else if (strcmp(brief, "no"))
82 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
91 if (!strcmp(termlist, "yes"))
93 else if (strcmp(termlist, "no"))
95 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
103 md->rank = atoi(rank);
109 if (!strcmp(type, "generic"))
110 md->type = Metadata_type_generic;
111 else if (!strcmp(type, "integer"))
112 md->type = Metadata_type_integer;
113 else if (!strcmp(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(sortkey, "no"))
127 md->sortkey = Metadata_sortkey_no;
128 else if (!strcmp(sortkey, "numeric"))
129 md->sortkey = Metadata_sortkey_numeric;
130 else if (!strcmp(sortkey, "range"))
131 md->sortkey = Metadata_sortkey_range;
132 else if (!strcmp(sortkey, "skiparticle"))
133 md->sortkey = Metadata_sortkey_skiparticle;
136 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
141 md->sortkey = Metadata_sortkey_no;
145 if (!strcmp(merge, "no"))
146 md->merge = Metadata_merge_no;
147 else if (!strcmp(merge, "unique"))
148 md->merge = Metadata_merge_unique;
149 else if (!strcmp(merge, "longest"))
150 md->merge = Metadata_merge_longest;
151 else if (!strcmp(merge, "range"))
152 md->merge = Metadata_merge_range;
153 else if (!strcmp(merge, "all"))
154 md->merge = Metadata_merge_all;
157 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
162 md->merge = Metadata_merge_no;
174 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
181 static struct conf_server *parse_server(xmlNode *node)
184 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
193 for (n = node->children; n; n = n->next)
195 if (n->type != XML_ELEMENT_NODE)
197 if (!strcmp(n->name, "listen"))
199 xmlChar *port = xmlGetProp(n, "port");
200 xmlChar *host = xmlGetProp(n, "host");
202 r->port = atoi(port);
204 r->host = nmem_strdup(nmem, host);
208 else if (!strcmp(n->name, "proxy"))
210 xmlChar *port = xmlGetProp(n, "port");
211 xmlChar *host = xmlGetProp(n, "host");
213 r->proxy_port = atoi(port);
215 r->proxy_host = nmem_strdup(nmem, host);
219 else if (!strcmp(n->name, "service"))
221 struct conf_service *s = parse_service(n);
228 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
235 static xsltStylesheet *load_stylesheet(const char *fname)
238 sprintf(path, "%s/%s", confdir, fname);
239 return xsltParseStylesheetFile(path);
242 static void setup_marc(struct conf_retrievalprofile *r)
245 r->yaz_marc = yaz_marc_create();
246 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
248 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
251 yaz_marc_iconv(r->yaz_marc, cm);
254 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
256 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
258 struct conf_retrievalmap **rm = &r->maplist;
260 r->requestsyntax = 0;
261 r->native_syntax = Nativesyn_xml;
262 r->native_format = Nativeform_na;
263 r->native_encoding = 0;
264 r->native_mapto = Nativemapto_na;
269 for (n = node->children; n; n = n->next)
271 if (n->type != XML_ELEMENT_NODE)
273 if (!strcmp(n->name, "requestsyntax"))
275 xmlChar *content = xmlNodeGetContent(n);
277 r->requestsyntax = nmem_strdup(nmem, content);
279 else if (!strcmp(n->name, "nativesyntax"))
281 xmlChar *name = xmlGetProp(n, "name");
282 xmlChar *format = xmlGetProp(n, "format");
283 xmlChar *encoding = xmlGetProp(n, "encoding");
284 xmlChar *mapto = xmlGetProp(n, "mapto");
287 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
290 if (!strcmp(name, "iso2709"))
292 r->native_syntax = Nativesyn_iso2709;
293 // Set a few defaults, too
294 r->native_format = Nativeform_marc21;
295 r->native_mapto = Nativemapto_marcxml;
296 r->native_encoding = "marc-8";
299 else if (!strcmp(name, "xml"))
300 r->native_syntax = Nativesyn_xml;
303 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
308 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
309 r->native_format = Nativeform_marc21;
312 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
317 r->native_encoding = encoding;
320 if (!strcmp(mapto, "marcxml"))
321 r->native_mapto = Nativemapto_marcxml;
322 else if (!strcmp(mapto, "marcxchange"))
323 r->native_mapto = Nativemapto_marcxchange;
326 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
335 else if (!strcmp(n->name, "map"))
337 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
338 xmlChar *type = xmlGetProp(n, "type");
339 xmlChar *charset = xmlGetProp(n, "charset");
340 xmlChar *format = xmlGetProp(n, "format");
341 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
342 memset(m, 0, sizeof(*m));
345 if (!strcmp(type, "xslt"))
349 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
354 m->charset = nmem_strdup(nmem, charset);
356 m->format = nmem_strdup(nmem, format);
359 if (!(m->stylesheet = load_stylesheet(stylesheet)))
371 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
379 static struct conf_config *parse_config(xmlNode *root)
382 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
383 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
386 r->queryprofiles = 0;
387 r->retrievalprofiles = 0;
389 for (n = root->children; n; n = n->next)
391 if (n->type != XML_ELEMENT_NODE)
393 if (!strcmp(n->name, "server"))
395 struct conf_server *tmp = parse_server(n);
398 tmp->next = r->servers;
401 else if (!strcmp(n->name, "queryprofile"))
404 else if (!strcmp(n->name, "retrievalprofile"))
406 if (!(*rp = parse_retrievalprofile(n)))
412 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
419 int read_config(const char *fname)
421 xmlDoc *doc = xmlParseFile(fname);
424 if (!nmem) // Initialize
426 nmem = nmem_create();
427 xmlSubstituteEntitiesDefault(1);
428 xmlLoadExtDtdDefaultValue = 1;
432 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
435 if ((p = strrchr(fname, '/')))
438 strncpy(confdir, fname, len);
441 config = parse_config(xmlDocGetRootElement(doc));
454 * indent-tabs-mode: nil
456 * vim: shiftwidth=4 tabstop=8 expandtab