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