1 /* $Id: xslt.c,v 1.17 2005-08-24 08:30:37 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/xmlIO.h>
32 #include <libxml/xmlreader.h>
33 #include <libxslt/transform.h>
35 #include <idzebra/util.h>
36 #include <idzebra/recctrl.h>
38 struct filter_schema {
40 const char *identifier;
41 const char *stylesheet;
42 struct filter_schema *next;
43 const char *default_schema;
44 const char *include_snippet;
45 xsltStylesheetPtr stylesheet_xsp;
51 const char *split_level;
52 const char *split_path;
54 struct filter_schema *schemas;
55 xmlTextReaderPtr reader;
58 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
60 #define XML_STRCMP(a,b) strcmp((char*)a, b)
61 #define XML_STRLEN(a) strlen((char*)a)
63 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
65 static void set_param_xml(const char **params, const char *name,
66 const char *value, ODR odr)
75 static void set_param_str(const char **params, const char *name,
76 const char *value, ODR odr)
78 char *quoted = odr_malloc(odr, 3 + strlen(value));
79 sprintf(quoted, "'%s'", value);
87 static void set_param_int(const char **params, const char *name,
90 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
93 sprintf(quoted, "'" ZINT_FORMAT "'", value);
99 #define ENABLE_INPUT_CALLBACK 0
101 #if ENABLE_INPUT_CALLBACK
102 static int zebra_xmlInputMatchCallback (char const *filename)
104 yaz_log(YLOG_LOG, "match %s", filename);
108 static void * zebra_xmlInputOpenCallback (char const *filename)
113 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
118 static int zebra_xmlInputCloseCallback (void * context)
124 static void *filter_init(Res res, RecType recType)
126 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
129 tinfo->split_level = 0;
130 tinfo->split_path = 0;
131 tinfo->odr = odr_createmem(ODR_ENCODE);
135 #if ENABLE_INPUT_CALLBACK
136 xmlRegisterDefaultInputCallbacks();
137 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
138 zebra_xmlInputOpenCallback,
139 zebra_xmlInputReadCallback,
140 zebra_xmlInputCloseCallback);
145 static int attr_content(struct _xmlAttr *attr, const char *name,
146 const char **dst_content)
148 if (!XML_STRCMP(attr->name, name) && attr->children &&
149 attr->children->type == XML_TEXT_NODE)
151 *dst_content = (const char *)(attr->children->content);
157 static void destroy_schemas(struct filter_info *tinfo)
159 struct filter_schema *schema = tinfo->schemas;
162 struct filter_schema *schema_next = schema->next;
163 if (schema->stylesheet_xsp)
164 xsltFreeStylesheet(schema->stylesheet_xsp);
166 schema = schema_next;
171 xmlFreeDoc(tinfo->doc);
175 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
178 tinfo->fname = xstrdup(fname);
179 tinfo->doc = xmlParseFile(tinfo->fname);
182 ptr = xmlDocGetRootElement(tinfo->doc);
183 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
184 XML_STRCMP(ptr->name, "schemaInfo"))
186 for (ptr = ptr->children; ptr; ptr = ptr->next)
188 if (ptr->type != XML_ELEMENT_NODE)
190 if (!XML_STRCMP(ptr->name, "schema"))
192 struct _xmlAttr *attr;
193 struct filter_schema *schema = xmalloc(sizeof(*schema));
195 schema->identifier = 0;
196 schema->stylesheet = 0;
197 schema->default_schema = 0;
198 schema->next = tinfo->schemas;
199 schema->stylesheet_xsp = 0;
200 schema->include_snippet = 0;
201 tinfo->schemas = schema;
202 for (attr = ptr->properties; attr; attr = attr->next)
204 attr_content(attr, "identifier", &schema->identifier);
205 attr_content(attr, "name", &schema->name);
206 attr_content(attr, "stylesheet", &schema->stylesheet);
207 attr_content(attr, "default", &schema->default_schema);
208 attr_content(attr, "snippet", &schema->include_snippet);
210 if (schema->stylesheet)
211 schema->stylesheet_xsp =
212 xsltParseStylesheetFile(
213 (const xmlChar*) schema->stylesheet);
215 else if (!XML_STRCMP(ptr->name, "split"))
217 struct _xmlAttr *attr;
218 for (attr = ptr->properties; attr; attr = attr->next)
220 attr_content(attr, "level", &tinfo->split_level);
221 attr_content(attr, "path", &tinfo->split_path);
226 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
233 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
236 struct filter_schema *schema;
237 for (schema = tinfo->schemas; schema; schema = schema->next)
241 if (schema->identifier && !strcmp(schema->identifier, est))
243 if (schema->name && !strcmp(schema->name, est))
246 if (schema->default_schema)
252 static void filter_config(void *clientData, Res res, const char *args)
254 struct filter_info *tinfo = clientData;
256 args = "xsltfilter.xml";
257 if (tinfo->fname && !strcmp(args, tinfo->fname))
259 destroy_schemas(tinfo);
260 create_schemas(tinfo, args);
263 static void filter_destroy(void *clientData)
265 struct filter_info *tinfo = clientData;
266 destroy_schemas(tinfo);
268 xmlFreeTextReader(tinfo->reader);
269 odr_destroy(tinfo->odr);
273 static int ioread_ex(void *context, char *buffer, int len)
275 struct recExtractCtrl *p = context;
276 return (*p->readf)(p->fh, buffer, len);
279 static int ioclose_ex(void *context)
284 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
285 xmlNodePtr ptr, RecWord *recWord)
287 for(; ptr; ptr = ptr->next)
289 index_cdata(tinfo, ctrl, ptr->children, recWord);
290 if (ptr->type != XML_TEXT_NODE)
292 recWord->term_buf = (const char *)ptr->content;
293 recWord->term_len = XML_STRLEN(ptr->content);
294 (*ctrl->tokenAdd)(recWord);
298 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
299 xmlNodePtr ptr, RecWord *recWord)
301 for(; ptr; ptr = ptr->next)
303 index_node(tinfo, ctrl, ptr->children, recWord);
304 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
305 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
307 if (!XML_STRCMP(ptr->name, "index"))
309 const char *name_str = 0;
310 const char *type_str = 0;
311 const char *xpath_str = 0;
312 struct _xmlAttr *attr;
313 for (attr = ptr->properties; attr; attr = attr->next)
315 attr_content(attr, "name", &name_str);
316 attr_content(attr, "xpath", &xpath_str);
317 attr_content(attr, "type", &type_str);
321 int prev_type = recWord->index_type; /* save default type */
323 if (type_str && *type_str)
324 recWord->index_type = *type_str; /* type was given */
325 recWord->index_name = name_str;
326 index_cdata(tinfo, ctrl, ptr->children, recWord);
328 recWord->index_type = prev_type; /* restore it again */
334 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
335 xmlNodePtr ptr, RecWord *recWord)
337 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
338 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
339 && !XML_STRCMP(ptr->name, "record"))
341 const char *type_str = "update";
342 const char *id_str = 0;
343 const char *rank_str = 0;
344 struct _xmlAttr *attr;
345 for (attr = ptr->properties; attr; attr = attr->next)
347 attr_content(attr, "type", &type_str);
348 attr_content(attr, "id", &id_str);
349 attr_content(attr, "rank", &rank_str);
352 sscanf(id_str, "%255s", ctrl->match_criteria);
355 ctrl->staticrank = atoi(rank_str);
356 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
359 yaz_log(YLOG_LOG, "no rank");
363 index_node(tinfo, ctrl, ptr, recWord);
366 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
370 const char *params[10];
374 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
377 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
379 (*p->init)(p, &recWord);
381 if (schema && schema->stylesheet_xsp)
385 xsltApplyStylesheet(schema->stylesheet_xsp,
387 if (p->flagShowRecords)
389 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
390 fwrite(buf_out, len_out, 1, stdout);
393 root_ptr = xmlDocGetRootElement(resDoc);
395 index_record(tinfo, p, root_ptr, &recWord);
398 yaz_log(YLOG_WARN, "No root for index XML record."
399 " split_level=%s stylesheet=%s",
400 tinfo->split_level, schema->stylesheet);
404 xmlDocDumpMemory(doc, &buf_out, &len_out);
405 if (p->flagShowRecords)
406 fwrite(buf_out, len_out, 1, stdout);
407 (*p->setStoreData)(p, buf_out, len_out);
411 return RECCTRL_EXTRACT_OK;
414 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
421 xmlFreeTextReader(tinfo->reader);
422 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
429 return RECCTRL_EXTRACT_ERROR_GENERIC;
431 if (tinfo->split_level)
432 split_depth = atoi(tinfo->split_level);
433 ret = xmlTextReaderRead(tinfo->reader);
435 int type = xmlTextReaderNodeType(tinfo->reader);
436 int depth = xmlTextReaderDepth(tinfo->reader);
437 if (split_depth == 0 ||
439 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
441 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
442 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
443 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
445 xmlDocSetRootElement(doc, ptr2);
447 return extract_doc(tinfo, p, doc);
449 ret = xmlTextReaderRead(tinfo->reader);
451 xmlFreeTextReader(tinfo->reader);
453 return RECCTRL_EXTRACT_EOF;
456 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
458 if (p->first_record) /* only one record per stream */
460 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
466 return RECCTRL_EXTRACT_ERROR_GENERIC;
468 return extract_doc(tinfo, p, doc);
471 return RECCTRL_EXTRACT_EOF;
474 static int filter_extract(void *clientData, struct recExtractCtrl *p)
476 struct filter_info *tinfo = clientData;
478 odr_reset(tinfo->odr);
480 if (tinfo->split_level == 0 && tinfo->split_path == 0)
481 return extract_full(tinfo, p);
484 return extract_split(tinfo, p);
488 static int ioread_ret(void *context, char *buffer, int len)
490 struct recRetrieveCtrl *p = context;
491 return (*p->readf)(p->fh, buffer, len);
494 static int ioclose_ret(void *context)
500 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
503 const char *xml_doc_str;
505 WRBUF wrbuf = wrbuf_alloc();
506 zebra_snippets *res =
507 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
508 zebra_snippet_word *w = zebra_snippets_list(res);
511 wrbuf_printf(wrbuf, "\'");
513 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
514 for (; w; w = w->next)
518 else if (ord != w->ord)
522 wrbuf_printf(wrbuf, "%s%s%s ",
525 w->match ? "*" : "");
528 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
530 (w->match ? "match='1'" : ""));
531 wrbuf_xmlputs(wrbuf, w->term);
532 wrbuf_printf(wrbuf, "</term>\n");
536 wrbuf_printf(wrbuf, "\'");
538 wrbuf_printf(wrbuf, "</snippet>\n");
540 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
542 zebra_snippets_destroy(res);
543 wrbuf_free(wrbuf, 1);
547 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
549 const char *esn = zebra_xslt_ns;
550 const char *params[20];
551 struct filter_info *tinfo = clientData;
554 struct filter_schema *schema;
555 int window_size = -1;
559 if (p->comp->which == Z_RecordComp_simple
560 && p->comp->u.simple->which == Z_ElementSetNames_generic)
562 esn = p->comp->u.simple->u.generic;
564 else if (p->comp->which == Z_RecordComp_complex
565 && p->comp->u.complex->generic->elementSpec
566 && p->comp->u.complex->generic->elementSpec->which ==
567 Z_ElementSpec_elementSetName)
569 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
572 schema = lookup_schema(tinfo, esn);
576 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
580 if (schema->include_snippet)
581 window_size = atoi(schema->include_snippet);
584 set_param_str(params, "schema", esn, p->odr);
586 set_param_str(params, "filename", p->fname, p->odr);
588 set_param_int(params, "score", p->score, p->odr);
589 set_param_int(params, "size", p->recordSize, p->odr);
590 set_param_int(params, "id", p->localno, p->odr);
592 if (window_size >= 0)
593 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
595 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
601 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
605 if (window_size >= 0)
607 xmlNodePtr node = xmlDocGetRootElement(doc);
608 const char *snippet_str = snippet_doc(p, 0, window_size);
609 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
610 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
612 if (!schema->stylesheet_xsp)
616 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
622 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
624 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
628 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
630 p->output_format = VAL_TEXT_XML;
631 p->rec_len = len_out;
632 p->rec_buf = odr_malloc(p->odr, p->rec_len);
633 memcpy(p->rec_buf, buf_out, p->rec_len);
637 else if (p->output_format == VAL_SUTRS)
641 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
643 p->output_format = VAL_SUTRS;
644 p->rec_len = len_out;
645 p->rec_buf = odr_malloc(p->odr, p->rec_len);
646 memcpy(p->rec_buf, buf_out, p->rec_len);
652 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
658 static struct recType filter_type = {
669 #ifdef IDZEBRA_STATIC_XSLT