1 /* $Id: config.c,v 1.3 2007-01-03 06:23:44 quinn Exp $ */
5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
7 #include <libxslt/xslt.h>
8 #include <libxslt/transform.h>
9 #include <libxslt/xsltutils.h>
11 #include <yaz/yaz-util.h>
14 #define CONFIG_NOEXTERNS
18 static char confdir[256] = ".";
20 struct conf_config *config = 0;
22 /* Code to parse configuration file */
23 /* ==================================================== */
25 static struct conf_service *parse_service(xmlNode *node)
28 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
32 for (n = node->children; n; n = n->next)
34 if (n->type != XML_ELEMENT_NODE)
36 if (!strcmp(n->name, "termlist"))
38 struct conf_termlist *tl = nmem_malloc(nmem, sizeof(struct conf_termlist));
39 xmlChar *name = xmlGetProp(n, "name");
42 yaz_log(YLOG_WARN, "Missing name attribute in termlist");
45 tl->name = nmem_strdup(nmem, name);
46 tl->next = r->termlists;
51 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
58 static struct conf_server *parse_server(xmlNode *node)
61 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
70 for (n = node->children; n; n = n->next)
72 if (n->type != XML_ELEMENT_NODE)
74 if (!strcmp(n->name, "listen"))
76 xmlChar *port = xmlGetProp(n, "port");
77 xmlChar *host = xmlGetProp(n, "host");
81 r->host = nmem_strdup(nmem, host);
83 else if (!strcmp(n->name, "proxy"))
85 xmlChar *port = xmlGetProp(n, "port");
86 xmlChar *host = xmlGetProp(n, "host");
88 r->proxy_port = atoi(port);
90 r->proxy_host = nmem_strdup(nmem, host);
92 else if (!strcmp(n->name, "service"))
94 struct conf_service *s = parse_service(n);
101 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
108 static xsltStylesheet *load_stylesheet(const char *fname)
111 sprintf(path, "%s/%s", confdir, fname);
112 return xsltParseStylesheetFile(path);
115 static void setup_marc(struct conf_retrievalprofile *r)
118 r->yaz_marc = yaz_marc_create();
119 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
121 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
124 yaz_marc_iconv(r->yaz_marc, cm);
127 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
129 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
131 struct conf_retrievalmap **rm = &r->maplist;
133 r->requestsyntax = 0;
134 r->native_syntax = Nativesyn_xml;
135 r->native_format = Nativeform_na;
136 r->native_encoding = 0;
137 r->native_mapto = Nativemapto_na;
142 for (n = node->children; n; n = n->next)
144 if (n->type != XML_ELEMENT_NODE)
146 if (!strcmp(n->name, "requestsyntax"))
148 xmlChar *content = xmlNodeGetContent(n);
150 r->requestsyntax = nmem_strdup(nmem, content);
152 else if (!strcmp(n->name, "nativesyntax"))
154 xmlChar *name = xmlGetProp(n, "name");
155 xmlChar *format = xmlGetProp(n, "format");
156 xmlChar *encoding = xmlGetProp(n, "encoding");
157 xmlChar *mapto = xmlGetProp(n, "mapto");
160 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
163 if (!strcmp(name, "iso2709"))
165 r->native_syntax = Nativesyn_iso2709;
166 // Set a few defaults, too
167 r->native_format = Nativeform_marc21;
168 r->native_mapto = Nativemapto_marcxml;
169 r->native_encoding = "marc-8";
172 else if (!strcmp(name, "xml"))
173 r->native_syntax = Nativesyn_xml;
176 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
181 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
182 r->native_format = Nativeform_marc21;
185 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
190 r->native_encoding = encoding;
193 if (!strcmp(mapto, "marcxml"))
194 r->native_mapto = Nativemapto_marcxml;
195 else if (!strcmp(mapto, "marcxchange"))
196 r->native_mapto = Nativemapto_marcxchange;
199 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
204 else if (!strcmp(n->name, "map"))
206 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
207 xmlChar *type = xmlGetProp(n, "type");
208 xmlChar *charset = xmlGetProp(n, "charset");
209 xmlChar *format = xmlGetProp(n, "format");
210 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
211 bzero(m, sizeof(*m));
214 if (!strcmp(type, "xslt"))
218 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
223 m->charset = nmem_strdup(nmem, charset);
225 m->format = nmem_strdup(nmem, format);
228 if (!(m->stylesheet = load_stylesheet(stylesheet)))
236 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
244 static struct conf_config *parse_config(xmlNode *root)
247 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
248 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
251 r->queryprofiles = 0;
252 r->retrievalprofiles = 0;
254 for (n = root->children; n; n = n->next)
256 if (n->type != XML_ELEMENT_NODE)
258 if (!strcmp(n->name, "server"))
260 struct conf_server *tmp = parse_server(n);
263 tmp->next = r->servers;
266 else if (!strcmp(n->name, "queryprofile"))
269 else if (!strcmp(n->name, "retrievalprofile"))
271 if (!(*rp = parse_retrievalprofile(n)))
277 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
284 int read_config(const char *fname)
286 xmlDoc *doc = xmlReadFile(fname, NULL, 0);
289 if (!nmem) // Initialize
291 nmem = nmem_create();
292 xmlSubstituteEntitiesDefault(1);
293 xmlLoadExtDtdDefaultValue = 1;
297 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
300 if ((p = rindex(fname, '/')))
303 strncpy(confdir, fname, len);
306 config = parse_config(xmlDocGetRootElement(doc));
319 * indent-tabs-mode: nil
321 * vim: shiftwidth=4 tabstop=8 expandtab