1 /* $Id: config.c,v 1.23 2007-04-10 08:48:56 adam 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.23 2007-04-10 08:48:56 adam 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;
200 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
207 static char *parse_settings(xmlNode *node)
209 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
213 r = nmem_strdup(nmem, (const char *) src);
216 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
223 static struct conf_server *parse_server(xmlNode *node)
226 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
239 for (n = node->children; n; n = n->next)
241 if (n->type != XML_ELEMENT_NODE)
243 if (!strcmp((const char *) n->name, "listen"))
245 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
246 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
248 r->port = atoi((const char *) port);
250 r->host = nmem_strdup(nmem, (const char *) host);
254 else if (!strcmp((const char *) n->name, "proxy"))
256 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
257 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
258 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
260 r->proxy_port = atoi((const char *) port);
262 r->proxy_host = nmem_strdup(nmem, (const char *) host);
264 r->myurl = nmem_strdup(nmem, (const char *) myurl);
268 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
276 else if (!strcmp((const char *) n->name, "zproxy"))
281 port = xmlGetProp(n, (xmlChar *) "port");
282 host = xmlGetProp(n, (xmlChar *) "host");
285 r->zproxy_port = atoi((const char *) port);
287 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
292 else if (!strcmp((const char *) n->name, "settings"))
296 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
299 if (!(r->settings = parse_settings(n)))
302 else if (!strcmp((const char *) n->name, "service"))
304 struct conf_service *s = parse_service(n);
311 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
318 xsltStylesheet *conf_load_stylesheet(const char *fname)
321 sprintf(path, "%s/%s", confdir, fname);
322 return xsltParseStylesheetFile((xmlChar *) path);
325 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
327 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
328 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
329 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
331 memset(r, 0, sizeof(*r));
335 if (!strcmp((const char *) type, "local"))
336 r->type = Targetprofiles_local;
339 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
345 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
350 r->src = nmem_strdup(nmem, (const char *) src);
353 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
361 static struct conf_config *parse_config(xmlNode *root)
364 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
367 r->targetprofiles = 0;
369 for (n = root->children; n; n = n->next)
371 if (n->type != XML_ELEMENT_NODE)
373 if (!strcmp((const char *) n->name, "server"))
375 struct conf_server *tmp = parse_server(n);
378 tmp->next = r->servers;
381 else if (!strcmp((const char *) n->name, "targetprofiles"))
383 // It would be fun to be able to fix this sometime
384 if (r->targetprofiles)
386 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
389 if (!(r->targetprofiles = parse_targetprofiles(n)))
394 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
401 int read_config(const char *fname)
403 xmlDoc *doc = xmlParseFile(fname);
406 if (!nmem) // Initialize
408 nmem = nmem_create();
409 xmlSubstituteEntitiesDefault(1);
410 xmlLoadExtDtdDefaultValue = 1;
414 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
417 if ((p = strrchr(fname, '/')))
420 strncpy(confdir, fname, len);
423 config = parse_config(xmlDocGetRootElement(doc));
436 * indent-tabs-mode: nil
438 * vim: shiftwidth=4 tabstop=8 expandtab