1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* $Id: config.c,v 1.42 2007-10-31 05:29:08 quinn Exp $ */
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <libxslt/xslt.h>
27 #include <libxslt/transform.h>
28 #include <libxslt/xsltutils.h>
34 #include <yaz/yaz-util.h>
36 #include <yaz/snprintf.h>
38 #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,
53 enum conf_setting_type setting,
59 if (!nmem || !metadata || !name)
62 metadata->name = nmem_strdup(nmem, name);
64 metadata->type = type;
66 // enforcing that type_year is always range_merge
67 if (metadata->type == Metadata_type_year)
68 metadata->merge = Metadata_merge_range;
70 metadata->merge = merge;
72 metadata->setting = setting;
73 metadata->brief = brief;
74 metadata->termlist = termlist;
75 metadata->rank = rank;
76 metadata->sortkey_offset = sortkey_offset;
81 struct conf_sortkey * conf_sortkey_assign(NMEM nmem,
82 struct conf_sortkey * sortkey,
84 enum conf_sortkey_type type)
86 if (!nmem || !sortkey || !name)
89 sortkey->name = nmem_strdup(nmem, name);
96 struct conf_service * conf_service_create(NMEM nmem,
97 int num_metadata, int num_sortkeys)
99 struct conf_service * service = 0;
103 service = nmem_malloc(nmem, sizeof(struct conf_service));
105 service->num_metadata = num_metadata;
106 service->metadata = 0;
107 if (service->num_metadata)
110 sizeof(struct conf_metadata) * service->num_metadata);
111 service->num_sortkeys = num_sortkeys;
112 service->sortkeys = 0;
113 if (service->num_sortkeys)
116 sizeof(struct conf_sortkey) * service->num_sortkeys);
121 struct conf_metadata* conf_service_add_metadata(NMEM nmem,
122 struct conf_service *service,
125 enum conf_metadata_type type,
126 enum conf_metadata_merge merge,
127 enum conf_setting_type setting,
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, setting,
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");
242 xmlChar *xml_setting = xmlGetProp(n, (xmlChar *) "setting");
244 enum conf_metadata_type type = Metadata_type_generic;
245 enum conf_metadata_merge merge = Metadata_merge_no;
246 enum conf_setting_type setting = Metadata_setting_no;
247 enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
251 int sortkey_offset = 0;
253 // now do the parsing logic
256 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
261 if (!strcmp((const char *) xml_brief, "yes"))
263 else if (strcmp((const char *) xml_brief, "no"))
265 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
274 if (!strcmp((const char *) xml_termlist, "yes"))
276 else if (strcmp((const char *) xml_termlist, "no"))
278 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
286 rank = atoi((const char *) xml_rank);
292 if (!strcmp((const char *) xml_type, "generic"))
293 type = Metadata_type_generic;
294 else if (!strcmp((const char *) xml_type, "year"))
295 type = Metadata_type_year;
296 else if (!strcmp((const char *) xml_type, "date"))
297 type = Metadata_type_date;
301 "Unknown value for metadata/type: %s", xml_type);
306 type = Metadata_type_generic;
310 if (!strcmp((const char *) xml_merge, "no"))
311 merge = Metadata_merge_no;
312 else if (!strcmp((const char *) xml_merge, "unique"))
313 merge = Metadata_merge_unique;
314 else if (!strcmp((const char *) xml_merge, "longest"))
315 merge = Metadata_merge_longest;
316 else if (!strcmp((const char *) xml_merge, "range"))
317 merge = Metadata_merge_range;
318 else if (!strcmp((const char *) xml_merge, "all"))
319 merge = Metadata_merge_all;
323 "Unknown value for metadata/merge: %s", xml_merge);
328 merge = Metadata_merge_no;
332 if (!strcmp((const char *) xml_setting, "no"))
333 setting = Metadata_setting_no;
334 else if (!strcmp((const char *) xml_setting, "postproc"))
335 setting = Metadata_setting_postproc;
336 else if (!strcmp((const char *) xml_setting, "parameter"))
337 setting = Metadata_setting_parameter;
341 "Unknown value for medadata/setting: %s", xml_setting);
346 // add a sortkey if so specified
347 if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
349 if (merge == Metadata_merge_no)
352 "Can't specify sortkey on a non-merged field");
355 if (!strcmp((const char *) xml_sortkey, "numeric"))
356 sk_type = Metadata_sortkey_numeric;
357 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
358 sk_type = Metadata_sortkey_skiparticle;
362 "Unknown sortkey in metadata element: %s",
366 sortkey_offset = sk_node;
368 conf_service_add_sortkey(nmem, service, sk_node,
369 (const char *) xml_name, sk_type);
376 // metadata known, assign values
377 conf_service_add_metadata(nmem, service, md_node,
378 (const char *) xml_name,
379 type, merge, setting,
380 brief, termlist, rank, sortkey_offset);
384 xmlFree(xml_sortkey);
387 xmlFree(xml_termlist);
393 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
400 static char *parse_settings(xmlNode *node)
402 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
406 r = nmem_strdup(nmem, (const char *) src);
409 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
416 static struct conf_server *parse_server(xmlNode *node)
419 struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
423 server->proxy_host = 0;
424 server->proxy_port = 0;
428 server->settings = 0;
429 server->relevance_pct = 0;
430 server->sort_pct = 0;
431 server->mergekey_pct = 0;
433 for (n = node->children; n; n = n->next)
435 if (n->type != XML_ELEMENT_NODE)
437 if (!strcmp((const char *) n->name, "listen"))
439 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
440 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
442 server->port = atoi((const char *) port);
444 server->host = nmem_strdup(nmem, (const char *) host);
448 else if (!strcmp((const char *) n->name, "proxy"))
450 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
451 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
452 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
454 server->proxy_port = atoi((const char *) port);
456 server->proxy_host = nmem_strdup(nmem, (const char *) host);
458 server->myurl = nmem_strdup(nmem, (const char *) myurl);
462 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
470 else if (!strcmp((const char *) n->name, "settings"))
472 if (server->settings)
474 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
477 if (!(server->settings = parse_settings(n)))
480 else if (!strcmp((const char *) n->name, "relevance"))
482 server->relevance_pct = pp2_charset_create_xml(n->children);
484 else if (!strcmp((const char *) n->name, "sort"))
486 server->sort_pct = pp2_charset_create_xml(n->children);
488 else if (!strcmp((const char *) n->name, "mergekey"))
490 server->mergekey_pct = pp2_charset_create_xml(n->children);
492 else if (!strcmp((const char *) n->name, "service"))
494 struct conf_service *s = parse_service(n);
501 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
505 if (!server->relevance_pct)
506 server->relevance_pct = pp2_charset_create(0);
507 if (!server->sort_pct)
508 server->sort_pct = pp2_charset_create(0);
509 if (!server->mergekey_pct)
510 server->mergekey_pct = pp2_charset_create(0);
514 xsltStylesheet *conf_load_stylesheet(const char *fname)
518 yaz_snprintf(path, sizeof(path), fname);
520 yaz_snprintf(path, sizeof(path), "%s/%s", confdir, fname);
521 return xsltParseStylesheetFile((xmlChar *) path);
524 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
526 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
527 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
528 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
530 memset(r, 0, sizeof(*r));
534 if (!strcmp((const char *) type, "local"))
535 r->type = Targetprofiles_local;
538 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
544 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
549 r->src = nmem_strdup(nmem, (const char *) src);
552 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
560 static struct conf_config *parse_config(xmlNode *root)
563 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
566 r->targetprofiles = 0;
568 for (n = root->children; n; n = n->next)
570 if (n->type != XML_ELEMENT_NODE)
572 if (!strcmp((const char *) n->name, "server"))
574 struct conf_server *tmp = parse_server(n);
577 tmp->next = r->servers;
580 else if (!strcmp((const char *) n->name, "targetprofiles"))
582 // It would be fun to be able to fix this sometime
583 if (r->targetprofiles)
585 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
588 if (!(r->targetprofiles = parse_targetprofiles(n)))
593 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
600 int read_config(const char *fname)
602 xmlDoc *doc = xmlParseFile(fname);
605 if (!nmem) // Initialize
607 nmem = nmem_create();
608 xmlSubstituteEntitiesDefault(1);
609 xmlLoadExtDtdDefaultValue = 1;
613 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
616 if ((p = strrchr(fname, '/')))
619 if (len >= sizeof(confdir))
620 len = sizeof(confdir)-1;
621 strncpy(confdir, fname, len);
624 config = parse_config(xmlDocGetRootElement(doc));
637 * indent-tabs-mode: nil
639 * vim: shiftwidth=4 tabstop=8 expandtab