3f02149a02e420249c9ff9ac1b631cacccee9229
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.3 2007-01-03 06:23:44 quinn Exp $ */
2
3 #include <string.h>
4
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>
10
11 #include <yaz/yaz-util.h>
12 #include <yaz/nmem.h>
13
14 #define CONFIG_NOEXTERNS
15 #include "config.h"
16
17 static NMEM nmem = 0;
18 static char confdir[256] = ".";
19
20 struct conf_config *config = 0;
21
22 /* Code to parse configuration file */
23 /* ==================================================== */
24
25 static struct conf_service *parse_service(xmlNode *node)
26 {
27     xmlNode *n;
28     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
29
30     r->termlists = 0;
31
32     for (n = node->children; n; n = n->next)
33     {
34         if (n->type != XML_ELEMENT_NODE)
35             continue;
36         if (!strcmp(n->name, "termlist"))
37         {
38             struct conf_termlist *tl = nmem_malloc(nmem, sizeof(struct conf_termlist));
39             xmlChar *name = xmlGetProp(n, "name");
40             if (!name)
41             {
42                 yaz_log(YLOG_WARN, "Missing name attribute in termlist");
43                 continue;
44             }
45             tl->name = nmem_strdup(nmem, name);
46             tl->next = r->termlists;
47             r->termlists = tl;
48         }
49         else
50         {
51             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
52             return 0;
53         }
54     }
55     return r;
56 }
57
58 static struct conf_server *parse_server(xmlNode *node)
59 {
60     xmlNode *n;
61     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
62
63     r->host = 0;
64     r->port = 0;
65     r->proxy_host = 0;
66     r->proxy_port = 0;
67     r->service = 0;
68     r->next = 0;
69
70     for (n = node->children; n; n = n->next)
71     {
72         if (n->type != XML_ELEMENT_NODE)
73             continue;
74         if (!strcmp(n->name, "listen"))
75         {
76             xmlChar *port = xmlGetProp(n, "port");
77             xmlChar *host = xmlGetProp(n, "host");
78             if (port)
79                 r->port = atoi(port);
80             if (host)
81                 r->host = nmem_strdup(nmem, host);
82         }
83         else if (!strcmp(n->name, "proxy"))
84         {
85             xmlChar *port = xmlGetProp(n, "port");
86             xmlChar *host = xmlGetProp(n, "host");
87             if (port)
88                 r->proxy_port = atoi(port);
89             if (host)
90                 r->proxy_host = nmem_strdup(nmem, host);
91         }
92         else if (!strcmp(n->name, "service"))
93         {
94             struct conf_service *s = parse_service(n);
95             if (!s)
96                 return 0;
97             r->service = s;
98         }
99         else
100         {
101             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
102             return 0;
103         }
104     }
105     return r;
106 }
107
108 static xsltStylesheet *load_stylesheet(const char *fname)
109 {
110     char path[256];
111     sprintf(path, "%s/%s", confdir, fname);
112     return xsltParseStylesheetFile(path);
113 }
114
115 static void setup_marc(struct conf_retrievalprofile *r)
116 {
117     yaz_iconv_t cm;
118     r->yaz_marc = yaz_marc_create();
119     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
120     {
121         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
122         return;
123     }
124     yaz_marc_iconv(r->yaz_marc, cm);
125 }
126
127 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
128 {
129     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
130     xmlNode *n;
131     struct conf_retrievalmap **rm = &r->maplist;
132
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;
138     r->yaz_marc = 0;
139     r->maplist = 0;
140     r->next = 0;
141
142     for (n = node->children; n; n = n->next)
143     {
144         if (n->type != XML_ELEMENT_NODE)
145             continue;
146         if (!strcmp(n->name, "requestsyntax"))
147         {
148             xmlChar *content = xmlNodeGetContent(n);
149             if (content)
150                 r->requestsyntax = nmem_strdup(nmem, content);
151         }
152         else if (!strcmp(n->name, "nativesyntax"))
153         {
154             xmlChar *name = xmlGetProp(n, "name");
155             xmlChar *format = xmlGetProp(n, "format");
156             xmlChar *encoding = xmlGetProp(n, "encoding");
157             xmlChar *mapto = xmlGetProp(n, "mapto");
158             if (!name)
159             {
160                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
161                 return 0;
162             }
163             if (!strcmp(name, "iso2709"))
164             {
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";
170                 setup_marc(r);
171             }
172             else if (!strcmp(name, "xml"))
173                 r->native_syntax = Nativesyn_xml;
174             else
175             {
176                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
177                 return 0;
178             }
179             if (format)
180             {
181                 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
182                     r->native_format = Nativeform_marc21;
183                 else
184                 {
185                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
186                     return 0;
187                 }
188             }
189             if (encoding)
190                 r->native_encoding = encoding;
191             if (mapto)
192             {
193                 if (!strcmp(mapto, "marcxml"))
194                     r->native_mapto = Nativemapto_marcxml;
195                 else if (!strcmp(mapto, "marcxchange"))
196                     r->native_mapto = Nativemapto_marcxchange;
197                 else
198                 {
199                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
200                     return 0;
201                 }
202             }
203         }
204         else if (!strcmp(n->name, "map"))
205         {
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));
212             if (type)
213             {
214                 if (!strcmp(type, "xslt"))
215                     m->type = Map_xslt;
216                 else
217                 {
218                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
219                     return 0;
220                 }
221             }
222             if (charset)
223                 m->charset = nmem_strdup(nmem, charset);
224             if (format)
225                 m->format = nmem_strdup(nmem, format);
226             if (stylesheet)
227             {
228                 if (!(m->stylesheet = load_stylesheet(stylesheet)))
229                     return 0;
230             }
231             *rm = m;
232             rm = &m->next;
233         }
234         else
235         {
236             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
237             return 0;
238         }
239     }
240
241     return r;
242 }
243
244 static struct conf_config *parse_config(xmlNode *root)
245 {
246     xmlNode *n;
247     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
248     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
249
250     r->servers = 0;
251     r->queryprofiles = 0;
252     r->retrievalprofiles = 0;
253
254     for (n = root->children; n; n = n->next)
255     {
256         if (n->type != XML_ELEMENT_NODE)
257             continue;
258         if (!strcmp(n->name, "server"))
259         {
260             struct conf_server *tmp = parse_server(n);
261             if (!tmp)
262                 return 0;
263             tmp->next = r->servers;
264             r->servers = tmp;
265         }
266         else if (!strcmp(n->name, "queryprofile"))
267         {
268         }
269         else if (!strcmp(n->name, "retrievalprofile"))
270         {
271             if (!(*rp = parse_retrievalprofile(n)))
272                 return 0;
273             rp = &(*rp)->next;
274         }
275         else
276         {
277             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
278             return 0;
279         }
280     }
281     return r;
282 }
283
284 int read_config(const char *fname)
285 {
286     xmlDoc *doc = xmlReadFile(fname, NULL, 0);
287     const char *p;
288
289     if (!nmem)  // Initialize
290     {
291         nmem = nmem_create();
292         xmlSubstituteEntitiesDefault(1);
293         xmlLoadExtDtdDefaultValue = 1;
294     }
295     if (!doc)
296     {
297         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
298         exit(1);
299     }
300     if ((p = rindex(fname, '/')))
301     {
302         int len = p - fname;
303         strncpy(confdir, fname, len);
304         confdir[len] = '\0';
305     }
306     config = parse_config(xmlDocGetRootElement(doc));
307     xmlFreeDoc(doc);
308
309     if (config)
310         return 1;
311     else
312         return 0;
313 }
314
315
316 /*
317  * Local variables:
318  * c-basic-offset: 4
319  * indent-tabs-mode: nil
320  * End:
321  * vim: shiftwidth=4 tabstop=8 expandtab
322  */