1 /* $Id: xslt.c,v 1.7 2005-06-01 07:32: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/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xmlreader.h>
32 #include <libxslt/transform.h>
34 #include <idzebra/util.h>
35 #include <idzebra/recctrl.h>
37 struct filter_schema {
39 const char *identifier;
40 const char *stylesheet;
41 struct filter_schema *next;
42 const char *default_schema;
43 xsltStylesheetPtr stylesheet_xsp;
49 const char *split_level;
50 const char *split_path;
52 struct filter_schema *schemas;
53 xmlTextReaderPtr reader;
56 #define ZEBRA_INDEX_NS "http://indexdata.dk/zebra/indexing/1"
57 #define ZEBRA_SCHEMA_IDENTITY_NS "http://indexdata.dk/zebra/identity/1"
58 static const char *zebra_index_ns = ZEBRA_INDEX_NS;
60 static void set_param_str(const char **params, const char *name,
61 const char *value, ODR odr)
63 char *quoted = odr_malloc(odr, 3 + strlen(value));
64 sprintf(quoted, "'%s'", value);
72 static void set_param_int(const char **params, const char *name,
75 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
78 sprintf(quoted, "'" ZINT_FORMAT "'", value);
85 static void *filter_init_xslt(Res res, RecType recType)
87 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
90 tinfo->split_level = 0;
91 tinfo->split_path = 0;
92 tinfo->odr = odr_createmem(ODR_ENCODE);
98 static void *filter_init_xslt1(Res res, RecType recType)
100 struct filter_info *tinfo = (struct filter_info *)
101 filter_init_xslt(res, recType);
102 tinfo->split_level = "1";
106 static int attr_content(struct _xmlAttr *attr, const char *name,
107 const char **dst_content)
109 if (!strcmp(attr->name, name) && attr->children &&
110 attr->children->type == XML_TEXT_NODE)
112 *dst_content = attr->children->content;
118 static void destroy_schemas(struct filter_info *tinfo)
120 struct filter_schema *schema = tinfo->schemas;
123 struct filter_schema *schema_next = schema->next;
124 if (schema->stylesheet_xsp)
125 xsltFreeStylesheet(schema->stylesheet_xsp);
127 schema = schema_next;
132 xmlFreeDoc(tinfo->doc);
136 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
139 tinfo->fname = xstrdup(fname);
140 tinfo->doc = xmlParseFile(tinfo->fname);
143 ptr = xmlDocGetRootElement(tinfo->doc);
144 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
145 strcmp(ptr->name, "schemaInfo"))
147 for (ptr = ptr->children; ptr; ptr = ptr->next)
149 if (ptr->type != XML_ELEMENT_NODE)
151 if (!strcmp(ptr->name, "schema"))
153 struct _xmlAttr *attr;
154 struct filter_schema *schema = xmalloc(sizeof(*schema));
156 schema->identifier = 0;
157 schema->stylesheet = 0;
158 schema->default_schema = 0;
159 schema->next = tinfo->schemas;
160 schema->stylesheet_xsp = 0;
161 tinfo->schemas = schema;
162 for (attr = ptr->properties; attr; attr = attr->next)
164 attr_content(attr, "identifier", &schema->identifier);
165 attr_content(attr, "name", &schema->name);
166 attr_content(attr, "stylesheet", &schema->stylesheet);
167 attr_content(attr, "default", &schema->default_schema);
169 if (schema->stylesheet)
170 schema->stylesheet_xsp =
171 xsltParseStylesheetFile(
172 (const xmlChar*) schema->stylesheet);
174 else if (!strcmp(ptr->name, "split"))
176 struct _xmlAttr *attr;
177 for (attr = ptr->properties; attr; attr = attr->next)
179 attr_content(attr, "level", &tinfo->split_level);
180 attr_content(attr, "path", &tinfo->split_path);
185 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
192 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
195 struct filter_schema *schema;
196 for (schema = tinfo->schemas; schema; schema = schema->next)
200 if (schema->identifier && !strcmp(schema->identifier, est))
202 if (schema->name && !strcmp(schema->name, est))
205 if (schema->default_schema)
211 static void filter_config(void *clientData, Res res, const char *args)
213 struct filter_info *tinfo = clientData;
215 args = "xsltfilter.xml";
216 if (tinfo->fname && !strcmp(args, tinfo->fname))
218 destroy_schemas(tinfo);
219 create_schemas(tinfo, args);
222 static void filter_destroy(void *clientData)
224 struct filter_info *tinfo = clientData;
225 destroy_schemas(tinfo);
227 xmlFreeTextReader(tinfo->reader);
228 odr_destroy(tinfo->odr);
232 static int ioread_ex(void *context, char *buffer, int len)
234 struct recExtractCtrl *p = context;
235 return (*p->readf)(p->fh, buffer, len);
238 static int ioclose_ex(void *context)
243 static void index_field(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
244 xmlNodePtr ptr, RecWord *recWord)
246 for(; ptr; ptr = ptr->next)
248 index_field(tinfo, ctrl, ptr->children, recWord);
249 if (ptr->type != XML_TEXT_NODE)
251 recWord->term_buf = ptr->content;
252 recWord->term_len = strlen(ptr->content);
253 (*ctrl->tokenAdd)(recWord);
257 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
258 xmlNodePtr ptr, RecWord *recWord)
260 for(; ptr; ptr = ptr->next)
262 index_node(tinfo, ctrl, ptr->children, recWord);
263 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
264 strcmp(ptr->ns->href, zebra_index_ns))
266 if (!strcmp(ptr->name, "index"))
269 const char *xpath_str = 0;
270 struct _xmlAttr *attr;
271 for (attr = ptr->properties; attr; attr = attr->next)
273 if (!strcmp(attr->name, "field")
274 && attr->children && attr->children->type == XML_TEXT_NODE)
275 field_str = attr->children->content;
276 if (!strcmp(attr->name, "xpath")
277 && attr->children && attr->children->type == XML_TEXT_NODE)
278 xpath_str = attr->children->content;
282 recWord->attrStr = field_str;
283 index_field(tinfo, ctrl, ptr->children, recWord);
289 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
293 const char *params[10];
297 struct filter_schema *schema = lookup_schema(tinfo, ZEBRA_INDEX_NS);
300 set_param_str(params, "schema", ZEBRA_INDEX_NS, tinfo->odr);
302 (*p->init)(p, &recWord);
303 recWord.reg_type = 'w';
305 if (schema && schema->stylesheet_xsp)
308 xsltApplyStylesheet(schema->stylesheet_xsp,
310 if (p->flagShowRecords)
312 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
313 fwrite(buf_out, len_out, 1, stdout);
316 index_node(tinfo, p, xmlDocGetRootElement(resDoc), &recWord);
319 xmlDocDumpMemory(doc, &buf_out, &len_out);
320 if (p->flagShowRecords)
321 fwrite(buf_out, len_out, 1, stdout);
322 (*p->setStoreData)(p, buf_out, len_out);
326 return RECCTRL_EXTRACT_OK;
329 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
336 xmlFreeTextReader(tinfo->reader);
337 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
344 return RECCTRL_EXTRACT_ERROR_GENERIC;
346 if (tinfo->split_level)
347 split_depth = atoi(tinfo->split_level);
348 ret = xmlTextReaderRead(tinfo->reader);
350 int type = xmlTextReaderNodeType(tinfo->reader);
351 int depth = xmlTextReaderDepth(tinfo->reader);
352 if (split_depth == 0 ||
354 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
356 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
357 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
358 xmlDocPtr doc = xmlNewDoc("1.0");
360 xmlDocSetRootElement(doc, ptr2);
362 return extract_doc(tinfo, p, doc);
364 ret = xmlTextReaderRead(tinfo->reader);
366 xmlFreeTextReader(tinfo->reader);
368 return RECCTRL_EXTRACT_EOF;
371 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
373 if (p->first_record) /* only one record per stream */
375 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
381 return RECCTRL_EXTRACT_ERROR_GENERIC;
383 return extract_doc(tinfo, p, doc);
386 return RECCTRL_EXTRACT_EOF;
389 static int filter_extract(void *clientData, struct recExtractCtrl *p)
391 struct filter_info *tinfo = clientData;
393 odr_reset(tinfo->odr);
395 if (tinfo->split_level == 0 && tinfo->split_path == 0)
396 return extract_full(tinfo, p);
399 return extract_split(tinfo, p);
403 static int ioread_ret(void *context, char *buffer, int len)
405 struct recRetrieveCtrl *p = context;
406 return (*p->readf)(p->fh, buffer, len);
409 static int ioclose_ret(void *context)
415 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
417 const char *esn = ZEBRA_SCHEMA_IDENTITY_NS;
418 const char *params[10];
419 struct filter_info *tinfo = clientData;
422 struct filter_schema *schema;
426 if (p->comp->which != Z_RecordComp_simple
427 || p->comp->u.simple->which != Z_ElementSetNames_generic)
429 p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
432 esn = p->comp->u.simple->u.generic;
434 schema = lookup_schema(tinfo, esn);
438 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
443 set_param_str(params, "schema", esn, p->odr);
445 set_param_str(params, "filename", p->fname, p->odr);
447 set_param_int(params, "score", p->score, p->odr);
448 set_param_int(params, "size", p->recordSize, p->odr);
450 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
456 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
460 if (!schema->stylesheet_xsp)
464 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
470 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
472 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
476 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
478 p->output_format = VAL_TEXT_XML;
479 p->rec_len = len_out;
480 p->rec_buf = odr_malloc(p->odr, p->rec_len);
481 memcpy(p->rec_buf, buf_out, p->rec_len);
485 else if (p->output_format == VAL_SUTRS)
489 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
491 p->output_format = VAL_SUTRS;
492 p->rec_len = len_out;
493 p->rec_buf = odr_malloc(p->odr, p->rec_len);
494 memcpy(p->rec_buf, buf_out, p->rec_len);
500 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
506 static struct recType filter_type_xslt = {
516 static struct recType filter_type_xslt1 = {
527 #ifdef IDZEBRA_STATIC_XSLT
535 #ifdef LIBXML_READER_ENABLED