1 /* $Id: config.c,v 1.13 2007-01-15 16:56:51 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));
197 for (n = node->children; n; n = n->next)
199 if (n->type != XML_ELEMENT_NODE)
201 if (!strcmp(n->name, "listen"))
203 xmlChar *port = xmlGetProp(n, "port");
204 xmlChar *host = xmlGetProp(n, "host");
206 r->port = atoi(port);
208 r->host = nmem_strdup(nmem, host);
212 else if (!strcmp(n->name, "proxy"))
214 xmlChar *port = xmlGetProp(n, "port");
215 xmlChar *host = xmlGetProp(n, "host");
217 r->proxy_port = atoi(port);
219 r->proxy_host = nmem_strdup(nmem, host);
223 else if (!strcmp(n->name, "service"))
225 struct conf_service *s = parse_service(n);
232 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
239 static xsltStylesheet *load_stylesheet(const char *fname)
242 sprintf(path, "%s/%s", confdir, fname);
243 return xsltParseStylesheetFile(path);
246 static void setup_marc(struct conf_retrievalprofile *r)
249 r->yaz_marc = yaz_marc_create();
250 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
252 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
255 yaz_marc_iconv(r->yaz_marc, cm);
258 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
260 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
262 struct conf_retrievalmap **rm = &r->maplist;
264 r->requestsyntax = 0;
265 r->native_syntax = Nativesyn_xml;
266 r->native_format = Nativeform_na;
267 r->native_encoding = 0;
268 r->native_mapto = Nativemapto_na;
273 for (n = node->children; n; n = n->next)
275 if (n->type != XML_ELEMENT_NODE)
277 if (!strcmp(n->name, "requestsyntax"))
279 xmlChar *content = xmlNodeGetContent(n);
281 r->requestsyntax = nmem_strdup(nmem, content);
283 else if (!strcmp(n->name, "nativesyntax"))
285 xmlChar *name = xmlGetProp(n, "name");
286 xmlChar *format = xmlGetProp(n, "format");
287 xmlChar *encoding = xmlGetProp(n, "encoding");
288 xmlChar *mapto = xmlGetProp(n, "mapto");
291 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
295 r->native_encoding = encoding;
296 if (!strcmp(name, "iso2709"))
298 r->native_syntax = Nativesyn_iso2709;
299 // Set a few defaults, too
300 r->native_format = Nativeform_marc21;
301 r->native_mapto = Nativemapto_marcxml;
302 if (!r->native_encoding)
303 r->native_encoding = "marc-8";
306 else if (!strcmp(name, "xml"))
307 r->native_syntax = Nativesyn_xml;
310 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
315 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
316 r->native_format = Nativeform_marc21;
319 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
325 if (!strcmp(mapto, "marcxml"))
326 r->native_mapto = Nativemapto_marcxml;
327 else if (!strcmp(mapto, "marcxchange"))
328 r->native_mapto = Nativemapto_marcxchange;
331 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
340 else if (!strcmp(n->name, "map"))
342 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
343 xmlChar *type = xmlGetProp(n, "type");
344 xmlChar *charset = xmlGetProp(n, "charset");
345 xmlChar *format = xmlGetProp(n, "format");
346 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
347 memset(m, 0, sizeof(*m));
350 if (!strcmp(type, "xslt"))
354 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
359 m->charset = nmem_strdup(nmem, charset);
361 m->format = nmem_strdup(nmem, format);
364 if (!(m->stylesheet = load_stylesheet(stylesheet)))
376 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
384 static struct conf_config *parse_config(xmlNode *root)
387 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
388 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
391 r->queryprofiles = 0;
392 r->retrievalprofiles = 0;
394 for (n = root->children; n; n = n->next)
396 if (n->type != XML_ELEMENT_NODE)
398 if (!strcmp(n->name, "server"))
400 struct conf_server *tmp = parse_server(n);
403 tmp->next = r->servers;
406 else if (!strcmp(n->name, "queryprofile"))
409 else if (!strcmp(n->name, "retrievalprofile"))
411 if (!(*rp = parse_retrievalprofile(n)))
417 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
424 int read_config(const char *fname)
426 xmlDoc *doc = xmlParseFile(fname);
429 if (!nmem) // Initialize
431 nmem = nmem_create();
432 xmlSubstituteEntitiesDefault(1);
433 xmlLoadExtDtdDefaultValue = 1;
437 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
440 if ((p = strrchr(fname, '/')))
443 strncpy(confdir, fname, len);
446 config = parse_config(xmlDocGetRootElement(doc));
459 * indent-tabs-mode: nil
461 * vim: shiftwidth=4 tabstop=8 expandtab