Fixed bug related to ranking, introduced by 'metadata' update.
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.6 2007-01-08 19:39:12 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 #if HAVE_CONFIG_H
12 #include <cconfig.h>
13 #endif
14
15 #include <yaz/yaz-util.h>
16 #include <yaz/nmem.h>
17
18 #define CONFIG_NOEXTERNS
19 #include "config.h"
20
21 static NMEM nmem = 0;
22 static char confdir[256] = ".";
23
24 struct conf_config *config = 0;
25
26 /* Code to parse configuration file */
27 /* ==================================================== */
28
29 static struct conf_service *parse_service(xmlNode *node)
30 {
31     xmlNode *n;
32     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
33     int num_metadata = 0;
34     int md_node = 0;
35
36     // Allocate array of conf metadata structs, if necessary
37     for (n = node->children; n; n = n->next)
38         if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, "metadata"))
39             num_metadata++;
40     if (num_metadata)
41         r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * num_metadata);
42     r->num_metadata = num_metadata;
43
44     for (n = node->children; n; n = n->next)
45     {
46         if (n->type != XML_ELEMENT_NODE)
47             continue;
48         if (!strcmp(n->name, "metadata"))
49         {
50             struct conf_metadata *md = &r->metadata[md_node];
51             xmlChar *name = xmlGetProp(n, "name");
52             xmlChar *brief = xmlGetProp(n, "brief");
53             xmlChar *sortkey = xmlGetProp(n, "sortkey");
54             xmlChar *merge = xmlGetProp(n, "merge");
55             xmlChar *type = xmlGetProp(n, "type");
56             xmlChar *termlist = xmlGetProp(n, "termlist");
57             xmlChar *rank = xmlGetProp(n, "rank");
58
59             if (!name)
60             {
61                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
62                 return 0;
63             }
64             md->name = nmem_strdup(nmem, name);
65             if (brief)
66             {
67                 if (!strcmp(brief, "yes"))
68                     md->brief = 1;
69                 else if (strcmp(brief, "no"))
70                 {
71                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
72                     return 0;
73                 }
74             }
75             else
76                 md->brief = 0;
77
78             if (termlist)
79             {
80                 if (!strcmp(termlist, "yes"))
81                     md->termlist = 1;
82                 else if (strcmp(termlist, "no"))
83                 {
84                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
85                     return 0;
86                 }
87             }
88             else
89                 md->termlist = 0;
90
91             if (rank)
92                 md->rank = atoi(rank);
93             else
94                 md->rank = 1;
95
96             if (type)
97             {
98                 if (!strcmp(type, "generic"))
99                     md->type = Metadata_type_generic;
100                 else if (!strcmp(type, "integer"))
101                     md->type = Metadata_type_integer;
102                 else if (!strcmp(type, "year"))
103                     md->type = Metadata_type_year;
104                 else
105                 {
106                     yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
107                     return 0;
108                 }
109             }
110             md->type = Metadata_type_generic;
111
112             if (sortkey)
113             {
114                 if (!strcmp(sortkey, "no"))
115                     md->sortkey = Metadata_sortkey_no;
116                 else if (!strcmp(sortkey, "numeric"))
117                     md->sortkey = Metadata_sortkey_numeric;
118                 else if (!strcmp(sortkey, "range"))
119                     md->sortkey = Metadata_sortkey_range;
120                 else if (!strcmp(sortkey, "skiparticle"))
121                     md->sortkey = Metadata_sortkey_skiparticle;
122                 else
123                 {
124                     yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
125                     return 0;
126                 }
127             }
128             else
129                 md->sortkey = Metadata_sortkey_no;
130
131             if (merge)
132             {
133                 if (!strcmp(merge, "no"))
134                     md->merge = Metadata_merge_no;
135                 else if (!strcmp(merge, "unique"))
136                     md->merge = Metadata_merge_unique;
137                 else if (!strcmp(merge, "longest"))
138                     md->merge = Metadata_merge_longest;
139                 else if (!strcmp(merge, "range"))
140                     md->merge = Metadata_merge_range;
141                 else if (!strcmp(merge, "all"))
142                     md->merge = Metadata_merge_all;
143                 else
144                 {
145                     yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
146                     return 0;
147                 }
148             }
149             else
150                 md->merge = Metadata_merge_no;
151
152             xmlFree(name);
153             xmlFree(brief);
154             xmlFree(sortkey);
155             xmlFree(merge);
156             xmlFree(termlist);
157             xmlFree(rank);
158             md_node++;
159         }
160         else
161         {
162             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
163             return 0;
164         }
165     }
166     return r;
167 }
168
169 static struct conf_server *parse_server(xmlNode *node)
170 {
171     xmlNode *n;
172     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
173
174     r->host = 0;
175     r->port = 0;
176     r->proxy_host = 0;
177     r->proxy_port = 0;
178     r->service = 0;
179     r->next = 0;
180
181     for (n = node->children; n; n = n->next)
182     {
183         if (n->type != XML_ELEMENT_NODE)
184             continue;
185         if (!strcmp(n->name, "listen"))
186         {
187             xmlChar *port = xmlGetProp(n, "port");
188             xmlChar *host = xmlGetProp(n, "host");
189             if (port)
190                 r->port = atoi(port);
191             if (host)
192                 r->host = nmem_strdup(nmem, host);
193             xmlFree(port);
194             xmlFree(host);
195         }
196         else if (!strcmp(n->name, "proxy"))
197         {
198             xmlChar *port = xmlGetProp(n, "port");
199             xmlChar *host = xmlGetProp(n, "host");
200             if (port)
201                 r->proxy_port = atoi(port);
202             if (host)
203                 r->proxy_host = nmem_strdup(nmem, host);
204             xmlFree(port);
205             xmlFree(host);
206         }
207         else if (!strcmp(n->name, "service"))
208         {
209             struct conf_service *s = parse_service(n);
210             if (!s)
211                 return 0;
212             r->service = s;
213         }
214         else
215         {
216             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
217             return 0;
218         }
219     }
220     return r;
221 }
222
223 static xsltStylesheet *load_stylesheet(const char *fname)
224 {
225     char path[256];
226     sprintf(path, "%s/%s", confdir, fname);
227     return xsltParseStylesheetFile(path);
228 }
229
230 static void setup_marc(struct conf_retrievalprofile *r)
231 {
232     yaz_iconv_t cm;
233     r->yaz_marc = yaz_marc_create();
234     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
235     {
236         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
237         return;
238     }
239     yaz_marc_iconv(r->yaz_marc, cm);
240 }
241
242 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
243 {
244     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
245     xmlNode *n;
246     struct conf_retrievalmap **rm = &r->maplist;
247
248     r->requestsyntax = 0;
249     r->native_syntax = Nativesyn_xml;
250     r->native_format = Nativeform_na;
251     r->native_encoding = 0;
252     r->native_mapto = Nativemapto_na;
253     r->yaz_marc = 0;
254     r->maplist = 0;
255     r->next = 0;
256
257     for (n = node->children; n; n = n->next)
258     {
259         if (n->type != XML_ELEMENT_NODE)
260             continue;
261         if (!strcmp(n->name, "requestsyntax"))
262         {
263             xmlChar *content = xmlNodeGetContent(n);
264             if (content)
265                 r->requestsyntax = nmem_strdup(nmem, content);
266         }
267         else if (!strcmp(n->name, "nativesyntax"))
268         {
269             xmlChar *name = xmlGetProp(n, "name");
270             xmlChar *format = xmlGetProp(n, "format");
271             xmlChar *encoding = xmlGetProp(n, "encoding");
272             xmlChar *mapto = xmlGetProp(n, "mapto");
273             if (!name)
274             {
275                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
276                 return 0;
277             }
278             if (!strcmp(name, "iso2709"))
279             {
280                 r->native_syntax = Nativesyn_iso2709;
281                 // Set a few defaults, too
282                 r->native_format = Nativeform_marc21;
283                 r->native_mapto = Nativemapto_marcxml;
284                 r->native_encoding = "marc-8";
285                 setup_marc(r);
286             }
287             else if (!strcmp(name, "xml"))
288                 r->native_syntax = Nativesyn_xml;
289             else
290             {
291                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
292                 return 0;
293             }
294             if (format)
295             {
296                 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
297                     r->native_format = Nativeform_marc21;
298                 else
299                 {
300                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
301                     return 0;
302                 }
303             }
304             if (encoding)
305                 r->native_encoding = encoding;
306             if (mapto)
307             {
308                 if (!strcmp(mapto, "marcxml"))
309                     r->native_mapto = Nativemapto_marcxml;
310                 else if (!strcmp(mapto, "marcxchange"))
311                     r->native_mapto = Nativemapto_marcxchange;
312                 else
313                 {
314                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
315                     return 0;
316                 }
317             }
318             xmlFree(name);
319             xmlFree(format);
320             xmlFree(encoding);
321             xmlFree(mapto);
322         }
323         else if (!strcmp(n->name, "map"))
324         {
325             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
326             xmlChar *type = xmlGetProp(n, "type");
327             xmlChar *charset = xmlGetProp(n, "charset");
328             xmlChar *format = xmlGetProp(n, "format");
329             xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
330             bzero(m, sizeof(*m));
331             if (type)
332             {
333                 if (!strcmp(type, "xslt"))
334                     m->type = Map_xslt;
335                 else
336                 {
337                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
338                     return 0;
339                 }
340             }
341             if (charset)
342                 m->charset = nmem_strdup(nmem, charset);
343             if (format)
344                 m->format = nmem_strdup(nmem, format);
345             if (stylesheet)
346             {
347                 if (!(m->stylesheet = load_stylesheet(stylesheet)))
348                     return 0;
349             }
350             *rm = m;
351             rm = &m->next;
352             xmlFree(type);
353             xmlFree(charset);
354             xmlFree(format);
355             xmlFree(stylesheet);
356         }
357         else
358         {
359             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
360             return 0;
361         }
362     }
363
364     return r;
365 }
366
367 static struct conf_config *parse_config(xmlNode *root)
368 {
369     xmlNode *n;
370     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
371     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
372
373     r->servers = 0;
374     r->queryprofiles = 0;
375     r->retrievalprofiles = 0;
376
377     for (n = root->children; n; n = n->next)
378     {
379         if (n->type != XML_ELEMENT_NODE)
380             continue;
381         if (!strcmp(n->name, "server"))
382         {
383             struct conf_server *tmp = parse_server(n);
384             if (!tmp)
385                 return 0;
386             tmp->next = r->servers;
387             r->servers = tmp;
388         }
389         else if (!strcmp(n->name, "queryprofile"))
390         {
391         }
392         else if (!strcmp(n->name, "retrievalprofile"))
393         {
394             if (!(*rp = parse_retrievalprofile(n)))
395                 return 0;
396             rp = &(*rp)->next;
397         }
398         else
399         {
400             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
401             return 0;
402         }
403     }
404     return r;
405 }
406
407 int read_config(const char *fname)
408 {
409     xmlDoc *doc = xmlReadFile(fname, NULL, 0);
410     const char *p;
411
412     if (!nmem)  // Initialize
413     {
414         nmem = nmem_create();
415         xmlSubstituteEntitiesDefault(1);
416         xmlLoadExtDtdDefaultValue = 1;
417     }
418     if (!doc)
419     {
420         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
421         exit(1);
422     }
423     if ((p = rindex(fname, '/')))
424     {
425         int len = p - fname;
426         strncpy(confdir, fname, len);
427         confdir[len] = '\0';
428     }
429     config = parse_config(xmlDocGetRootElement(doc));
430     xmlFreeDoc(doc);
431
432     if (config)
433         return 1;
434     else
435         return 0;
436 }
437
438
439 /*
440  * Local variables:
441  * c-basic-offset: 4
442  * indent-tabs-mode: nil
443  * End:
444  * vim: shiftwidth=4 tabstop=8 expandtab
445  */