Use OID_STR_MAX for size of oid dot string
[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.9 2006-05-09 11:35:28 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     int syntax_matches = 0;
243     int schema_matches = 0;
244
245     wrbuf_rewind(p->wr_error);
246     if (!el)
247         return 0;
248     for(; el; el = el->next)
249     {
250         int schema_ok = 0;
251         int syntax_ok = 0;
252
253         if (schema && el->schema && !strcmp(schema, el->schema))
254             schema_ok = 1;
255         if (schema && el->identifier && !strcmp(schema, el->identifier))
256             schema_ok = 1;
257         if (!schema)
258             schema_ok = 1;
259         if (schema && !el->schema)
260             schema_ok = 1;
261         
262         if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
263             syntax_ok = 1;
264         if (!syntax)
265             syntax_ok = 1;
266
267         if (syntax_ok)
268             syntax_matches++;
269         if (schema_ok)
270             schema_matches++;
271         if (syntax_ok && schema_ok)
272         {
273             *match_syntax = el->syntax;
274             *match_schema = el->schema;
275             if (backend_schema)
276                 *backend_schema = el->backend_schema;
277             if (backend_syntax)
278                 *backend_syntax = el->backend_syntax;
279             if (rc)
280                 *rc = el->record_conv;
281             return 0;
282         }
283     }
284     if (!syntax_matches && syntax)
285     {
286         char buf[OID_STR_MAX];
287         wrbuf_printf(p->wr_error, "%s", oid_to_dotstring(syntax, buf));
288         return 2;
289     }
290     if (schema)
291         wrbuf_printf(p->wr_error, "%s", schema);
292     if (!schema_matches)
293         return 1;
294     return 3;
295 }
296
297 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
298 {
299     return wrbuf_buf(p->wr_error);
300 }
301
302 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
303 {
304     xfree(p->path);
305     p->path = 0;
306     if (path)
307         p->path = xstrdup(path);
308 }
309
310 #endif
311
312 /*
313  * Local variables:
314  * c-basic-offset: 4
315  * indent-tabs-mode: nil
316  * End:
317  * vim: shiftwidth=4 tabstop=8 expandtab
318  */
319