LibEXSLT support. New configure option --with-exslt.
[yaz-moved-to-github.git] / src / retrieval.c
1 /*
2  * Copyright (C) 2005-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: retrieval.c,v 1.5 2006-05-08 10:16:47 adam Exp $
6  */
7 /**
8  * \file retrieval.c
9  * \brief Retrieval utility
10  */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <string.h>
17 #include <yaz/retrieval.h>
18 #include <yaz/wrbuf.h>
19 #include <yaz/xmalloc.h>
20 #include <yaz/nmem.h>
21 #include <yaz/tpath.h>
22 #include <yaz/proto.h>
23
24 #if HAVE_XML2
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27 #include <libxml/xinclude.h>
28
29 /** \brief The internal structure for yaz_retrieval_t */
30 struct yaz_retrieval_struct {
31     /** \brief ODR memory for configuration */
32     ODR odr;
33
34     /** \brief odr's NMEM memory (odr->mem) */
35     NMEM nmem;
36
37     /** \brief string buffer for error messages */
38     WRBUF wr_error;
39
40     /** \brief path for opening files  */
41     char *path;
42
43     /** \brief retrieval list */
44     struct yaz_retrieval_elem *list;
45
46     /** \brief last pointer in retrieval list */
47     struct yaz_retrieval_elem **list_p;
48 };
49
50 /** \brief information per 'retrieval' construct */
51 struct yaz_retrieval_elem {
52     /** \brief schema identifier */
53     const char *identifier;
54     /** \brief schema short-hand (such sa "dc") */
55     const char *schema;
56     /** \brief record syntax */
57     int *syntax;
58     /** \brief backend schema */
59     const char *backend_schema;
60     /** \brief backend syntax */
61     int *backend_syntax;
62
63     /** \brief record conversion */
64     yaz_record_conv_t record_conv;
65
66     /** \brief next element in list */
67     struct yaz_retrieval_elem *next;
68 };
69
70 static void yaz_retrieval_reset(yaz_retrieval_t p);
71
72 yaz_retrieval_t yaz_retrieval_create()
73 {
74     yaz_retrieval_t p = xmalloc(sizeof(*p));
75     p->odr = odr_createmem(ODR_ENCODE);
76     p->nmem = p->odr->mem;
77     p->wr_error = wrbuf_alloc();
78     p->list = 0;
79     p->path = 0;
80     yaz_retrieval_reset(p);
81     return p;
82 }
83
84 void yaz_retrieval_destroy(yaz_retrieval_t p)
85 {
86     if (p)
87     {
88         yaz_retrieval_reset(p);
89         odr_destroy(p->odr);
90         wrbuf_free(p->wr_error, 1);
91         xfree(p->path);
92         xfree(p);
93     }
94 }
95
96 void yaz_retrieval_reset(yaz_retrieval_t p)
97 {
98     struct yaz_retrieval_elem *el = p->list;
99     for(; el; el = el->next)
100         yaz_record_conv_destroy(el->record_conv);
101
102     wrbuf_rewind(p->wr_error);
103     odr_reset(p->odr);
104
105     p->list = 0;
106     p->list_p = &p->list;
107 }
108
109 /** \brief parse retrieval XML config */
110 static int conf_retrieval(yaz_retrieval_t p, const xmlNode *ptr)
111 {
112
113     struct _xmlAttr *attr;
114     struct yaz_retrieval_elem *el = nmem_malloc(p->nmem, sizeof(*el));
115
116     el->syntax = 0;
117     el->identifier = 0;
118     el->schema = 0;
119     el->backend_schema = 0;
120     el->backend_syntax = 0;
121
122     el->next = 0;
123
124     for (attr = ptr->properties; attr; attr = attr->next)
125     {
126         if (!xmlStrcmp(attr->name, BAD_CAST "syntax") &&
127             attr->children && attr->children->type == XML_TEXT_NODE)
128         {
129             el->syntax = yaz_str_to_z3950oid(
130                 p->odr, CLASS_RECSYN,
131                 (const char *) attr->children->content);
132             if (!el->syntax)
133             {
134                 wrbuf_printf(p->wr_error, "Bad syntax '%s'",
135                              (const char *) attr->children->content);
136                 return -1;
137             }
138         }
139         else if (!xmlStrcmp(attr->name, BAD_CAST "identifier") &&
140             attr->children && attr->children->type == XML_TEXT_NODE)
141             el->identifier =
142                 nmem_strdup(p->nmem, (const char *) attr->children->content);
143         else if (!xmlStrcmp(attr->name, BAD_CAST "schema") &&
144                  attr->children && attr->children->type == XML_TEXT_NODE)
145             el->schema = 
146                 nmem_strdup(p->nmem, (const char *) attr->children->content);
147         else if (!xmlStrcmp(attr->name, BAD_CAST "backendschema") &&
148                  attr->children && attr->children->type == XML_TEXT_NODE)
149             el->backend_schema = 
150                 nmem_strdup(p->nmem, (const char *) attr->children->content);
151         else if (!xmlStrcmp(attr->name, BAD_CAST "backendsyntax") &&
152                  attr->children && attr->children->type == XML_TEXT_NODE)
153         {
154             el->backend_syntax = yaz_str_to_z3950oid(
155                 p->odr, CLASS_RECSYN,
156                 (const char *) attr->children->content);
157             if (!el->backend_syntax)
158             {
159                 wrbuf_printf(p->wr_error, "Bad backendsyntax '%s'",
160                              (const char *) attr->children->content);
161                 return -1;
162             }
163         }
164         else
165         {
166             wrbuf_printf(p->wr_error, "Bad attribute '%s'.", attr->name);
167             return -1;
168         }
169     }
170     if (!el->syntax)
171     {
172         wrbuf_printf(p->wr_error, "Missing 'syntax' attribute");
173         return -1;
174     }
175
176     el->record_conv = 0; /* OK to have no 'convert' sub content */
177     for (ptr = ptr->children; ptr; ptr = ptr->next)
178     {
179         if (ptr->type == XML_ELEMENT_NODE)
180         {
181             el->record_conv = yaz_record_conv_create();
182             
183             yaz_record_conv_set_path(el->record_conv, p->path);
184         
185             if (yaz_record_conv_configure(el->record_conv, ptr))
186             {
187                 wrbuf_printf(p->wr_error, "%s",
188                              yaz_record_conv_get_error(el->record_conv));
189                 yaz_record_conv_destroy(el->record_conv);
190                 return -1;
191             }
192         }
193     }
194     
195     *p->list_p = el;
196     p->list_p = &el->next;
197     return 0;
198 }
199
200 int yaz_retrieval_configure(yaz_retrieval_t p, const void *ptr_v)
201 {
202     const xmlNode *ptr = ptr_v; 
203
204     yaz_retrieval_reset(p);
205
206     if (ptr && ptr->type == XML_ELEMENT_NODE &&
207         !strcmp((const char *) ptr->name, "retrievalinfo"))
208     {
209         for (ptr = ptr->children; ptr; ptr = ptr->next)
210         {
211             if (ptr->type != XML_ELEMENT_NODE)
212                 continue;
213             if (!strcmp((const char *) ptr->name, "retrieval"))
214             {
215                 if (conf_retrieval(p, ptr))
216                     return -1;
217             }
218             else
219             {
220                 wrbuf_printf(p->wr_error, "Bad element '%s'."
221                              " Expected 'retrieval'", ptr->name);
222                 return -1;
223             }
224         }
225     }
226     else
227     {
228         wrbuf_printf(p->wr_error, "Missing 'retrievalinfo' element");
229         return -1;
230     }
231     return 0;
232 }
233
234 int yaz_retrieval_request(yaz_retrieval_t p,
235                           const char *schema, int *syntax,
236                           const char **match_schema, int **match_syntax,
237                           yaz_record_conv_t *rc,
238                           const char **backend_schema,
239                           int **backend_syntax)
240 {
241     struct yaz_retrieval_elem *el = p->list;
242
243     int syntax_matches = 0;
244     int schema_matches = 0;
245     for(; el; el = el->next)
246     {
247         int schema_ok = 0;
248         int syntax_ok = 0;
249
250         if (schema && el->schema && !strcmp(schema, el->schema))
251             schema_ok = 1;
252         if (schema && el->identifier && !strcmp(schema, el->identifier))
253             schema_ok = 1;
254         if (!schema)
255             schema_ok = 1;
256         
257         if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
258             syntax_ok = 1;
259         if (!syntax)
260             syntax_ok = 1;
261
262         if (syntax_ok)
263             syntax_matches++;
264         if (schema_ok)
265             schema_matches++;
266         if (syntax_ok && schema_ok)
267         {
268             *match_syntax = el->syntax;
269             *match_schema = el->schema;
270             if (backend_schema)
271                 *backend_schema = el->backend_schema;
272             if (backend_syntax)
273                 *backend_syntax = el->backend_syntax;
274             if (rc)
275                 *rc = el->record_conv;
276             return 0;
277         }
278     }
279     if (syntax_matches && !schema_matches)
280         return 1;
281     if (!syntax_matches && schema_matches)
282         return 2;
283     if (!syntax_matches && !schema_matches)
284         return 3;
285     return 4;
286 }
287
288 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
289 {
290     return wrbuf_buf(p->wr_error);
291 }
292
293 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
294 {
295     xfree(p->path);
296     p->path = 0;
297     if (path)
298         p->path = xstrdup(path);
299 }
300
301 #endif
302
303 /*
304  * Local variables:
305  * c-basic-offset: 4
306  * indent-tabs-mode: nil
307  * End:
308  * vim: shiftwidth=4 tabstop=8 expandtab
309  */
310