1 /* $Id: xslt.c,v 1.4 2005-05-01 07:17:46 adam Exp $
2 Copyright (C) 1995-2005
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include <yaz/diagbib1.h>
28 #include <libxml/xmlversion.h>
29 #include <libxml/tree.h>
30 #ifdef LIBXML_READER_ENABLED
31 #include <libxml/xmlreader.h>
33 #include <libxslt/transform.h>
35 #include <idzebra/util.h>
36 #include <idzebra/recctrl.h>
39 xsltStylesheetPtr stylesheet_xsp;
40 #ifdef LIBXML_READER_ENABLED
41 xmlTextReaderPtr reader;
48 #define ZEBRA_INDEX_NS "http://indexdata.dk/zebra/indexing/1"
49 #define ZEBRA_SCHEMA_IDENTITY_NS "http://indexdata.dk/zebra/identity/1"
50 static const char *zebra_index_ns = ZEBRA_INDEX_NS;
52 static void set_param_str(const char **params, const char *name,
53 const char *value, ODR odr)
55 char *quoted = odr_malloc(odr, 3 + strlen(value));
56 sprintf(quoted, "'%s'", value);
64 static void set_param_int(const char **params, const char *name,
67 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
70 sprintf(quoted, "'" ZINT_FORMAT "'", value);
77 static void *filter_init_xslt(Res res, RecType recType)
79 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
80 tinfo->stylesheet_xsp = 0;
81 #ifdef LIBXML_READER_ENABLED
85 tinfo->split_depth = 0;
86 tinfo->odr = odr_createmem(ODR_ENCODE);
90 static void *filter_init_xslt1(Res res, RecType recType)
92 struct filter_info *tinfo = (struct filter_info *)
93 filter_init_xslt(res, recType);
94 tinfo->split_depth = 1;
98 static void filter_config(void *clientData, Res res, const char *args)
100 struct filter_info *tinfo = clientData;
102 args = "default.xsl";
103 if (!tinfo->fname || strcmp(args, tinfo->fname))
105 /* different filename so must reread stylesheet */
107 tinfo->fname = xstrdup(args);
108 if (tinfo->stylesheet_xsp)
109 xsltFreeStylesheet(tinfo->stylesheet_xsp);
110 tinfo->stylesheet_xsp =
111 xsltParseStylesheetFile((const xmlChar*) tinfo->fname);
115 static void filter_destroy(void *clientData)
117 struct filter_info *tinfo = clientData;
118 if (tinfo->stylesheet_xsp)
119 xsltFreeStylesheet(tinfo->stylesheet_xsp);
120 #ifdef LIBXML_READER_ENABLED
122 xmlFreeTextReader(tinfo->reader);
125 odr_destroy(tinfo->odr);
129 static int ioread_ex(void *context, char *buffer, int len)
131 struct recExtractCtrl *p = context;
132 return (*p->readf)(p->fh, buffer, len);
135 static int ioclose_ex(void *context)
140 static void index_field(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
141 xmlNodePtr ptr, RecWord *recWord)
143 for(; ptr; ptr = ptr->next)
145 index_field(tinfo, ctrl, ptr->children, recWord);
146 if (ptr->type != XML_TEXT_NODE)
148 recWord->term_buf = ptr->content;
149 recWord->term_len = strlen(ptr->content);
150 (*ctrl->tokenAdd)(recWord);
154 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
155 xmlNodePtr ptr, RecWord *recWord)
157 for(; ptr; ptr = ptr->next)
159 index_node(tinfo, ctrl, ptr->children, recWord);
160 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
161 strcmp(ptr->ns->href, zebra_index_ns))
163 if (!strcmp(ptr->name, "index"))
166 const char *xpath_str = 0;
167 struct _xmlAttr *attr;
168 for (attr = ptr->properties; attr; attr = attr->next)
170 if (!strcmp(attr->name, "field")
171 && attr->children && attr->children->type == XML_TEXT_NODE)
172 field_str = attr->children->content;
173 if (!strcmp(attr->name, "xpath")
174 && attr->children && attr->children->type == XML_TEXT_NODE)
175 xpath_str = attr->children->content;
179 recWord->attrStr = field_str;
180 index_field(tinfo, ctrl, ptr->children, recWord);
186 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
190 const char *params[10];
195 set_param_str(params, "schema", ZEBRA_INDEX_NS, tinfo->odr);
197 (*p->init)(p, &recWord);
198 recWord.reg_type = 'w';
200 if (tinfo->stylesheet_xsp)
203 xsltApplyStylesheet(tinfo->stylesheet_xsp,
205 if (p->flagShowRecords)
207 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
208 fwrite(buf_out, len_out, 1, stdout);
211 index_node(tinfo, p, xmlDocGetRootElement(resDoc), &recWord);
214 xmlDocDumpMemory(doc, &buf_out, &len_out);
215 if (p->flagShowRecords)
216 fwrite(buf_out, len_out, 1, stdout);
217 (*p->setStoreData)(p, buf_out, len_out);
221 return RECCTRL_EXTRACT_OK;
224 #ifdef LIBXML_READER_ENABLED
225 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
231 xmlFreeTextReader(tinfo->reader);
232 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
239 return RECCTRL_EXTRACT_ERROR_GENERIC;
241 if (!tinfo->stylesheet_xsp)
242 return RECCTRL_EXTRACT_ERROR_GENERIC;
244 ret = xmlTextReaderRead(tinfo->reader);
246 int type = xmlTextReaderNodeType(tinfo->reader);
247 int depth = xmlTextReaderDepth(tinfo->reader);
248 if (tinfo->split_depth == 0 ||
249 (type == XML_READER_TYPE_ELEMENT && tinfo->split_depth == depth))
251 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
252 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
253 xmlDocPtr doc = xmlNewDoc("1.0");
255 xmlDocSetRootElement(doc, ptr2);
257 return extract_doc(tinfo, p, doc);
259 ret = xmlTextReaderRead(tinfo->reader);
261 xmlFreeTextReader(tinfo->reader);
263 return RECCTRL_EXTRACT_EOF;
267 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
269 if (p->first_record) /* only one record per stream */
271 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
277 return RECCTRL_EXTRACT_ERROR_GENERIC;
279 return extract_doc(tinfo, p, doc);
282 return RECCTRL_EXTRACT_EOF;
285 static int filter_extract(void *clientData, struct recExtractCtrl *p)
287 struct filter_info *tinfo = clientData;
289 odr_reset(tinfo->odr);
291 if (tinfo->split_depth == 0)
292 return extract_full(tinfo, p);
295 #ifdef LIBXML_READER_ENABLED
296 return extract_split(tinfo, p);
298 /* no xmlreader so we can't split it */
299 return RECCTRL_EXTRACT_ERROR_GENERIC;
304 static int ioread_ret(void *context, char *buffer, int len)
306 struct recRetrieveCtrl *p = context;
307 return (*p->readf)(p->fh, buffer, len);
310 static int ioclose_ret(void *context)
315 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
317 const char *esn = ZEBRA_SCHEMA_IDENTITY_NS;
318 const char *params[10];
319 struct filter_info *tinfo = clientData;
325 if (p->comp->which != Z_RecordComp_simple
326 || p->comp->u.simple->which != Z_ElementSetNames_generic)
328 p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
331 esn = p->comp->u.simple->u.generic;
335 set_param_str(params, "schema", esn, p->odr);
337 set_param_str(params, "filename", p->fname, p->odr);
339 set_param_int(params, "score", p->score, p->odr);
340 set_param_int(params, "size", p->recordSize, p->odr);
342 if (!tinfo->stylesheet_xsp)
344 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
347 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
353 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
357 if (!strcmp(esn, ZEBRA_SCHEMA_IDENTITY_NS))
361 resDoc = xsltApplyStylesheet(tinfo->stylesheet_xsp,
367 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
369 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
373 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
375 p->output_format = VAL_TEXT_XML;
376 p->rec_len = len_out;
377 p->rec_buf = odr_malloc(p->odr, p->rec_len);
378 memcpy(p->rec_buf, buf_out, p->rec_len);
382 else if (p->output_format == VAL_SUTRS)
386 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
388 p->output_format = VAL_SUTRS;
389 p->rec_len = len_out;
390 p->rec_buf = odr_malloc(p->odr, p->rec_len);
391 memcpy(p->rec_buf, buf_out, p->rec_len);
397 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
403 static struct recType filter_type_xslt = {
413 static struct recType filter_type_xslt1 = {
424 #ifdef IDZEBRA_STATIC_XSLT
432 #ifdef LIBXML_READER_ENABLED