1 /* $Id: xslt.c,v 1.3 2005-04-28 13:33:20 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 */
64 sprintf(quoted, "'" ZINT_FORMAT "'", value);
71 static void *filter_init (Res res, RecType recType)
73 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
74 tinfo->stylesheet_xsp = 0;
77 tinfo->split_depth = 1;
78 tinfo->odr = odr_createmem(ODR_ENCODE);
82 static void filter_config(void *clientData, Res res, const char *args)
84 struct filter_info *tinfo = clientData;
87 if (!tinfo->fname || strcmp(args, tinfo->fname))
89 /* different filename so must reread stylesheet */
91 tinfo->fname = xstrdup(args);
92 if (tinfo->stylesheet_xsp)
93 xsltFreeStylesheet(tinfo->stylesheet_xsp);
94 tinfo->stylesheet_xsp =
95 xsltParseStylesheetFile((const xmlChar*) tinfo->fname);
99 static void filter_destroy(void *clientData)
101 struct filter_info *tinfo = clientData;
102 if (tinfo->stylesheet_xsp)
103 xsltFreeStylesheet(tinfo->stylesheet_xsp);
105 odr_destroy(tinfo->odr);
109 static int ioread_ex(void *context, char *buffer, int len)
111 struct recExtractCtrl *p = context;
112 return (*p->readf)(p->fh, buffer, len);
115 static int ioclose_ex(void *context)
120 static void index_field(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
121 xmlNodePtr ptr, RecWord *recWord)
123 for(; ptr; ptr = ptr->next)
125 index_field(tinfo, ctrl, ptr->children, recWord);
126 if (ptr->type != XML_TEXT_NODE)
128 recWord->term_buf = ptr->content;
129 recWord->term_len = strlen(ptr->content);
130 (*ctrl->tokenAdd)(recWord);
134 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
135 xmlNodePtr ptr, RecWord *recWord)
137 for(; ptr; ptr = ptr->next)
139 index_node(tinfo, ctrl, ptr->children, recWord);
140 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
141 strcmp(ptr->ns->href, zebra_index_ns))
143 if (!strcmp(ptr->name, "index"))
146 const char *xpath_str = 0;
147 struct _xmlAttr *attr;
148 for (attr = ptr->properties; attr; attr = attr->next)
150 if (!strcmp(attr->name, "field")
151 && attr->children && attr->children->type == XML_TEXT_NODE)
152 field_str = attr->children->content;
153 if (!strcmp(attr->name, "xpath")
154 && attr->children && attr->children->type == XML_TEXT_NODE)
155 xpath_str = attr->children->content;
159 recWord->attrStr = field_str;
160 index_field(tinfo, ctrl, ptr->children, recWord);
166 static int filter_extract(void *clientData, struct recExtractCtrl *p)
168 const char *params[10];
169 struct filter_info *tinfo = clientData;
175 odr_reset(tinfo->odr);
176 set_param_str(params, "schema", ZEBRA_INDEX_NS, tinfo->odr);
181 xmlFreeTextReader(tinfo->reader);
182 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
189 return RECCTRL_EXTRACT_ERROR_GENERIC;
191 if (!tinfo->stylesheet_xsp)
192 return RECCTRL_EXTRACT_ERROR_GENERIC;
194 (*p->init)(p, &recWord);
195 recWord.reg_type = 'w';
197 ret = xmlTextReaderRead(tinfo->reader);
199 int type = xmlTextReaderNodeType(tinfo->reader);
200 int depth = xmlTextReaderDepth(tinfo->reader);
201 if (tinfo->split_depth == 0 ||
202 (type == XML_READER_TYPE_ELEMENT && tinfo->split_depth == depth))
207 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
208 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
209 xmlDocPtr doc = xmlNewDoc("1.0");
211 xmlDocSetRootElement(doc, ptr2);
213 if (tinfo->stylesheet_xsp)
216 xsltApplyStylesheet(tinfo->stylesheet_xsp,
218 if (p->flagShowRecords)
220 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
221 fwrite(buf_out, len_out, 1, stdout);
224 index_node(tinfo, p, xmlDocGetRootElement(resDoc), &recWord);
227 xmlDocDumpMemory(doc, &buf_out, &len_out);
228 if (p->flagShowRecords)
229 fwrite(buf_out, len_out, 1, stdout);
230 (*p->setStoreData)(p, buf_out, len_out);
234 return RECCTRL_EXTRACT_OK;
236 ret = xmlTextReaderRead(tinfo->reader);
238 xmlFreeTextReader(tinfo->reader);
240 return RECCTRL_EXTRACT_EOF;
243 static int ioread_ret(void *context, char *buffer, int len)
245 struct recRetrieveCtrl *p = context;
246 return (*p->readf)(p->fh, buffer, len);
249 static int ioclose_ret(void *context)
254 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
256 const char *esn = ZEBRA_SCHEMA_IDENTITY_NS;
257 const char *params[10];
258 struct filter_info *tinfo = clientData;
264 if (p->comp->which != Z_RecordComp_simple
265 || p->comp->u.simple->which != Z_ElementSetNames_generic)
267 p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
270 esn = p->comp->u.simple->u.generic;
274 set_param_str(params, "schema", esn, p->odr);
276 set_param_str(params, "filename", p->fname, p->odr);
278 set_param_int(params, "score", p->score, p->odr);
279 set_param_int(params, "size", p->recordSize, p->odr);
281 if (!tinfo->stylesheet_xsp)
283 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
286 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
292 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
296 if (!strcmp(esn, ZEBRA_SCHEMA_IDENTITY_NS))
300 resDoc = xsltApplyStylesheet(tinfo->stylesheet_xsp,
306 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
308 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
312 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
314 p->output_format = VAL_TEXT_XML;
315 p->rec_len = len_out;
316 p->rec_buf = odr_malloc(p->odr, p->rec_len);
317 memcpy(p->rec_buf, buf_out, p->rec_len);
321 else if (p->output_format == VAL_SUTRS)
325 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
327 p->output_format = VAL_SUTRS;
328 p->rec_len = len_out;
329 p->rec_buf = odr_malloc(p->odr, p->rec_len);
330 memcpy(p->rec_buf, buf_out, p->rec_len);
336 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
342 static struct recType filter_type = {
353 #ifdef IDZEBRA_STATIC_XSLT