1 /* $Id: config.c,v 1.6 2007-01-08 19:39:12 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");
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);
110 md->type = Metadata_type_generic;
114 if (!strcmp(sortkey, "no"))
115 md->sortkey = Metadata_sortkey_no;
116 else if (!strcmp(sortkey, "numeric"))
117 md->sortkey = Metadata_sortkey_numeric;
118 else if (!strcmp(sortkey, "range"))
119 md->sortkey = Metadata_sortkey_range;
120 else if (!strcmp(sortkey, "skiparticle"))
121 md->sortkey = Metadata_sortkey_skiparticle;
124 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
129 md->sortkey = Metadata_sortkey_no;
133 if (!strcmp(merge, "no"))
134 md->merge = Metadata_merge_no;
135 else if (!strcmp(merge, "unique"))
136 md->merge = Metadata_merge_unique;
137 else if (!strcmp(merge, "longest"))
138 md->merge = Metadata_merge_longest;
139 else if (!strcmp(merge, "range"))
140 md->merge = Metadata_merge_range;
141 else if (!strcmp(merge, "all"))
142 md->merge = Metadata_merge_all;
145 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
150 md->merge = Metadata_merge_no;
162 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
169 static struct conf_server *parse_server(xmlNode *node)
172 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
181 for (n = node->children; n; n = n->next)
183 if (n->type != XML_ELEMENT_NODE)
185 if (!strcmp(n->name, "listen"))
187 xmlChar *port = xmlGetProp(n, "port");
188 xmlChar *host = xmlGetProp(n, "host");
190 r->port = atoi(port);
192 r->host = nmem_strdup(nmem, host);
196 else if (!strcmp(n->name, "proxy"))
198 xmlChar *port = xmlGetProp(n, "port");
199 xmlChar *host = xmlGetProp(n, "host");
201 r->proxy_port = atoi(port);
203 r->proxy_host = nmem_strdup(nmem, host);
207 else if (!strcmp(n->name, "service"))
209 struct conf_service *s = parse_service(n);
216 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
223 static xsltStylesheet *load_stylesheet(const char *fname)
226 sprintf(path, "%s/%s", confdir, fname);
227 return xsltParseStylesheetFile(path);
230 static void setup_marc(struct conf_retrievalprofile *r)
233 r->yaz_marc = yaz_marc_create();
234 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
236 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
239 yaz_marc_iconv(r->yaz_marc, cm);
242 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
244 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
246 struct conf_retrievalmap **rm = &r->maplist;
248 r->requestsyntax = 0;
249 r->native_syntax = Nativesyn_xml;
250 r->native_format = Nativeform_na;
251 r->native_encoding = 0;
252 r->native_mapto = Nativemapto_na;
257 for (n = node->children; n; n = n->next)
259 if (n->type != XML_ELEMENT_NODE)
261 if (!strcmp(n->name, "requestsyntax"))
263 xmlChar *content = xmlNodeGetContent(n);
265 r->requestsyntax = nmem_strdup(nmem, content);
267 else if (!strcmp(n->name, "nativesyntax"))
269 xmlChar *name = xmlGetProp(n, "name");
270 xmlChar *format = xmlGetProp(n, "format");
271 xmlChar *encoding = xmlGetProp(n, "encoding");
272 xmlChar *mapto = xmlGetProp(n, "mapto");
275 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
278 if (!strcmp(name, "iso2709"))
280 r->native_syntax = Nativesyn_iso2709;
281 // Set a few defaults, too
282 r->native_format = Nativeform_marc21;
283 r->native_mapto = Nativemapto_marcxml;
284 r->native_encoding = "marc-8";
287 else if (!strcmp(name, "xml"))
288 r->native_syntax = Nativesyn_xml;
291 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
296 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
297 r->native_format = Nativeform_marc21;
300 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
305 r->native_encoding = encoding;
308 if (!strcmp(mapto, "marcxml"))
309 r->native_mapto = Nativemapto_marcxml;
310 else if (!strcmp(mapto, "marcxchange"))
311 r->native_mapto = Nativemapto_marcxchange;
314 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
323 else if (!strcmp(n->name, "map"))
325 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
326 xmlChar *type = xmlGetProp(n, "type");
327 xmlChar *charset = xmlGetProp(n, "charset");
328 xmlChar *format = xmlGetProp(n, "format");
329 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
330 bzero(m, sizeof(*m));
333 if (!strcmp(type, "xslt"))
337 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
342 m->charset = nmem_strdup(nmem, charset);
344 m->format = nmem_strdup(nmem, format);
347 if (!(m->stylesheet = load_stylesheet(stylesheet)))
359 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
367 static struct conf_config *parse_config(xmlNode *root)
370 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
371 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
374 r->queryprofiles = 0;
375 r->retrievalprofiles = 0;
377 for (n = root->children; n; n = n->next)
379 if (n->type != XML_ELEMENT_NODE)
381 if (!strcmp(n->name, "server"))
383 struct conf_server *tmp = parse_server(n);
386 tmp->next = r->servers;
389 else if (!strcmp(n->name, "queryprofile"))
392 else if (!strcmp(n->name, "retrievalprofile"))
394 if (!(*rp = parse_retrievalprofile(n)))
400 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
407 int read_config(const char *fname)
409 xmlDoc *doc = xmlReadFile(fname, NULL, 0);
412 if (!nmem) // Initialize
414 nmem = nmem_create();
415 xmlSubstituteEntitiesDefault(1);
416 xmlLoadExtDtdDefaultValue = 1;
420 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
423 if ((p = rindex(fname, '/')))
426 strncpy(confdir, fname, len);
429 config = parse_config(xmlDocGetRootElement(doc));
442 * indent-tabs-mode: nil
444 * vim: shiftwidth=4 tabstop=8 expandtab