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