1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2012 Index Data
3 * See the file LICENSE for details.
7 * \brief Retrieval utility
15 #include <yaz/retrieval.h>
16 #include <yaz/wrbuf.h>
17 #include <yaz/xmalloc.h>
19 #include <yaz/tpath.h>
20 #include <yaz/match_glob.h>
21 #include <yaz/proto.h>
22 #include <yaz/oid_db.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 */
62 Odr_oid *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 = (yaz_retrieval_t) xmalloc(sizeof(*p));
76 p->odr = odr_createmem(ODR_ENCODE);
77 p->nmem = odr_getmem(p->odr);
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_destroy(p->wr_error);
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 = (struct yaz_retrieval_elem *)
116 nmem_malloc(p->nmem, sizeof(*el));
121 el->backend_name = 0;
122 el->backend_syntax = 0;
126 for (attr = ptr->properties; attr; attr = attr->next)
128 if (!xmlStrcmp(attr->name, BAD_CAST "syntax") &&
129 attr->children && attr->children->type == XML_TEXT_NODE)
131 el->syntax = yaz_string_to_oid_odr(
134 (const char *) attr->children->content,
138 wrbuf_printf(p->wr_error, "Element <retrieval>: "
139 " unknown attribute value syntax='%s'",
140 (const char *) attr->children->content);
144 else if (!xmlStrcmp(attr->name, BAD_CAST "identifier") &&
145 attr->children && attr->children->type == XML_TEXT_NODE)
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)
151 nmem_strdup(p->nmem, (const char *) attr->children->content);
154 wrbuf_printf(p->wr_error, "Element <retrieval>: "
155 " expected attributes 'syntax', identifier' or "
156 "'name', got '%s'", attr->name);
163 wrbuf_printf(p->wr_error, "Missing 'syntax' attribute");
167 /* parsing backend element */
169 el->record_conv = 0; /* OK to have no 'backend' sub content */
170 for (ptr = ptr->children; ptr; ptr = ptr->next)
172 if (ptr->type != XML_ELEMENT_NODE)
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);
185 wrbuf_printf(p->wr_error, "Element <retrieval>: "
186 "only one <backend> allowed");
187 yaz_record_conv_destroy(el->record_conv);
191 /* parsing attributees */
192 struct _xmlAttr *attr;
193 for (attr = ptr->properties; attr; attr = attr->next){
195 if (!xmlStrcmp(attr->name, BAD_CAST "name")
197 && attr->children->type == XML_TEXT_NODE)
199 = nmem_strdup(p->nmem,
200 (const char *) attr->children->content);
202 else if (!xmlStrcmp(attr->name, BAD_CAST "syntax")
204 && attr->children->type == XML_TEXT_NODE){
206 = yaz_string_to_oid_odr(
209 (const char *) attr->children->content,
211 if (!el->backend_syntax){
212 wrbuf_printf(p->wr_error,
213 "Element <backend syntax='%s'>: "
214 "attribute 'syntax' has invalid "
216 attr->children->content,
217 attr->children->content);
222 wrbuf_printf(p->wr_error, "Element <backend>: expected "
223 "attributes 'syntax' or 'name, got '%s'",
230 /* parsing internal of record conv */
231 el->record_conv = yaz_record_conv_create();
233 yaz_record_conv_set_path(el->record_conv, p->path);
236 if (yaz_record_conv_configure(el->record_conv, ptr))
238 wrbuf_printf(p->wr_error, "%s",
239 yaz_record_conv_get_error(el->record_conv));
240 yaz_record_conv_destroy(el->record_conv);
247 p->list_p = &el->next;
251 int yaz_retrieval_configure(yaz_retrieval_t p, const xmlNode *ptr)
253 yaz_retrieval_reset(p);
255 if (ptr && ptr->type == XML_ELEMENT_NODE &&
256 !strcmp((const char *) ptr->name, "retrievalinfo"))
258 for (ptr = ptr->children; ptr; ptr = ptr->next)
260 if (ptr->type != XML_ELEMENT_NODE)
262 if (!strcmp((const char *) ptr->name, "retrieval"))
264 if (conf_retrieval(p, ptr))
269 wrbuf_printf(p->wr_error, "Element <retrievalinfo>: "
270 "expected element <retrieval>, got <%s>",
278 wrbuf_printf(p->wr_error, "Expected element <retrievalinfo>");
284 int yaz_retrieval_request(yaz_retrieval_t p,
285 const char *schema, Odr_oid *syntax,
286 const char **match_schema, Odr_oid **match_syntax,
287 yaz_record_conv_t *rc,
288 const char **backend_schema,
289 Odr_oid **backend_syntax)
291 struct yaz_retrieval_elem *el = p->list;
292 int syntax_matches = 0;
293 int schema_matches = 0;
295 wrbuf_rewind(p->wr_error);
298 for(; el; el = el->next)
307 if (el->name && yaz_match_glob(el->name, schema))
309 if (el->identifier && !strcmp(schema, el->identifier))
311 if (!el->name && !el->identifier)
315 if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
324 if (syntax_ok && schema_ok)
326 *match_syntax = el->syntax;
328 *match_schema = el->identifier;
333 if (el->backend_name)
334 *backend_schema = el->backend_name;
336 *backend_schema = el->name;
338 *backend_schema = schema;
342 if (el->backend_syntax)
343 *backend_syntax = el->backend_syntax;
345 *backend_syntax = el->syntax;
348 *rc = el->record_conv;
352 if (!syntax_matches && syntax)
354 char buf[OID_STR_MAX];
355 wrbuf_printf(p->wr_error, "%s", oid_oid_to_dotstring(syntax, buf));
359 wrbuf_printf(p->wr_error, "%s", schema);
365 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
367 return wrbuf_cstr(p->wr_error);
370 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
375 p->path = xstrdup(path);
383 * c-file-style: "Stroustrup"
384 * indent-tabs-mode: nil
386 * vim: shiftwidth=4 tabstop=8 expandtab