1 /* $Id: config.c,v 1.33 2007-04-27 10:27:35 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.33 2007-04-27 10:27:35 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);
63 // enforcing that merge_range is always type_year
64 if (merge == Metadata_merge_range)
65 metadata->type = Metadata_type_year;
67 metadata->type = type;
69 // enforcing that type_year is always range_merge
70 if (metadata->type == Metadata_type_year)
71 metadata->merge = Metadata_merge_range;
73 metadata->merge = merge;
75 metadata->brief = brief;
76 metadata->termlist = termlist;
77 metadata->rank = rank;
78 metadata->sortkey_offset = sortkey_offset;
83 struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
84 struct conf_sortkey * sortkey,
86 enum conf_sortkey_type type)
88 if (!nmem || !sortkey || !name)
91 sortkey->name = nmem_strdup(nmem, name);
98 struct conf_service * conf_service_create(NMEM nmem,
99 int num_metadata, int num_sortkeys)
101 struct conf_service * service = 0;
105 service = nmem_malloc(nmem, sizeof(struct conf_service));
107 service->num_metadata = num_metadata;
108 service->metadata = 0;
109 if (service->num_metadata)
112 sizeof(struct conf_metadata) * service->num_metadata);
113 service->num_sortkeys = num_sortkeys;
114 service->sortkeys = 0;
115 if (service->num_sortkeys)
118 sizeof(struct conf_sortkey) * service->num_sortkeys);
122 struct conf_metadata* conf_service_add_metadata(NMEM nmem,
123 struct conf_service *service,
126 enum conf_metadata_type type,
127 enum conf_metadata_merge merge,
133 struct conf_metadata * md = 0;
135 if (!service || !service->metadata || !service->num_metadata
136 || field_id < 0 || !(field_id < service->num_metadata))
139 //md = &((service->metadata)[field_id]);
140 md = service->metadata + field_id;
141 md = conf_metadata_assign(nmem, md, name, type, merge,
142 brief, termlist, rank, sortkey_offset);
147 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
148 struct conf_service *service,
151 enum conf_sortkey_type type)
153 struct conf_sortkey * sk = 0;
155 if (!service || !service->sortkeys || !service->num_sortkeys
156 || field_id < 0 || !(field_id < service->num_sortkeys))
159 //sk = &((service->sortkeys)[field_id]);
160 sk = service->sortkeys + field_id;
161 sk = conf_sortkey_assign(nmem, sk, name, type);
167 int conf_service_metadata_field_id(struct conf_service *service,
172 if (!service || !service->metadata || !service->num_metadata)
175 for(i = 0; i < service->num_metadata; i++) {
176 if (!strcmp(name, (service->metadata[i]).name))
184 int conf_service_sortkey_field_id(struct conf_service *service,
189 if (!service || !service->sortkeys || !service->num_sortkeys)
192 for(i = 0; i < service->num_sortkeys; i++) {
193 if (!strcmp(name, (service->sortkeys[i]).name))
202 /* Code to parse configuration file */
203 /* ==================================================== */
205 static struct conf_service *parse_service(xmlNode *node)
211 struct conf_service *service = 0;
212 int num_metadata = 0;
213 int num_sortkeys = 0;
215 // count num_metadata and num_sortkeys
216 for (n = node->children; n; n = n->next)
217 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
218 n->name, "metadata"))
220 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
222 if (sortkey && strcmp((const char *) sortkey, "no"))
227 service = conf_service_create(nmem, num_metadata, num_sortkeys);
229 for (n = node->children; n; n = n->next)
231 if (n->type != XML_ELEMENT_NODE)
233 if (!strcmp((const char *) n->name, (const char *) "metadata"))
235 xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
236 xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
237 xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
238 xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
239 xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
240 xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
241 xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
243 enum conf_metadata_type type = Metadata_type_generic;
244 enum conf_metadata_merge merge = Metadata_merge_no;
248 int sortkey_offset = 0;
249 enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
251 // now do the parsing logic
254 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
259 if (!strcmp((const char *) xml_brief, "yes"))
261 else if (strcmp((const char *) xml_brief, "no"))
263 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
272 if (!strcmp((const char *) xml_termlist, "yes"))
274 else if (strcmp((const char *) xml_termlist, "no"))
276 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
284 rank = atoi((const char *) xml_rank);
290 if (!strcmp((const char *) xml_type, "generic"))
291 type = Metadata_type_generic;
292 else if (!strcmp((const char *) xml_type, "year"))
293 type = Metadata_type_year;
297 "Unknown value for metadata/type: %s", xml_type);
302 type = Metadata_type_generic;
306 if (!strcmp((const char *) xml_merge, "no"))
307 merge = Metadata_merge_no;
308 else if (!strcmp((const char *) xml_merge, "unique"))
309 merge = Metadata_merge_unique;
310 else if (!strcmp((const char *) xml_merge, "longest"))
311 merge = Metadata_merge_longest;
312 else if (!strcmp((const char *) xml_merge, "range"))
313 merge = Metadata_merge_range;
314 else if (!strcmp((const char *) xml_merge, "all"))
315 merge = Metadata_merge_all;
319 "Unknown value for metadata/merge: %s", xml_merge);
324 merge = Metadata_merge_no;
326 // add a sortkey if so specified
327 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
329 if (merge == Metadata_merge_no)
332 "Can't specify sortkey on a non-merged field");
335 if (!strcmp((const char *) xml_sortkey, "numeric"))
336 sk_type = Metadata_sortkey_numeric;
337 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
338 sk_type = Metadata_sortkey_skiparticle;
342 "Unknown sortkey in metadata element: %s",
346 sortkey_offset = sk_node;
348 conf_service_add_sortkey(nmem, service, sk_node,
349 (const char *) xml_name, sk_type);
356 // metadata known, assign values
357 conf_service_add_metadata(nmem, service, md_node,
358 (const char *) xml_name,
360 brief, termlist, rank, sortkey_offset);
364 xmlFree(xml_sortkey);
367 xmlFree(xml_termlist);
373 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
380 static char *parse_settings(xmlNode *node)
382 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
386 r = nmem_strdup(nmem, (const char *) src);
389 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
396 static struct conf_server *parse_server(xmlNode *node)
399 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
412 for (n = node->children; n; n = n->next)
414 if (n->type != XML_ELEMENT_NODE)
416 if (!strcmp((const char *) n->name, "listen"))
418 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
419 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
421 r->port = atoi((const char *) port);
423 r->host = nmem_strdup(nmem, (const char *) host);
427 else if (!strcmp((const char *) n->name, "proxy"))
429 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
430 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
431 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
433 r->proxy_port = atoi((const char *) port);
435 r->proxy_host = nmem_strdup(nmem, (const char *) host);
437 r->myurl = nmem_strdup(nmem, (const char *) myurl);
441 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
449 else if (!strcmp((const char *) n->name, "zproxy"))
454 port = xmlGetProp(n, (xmlChar *) "port");
455 host = xmlGetProp(n, (xmlChar *) "host");
458 r->zproxy_port = atoi((const char *) port);
460 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
465 else if (!strcmp((const char *) n->name, "settings"))
469 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
472 if (!(r->settings = parse_settings(n)))
475 else if (!strcmp((const char *) n->name, "service"))
477 struct conf_service *s = parse_service(n);
484 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
491 xsltStylesheet *conf_load_stylesheet(const char *fname)
494 sprintf(path, "%s/%s", confdir, fname);
495 return xsltParseStylesheetFile((xmlChar *) path);
498 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
500 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
501 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
502 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
504 memset(r, 0, sizeof(*r));
508 if (!strcmp((const char *) type, "local"))
509 r->type = Targetprofiles_local;
512 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
518 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
523 r->src = nmem_strdup(nmem, (const char *) src);
526 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
534 static struct conf_config *parse_config(xmlNode *root)
537 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
540 r->targetprofiles = 0;
542 for (n = root->children; n; n = n->next)
544 if (n->type != XML_ELEMENT_NODE)
546 if (!strcmp((const char *) n->name, "server"))
548 struct conf_server *tmp = parse_server(n);
551 tmp->next = r->servers;
554 else if (!strcmp((const char *) n->name, "targetprofiles"))
556 // It would be fun to be able to fix this sometime
557 if (r->targetprofiles)
559 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
562 if (!(r->targetprofiles = parse_targetprofiles(n)))
567 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
574 int read_config(const char *fname)
576 xmlDoc *doc = xmlParseFile(fname);
579 if (!nmem) // Initialize
581 nmem = nmem_create();
582 xmlSubstituteEntitiesDefault(1);
583 xmlLoadExtDtdDefaultValue = 1;
587 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
590 if ((p = strrchr(fname, '/')))
593 strncpy(confdir, fname, len);
596 config = parse_config(xmlDocGetRootElement(doc));
609 * indent-tabs-mode: nil
611 * vim: shiftwidth=4 tabstop=8 expandtab