2 * Copyright (C) 2005-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: retrieval.c,v 1.16 2007-02-04 02:24:56 quinn 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)
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);
180 /* parsing attributees */
181 struct _xmlAttr *attr;
182 for (attr = ptr->properties; attr; attr = attr->next){
184 if (!xmlStrcmp(attr->name, BAD_CAST "name")
186 && attr->children->type == XML_TEXT_NODE)
188 = nmem_strdup(p->nmem,
189 (const char *) attr->children->content);
191 else if (!xmlStrcmp(attr->name, BAD_CAST "syntax")
193 && attr->children->type == XML_TEXT_NODE){
195 = yaz_str_to_z3950oid(p->odr, CLASS_RECSYN,
196 (const char *) attr->children->content);
198 if (!el->backend_syntax){
199 wrbuf_printf(p->wr_error,
200 "Element <backend syntax='%s'>: "
201 "attribute 'syntax' has invalid "
203 attr->children->content,
204 attr->children->content);
209 wrbuf_printf(p->wr_error, "Element <backend>: expected "
210 "attributes 'syntax' or 'name, got '%s'",
217 /* parsing internal of record conv */
218 el->record_conv = yaz_record_conv_create();
220 yaz_record_conv_set_path(el->record_conv, p->path);
223 if (yaz_record_conv_configure(el->record_conv, ptr))
225 wrbuf_printf(p->wr_error, "%s",
226 yaz_record_conv_get_error(el->record_conv));
227 yaz_record_conv_destroy(el->record_conv);
234 p->list_p = &el->next;
238 int yaz_retrieval_configure(yaz_retrieval_t p, const xmlNode *ptr)
240 yaz_retrieval_reset(p);
242 if (ptr && ptr->type == XML_ELEMENT_NODE &&
243 !strcmp((const char *) ptr->name, "retrievalinfo"))
245 for (ptr = ptr->children; ptr; ptr = ptr->next)
247 if (ptr->type != XML_ELEMENT_NODE)
249 if (!strcmp((const char *) ptr->name, "retrieval"))
251 if (conf_retrieval(p, ptr))
256 wrbuf_printf(p->wr_error, "Element <retrievalinfo>: "
257 "expected element <retrieval>, got <%s>",
265 wrbuf_printf(p->wr_error, "Expected element <retrievalinfo>");
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)
278 struct yaz_retrieval_elem *el = p->list;
279 int syntax_matches = 0;
280 int schema_matches = 0;
282 wrbuf_rewind(p->wr_error);
285 for(; el; el = el->next)
294 if (el->name && !strcmp(schema, el->name))
296 if (el->identifier && !strcmp(schema, el->identifier))
298 if (!el->name && !el->identifier)
302 if (syntax && el->syntax && !oid_oidcmp(syntax, el->syntax))
311 if (syntax_ok && schema_ok)
313 *match_syntax = el->syntax;
315 *match_schema = el->identifier;
320 if (el->backend_name)
321 *backend_schema = el->backend_name;
323 *backend_schema = el->name;
325 *backend_schema = schema;
329 if (el->backend_syntax)
330 *backend_syntax = el->backend_syntax;
332 *backend_syntax = el->syntax;
335 *rc = el->record_conv;
339 if (!syntax_matches && syntax)
341 char buf[OID_STR_MAX];
342 wrbuf_printf(p->wr_error, "%s", oid_to_dotstring(syntax, buf));
346 wrbuf_printf(p->wr_error, "%s", schema);
352 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
354 return wrbuf_buf(p->wr_error);
357 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
362 p->path = xstrdup(path);
370 * indent-tabs-mode: nil
372 * vim: shiftwidth=4 tabstop=8 expandtab