1 /* $Id: config.c,v 1.5 2007-01-08 18:32:35 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 // 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"))
41 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * num_metadata);
42 r->num_metadata = num_metadata;
44 for (n = node->children; n; n = n->next)
46 if (n->type != XML_ELEMENT_NODE)
48 if (!strcmp(n->name, "metadata"))
50 struct conf_metadata *md = &r->metadata[md_node];
51 xmlChar *name = xmlGetProp(n, "name");
52 xmlChar *brief = xmlGetProp(n, "brief");
53 xmlChar *sortkey = xmlGetProp(n, "sortkey");
54 xmlChar *merge = xmlGetProp(n, "merge");
55 xmlChar *type = xmlGetProp(n, "type");
56 xmlChar *termlist = xmlGetProp(n, "termlist");
60 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
63 md->name = nmem_strdup(nmem, name);
66 if (!strcmp(brief, "yes"))
68 else if (strcmp(brief, "no"))
70 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
79 if (!strcmp(termlist, "yes"))
81 else if (strcmp(termlist, "no"))
83 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
92 if (!strcmp(type, "generic"))
93 md->type = Metadata_type_generic;
94 else if (!strcmp(type, "integer"))
95 md->type = Metadata_type_integer;
96 else if (!strcmp(type, "year"))
97 md->type = Metadata_type_year;
100 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
104 md->type = Metadata_type_generic;
108 if (!strcmp(sortkey, "no"))
109 md->sortkey = Metadata_sortkey_no;
110 else if (!strcmp(sortkey, "numeric"))
111 md->sortkey = Metadata_sortkey_numeric;
112 else if (!strcmp(sortkey, "range"))
113 md->sortkey = Metadata_sortkey_range;
114 else if (!strcmp(sortkey, "skiparticle"))
115 md->sortkey = Metadata_sortkey_skiparticle;
118 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
123 md->sortkey = Metadata_sortkey_no;
127 if (!strcmp(merge, "no"))
128 md->merge = Metadata_merge_no;
129 else if (!strcmp(merge, "unique"))
130 md->merge = Metadata_merge_unique;
131 else if (!strcmp(merge, "longest"))
132 md->merge = Metadata_merge_longest;
133 else if (!strcmp(merge, "range"))
134 md->merge = Metadata_merge_range;
135 else if (!strcmp(merge, "all"))
136 md->merge = Metadata_merge_all;
139 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
144 md->merge = Metadata_merge_no;
155 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
162 static struct conf_server *parse_server(xmlNode *node)
165 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
174 for (n = node->children; n; n = n->next)
176 if (n->type != XML_ELEMENT_NODE)
178 if (!strcmp(n->name, "listen"))
180 xmlChar *port = xmlGetProp(n, "port");
181 xmlChar *host = xmlGetProp(n, "host");
183 r->port = atoi(port);
185 r->host = nmem_strdup(nmem, host);
189 else if (!strcmp(n->name, "proxy"))
191 xmlChar *port = xmlGetProp(n, "port");
192 xmlChar *host = xmlGetProp(n, "host");
194 r->proxy_port = atoi(port);
196 r->proxy_host = nmem_strdup(nmem, host);
200 else if (!strcmp(n->name, "service"))
202 struct conf_service *s = parse_service(n);
209 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
216 static xsltStylesheet *load_stylesheet(const char *fname)
219 sprintf(path, "%s/%s", confdir, fname);
220 return xsltParseStylesheetFile(path);
223 static void setup_marc(struct conf_retrievalprofile *r)
226 r->yaz_marc = yaz_marc_create();
227 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
229 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
232 yaz_marc_iconv(r->yaz_marc, cm);
235 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
237 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
239 struct conf_retrievalmap **rm = &r->maplist;
241 r->requestsyntax = 0;
242 r->native_syntax = Nativesyn_xml;
243 r->native_format = Nativeform_na;
244 r->native_encoding = 0;
245 r->native_mapto = Nativemapto_na;
250 for (n = node->children; n; n = n->next)
252 if (n->type != XML_ELEMENT_NODE)
254 if (!strcmp(n->name, "requestsyntax"))
256 xmlChar *content = xmlNodeGetContent(n);
258 r->requestsyntax = nmem_strdup(nmem, content);
260 else if (!strcmp(n->name, "nativesyntax"))
262 xmlChar *name = xmlGetProp(n, "name");
263 xmlChar *format = xmlGetProp(n, "format");
264 xmlChar *encoding = xmlGetProp(n, "encoding");
265 xmlChar *mapto = xmlGetProp(n, "mapto");
268 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
271 if (!strcmp(name, "iso2709"))
273 r->native_syntax = Nativesyn_iso2709;
274 // Set a few defaults, too
275 r->native_format = Nativeform_marc21;
276 r->native_mapto = Nativemapto_marcxml;
277 r->native_encoding = "marc-8";
280 else if (!strcmp(name, "xml"))
281 r->native_syntax = Nativesyn_xml;
284 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
289 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
290 r->native_format = Nativeform_marc21;
293 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
298 r->native_encoding = encoding;
301 if (!strcmp(mapto, "marcxml"))
302 r->native_mapto = Nativemapto_marcxml;
303 else if (!strcmp(mapto, "marcxchange"))
304 r->native_mapto = Nativemapto_marcxchange;
307 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
316 else if (!strcmp(n->name, "map"))
318 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
319 xmlChar *type = xmlGetProp(n, "type");
320 xmlChar *charset = xmlGetProp(n, "charset");
321 xmlChar *format = xmlGetProp(n, "format");
322 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
323 bzero(m, sizeof(*m));
326 if (!strcmp(type, "xslt"))
330 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
335 m->charset = nmem_strdup(nmem, charset);
337 m->format = nmem_strdup(nmem, format);
340 if (!(m->stylesheet = load_stylesheet(stylesheet)))
352 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
360 static struct conf_config *parse_config(xmlNode *root)
363 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
364 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
367 r->queryprofiles = 0;
368 r->retrievalprofiles = 0;
370 for (n = root->children; n; n = n->next)
372 if (n->type != XML_ELEMENT_NODE)
374 if (!strcmp(n->name, "server"))
376 struct conf_server *tmp = parse_server(n);
379 tmp->next = r->servers;
382 else if (!strcmp(n->name, "queryprofile"))
385 else if (!strcmp(n->name, "retrievalprofile"))
387 if (!(*rp = parse_retrievalprofile(n)))
393 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
400 int read_config(const char *fname)
402 xmlDoc *doc = xmlReadFile(fname, NULL, 0);
405 if (!nmem) // Initialize
407 nmem = nmem_create();
408 xmlSubstituteEntitiesDefault(1);
409 xmlLoadExtDtdDefaultValue = 1;
413 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
416 if ((p = rindex(fname, '/')))
419 strncpy(confdir, fname, len);
422 config = parse_config(xmlDocGetRootElement(doc));
435 * indent-tabs-mode: nil
437 * vim: shiftwidth=4 tabstop=8 expandtab