2 * Copyright (C) 2005-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: retrieval.c,v 1.2 2006-05-05 18:37:08 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>
24 #include <libxml/parser.h>
25 #include <libxml/tree.h>
26 #include <libxml/xinclude.h>
27 #include <libxslt/xsltutils.h>
28 #include <libxslt/transform.h>
30 /** \brief The internal structure for yaz_retrieval_t */
31 struct yaz_retrieval_struct {
32 /** \brief memory for configuration */
35 /** \brief string buffer for error messages */
38 /** \brief path for opening files */
41 /** \brief retrieval list */
42 struct yaz_retrieval_elem *list;
44 /** \brief last pointer in retrieval list */
45 struct yaz_retrieval_elem **list_p;
48 /** \brief information per 'retrieval' construct */
49 struct yaz_retrieval_elem {
50 /** \brief schema identifier */
51 const char *identifier;
52 /** \brief schema short-hand (such sa "dc") */
54 /** \brief record syntax */
56 /** \brief backend schema */
57 const char *backend_schema;
58 /** \brief backend syntax */
59 const char *backend_syntax;
61 /** \brief record conversion */
62 yaz_record_conv_t record_conv;
64 /** \breif next element in list */
65 struct yaz_retrieval_elem *next;
68 static void yaz_retrieval_reset(yaz_retrieval_t p);
70 yaz_retrieval_t yaz_retrieval_create()
72 yaz_retrieval_t p = xmalloc(sizeof(*p));
73 p->nmem = nmem_create();
74 p->wr_error = wrbuf_alloc();
77 yaz_retrieval_reset(p);
81 void yaz_retrieval_destroy(yaz_retrieval_t p)
85 yaz_retrieval_reset(p);
86 nmem_destroy(p->nmem);
87 wrbuf_free(p->wr_error, 1);
93 void yaz_retrieval_reset(yaz_retrieval_t p)
95 struct yaz_retrieval_elem *el = p->list;
96 for(; el; el = el->next)
97 yaz_record_conv_destroy(el->record_conv);
99 wrbuf_rewind(p->wr_error);
103 p->list_p = &p->list;
106 /** \brief parse retrieval XML config */
107 static int conf_retrieval(yaz_retrieval_t p, const xmlNode *ptr)
110 struct _xmlAttr *attr;
111 struct yaz_retrieval_elem *el = nmem_malloc(p->nmem, sizeof(*el));
116 el->backend_schema = 0;
117 el->backend_syntax = 0;
121 for (attr = ptr->properties; attr; attr = attr->next)
123 if (!xmlStrcmp(attr->name, BAD_CAST "syntax") &&
124 attr->children && attr->children->type == XML_TEXT_NODE)
126 nmem_strdup(p->nmem, (const char *) attr->children->content);
127 else if (!xmlStrcmp(attr->name, BAD_CAST "identifier") &&
128 attr->children && attr->children->type == XML_TEXT_NODE)
130 nmem_strdup(p->nmem, (const char *) attr->children->content);
131 else if (!xmlStrcmp(attr->name, BAD_CAST "schema") &&
132 attr->children && attr->children->type == XML_TEXT_NODE)
134 nmem_strdup(p->nmem, (const char *) attr->children->content);
135 else if (!xmlStrcmp(attr->name, BAD_CAST "backendschema") &&
136 attr->children && attr->children->type == XML_TEXT_NODE)
138 nmem_strdup(p->nmem, (const char *) attr->children->content);
139 else if (!xmlStrcmp(attr->name, BAD_CAST "backendsyntax") &&
140 attr->children && attr->children->type == XML_TEXT_NODE)
142 nmem_strdup(p->nmem, (const char *) attr->children->content);
145 wrbuf_printf(p->wr_error, "Bad attribute '%s'.", attr->name);
149 el->record_conv = yaz_record_conv_create();
151 yaz_record_conv_set_path(el->record_conv, p->path);
153 if (yaz_record_conv_configure(el->record_conv, ptr->children))
155 wrbuf_printf(p->wr_error, "%s",
156 yaz_record_conv_get_error(el->record_conv));
157 yaz_record_conv_destroy(el->record_conv);
162 p->list_p = &el->next;
166 int yaz_retrieval_configure(yaz_retrieval_t p, const void *ptr_v)
168 const xmlNode *ptr = ptr_v;
170 yaz_retrieval_reset(p);
172 if (ptr && ptr->type == XML_ELEMENT_NODE &&
173 !strcmp((const char *) ptr->name, "retrievalinfo"))
175 for (ptr = ptr->children; ptr; ptr = ptr->next)
177 if (ptr->type != XML_ELEMENT_NODE)
179 if (!strcmp((const char *) ptr->name, "retrieval"))
181 if (conf_retrieval(p, ptr))
186 wrbuf_printf(p->wr_error, "Bad element '%s'."
187 " Expected 'retrieval'", ptr->name);
194 wrbuf_printf(p->wr_error, "Missing 'retrievalinfo' element");
200 int yaz_retrieval_request(yaz_retrieval_t p, const char *schema,
201 const char *syntax, yaz_record_conv_t *rc)
203 wrbuf_rewind(p->wr_error);
204 wrbuf_printf(p->wr_error, "yaz_retrieval_request: not implemented");
208 const char *yaz_retrieval_get_error(yaz_retrieval_t p)
210 return wrbuf_buf(p->wr_error);
213 void yaz_retrieval_set_path(yaz_retrieval_t p, const char *path)
218 p->path = xstrdup(path);
226 * indent-tabs-mode: nil
228 * vim: shiftwidth=4 tabstop=8 expandtab