1 /* $Id: config.c,v 1.28 2007-04-20 14:37:17 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.28 2007-04-20 14:37:17 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;
48 struct conf_metadata * conf_metadata_assign(NMEM nmem,
49 struct conf_metadata * metadata,
51 enum conf_metadata_type type,
52 enum conf_metadata_merge merge,
58 if (!nmem || !metadata || !name)
61 metadata->name = nmem_strdup(nmem, name);
62 metadata->type = type;
63 metadata->merge = merge;
64 metadata->brief = brief;
65 metadata->termlist = termlist;
66 metadata->rank = rank;
67 metadata->sortkey_offset = sortkey_offset;
73 struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
74 struct conf_sortkey * sortkey,
76 enum conf_sortkey_type type)
78 if (!nmem || !sortkey || !name)
81 sortkey->name = nmem_strdup(nmem, name);
88 struct conf_service * conf_service_create(NMEM nmem,
89 int num_metadata, int num_sortkeys)
91 struct conf_service * service = 0;
95 service = nmem_malloc(nmem, sizeof(struct conf_service));
97 service->num_metadata = num_metadata;
98 service->metadata = 0;
99 if (service->num_metadata)
102 sizeof(struct conf_metadata) * service->num_metadata);
103 service->num_sortkeys = num_sortkeys;
104 service->sortkeys = 0;
105 if (service->num_sortkeys)
108 sizeof(struct conf_sortkey) * service->num_sortkeys);
112 struct conf_metadata* conf_service_add_metadata(NMEM nmem,
113 struct conf_service *service,
116 enum conf_metadata_type type,
117 enum conf_metadata_merge merge,
123 struct conf_metadata * md = 0;
125 if (!service || !service->metadata || !service->num_metadata
126 || position < 0 || !(position < service->num_metadata))
129 //md = &((service->metadata)[position]);
130 md = service->metadata + position;
131 md = conf_metadata_assign(nmem, md, name, type, merge,
132 brief, termlist, rank, sortkey_offset);
137 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
138 struct conf_service *service,
141 enum conf_sortkey_type type)
143 struct conf_sortkey * sk = 0;
145 if (!service || !service->sortkeys || !service->num_sortkeys
146 || position < 0 || !(position < service->num_sortkeys))
149 //sk = &((service->sortkeys)[position]);
150 sk = service->sortkeys + position;
151 sk = conf_sortkey_assign(nmem, sk, name, type);
160 /* Code to parse configuration file */
161 /* ==================================================== */
163 static struct conf_service *parse_service(xmlNode *node)
169 struct conf_service *service = 0;
170 int num_metadata = 0;
171 int num_sortkeys = 0;
173 // count num_metadata and num_sortkeys
174 for (n = node->children; n; n = n->next)
175 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
176 n->name, "metadata"))
178 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
180 if (sortkey && strcmp((const char *) sortkey, "no"))
185 service = conf_service_create(nmem, num_metadata, num_sortkeys);
187 for (n = node->children; n; n = n->next)
189 if (n->type != XML_ELEMENT_NODE)
191 if (!strcmp((const char *) n->name, (const char *) "metadata"))
193 xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
194 xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
195 xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
196 xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
197 xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
198 xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
199 xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
201 enum conf_metadata_type type = Metadata_type_generic;
202 enum conf_metadata_merge merge = Metadata_merge_no;
206 int sortkey_offset = 0;
207 enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
209 // now do the parsing logic
212 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
217 if (!strcmp((const char *) xml_brief, "yes"))
219 else if (strcmp((const char *) xml_brief, "no"))
221 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
230 if (!strcmp((const char *) xml_termlist, "yes"))
232 else if (strcmp((const char *) xml_termlist, "no"))
234 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
242 rank = atoi((const char *) xml_rank);
248 if (!strcmp((const char *) xml_type, "generic"))
249 type = Metadata_type_generic;
250 else if (!strcmp((const char *) xml_type, "year"))
251 type = Metadata_type_year;
254 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", xml_type);
259 type = Metadata_type_generic;
263 if (!strcmp((const char *) xml_merge, "no"))
264 merge = Metadata_merge_no;
265 else if (!strcmp((const char *) xml_merge, "unique"))
266 merge = Metadata_merge_unique;
267 else if (!strcmp((const char *) xml_merge, "longest"))
268 merge = Metadata_merge_longest;
269 else if (!strcmp((const char *) xml_merge, "range"))
270 merge = Metadata_merge_range;
271 else if (!strcmp((const char *) xml_merge, "all"))
272 merge = Metadata_merge_all;
276 "Unknown value for metadata/merge: %s", xml_merge);
281 merge = Metadata_merge_no;
283 // add a sortkey if so specified
284 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
286 if (merge == Metadata_merge_no)
289 "Can't specify sortkey on a non-merged field");
292 if (!strcmp((const char *) xml_sortkey, "numeric"))
293 sk_type = Metadata_sortkey_numeric;
294 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
295 sk_type = Metadata_sortkey_skiparticle;
299 "Unknown sortkey in metadata element: %s",
303 sortkey_offset = sk_node;
305 conf_service_add_sortkey(nmem, service, sk_node,
306 (const char *) xml_name, sk_type);
313 // metadata known, assign values
314 conf_service_add_metadata(nmem, service, md_node,
315 (const char *) xml_name,
317 brief, termlist, rank, sortkey_offset);
321 xmlFree(xml_sortkey);
324 xmlFree(xml_termlist);
330 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
337 static char *parse_settings(xmlNode *node)
339 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
343 r = nmem_strdup(nmem, (const char *) src);
346 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
353 static struct conf_server *parse_server(xmlNode *node)
356 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
369 for (n = node->children; n; n = n->next)
371 if (n->type != XML_ELEMENT_NODE)
373 if (!strcmp((const char *) n->name, "listen"))
375 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
376 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
378 r->port = atoi((const char *) port);
380 r->host = nmem_strdup(nmem, (const char *) host);
384 else if (!strcmp((const char *) n->name, "proxy"))
386 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
387 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
388 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
390 r->proxy_port = atoi((const char *) port);
392 r->proxy_host = nmem_strdup(nmem, (const char *) host);
394 r->myurl = nmem_strdup(nmem, (const char *) myurl);
398 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
406 else if (!strcmp((const char *) n->name, "zproxy"))
411 port = xmlGetProp(n, (xmlChar *) "port");
412 host = xmlGetProp(n, (xmlChar *) "host");
415 r->zproxy_port = atoi((const char *) port);
417 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
422 else if (!strcmp((const char *) n->name, "settings"))
426 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
429 if (!(r->settings = parse_settings(n)))
432 else if (!strcmp((const char *) n->name, "service"))
434 struct conf_service *s = parse_service(n);
441 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
448 xsltStylesheet *conf_load_stylesheet(const char *fname)
451 sprintf(path, "%s/%s", confdir, fname);
452 return xsltParseStylesheetFile((xmlChar *) path);
455 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
457 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
458 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
459 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
461 memset(r, 0, sizeof(*r));
465 if (!strcmp((const char *) type, "local"))
466 r->type = Targetprofiles_local;
469 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
475 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
480 r->src = nmem_strdup(nmem, (const char *) src);
483 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
491 static struct conf_config *parse_config(xmlNode *root)
494 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
497 r->targetprofiles = 0;
499 for (n = root->children; n; n = n->next)
501 if (n->type != XML_ELEMENT_NODE)
503 if (!strcmp((const char *) n->name, "server"))
505 struct conf_server *tmp = parse_server(n);
508 tmp->next = r->servers;
511 else if (!strcmp((const char *) n->name, "targetprofiles"))
513 // It would be fun to be able to fix this sometime
514 if (r->targetprofiles)
516 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
519 if (!(r->targetprofiles = parse_targetprofiles(n)))
524 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
531 int read_config(const char *fname)
533 xmlDoc *doc = xmlParseFile(fname);
536 if (!nmem) // Initialize
538 nmem = nmem_create();
539 xmlSubstituteEntitiesDefault(1);
540 xmlLoadExtDtdDefaultValue = 1;
544 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
547 if ((p = strrchr(fname, '/')))
550 strncpy(confdir, fname, len);
553 config = parse_config(xmlDocGetRootElement(doc));
566 * indent-tabs-mode: nil
568 * vim: shiftwidth=4 tabstop=8 expandtab