1 /* $Id: config.c,v 1.4 2007-01-08 12:43:41 adam 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>
15 #include <yaz/yaz-util.h>
18 #define CONFIG_NOEXTERNS
22 static char confdir[256] = ".";
24 struct conf_config *config = 0;
26 /* Code to parse configuration file */
27 /* ==================================================== */
29 static struct conf_service *parse_service(xmlNode *node)
32 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
36 for (n = node->children; n; n = n->next)
38 if (n->type != XML_ELEMENT_NODE)
40 if (!strcmp(n->name, "termlist"))
42 struct conf_termlist *tl = nmem_malloc(nmem, sizeof(struct conf_termlist));
43 xmlChar *name = xmlGetProp(n, "name");
46 yaz_log(YLOG_WARN, "Missing name attribute in termlist");
49 tl->name = nmem_strdup(nmem, name);
50 tl->next = r->termlists;
55 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
62 static struct conf_server *parse_server(xmlNode *node)
65 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
74 for (n = node->children; n; n = n->next)
76 if (n->type != XML_ELEMENT_NODE)
78 if (!strcmp(n->name, "listen"))
80 xmlChar *port = xmlGetProp(n, "port");
81 xmlChar *host = xmlGetProp(n, "host");
85 r->host = nmem_strdup(nmem, host);
87 else if (!strcmp(n->name, "proxy"))
89 xmlChar *port = xmlGetProp(n, "port");
90 xmlChar *host = xmlGetProp(n, "host");
92 r->proxy_port = atoi(port);
94 r->proxy_host = nmem_strdup(nmem, host);
96 else if (!strcmp(n->name, "service"))
98 struct conf_service *s = parse_service(n);
105 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
112 static xsltStylesheet *load_stylesheet(const char *fname)
115 sprintf(path, "%s/%s", confdir, fname);
116 return xsltParseStylesheetFile(path);
119 static void setup_marc(struct conf_retrievalprofile *r)
122 r->yaz_marc = yaz_marc_create();
123 if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
125 yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
128 yaz_marc_iconv(r->yaz_marc, cm);
131 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
133 struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
135 struct conf_retrievalmap **rm = &r->maplist;
137 r->requestsyntax = 0;
138 r->native_syntax = Nativesyn_xml;
139 r->native_format = Nativeform_na;
140 r->native_encoding = 0;
141 r->native_mapto = Nativemapto_na;
146 for (n = node->children; n; n = n->next)
148 if (n->type != XML_ELEMENT_NODE)
150 if (!strcmp(n->name, "requestsyntax"))
152 xmlChar *content = xmlNodeGetContent(n);
154 r->requestsyntax = nmem_strdup(nmem, content);
156 else if (!strcmp(n->name, "nativesyntax"))
158 xmlChar *name = xmlGetProp(n, "name");
159 xmlChar *format = xmlGetProp(n, "format");
160 xmlChar *encoding = xmlGetProp(n, "encoding");
161 xmlChar *mapto = xmlGetProp(n, "mapto");
164 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
167 if (!strcmp(name, "iso2709"))
169 r->native_syntax = Nativesyn_iso2709;
170 // Set a few defaults, too
171 r->native_format = Nativeform_marc21;
172 r->native_mapto = Nativemapto_marcxml;
173 r->native_encoding = "marc-8";
176 else if (!strcmp(name, "xml"))
177 r->native_syntax = Nativesyn_xml;
180 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
185 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
186 r->native_format = Nativeform_marc21;
189 yaz_log(YLOG_WARN, "Unknown native format name %s", format);
194 r->native_encoding = encoding;
197 if (!strcmp(mapto, "marcxml"))
198 r->native_mapto = Nativemapto_marcxml;
199 else if (!strcmp(mapto, "marcxchange"))
200 r->native_mapto = Nativemapto_marcxchange;
203 yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
208 else if (!strcmp(n->name, "map"))
210 struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
211 xmlChar *type = xmlGetProp(n, "type");
212 xmlChar *charset = xmlGetProp(n, "charset");
213 xmlChar *format = xmlGetProp(n, "format");
214 xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
215 bzero(m, sizeof(*m));
218 if (!strcmp(type, "xslt"))
222 yaz_log(YLOG_WARN, "Unknown map type: %s", type);
227 m->charset = nmem_strdup(nmem, charset);
229 m->format = nmem_strdup(nmem, format);
232 if (!(m->stylesheet = load_stylesheet(stylesheet)))
240 yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
248 static struct conf_config *parse_config(xmlNode *root)
251 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
252 struct conf_retrievalprofile **rp = &r->retrievalprofiles;
255 r->queryprofiles = 0;
256 r->retrievalprofiles = 0;
258 for (n = root->children; n; n = n->next)
260 if (n->type != XML_ELEMENT_NODE)
262 if (!strcmp(n->name, "server"))
264 struct conf_server *tmp = parse_server(n);
267 tmp->next = r->servers;
270 else if (!strcmp(n->name, "queryprofile"))
273 else if (!strcmp(n->name, "retrievalprofile"))
275 if (!(*rp = parse_retrievalprofile(n)))
281 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
288 int read_config(const char *fname)
290 xmlDoc *doc = xmlReadFile(fname, NULL, 0);
293 if (!nmem) // Initialize
295 nmem = nmem_create();
296 xmlSubstituteEntitiesDefault(1);
297 xmlLoadExtDtdDefaultValue = 1;
301 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
304 if ((p = rindex(fname, '/')))
307 strncpy(confdir, fname, len);
310 config = parse_config(xmlDocGetRootElement(doc));
323 * indent-tabs-mode: nil
325 * vim: shiftwidth=4 tabstop=8 expandtab