1 /* $Id: xslt.c,v 1.2 2005-04-28 12:34:21 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/xmlreader.h>
29 #include <libxslt/transform.h>
31 #include <idzebra/util.h>
32 #include <idzebra/recctrl.h>
35 xsltStylesheetPtr stylesheet_xsp;
36 xmlTextReaderPtr reader;
42 #define ZEBRA_INDEX_NS "http://indexdata.dk/zebra/indexing/1"
43 #define ZEBRA_SCHEMA_IDENTITY_NS "http://indexdata.dk/zebra/identity/1"
44 static const char *zebra_index_ns = ZEBRA_INDEX_NS;
46 static void set_param_str(const char **params, const char *name,
47 const char *value, ODR odr)
49 char *quoted = odr_malloc(odr, 3 + strlen(value));
50 sprintf(quoted, "'%s'", value);
58 static void set_param_int(const char **params, const char *name,
61 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
62 sprintf(quoted, "'" ZINT_FORMAT "'", value);
69 static void *filter_init (Res res, RecType recType)
71 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
72 tinfo->stylesheet_xsp = 0;
75 tinfo->split_depth = 1;
76 tinfo->odr = odr_createmem(ODR_ENCODE);
80 static void filter_config(void *clientData, Res res, const char *args)
82 struct filter_info *tinfo = clientData;
85 if (!tinfo->fname || strcmp(args, tinfo->fname))
87 /* different filename so must reread stylesheet */
89 tinfo->fname = xstrdup(args);
90 if (tinfo->stylesheet_xsp)
91 xsltFreeStylesheet(tinfo->stylesheet_xsp);
92 tinfo->stylesheet_xsp =
93 xsltParseStylesheetFile((const xmlChar*) tinfo->fname);
97 static void filter_destroy(void *clientData)
99 struct filter_info *tinfo = clientData;
100 if (tinfo->stylesheet_xsp)
101 xsltFreeStylesheet(tinfo->stylesheet_xsp);
103 odr_destroy(tinfo->odr);
107 static int ioread_ex(void *context, char *buffer, int len)
109 struct recExtractCtrl *p = context;
110 return (*p->readf)(p->fh, buffer, len);
113 static int ioclose_ex(void *context)
118 static void index_field(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
119 xmlNodePtr ptr, RecWord *recWord)
121 for(; ptr; ptr = ptr->next)
123 index_field(tinfo, ctrl, ptr->children, recWord);
124 if (ptr->type != XML_TEXT_NODE)
126 recWord->term_buf = ptr->content;
127 recWord->term_len = strlen(ptr->content);
128 (*ctrl->tokenAdd)(recWord);
132 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
133 xmlNodePtr ptr, RecWord *recWord)
135 for(; ptr; ptr = ptr->next)
137 index_node(tinfo, ctrl, ptr->children, recWord);
138 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
139 strcmp(ptr->ns->href, zebra_index_ns))
141 if (!strcmp(ptr->name, "index"))
144 const char *xpath_str = 0;
145 struct _xmlAttr *attr;
146 for (attr = ptr->properties; attr; attr = attr->next)
148 if (!strcmp(attr->name, "field")
149 && attr->children && attr->children->type == XML_TEXT_NODE)
150 field_str = attr->children->content;
151 if (!strcmp(attr->name, "xpath")
152 && attr->children && attr->children->type == XML_TEXT_NODE)
153 xpath_str = attr->children->content;
157 recWord->attrStr = field_str;
158 index_field(tinfo, ctrl, ptr->children, recWord);
164 static int filter_extract(void *clientData, struct recExtractCtrl *p)
166 const char *params[10];
167 struct filter_info *tinfo = clientData;
173 odr_reset(tinfo->odr);
174 set_param_str(params, "schema", ZEBRA_INDEX_NS, tinfo->odr);
179 xmlFreeTextReader(tinfo->reader);
180 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
187 return RECCTRL_EXTRACT_ERROR_GENERIC;
189 if (!tinfo->stylesheet_xsp)
190 return RECCTRL_EXTRACT_ERROR_GENERIC;
192 (*p->init)(p, &recWord);
193 recWord.reg_type = 'w';
195 ret = xmlTextReaderRead(tinfo->reader);
197 int type = xmlTextReaderNodeType(tinfo->reader);
198 int depth = xmlTextReaderDepth(tinfo->reader);
199 if (tinfo->split_depth == 0 ||
200 (type == XML_READER_TYPE_ELEMENT && tinfo->split_depth == depth))
205 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
206 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
207 xmlDocPtr doc = xmlNewDoc("1.0");
209 xmlDocSetRootElement(doc, ptr2);
211 if (tinfo->stylesheet_xsp)
214 xsltApplyStylesheet(tinfo->stylesheet_xsp,
216 if (p->flagShowRecords)
218 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
219 fwrite(buf_out, len_out, 1, stdout);
222 index_node(tinfo, p, xmlDocGetRootElement(resDoc), &recWord);
225 xmlDocDumpMemory(doc, &buf_out, &len_out);
226 if (p->flagShowRecords)
227 fwrite(buf_out, len_out, 1, stdout);
228 (*p->setStoreData)(p, buf_out, len_out);
232 return RECCTRL_EXTRACT_OK;
234 ret = xmlTextReaderRead(tinfo->reader);
236 xmlFreeTextReader(tinfo->reader);
238 return RECCTRL_EXTRACT_EOF;
241 static int ioread_ret(void *context, char *buffer, int len)
243 struct recRetrieveCtrl *p = context;
244 return (*p->readf)(p->fh, buffer, len);
247 static int ioclose_ret(void *context)
252 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
254 const char *esn = ZEBRA_SCHEMA_IDENTITY_NS;
255 const char *params[10];
256 struct filter_info *tinfo = clientData;
262 if (p->comp->which != Z_RecordComp_simple
263 || p->comp->u.simple->which != Z_ElementSetNames_generic)
265 p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
268 esn = p->comp->u.simple->u.generic;
272 set_param_str(params, "schema", esn, p->odr);
274 set_param_str(params, "filename", p->fname, p->odr);
276 set_param_int(params, "score", p->score, p->odr);
277 set_param_int(params, "size", p->recordSize, p->odr);
279 if (!tinfo->stylesheet_xsp)
281 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
284 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
290 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
294 if (!strcmp(esn, ZEBRA_SCHEMA_IDENTITY_NS))
298 resDoc = xsltApplyStylesheet(tinfo->stylesheet_xsp,
304 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
306 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
310 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
312 p->output_format = VAL_TEXT_XML;
313 p->rec_len = len_out;
314 p->rec_buf = odr_malloc(p->odr, p->rec_len);
315 memcpy(p->rec_buf, buf_out, p->rec_len);
319 else if (p->output_format == VAL_SUTRS)
323 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
325 p->output_format = VAL_SUTRS;
326 p->rec_len = len_out;
327 p->rec_buf = odr_malloc(p->odr, p->rec_len);
328 memcpy(p->rec_buf, buf_out, p->rec_len);
334 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
340 static struct recType filter_type = {
351 #ifdef IDZEBRA_STATIC_XSLT