2 * Copyright (C) 2005-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: retrieval.c,v 1.15 2007-01-03 08:42:15 adam Exp $
9 * \brief Retrieval utility
17 #include <yaz/retrieval.h>
18 #include <yaz/wrbuf.h>
19 #include <yaz/xmalloc.h>
21 #include <yaz/tpath.h>
22 #include <yaz/proto.h>
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
27 #include <libxml/xinclude.h>
29 /** \brief The internal structure for yaz_retrieval_t */
30 struct yaz_retrieval_struct {
31 /** \brief ODR memory for configuration */
34 /** \brief odr's NMEM memory (odr->mem) */
37 /** \brief string buffer for error messages */
40 /** \brief path for opening files */
43 /** \brief retrieval list */
44 struct yaz_retrieval_elem *list;
46 /** \brief last pointer in retrieval list */
47 struct yaz_retrieval_elem **list_p;
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" */
56 /** \brief record syntax */
59 /** \brief backend name */
60 const char *backend_name;
61 /** \brief backend syntax */
64 /** \brief record conversion */
65 yaz_record_conv_t record_conv;
67 /** \brief next element in list */
68 struct yaz_retrieval_elem *next;
71 static void yaz_retrieval_reset(yaz_retrieval_t p);
73 yaz_retrieval_t yaz_retrieval_create()
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();
81 yaz_retrieval_reset(p);
85 void yaz_retrieval_destroy(yaz_retrieval_t p)
89 yaz_retrieval_reset(p);
91 wrbuf_free(p->wr_error, 1);
97 void yaz_retrieval_reset(yaz_retrieval_t p)
99 struct yaz_retrieval_elem *el = p->list;
100 for(; el; el = el->next)
101 yaz_record_conv_destroy(el->record_conv);
103 wrbuf_rewind(p->wr_error);
107 p->list_p = &p->list;
110 /** \brief parse retrieval XML config */
111 static int conf_retrieval(yaz_retrieval_t p, const xmlNode *ptr)
114 struct _xmlAttr *attr;
115 struct yaz_retrieval_elem *el = nmem_malloc(p->nmem, sizeof(*el));
120 el->backend_name = 0;
121 el->backend_syntax = 0;
125 for (attr = ptr->properties; attr; attr = attr->next)
127 if (!xmlStrcmp(attr->name, BAD_CAST "syntax") &&
128 attr->children && attr->children->type == XML_TEXT_NODE)
130 el->syntax = yaz_str_to_z3950oid(
131 p->odr, CLASS_RECSYN,
132 (const char *) attr->children->content);
135 wrbuf_printf(p->wr_error, "Element <retrieval>: "
136 " unknown attribute value syntax='%s'",
137 (const char *) attr->children->content);
141 else if (!xmlStrcmp(attr->name, BAD_CAST "identifier") &&
142 attr->children && attr->children->type == XML_TEXT_NODE)
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)
148 nmem_strdup(p->nmem, (const char *) attr->children->content);
151 wrbuf_printf(p->wr_error, "Element <retrieval>: "
152 " expected attributes 'syntax', identifier' or "
153 "'name', got '%s'", attr->name);
160 wrbuf_printf(p->wr_error, "Missing 'syntax' attribute");
164 /* parsing backend element */
166 el->record_conv = 0; /* OK to have no 'backend' sub content */
167 for (ptr = ptr->children; ptr; ptr = ptr->next)
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);
179 /* parsing attributees */
180 struct _xmlAttr *attr;
181 for (attr = ptr->properties; attr; attr = attr->next){
183 if (!xmlStrcmp(attr->name, BAD_CAST "name")
185 && attr->children->type == XML_TEXT_NODE)
187 = nmem_strdup(p->nmem,
188 (const char *) attr->children->content);
190 else if (!xmlStrcmp(attr->name, BAD_CAST "syntax")
192 && attr->children->type == XML_TEXT_NODE){
194 = yaz_str_to_z3950oid(p->odr, CLASS_RECSYN,
195 (const char *) attr->children->content);
197 if (!el->backend_syntax){
198 wrbuf_printf(p->wr_error,
199 "Element <backend syntax='%s'>: "
200 "attribute 'syntax' has invalid "
202 attr->children->content,
203 attr->children->content);
208 wrbuf_printf(p->wr_error, "Element <backend>: expected "
209 "attributes 'syntax' or 'name, got '%s'",
216 /* parsing internal of record conv */
217 el->record_conv = yaz_record_conv_create();
219 yaz_record_conv_set_path(el->record_conv, p->path);
222 if (yaz_record_conv_configure(el->record_conv, ptr))
224 wrbuf_printf(p->wr_error, "%s",
225 yaz_record_conv_get_error(el->record_conv));
226 yaz_record_conv_destroy(el->record_conv);
233 p->list_p = &el->next;
237 int yaz_retrieval_configure(yaz_retrieval_t p, const xmlNode *ptr)
239 yaz_retrieval_reset(p);
241 if (ptr && ptr->type == XML_ELEMENT_NODE &&
242 !strcmp((const char *) ptr->name, "retrievalinfo"))
244 for (ptr = ptr->children; ptr; ptr = ptr->next)
246 if (ptr->type != XML_ELEMENT_NODE)
248 if (!strcmp((const char *) ptr->name, "retrieval"))
250 if (conf_retrieval(p, ptr))
255 wrbuf_printf(p->wr_error, "Element <retrievalinfo>: "
256 "expected element <retrieval>, got <%s>",
264 wrbuf_printf(p->wr_error, "Expected element <retrievalinfo>");
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)
277 struct yaz_retrieval_elem *el = p->list;
278 int syntax_matches = 0;
279 int schema_matches = 0;
281 wrbuf_rewind(p->wr_error);
284 for(; el; el = el->next)
293 if (el->name && !strcmp(schema, el->name))
295 if (el->identifier && !strcmp(schema, el->identifier))
297 if (!el->name && !el->identifier)
301 if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
310 if (syntax_ok && schema_ok)
312 *match_syntax = el->syntax;
314 *match_schema = el->identifier;
319 if (el->backend_name)
320 *backend_schema = el->backend_name;
322 *backend_schema = el->name;
324 *backend_schema = schema;
328 if (el->backend_syntax)
329 *backend_syntax = el->backend_syntax;
331 *backend_syntax = el->syntax;
334 *rc = el->record_conv;
338 if (!syntax_matches && syntax)
340 char buf[OID_STR_MAX];
341 wrbuf_printf(p->wr_error, "%s", oid_to_dotstring(syntax, buf));
345 wrbuf_printf(p->wr_error, "%s", schema);
351 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
353 return wrbuf_buf(p->wr_error);
356 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
361 p->path = xstrdup(path);
369 * indent-tabs-mode: nil
371 * vim: shiftwidth=4 tabstop=8 expandtab