1 /* $Id: config.c,v 1.24 2007-04-11 11:01:45 marc Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* $Id: config.c,v 1.24 2007-04-11 11:01:45 marc Exp $ */
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxslt/xslt.h>
29 #include <libxslt/transform.h>
30 #include <libxslt/xsltutils.h>
36 #include <yaz/yaz-util.h>
39 #define CONFIG_NOEXTERNS
43 static char confdir[256] = ".";
45 struct conf_config *config = 0;
47 /* Code to parse configuration file */
48 /* ==================================================== */
50 static struct conf_service *parse_service(xmlNode *node)
53 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
57 r->num_sortkeys = r->num_metadata = 0;
58 // Allocate array of conf metadata and sortkey tructs, if necessary
59 for (n = node->children; n; n = n->next)
60 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
63 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
65 if (sortkey && strcmp((const char *) sortkey, "no"))
70 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
74 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
78 for (n = node->children; n; n = n->next)
80 if (n->type != XML_ELEMENT_NODE)
82 if (!strcmp((const char *) n->name, (const char *) "metadata"))
84 struct conf_metadata *md = &r->metadata[md_node];
85 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
86 xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
87 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
88 xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
89 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
90 xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
91 xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
95 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
98 md->name = nmem_strdup(nmem, (const char *) name);
101 if (!strcmp((const char *) brief, "yes"))
103 else if (strcmp((const char *) brief, "no"))
105 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
114 if (!strcmp((const char *) termlist, "yes"))
116 else if (strcmp((const char *) termlist, "no"))
118 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
126 md->rank = atoi((const char *) rank);
132 if (!strcmp((const char *) type, "generic"))
133 md->type = Metadata_type_generic;
134 else if (!strcmp((const char *) type, "year"))
135 md->type = Metadata_type_year;
138 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
143 md->type = Metadata_type_generic;
147 if (!strcmp((const char *) merge, "no"))
148 md->merge = Metadata_merge_no;
149 else if (!strcmp((const char *) merge, "unique"))
150 md->merge = Metadata_merge_unique;
151 else if (!strcmp((const char *) merge, "longest"))
152 md->merge = Metadata_merge_longest;
153 else if (!strcmp((const char *) merge, "range"))
154 md->merge = Metadata_merge_range;
155 else if (!strcmp((const char *) merge, "all"))
156 md->merge = Metadata_merge_all;
159 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
164 md->merge = Metadata_merge_no;
166 if (sortkey && strcmp((const char *) sortkey, "no"))
168 struct conf_sortkey *sk = &r->sortkeys[sk_node];
169 if (md->merge == Metadata_merge_no)
171 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
174 if (!strcmp((const char *) sortkey, "numeric"))
175 sk->type = Metadata_sortkey_numeric;
176 else if (!strcmp((const char *) sortkey, "skiparticle"))
177 sk->type = Metadata_sortkey_skiparticle;
180 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
184 md->sortkey_offset = sk_node;
188 md->sortkey_offset = -1;
201 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
208 static char *parse_settings(xmlNode *node)
210 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
214 r = nmem_strdup(nmem, (const char *) src);
217 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
224 static struct conf_server *parse_server(xmlNode *node)
227 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
240 for (n = node->children; n; n = n->next)
242 if (n->type != XML_ELEMENT_NODE)
244 if (!strcmp((const char *) n->name, "listen"))
246 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
247 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
249 r->port = atoi((const char *) port);
251 r->host = nmem_strdup(nmem, (const char *) host);
255 else if (!strcmp((const char *) n->name, "proxy"))
257 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
258 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
259 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
261 r->proxy_port = atoi((const char *) port);
263 r->proxy_host = nmem_strdup(nmem, (const char *) host);
265 r->myurl = nmem_strdup(nmem, (const char *) myurl);
269 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
277 else if (!strcmp((const char *) n->name, "zproxy"))
282 port = xmlGetProp(n, (xmlChar *) "port");
283 host = xmlGetProp(n, (xmlChar *) "host");
286 r->zproxy_port = atoi((const char *) port);
288 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
293 else if (!strcmp((const char *) n->name, "settings"))
297 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
300 if (!(r->settings = parse_settings(n)))
303 else if (!strcmp((const char *) n->name, "service"))
305 struct conf_service *s = parse_service(n);
312 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
319 xsltStylesheet *conf_load_stylesheet(const char *fname)
322 sprintf(path, "%s/%s", confdir, fname);
323 return xsltParseStylesheetFile((xmlChar *) path);
326 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
328 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
329 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
330 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
332 memset(r, 0, sizeof(*r));
336 if (!strcmp((const char *) type, "local"))
337 r->type = Targetprofiles_local;
340 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
346 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
351 r->src = nmem_strdup(nmem, (const char *) src);
354 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
362 static struct conf_config *parse_config(xmlNode *root)
365 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
368 r->targetprofiles = 0;
370 for (n = root->children; n; n = n->next)
372 if (n->type != XML_ELEMENT_NODE)
374 if (!strcmp((const char *) n->name, "server"))
376 struct conf_server *tmp = parse_server(n);
379 tmp->next = r->servers;
382 else if (!strcmp((const char *) n->name, "targetprofiles"))
384 // It would be fun to be able to fix this sometime
385 if (r->targetprofiles)
387 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
390 if (!(r->targetprofiles = parse_targetprofiles(n)))
395 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
402 int read_config(const char *fname)
404 xmlDoc *doc = xmlParseFile(fname);
407 if (!nmem) // Initialize
409 nmem = nmem_create();
410 xmlSubstituteEntitiesDefault(1);
411 xmlLoadExtDtdDefaultValue = 1;
415 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
418 if ((p = strrchr(fname, '/')))
421 strncpy(confdir, fname, len);
424 config = parse_config(xmlDocGetRootElement(doc));
437 * indent-tabs-mode: nil
439 * vim: shiftwidth=4 tabstop=8 expandtab