1 /* $Id: config.c,v 1.9 2007-01-10 10:15:23 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));
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");
57 xmlChar *rank = xmlGetProp(n, "rank");
61 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
64 md->name = nmem_strdup(nmem, name);
67 if (!strcmp(brief, "yes"))
69 else if (strcmp(brief, "no"))
71 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
80 if (!strcmp(termlist, "yes"))
82 else if (strcmp(termlist, "no"))
84 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
92 md->rank = atoi(rank);
98 if (!strcmp(type, "generic"))
99 md->type = Metadata_type_generic;
100 else if (!strcmp(type, "integer"))
101 md->type = Metadata_type_integer;
102 else if (!strcmp(type, "year"))
103 md->type = Metadata_type_year;
106 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
111 md->type = Metadata_type_generic;
115 if (!strcmp(sortkey, "no"))
116 md->sortkey = Metadata_sortkey_no;
117 else if (!strcmp(sortkey, "numeric"))
118 md->sortkey = Metadata_sortkey_numeric;
119 else if (!strcmp(sortkey, "range"))
120 md->sortkey = Metadata_sortkey_range;
121 else if (!strcmp(sortkey, "skiparticle"))
122 md->sortkey = Metadata_sortkey_skiparticle;
125 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
130 md->sortkey = Metadata_sortkey_no;
134 if (!strcmp(merge, "no"))
135 md->merge = Metadata_merge_no;
136 else if (!strcmp(merge, "unique"))
137 md->merge = Metadata_merge_unique;
138 else if (!strcmp(merge, "longest"))
139 md->merge = Metadata_merge_longest;
140 else if (!strcmp(merge, "range"))
141 md->merge = Metadata_merge_range;
142 else if (!strcmp(merge, "all"))
143 md->merge = Metadata_merge_all;
146 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
151 md->merge = Metadata_merge_no;
163 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
170 static struct conf_server *parse_server(xmlNode *node)
173 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
182 for (n = node->children; n; n = n->next)
184 if (n->type != XML_ELEMENT_NODE)
186 if (!strcmp(n->name, "listen"))
188 xmlChar *port = xmlGetProp(n, "port");
189 xmlChar *host = xmlGetProp(n, "host");
191 r->port = atoi(port);
193 r->host = nmem_strdup(nmem, host);
197 else if (!strcmp(n->name, "proxy"))
199 xmlChar *port = xmlGetProp(n, "port");
200 xmlChar *host = xmlGetProp(n, "host");
202 r->proxy_port = atoi(port);
204 r->proxy_host = nmem_strdup(nmem, host);
208 else if (!strcmp(n->name, "service"))
210 struct conf_service *s = parse_service(n);
217 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
224 static xsltStylesheet *load_stylesheet(const char *fname)
227 sprintf(path, "%s/%s", confdir, fname);
228 return xsltParseStylesheetFile(path);
231 static void setup_marc(struct conf_retrievalprofile *r)
234 r->yaz_marc = yaz_marc_create();
235 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
237 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
240 yaz_marc_iconv(r->yaz_marc, cm);
243 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
245 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
247 struct conf_retrievalmap **rm = &r->maplist;
249 r->requestsyntax = 0;
250 r->native_syntax = Nativesyn_xml;
251 r->native_format = Nativeform_na;
252 r->native_encoding = 0;
253 r->native_mapto = Nativemapto_na;
258 for (n = node->children; n; n = n->next)
260 if (n->type != XML_ELEMENT_NODE)
262 if (!strcmp(n->name, "requestsyntax"))
264 xmlChar *content = xmlNodeGetContent(n);
266 r->requestsyntax = nmem_strdup(nmem, content);
268 else if (!strcmp(n->name, "nativesyntax"))
270 xmlChar *name = xmlGetProp(n, "name");
271 xmlChar *format = xmlGetProp(n, "format");
272 xmlChar *encoding = xmlGetProp(n, "encoding");
273 xmlChar *mapto = xmlGetProp(n, "mapto");
276 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
279 if (!strcmp(name, "iso2709"))
281 r->native_syntax = Nativesyn_iso2709;
282 // Set a few defaults, too
283 r->native_format = Nativeform_marc21;
284 r->native_mapto = Nativemapto_marcxml;
285 r->native_encoding = "marc-8";
288 else if (!strcmp(name, "xml"))
289 r->native_syntax = Nativesyn_xml;
292 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
297 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
298 r->native_format = Nativeform_marc21;
301 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
306 r->native_encoding = encoding;
309 if (!strcmp(mapto, "marcxml"))
310 r->native_mapto = Nativemapto_marcxml;
311 else if (!strcmp(mapto, "marcxchange"))
312 r->native_mapto = Nativemapto_marcxchange;
315 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
324 else if (!strcmp(n->name, "map"))
326 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
327 xmlChar *type = xmlGetProp(n, "type");
328 xmlChar *charset = xmlGetProp(n, "charset");
329 xmlChar *format = xmlGetProp(n, "format");
330 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
331 memset(m, 0, sizeof(*m));
334 if (!strcmp(type, "xslt"))
338 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
343 m->charset = nmem_strdup(nmem, charset);
345 m->format = nmem_strdup(nmem, format);
348 if (!(m->stylesheet = load_stylesheet(stylesheet)))
360 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
368 static struct conf_config *parse_config(xmlNode *root)
371 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
372 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
375 r->queryprofiles = 0;
376 r->retrievalprofiles = 0;
378 for (n = root->children; n; n = n->next)
380 if (n->type != XML_ELEMENT_NODE)
382 if (!strcmp(n->name, "server"))
384 struct conf_server *tmp = parse_server(n);
387 tmp->next = r->servers;
390 else if (!strcmp(n->name, "queryprofile"))
393 else if (!strcmp(n->name, "retrievalprofile"))
395 if (!(*rp = parse_retrievalprofile(n)))
401 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
408 int read_config(const char *fname)
410 xmlDoc *doc = xmlReadFile(fname, NULL, 0);
413 if (!nmem) // Initialize
415 nmem = nmem_create();
416 xmlSubstituteEntitiesDefault(1);
417 xmlLoadExtDtdDefaultValue = 1;
421 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
424 if ((p = strrchr(fname, '/')))
427 strncpy(confdir, fname, len);
430 config = parse_config(xmlDocGetRootElement(doc));
443 * indent-tabs-mode: nil
445 * vim: shiftwidth=4 tabstop=8 expandtab