Fixed bug #830: pkg-config support. YAZ installs yaz.pc for Debian
[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.15 2007-01-03 08:42:15 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_free(p->wr_error, 1);
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             && 0 != strcmp((const char *) ptr->name, "backend")){
171             wrbuf_printf(p->wr_error, "Element <retrieval>: expected"
172                          " zero or one element <backend>, got <%s>",
173                          (const char *) ptr->name);
174             return -1;
175         }
176
177         else {
178
179             /* parsing attributees */
180             struct _xmlAttr *attr;
181             for (attr = ptr->properties; attr; attr = attr->next){
182             
183                 if (!xmlStrcmp(attr->name, BAD_CAST "name") 
184                          && attr->children 
185                          && attr->children->type == XML_TEXT_NODE)
186                     el->backend_name 
187                         = nmem_strdup(p->nmem, 
188                                       (const char *) attr->children->content);
189
190                 else if (!xmlStrcmp(attr->name, BAD_CAST "syntax") 
191                          && attr->children 
192                          && attr->children->type == XML_TEXT_NODE){
193                     el->backend_syntax 
194                     = yaz_str_to_z3950oid(p->odr, CLASS_RECSYN,
195                        (const char *) attr->children->content);
196                     
197                     if (!el->backend_syntax){
198                         wrbuf_printf(p->wr_error, 
199                                      "Element <backend syntax='%s'>: "
200                                      "attribute 'syntax' has invalid "
201                                      "value '%s'", 
202                                      attr->children->content,
203                                      attr->children->content);
204                         return -1;
205                     } 
206                 }
207                 else {
208                     wrbuf_printf(p->wr_error, "Element <backend>: expected "
209                                  "attributes 'syntax' or 'name, got '%s'", 
210                                  attr->name);
211                     return -1;
212                 }
213             }
214           
215  
216             /* parsing internal of record conv */
217             el->record_conv = yaz_record_conv_create();
218             
219             yaz_record_conv_set_path(el->record_conv, p->path);
220
221         
222             if (yaz_record_conv_configure(el->record_conv, ptr))
223             {
224                 wrbuf_printf(p->wr_error, "%s",
225                              yaz_record_conv_get_error(el->record_conv));
226                 yaz_record_conv_destroy(el->record_conv);
227                 return -1;
228             }
229         }
230     }
231     
232     *p->list_p = el;
233     p->list_p = &el->next;
234     return 0;
235 }
236
237 int yaz_retrieval_configure(yaz_retrieval_t p, const xmlNode *ptr)
238 {
239     yaz_retrieval_reset(p);
240
241     if (ptr && ptr->type == XML_ELEMENT_NODE &&
242         !strcmp((const char *) ptr->name, "retrievalinfo"))
243     {
244         for (ptr = ptr->children; ptr; ptr = ptr->next)
245         {
246             if (ptr->type != XML_ELEMENT_NODE)
247                 continue;
248             if (!strcmp((const char *) ptr->name, "retrieval"))
249             {
250                 if (conf_retrieval(p, ptr))
251                     return -1;
252             }
253             else
254             {
255                 wrbuf_printf(p->wr_error, "Element <retrievalinfo>: "
256                              "expected element <retrieval>, got <%s>", 
257                              ptr->name);
258                 return -1;
259             }
260         }
261     }
262     else
263     {
264         wrbuf_printf(p->wr_error, "Expected element <retrievalinfo>");
265         return -1;
266     }
267     return 0;
268 }
269
270 int yaz_retrieval_request(yaz_retrieval_t p,
271                           const char *schema, int *syntax,
272                           const char **match_schema, int **match_syntax,
273                           yaz_record_conv_t *rc,
274                           const char **backend_schema,
275                           int **backend_syntax)
276 {
277     struct yaz_retrieval_elem *el = p->list;
278     int syntax_matches = 0;
279     int schema_matches = 0;
280
281     wrbuf_rewind(p->wr_error);
282     if (!el)
283         return 0;
284     for(; el; el = el->next)
285     {
286         int schema_ok = 0;
287         int syntax_ok = 0;
288
289         if (!schema)
290             schema_ok = 1;
291         else
292         {
293             if (el->name && !strcmp(schema, el->name))
294                 schema_ok = 1;
295             if (el->identifier && !strcmp(schema, el->identifier))
296                 schema_ok = 1;
297             if (!el->name && !el->identifier)
298                 schema_ok = 1;
299         }
300         
301         if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
302             syntax_ok = 1;
303         if (!syntax)
304             syntax_ok = 1;
305
306         if (syntax_ok)
307             syntax_matches++;
308         if (schema_ok)
309             schema_matches++;
310         if (syntax_ok && schema_ok)
311         {
312             *match_syntax = el->syntax;
313             if (el->identifier)
314                 *match_schema = el->identifier;
315             else
316                 *match_schema = 0;
317             if (backend_schema)
318             {
319                 if (el->backend_name)
320                     *backend_schema = el->backend_name;
321                 else if (el->name)
322                     *backend_schema = el->name;                    
323                 else
324                     *backend_schema = schema;
325             }
326             if (backend_syntax)
327             {
328                 if (el->backend_syntax)
329                     *backend_syntax = el->backend_syntax;
330                 else
331                     *backend_syntax = el->syntax;
332             }
333             if (rc)
334                 *rc = el->record_conv;
335             return 0;
336         }
337     }
338     if (!syntax_matches && syntax)
339     {
340         char buf[OID_STR_MAX];
341         wrbuf_printf(p->wr_error, "%s", oid_to_dotstring(syntax, buf));
342         return 2;
343     }
344     if (schema)
345         wrbuf_printf(p->wr_error, "%s", schema);
346     if (!schema_matches)
347         return 1;
348     return 3;
349 }
350
351 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
352 {
353     return wrbuf_buf(p->wr_error);
354 }
355
356 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
357 {
358     xfree(p->path);
359     p->path = 0;
360     if (path)
361         p->path = xstrdup(path);
362 }
363
364 #endif
365
366 /*
367  * Local variables:
368  * c-basic-offset: 4
369  * indent-tabs-mode: nil
370  * End:
371  * vim: shiftwidth=4 tabstop=8 expandtab
372  */
373