7f0b17d2b27ebf7daabfaf037d86cecc1716586f
[yaz-moved-to-github.git] / src / retrieval.c
1 /*
2  * Copyright (C) 2005-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: retrieval.c,v 1.18 2007-04-12 13:52:57 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 #include <yaz/oid_db.h>
24
25 #if YAZ_HAVE_XML2
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxml/xinclude.h>
29
30 /** \brief The internal structure for yaz_retrieval_t */
31 struct yaz_retrieval_struct {
32     /** \brief ODR memory for configuration */
33     ODR odr;
34
35     /** \brief odr's NMEM memory (odr->mem) */
36     NMEM nmem;
37
38     /** \brief string buffer for error messages */
39     WRBUF wr_error;
40
41     /** \brief path for opening files  */
42     char *path;
43
44     /** \brief retrieval list */
45     struct yaz_retrieval_elem *list;
46
47     /** \brief last pointer in retrieval list */
48     struct yaz_retrieval_elem **list_p;
49 };
50
51 /** \brief information per 'retrieval' construct */
52 struct yaz_retrieval_elem {
53     /** \brief schema identifier */
54     const char *identifier;
55     /** \brief schema name , short-hand such as "dc" */
56     const char *name;
57     /** \brief record syntax */
58     int *syntax;
59
60     /** \brief backend name */
61     const char *backend_name;
62     /** \brief backend syntax */
63     int *backend_syntax;
64
65     /** \brief record conversion */
66     yaz_record_conv_t record_conv;
67
68     /** \brief next element in list */
69     struct yaz_retrieval_elem *next;
70 };
71
72 static void yaz_retrieval_reset(yaz_retrieval_t p);
73
74 yaz_retrieval_t yaz_retrieval_create()
75 {
76     yaz_retrieval_t p = xmalloc(sizeof(*p));
77     p->odr = odr_createmem(ODR_ENCODE);
78     p->nmem = odr_getmem(p->odr);
79     p->wr_error = wrbuf_alloc();
80     p->list = 0;
81     p->path = 0;
82     yaz_retrieval_reset(p);
83     return p;
84 }
85
86 void yaz_retrieval_destroy(yaz_retrieval_t p)
87 {
88     if (p)
89     {
90         yaz_retrieval_reset(p);
91         odr_destroy(p->odr);
92         wrbuf_destroy(p->wr_error);
93         xfree(p->path);
94         xfree(p);
95     }
96 }
97
98 void yaz_retrieval_reset(yaz_retrieval_t p)
99 {
100     struct yaz_retrieval_elem *el = p->list;
101     for(; el; el = el->next)
102         yaz_record_conv_destroy(el->record_conv);
103
104     wrbuf_rewind(p->wr_error);
105     odr_reset(p->odr);
106
107     p->list = 0;
108     p->list_p = &p->list;
109 }
110
111 /** \brief parse retrieval XML config */
112 static int conf_retrieval(yaz_retrieval_t p, const xmlNode *ptr)
113 {
114
115     struct _xmlAttr *attr;
116     struct yaz_retrieval_elem *el = nmem_malloc(p->nmem, sizeof(*el));
117
118     el->syntax = 0;
119     el->identifier = 0;
120     el->name = 0;
121     el->backend_name = 0;
122     el->backend_syntax = 0;
123
124     el->next = 0;
125
126     for (attr = ptr->properties; attr; attr = attr->next)
127     {
128         if (!xmlStrcmp(attr->name, BAD_CAST "syntax") &&
129             attr->children && attr->children->type == XML_TEXT_NODE)
130         {
131             el->syntax = yaz_string_to_oid_odr(
132                 yaz_oid_std(),
133                 CLASS_RECSYN,
134                 (const char *) attr->children->content,
135                 p->odr);
136             if (!el->syntax)
137             {
138                 wrbuf_printf(p->wr_error, "Element <retrieval>: "
139                              " unknown attribute value syntax='%s'",
140                              (const char *) attr->children->content);
141                 return -1;
142             }
143         }
144         else if (!xmlStrcmp(attr->name, BAD_CAST "identifier") &&
145                  attr->children && attr->children->type == XML_TEXT_NODE)
146             el->identifier =
147                 nmem_strdup(p->nmem, (const char *) attr->children->content);
148         else if (!xmlStrcmp(attr->name, BAD_CAST "name") &&
149                  attr->children && attr->children->type == XML_TEXT_NODE)
150             el->name = 
151                 nmem_strdup(p->nmem, (const char *) attr->children->content);
152         else
153         {
154             wrbuf_printf(p->wr_error, "Element <retrieval>: "
155                          " expected attributes 'syntax', identifier' or "
156                          "'name', got '%s'", attr->name);
157             return -1;
158         }
159     }
160
161     if (!el->syntax)
162     {
163         wrbuf_printf(p->wr_error, "Missing 'syntax' attribute");
164         return -1;
165     }
166
167     /* parsing backend element */
168
169     el->record_conv = 0; /* OK to have no 'backend' sub content */
170     for (ptr = ptr->children; ptr; ptr = ptr->next)
171     {
172         if (ptr->type != XML_ELEMENT_NODE)
173             continue;
174         if (strcmp((const char *) ptr->name, "backend")){
175             wrbuf_printf(p->wr_error, "Element <retrieval>: expected"
176                          " zero or one element <backend>, got <%s>",
177                          (const char *) ptr->name);
178             return -1;
179         }
180
181         else {
182
183             /* parsing attributees */
184             struct _xmlAttr *attr;
185             for (attr = ptr->properties; attr; attr = attr->next){
186             
187                 if (!xmlStrcmp(attr->name, BAD_CAST "name") 
188                          && attr->children 
189                          && attr->children->type == XML_TEXT_NODE)
190                     el->backend_name 
191                         = nmem_strdup(p->nmem, 
192                                       (const char *) attr->children->content);
193
194                 else if (!xmlStrcmp(attr->name, BAD_CAST "syntax") 
195                          && attr->children 
196                          && attr->children->type == XML_TEXT_NODE){
197                     el->backend_syntax 
198                         = yaz_string_to_oid_odr(
199                             yaz_oid_std(),
200                             CLASS_RECSYN,
201                             (const char *) attr->children->content,
202                             p->odr);
203                     if (!el->backend_syntax){
204                         wrbuf_printf(p->wr_error, 
205                                      "Element <backend syntax='%s'>: "
206                                      "attribute 'syntax' has invalid "
207                                      "value '%s'", 
208                                      attr->children->content,
209                                      attr->children->content);
210                         return -1;
211                     } 
212                 }
213                 else {
214                     wrbuf_printf(p->wr_error, "Element <backend>: expected "
215                                  "attributes 'syntax' or 'name, got '%s'", 
216                                  attr->name);
217                     return -1;
218                 }
219             }
220           
221  
222             /* parsing internal of record conv */
223             el->record_conv = yaz_record_conv_create();
224             
225             yaz_record_conv_set_path(el->record_conv, p->path);
226
227         
228             if (yaz_record_conv_configure(el->record_conv, ptr))
229             {
230                 wrbuf_printf(p->wr_error, "%s",
231                              yaz_record_conv_get_error(el->record_conv));
232                 yaz_record_conv_destroy(el->record_conv);
233                 return -1;
234             }
235         }
236     }
237     
238     *p->list_p = el;
239     p->list_p = &el->next;
240     return 0;
241 }
242
243 int yaz_retrieval_configure(yaz_retrieval_t p, const xmlNode *ptr)
244 {
245     yaz_retrieval_reset(p);
246
247     if (ptr && ptr->type == XML_ELEMENT_NODE &&
248         !strcmp((const char *) ptr->name, "retrievalinfo"))
249     {
250         for (ptr = ptr->children; ptr; ptr = ptr->next)
251         {
252             if (ptr->type != XML_ELEMENT_NODE)
253                 continue;
254             if (!strcmp((const char *) ptr->name, "retrieval"))
255             {
256                 if (conf_retrieval(p, ptr))
257                     return -1;
258             }
259             else
260             {
261                 wrbuf_printf(p->wr_error, "Element <retrievalinfo>: "
262                              "expected element <retrieval>, got <%s>", 
263                              ptr->name);
264                 return -1;
265             }
266         }
267     }
268     else
269     {
270         wrbuf_printf(p->wr_error, "Expected element <retrievalinfo>");
271         return -1;
272     }
273     return 0;
274 }
275
276 int yaz_retrieval_request(yaz_retrieval_t p,
277                           const char *schema, int *syntax,
278                           const char **match_schema, int **match_syntax,
279                           yaz_record_conv_t *rc,
280                           const char **backend_schema,
281                           int **backend_syntax)
282 {
283     struct yaz_retrieval_elem *el = p->list;
284     int syntax_matches = 0;
285     int schema_matches = 0;
286
287     wrbuf_rewind(p->wr_error);
288     if (!el)
289         return 0;
290     for(; el; el = el->next)
291     {
292         int schema_ok = 0;
293         int syntax_ok = 0;
294
295         if (!schema)
296             schema_ok = 1;
297         else
298         {
299             if (el->name && !strcmp(schema, el->name))
300                 schema_ok = 1;
301             if (el->identifier && !strcmp(schema, el->identifier))
302                 schema_ok = 1;
303             if (!el->name && !el->identifier)
304                 schema_ok = 1;
305         }
306         
307         if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
308             syntax_ok = 1;
309         if (!syntax)
310             syntax_ok = 1;
311
312         if (syntax_ok)
313             syntax_matches++;
314         if (schema_ok)
315             schema_matches++;
316         if (syntax_ok && schema_ok)
317         {
318             *match_syntax = el->syntax;
319             if (el->identifier)
320                 *match_schema = el->identifier;
321             else
322                 *match_schema = 0;
323             if (backend_schema)
324             {
325                 if (el->backend_name)
326                     *backend_schema = el->backend_name;
327                 else if (el->name)
328                     *backend_schema = el->name;                    
329                 else
330                     *backend_schema = schema;
331             }
332             if (backend_syntax)
333             {
334                 if (el->backend_syntax)
335                     *backend_syntax = el->backend_syntax;
336                 else
337                     *backend_syntax = el->syntax;
338             }
339             if (rc)
340                 *rc = el->record_conv;
341             return 0;
342         }
343     }
344     if (!syntax_matches && syntax)
345     {
346         char buf[OID_STR_MAX];
347         wrbuf_printf(p->wr_error, "%s", oid_to_dotstring(syntax, buf));
348         return 2;
349     }
350     if (schema)
351         wrbuf_printf(p->wr_error, "%s", schema);
352     if (!schema_matches)
353         return 1;
354     return 3;
355 }
356
357 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
358 {
359     return wrbuf_cstr(p->wr_error);
360 }
361
362 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
363 {
364     xfree(p->path);
365     p->path = 0;
366     if (path)
367         p->path = xstrdup(path);
368 }
369
370 #endif
371
372 /*
373  * Local variables:
374  * c-basic-offset: 4
375  * indent-tabs-mode: nil
376  * End:
377  * vim: shiftwidth=4 tabstop=8 expandtab
378  */
379