1 /* $Id: config.c,v 1.25 2007-04-19 11:57:53 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.25 2007-04-19 11:57:53 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;
47 struct conf_service * conf_service_create(NMEM nmem)
49 struct conf_service * service
50 = nmem_malloc(nmem, sizeof(struct conf_service));
51 service->num_metadata = 0;
52 service->metadata = 0;
53 service->num_sortkeys = 0;
54 service->sortkeys = 0;
59 struct conf_metadata * conf_metadata_create(NMEM nmem,
61 enum conf_metadata_type type,
62 enum conf_metadata_merge merge,
69 struct conf_metadata * metadata
70 = nmem_malloc(nmem, sizeof(struct conf_metadata));
72 metadata->name = nmem_strdup(nmem, name);
73 metadata->type = type;
74 metadata->merge = merge;
75 metadata->brief = brief;
76 metadata->termlist = termlist;
77 metadata->rank = rank;
78 metadata->sortkey_offset = sortkey_offset;
82 struct conf_metadata* conf_service_add_metadata(NMEM nmem,
83 struct conf_service *service,
85 enum conf_metadata_type type,
86 enum conf_metadata_merge merge,
92 struct conf_metadata * m = 0;
97 m = conf_metadata_create(nmem, name, type, merge,
98 brief, termlist, rank, sortkey_offset);
100 // Not finished, checked temporarily in for file move // if (m)
108 /* Code to parse configuration file */
109 /* ==================================================== */
111 static struct conf_service *parse_service(xmlNode *node)
114 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
118 r->num_sortkeys = r->num_metadata = 0;
119 // Allocate array of conf metadata and sortkey tructs, if necessary
120 for (n = node->children; n; n = n->next)
121 if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
122 n->name, "metadata"))
124 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
126 if (sortkey && strcmp((const char *) sortkey, "no"))
131 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
135 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
139 for (n = node->children; n; n = n->next)
141 if (n->type != XML_ELEMENT_NODE)
143 if (!strcmp((const char *) n->name, (const char *) "metadata"))
145 struct conf_metadata *md = &r->metadata[md_node];
146 xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
147 xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
148 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
149 xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
150 xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
151 xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
152 xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
156 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
159 md->name = nmem_strdup(nmem, (const char *) name);
162 if (!strcmp((const char *) brief, "yes"))
164 else if (strcmp((const char *) brief, "no"))
166 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
175 if (!strcmp((const char *) termlist, "yes"))
177 else if (strcmp((const char *) termlist, "no"))
179 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
187 md->rank = atoi((const char *) rank);
193 if (!strcmp((const char *) type, "generic"))
194 md->type = Metadata_type_generic;
195 else if (!strcmp((const char *) type, "year"))
196 md->type = Metadata_type_year;
199 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
204 md->type = Metadata_type_generic;
208 if (!strcmp((const char *) merge, "no"))
209 md->merge = Metadata_merge_no;
210 else if (!strcmp((const char *) merge, "unique"))
211 md->merge = Metadata_merge_unique;
212 else if (!strcmp((const char *) merge, "longest"))
213 md->merge = Metadata_merge_longest;
214 else if (!strcmp((const char *) merge, "range"))
215 md->merge = Metadata_merge_range;
216 else if (!strcmp((const char *) merge, "all"))
217 md->merge = Metadata_merge_all;
220 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
225 md->merge = Metadata_merge_no;
227 if (sortkey && strcmp((const char *) sortkey, "no"))
229 struct conf_sortkey *sk = &r->sortkeys[sk_node];
230 if (md->merge == Metadata_merge_no)
232 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
235 if (!strcmp((const char *) sortkey, "numeric"))
236 sk->type = Metadata_sortkey_numeric;
237 else if (!strcmp((const char *) sortkey, "skiparticle"))
238 sk->type = Metadata_sortkey_skiparticle;
241 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
245 md->sortkey_offset = sk_node;
249 md->sortkey_offset = -1;
262 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
269 static char *parse_settings(xmlNode *node)
271 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
275 r = nmem_strdup(nmem, (const char *) src);
278 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
285 static struct conf_server *parse_server(xmlNode *node)
288 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
301 for (n = node->children; n; n = n->next)
303 if (n->type != XML_ELEMENT_NODE)
305 if (!strcmp((const char *) n->name, "listen"))
307 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
308 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
310 r->port = atoi((const char *) port);
312 r->host = nmem_strdup(nmem, (const char *) host);
316 else if (!strcmp((const char *) n->name, "proxy"))
318 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
319 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
320 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
322 r->proxy_port = atoi((const char *) port);
324 r->proxy_host = nmem_strdup(nmem, (const char *) host);
326 r->myurl = nmem_strdup(nmem, (const char *) myurl);
330 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
338 else if (!strcmp((const char *) n->name, "zproxy"))
343 port = xmlGetProp(n, (xmlChar *) "port");
344 host = xmlGetProp(n, (xmlChar *) "host");
347 r->zproxy_port = atoi((const char *) port);
349 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
354 else if (!strcmp((const char *) n->name, "settings"))
358 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
361 if (!(r->settings = parse_settings(n)))
364 else if (!strcmp((const char *) n->name, "service"))
366 struct conf_service *s = parse_service(n);
373 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
380 xsltStylesheet *conf_load_stylesheet(const char *fname)
383 sprintf(path, "%s/%s", confdir, fname);
384 return xsltParseStylesheetFile((xmlChar *) path);
387 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
389 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
390 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
391 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
393 memset(r, 0, sizeof(*r));
397 if (!strcmp((const char *) type, "local"))
398 r->type = Targetprofiles_local;
401 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
407 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
412 r->src = nmem_strdup(nmem, (const char *) src);
415 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
423 static struct conf_config *parse_config(xmlNode *root)
426 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
429 r->targetprofiles = 0;
431 for (n = root->children; n; n = n->next)
433 if (n->type != XML_ELEMENT_NODE)
435 if (!strcmp((const char *) n->name, "server"))
437 struct conf_server *tmp = parse_server(n);
440 tmp->next = r->servers;
443 else if (!strcmp((const char *) n->name, "targetprofiles"))
445 // It would be fun to be able to fix this sometime
446 if (r->targetprofiles)
448 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
451 if (!(r->targetprofiles = parse_targetprofiles(n)))
456 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
463 int read_config(const char *fname)
465 xmlDoc *doc = xmlParseFile(fname);
468 if (!nmem) // Initialize
470 nmem = nmem_create();
471 xmlSubstituteEntitiesDefault(1);
472 xmlLoadExtDtdDefaultValue = 1;
476 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
479 if ((p = strrchr(fname, '/')))
482 strncpy(confdir, fname, len);
485 config = parse_config(xmlDocGetRootElement(doc));
498 * indent-tabs-mode: nil
500 * vim: shiftwidth=4 tabstop=8 expandtab