1 /* $Id: config.c,v 1.32 2007-04-27 09:38:13 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.32 2007-04-27 09:38:13 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;
64 // enforcing that years are always range merged
65 if (metadata->type == Metadata_type_year)
66 metadata->merge = Metadata_merge_range;
68 metadata->merge = merge;
70 metadata->brief = brief;
71 metadata->termlist = termlist;
72 metadata->rank = rank;
73 metadata->sortkey_offset = sortkey_offset;
78 struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
79 struct conf_sortkey * sortkey,
81 enum conf_sortkey_type type)
83 if (!nmem || !sortkey || !name)
86 sortkey->name = nmem_strdup(nmem, name);
93 struct conf_service * conf_service_create(NMEM nmem,
94 int num_metadata, int num_sortkeys)
96 struct conf_service * service = 0;
100 service = nmem_malloc(nmem, sizeof(struct conf_service));
102 service->num_metadata = num_metadata;
103 service->metadata = 0;
104 if (service->num_metadata)
107 sizeof(struct conf_metadata) * service->num_metadata);
108 service->num_sortkeys = num_sortkeys;
109 service->sortkeys = 0;
110 if (service->num_sortkeys)
113 sizeof(struct conf_sortkey) * service->num_sortkeys);
117 struct conf_metadata* conf_service_add_metadata(NMEM nmem,
118 struct conf_service *service,
121 enum conf_metadata_type type,
122 enum conf_metadata_merge merge,
128 struct conf_metadata * md = 0;
130 if (!service || !service->metadata || !service->num_metadata
131 || field_id < 0 || !(field_id < service->num_metadata))
134 //md = &((service->metadata)[field_id]);
135 md = service->metadata + field_id;
136 md = conf_metadata_assign(nmem, md, name, type, merge,
137 brief, termlist, rank, sortkey_offset);
142 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
143 struct conf_service *service,
146 enum conf_sortkey_type type)
148 struct conf_sortkey * sk = 0;
150 if (!service || !service->sortkeys || !service->num_sortkeys
151 || field_id < 0 || !(field_id < service->num_sortkeys))
154 //sk = &((service->sortkeys)[field_id]);
155 sk = service->sortkeys + field_id;
156 sk = conf_sortkey_assign(nmem, sk, name, type);
162 int conf_service_metadata_field_id(struct conf_service *service,
167 if (!service || !service->metadata || !service->num_metadata)
170 for(i = 0; i < service->num_metadata; i++) {
171 if (!strcmp(name, (service->metadata[i]).name))
179 int conf_service_sortkey_field_id(struct conf_service *service,
184 if (!service || !service->sortkeys || !service->num_sortkeys)
187 for(i = 0; i < service->num_sortkeys; i++) {
188 if (!strcmp(name, (service->sortkeys[i]).name))
197 /* Code to parse configuration file */
198 /* ==================================================== */
200 static struct conf_service *parse_service(xmlNode *node)
206 struct conf_service *service = 0;
207 int num_metadata = 0;
208 int num_sortkeys = 0;
210 // count num_metadata and num_sortkeys
211 for (n = node->children; n; n = n->next)
212 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
213 n->name, "metadata"))
215 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
217 if (sortkey && strcmp((const char *) sortkey, "no"))
222 service = conf_service_create(nmem, num_metadata, num_sortkeys);
224 for (n = node->children; n; n = n->next)
226 if (n->type != XML_ELEMENT_NODE)
228 if (!strcmp((const char *) n->name, (const char *) "metadata"))
230 xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
231 xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
232 xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
233 xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
234 xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
235 xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
236 xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
238 enum conf_metadata_type type = Metadata_type_generic;
239 enum conf_metadata_merge merge = Metadata_merge_no;
243 int sortkey_offset = 0;
244 enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
246 // now do the parsing logic
249 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
254 if (!strcmp((const char *) xml_brief, "yes"))
256 else if (strcmp((const char *) xml_brief, "no"))
258 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
267 if (!strcmp((const char *) xml_termlist, "yes"))
269 else if (strcmp((const char *) xml_termlist, "no"))
271 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
279 rank = atoi((const char *) xml_rank);
285 if (!strcmp((const char *) xml_type, "generic"))
286 type = Metadata_type_generic;
287 else if (!strcmp((const char *) xml_type, "year"))
288 type = Metadata_type_year;
291 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", xml_type);
296 type = Metadata_type_generic;
300 if (!strcmp((const char *) xml_merge, "no"))
301 merge = Metadata_merge_no;
302 else if (!strcmp((const char *) xml_merge, "unique"))
303 merge = Metadata_merge_unique;
304 else if (!strcmp((const char *) xml_merge, "longest"))
305 merge = Metadata_merge_longest;
306 else if (!strcmp((const char *) xml_merge, "range"))
307 merge = Metadata_merge_range;
308 else if (!strcmp((const char *) xml_merge, "all"))
309 merge = Metadata_merge_all;
313 "Unknown value for metadata/merge: %s", xml_merge);
318 merge = Metadata_merge_no;
320 // add a sortkey if so specified
321 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
323 if (merge == Metadata_merge_no)
326 "Can't specify sortkey on a non-merged field");
329 if (!strcmp((const char *) xml_sortkey, "numeric"))
330 sk_type = Metadata_sortkey_numeric;
331 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
332 sk_type = Metadata_sortkey_skiparticle;
336 "Unknown sortkey in metadata element: %s",
340 sortkey_offset = sk_node;
342 conf_service_add_sortkey(nmem, service, sk_node,
343 (const char *) xml_name, sk_type);
350 // metadata known, assign values
351 conf_service_add_metadata(nmem, service, md_node,
352 (const char *) xml_name,
354 brief, termlist, rank, sortkey_offset);
358 xmlFree(xml_sortkey);
361 xmlFree(xml_termlist);
367 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
374 static char *parse_settings(xmlNode *node)
376 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
380 r = nmem_strdup(nmem, (const char *) src);
383 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
390 static struct conf_server *parse_server(xmlNode *node)
393 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
406 for (n = node->children; n; n = n->next)
408 if (n->type != XML_ELEMENT_NODE)
410 if (!strcmp((const char *) n->name, "listen"))
412 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
413 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
415 r->port = atoi((const char *) port);
417 r->host = nmem_strdup(nmem, (const char *) host);
421 else if (!strcmp((const char *) n->name, "proxy"))
423 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
424 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
425 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
427 r->proxy_port = atoi((const char *) port);
429 r->proxy_host = nmem_strdup(nmem, (const char *) host);
431 r->myurl = nmem_strdup(nmem, (const char *) myurl);
435 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
443 else if (!strcmp((const char *) n->name, "zproxy"))
448 port = xmlGetProp(n, (xmlChar *) "port");
449 host = xmlGetProp(n, (xmlChar *) "host");
452 r->zproxy_port = atoi((const char *) port);
454 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
459 else if (!strcmp((const char *) n->name, "settings"))
463 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
466 if (!(r->settings = parse_settings(n)))
469 else if (!strcmp((const char *) n->name, "service"))
471 struct conf_service *s = parse_service(n);
478 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
485 xsltStylesheet *conf_load_stylesheet(const char *fname)
488 sprintf(path, "%s/%s", confdir, fname);
489 return xsltParseStylesheetFile((xmlChar *) path);
492 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
494 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
495 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
496 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
498 memset(r, 0, sizeof(*r));
502 if (!strcmp((const char *) type, "local"))
503 r->type = Targetprofiles_local;
506 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
512 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
517 r->src = nmem_strdup(nmem, (const char *) src);
520 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
528 static struct conf_config *parse_config(xmlNode *root)
531 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
534 r->targetprofiles = 0;
536 for (n = root->children; n; n = n->next)
538 if (n->type != XML_ELEMENT_NODE)
540 if (!strcmp((const char *) n->name, "server"))
542 struct conf_server *tmp = parse_server(n);
545 tmp->next = r->servers;
548 else if (!strcmp((const char *) n->name, "targetprofiles"))
550 // It would be fun to be able to fix this sometime
551 if (r->targetprofiles)
553 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
556 if (!(r->targetprofiles = parse_targetprofiles(n)))
561 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
568 int read_config(const char *fname)
570 xmlDoc *doc = xmlParseFile(fname);
573 if (!nmem) // Initialize
575 nmem = nmem_create();
576 xmlSubstituteEntitiesDefault(1);
577 xmlLoadExtDtdDefaultValue = 1;
581 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
584 if ((p = strrchr(fname, '/')))
587 strncpy(confdir, fname, len);
590 config = parse_config(xmlDocGetRootElement(doc));
603 * indent-tabs-mode: nil
605 * vim: shiftwidth=4 tabstop=8 expandtab