1 /* $Id: xslt.c,v 1.14 2005-08-19 21:41: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 static int zebra_xmlInputMatchCallback (char const *filename)
101 yaz_log(YLOG_LOG, "match %s", filename);
105 static void * zebra_xmlInputOpenCallback (char const *filename)
110 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
115 static int zebra_xmlInputCloseCallback (void * context)
120 static void *filter_init_xslt(Res res, RecType recType)
122 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
125 tinfo->split_level = 0;
126 tinfo->split_path = 0;
127 tinfo->odr = odr_createmem(ODR_ENCODE);
132 xmlRegisterDefaultInputCallbacks();
133 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
134 zebra_xmlInputOpenCallback,
135 zebra_xmlInputReadCallback,
136 zebra_xmlInputCloseCallback);
141 static int attr_content(struct _xmlAttr *attr, const char *name,
142 const char **dst_content)
144 if (!XML_STRCMP(attr->name, name) && attr->children &&
145 attr->children->type == XML_TEXT_NODE)
147 *dst_content = (const char *)(attr->children->content);
153 static void destroy_schemas(struct filter_info *tinfo)
155 struct filter_schema *schema = tinfo->schemas;
158 struct filter_schema *schema_next = schema->next;
159 if (schema->stylesheet_xsp)
160 xsltFreeStylesheet(schema->stylesheet_xsp);
162 schema = schema_next;
167 xmlFreeDoc(tinfo->doc);
171 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
174 tinfo->fname = xstrdup(fname);
175 tinfo->doc = xmlParseFile(tinfo->fname);
178 ptr = xmlDocGetRootElement(tinfo->doc);
179 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
180 XML_STRCMP(ptr->name, "schemaInfo"))
182 for (ptr = ptr->children; ptr; ptr = ptr->next)
184 if (ptr->type != XML_ELEMENT_NODE)
186 if (!XML_STRCMP(ptr->name, "schema"))
188 struct _xmlAttr *attr;
189 struct filter_schema *schema = xmalloc(sizeof(*schema));
191 schema->identifier = 0;
192 schema->stylesheet = 0;
193 schema->default_schema = 0;
194 schema->next = tinfo->schemas;
195 schema->stylesheet_xsp = 0;
196 schema->include_snippet = 0;
197 tinfo->schemas = schema;
198 for (attr = ptr->properties; attr; attr = attr->next)
200 attr_content(attr, "identifier", &schema->identifier);
201 attr_content(attr, "name", &schema->name);
202 attr_content(attr, "stylesheet", &schema->stylesheet);
203 attr_content(attr, "default", &schema->default_schema);
204 attr_content(attr, "snippet", &schema->include_snippet);
206 if (schema->stylesheet)
207 schema->stylesheet_xsp =
208 xsltParseStylesheetFile(
209 (const xmlChar*) schema->stylesheet);
211 else if (!XML_STRCMP(ptr->name, "split"))
213 struct _xmlAttr *attr;
214 for (attr = ptr->properties; attr; attr = attr->next)
216 attr_content(attr, "level", &tinfo->split_level);
217 attr_content(attr, "path", &tinfo->split_path);
222 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
229 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
232 struct filter_schema *schema;
233 for (schema = tinfo->schemas; schema; schema = schema->next)
237 if (schema->identifier && !strcmp(schema->identifier, est))
239 if (schema->name && !strcmp(schema->name, est))
242 if (schema->default_schema)
248 static void filter_config(void *clientData, Res res, const char *args)
250 struct filter_info *tinfo = clientData;
252 args = "xsltfilter.xml";
253 if (tinfo->fname && !strcmp(args, tinfo->fname))
255 destroy_schemas(tinfo);
256 create_schemas(tinfo, args);
259 static void filter_destroy(void *clientData)
261 struct filter_info *tinfo = clientData;
262 destroy_schemas(tinfo);
264 xmlFreeTextReader(tinfo->reader);
265 odr_destroy(tinfo->odr);
269 static int ioread_ex(void *context, char *buffer, int len)
271 struct recExtractCtrl *p = context;
272 return (*p->readf)(p->fh, buffer, len);
275 static int ioclose_ex(void *context)
280 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
281 xmlNodePtr ptr, RecWord *recWord)
283 for(; ptr; ptr = ptr->next)
285 index_cdata(tinfo, ctrl, ptr->children, recWord);
286 if (ptr->type != XML_TEXT_NODE)
288 recWord->term_buf = (const char *)ptr->content;
289 recWord->term_len = XML_STRLEN(ptr->content);
290 (*ctrl->tokenAdd)(recWord);
294 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
295 xmlNodePtr ptr, RecWord *recWord)
297 for(; ptr; ptr = ptr->next)
299 index_node(tinfo, ctrl, ptr->children, recWord);
300 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
301 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
303 if (!XML_STRCMP(ptr->name, "index"))
305 const char *name_str = 0;
306 const char *type_str = 0;
307 const char *xpath_str = 0;
308 struct _xmlAttr *attr;
309 for (attr = ptr->properties; attr; attr = attr->next)
311 attr_content(attr, "name", &name_str);
312 attr_content(attr, "xpath", &xpath_str);
313 attr_content(attr, "type", &type_str);
317 int prev_type = recWord->index_type; /* save default type */
319 if (type_str && *type_str)
320 recWord->index_type = *type_str; /* type was given */
321 recWord->index_name = name_str;
322 index_cdata(tinfo, ctrl, ptr->children, recWord);
324 recWord->index_type = prev_type; /* restore it again */
330 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
331 xmlNodePtr ptr, RecWord *recWord)
333 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
334 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
335 && !XML_STRCMP(ptr->name, "record"))
337 const char *type_str = "update";
338 const char *id_str = 0;
339 const char *rank_str = 0;
340 struct _xmlAttr *attr;
341 for (attr = ptr->properties; attr; attr = attr->next)
343 attr_content(attr, "type", &type_str);
344 attr_content(attr, "id", &id_str);
345 attr_content(attr, "rank", &rank_str);
348 sscanf(id_str, "%255s", ctrl->match_criteria);
351 ctrl->staticrank = atoi(rank_str);
352 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
355 yaz_log(YLOG_LOG, "no rank");
359 index_node(tinfo, ctrl, ptr, recWord);
362 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
366 const char *params[10];
370 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
373 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
375 (*p->init)(p, &recWord);
377 if (schema && schema->stylesheet_xsp)
381 xsltApplyStylesheet(schema->stylesheet_xsp,
383 if (p->flagShowRecords)
385 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
386 fwrite(buf_out, len_out, 1, stdout);
389 root_ptr = xmlDocGetRootElement(resDoc);
391 index_record(tinfo, p, root_ptr, &recWord);
394 yaz_log(YLOG_WARN, "No root for index XML record."
395 " split_level=%s stylesheet=%s",
396 tinfo->split_level, schema->stylesheet);
400 xmlDocDumpMemory(doc, &buf_out, &len_out);
401 if (p->flagShowRecords)
402 fwrite(buf_out, len_out, 1, stdout);
403 (*p->setStoreData)(p, buf_out, len_out);
407 return RECCTRL_EXTRACT_OK;
410 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
417 xmlFreeTextReader(tinfo->reader);
418 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
425 return RECCTRL_EXTRACT_ERROR_GENERIC;
427 if (tinfo->split_level)
428 split_depth = atoi(tinfo->split_level);
429 ret = xmlTextReaderRead(tinfo->reader);
431 int type = xmlTextReaderNodeType(tinfo->reader);
432 int depth = xmlTextReaderDepth(tinfo->reader);
433 if (split_depth == 0 ||
435 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
437 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
438 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
439 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
441 xmlDocSetRootElement(doc, ptr2);
443 return extract_doc(tinfo, p, doc);
445 ret = xmlTextReaderRead(tinfo->reader);
447 xmlFreeTextReader(tinfo->reader);
449 return RECCTRL_EXTRACT_EOF;
452 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
454 if (p->first_record) /* only one record per stream */
456 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
462 return RECCTRL_EXTRACT_ERROR_GENERIC;
464 return extract_doc(tinfo, p, doc);
467 return RECCTRL_EXTRACT_EOF;
470 static int filter_extract(void *clientData, struct recExtractCtrl *p)
472 struct filter_info *tinfo = clientData;
474 odr_reset(tinfo->odr);
476 if (tinfo->split_level == 0 && tinfo->split_path == 0)
477 return extract_full(tinfo, p);
480 return extract_split(tinfo, p);
484 static int ioread_ret(void *context, char *buffer, int len)
486 struct recRetrieveCtrl *p = context;
487 return (*p->readf)(p->fh, buffer, len);
490 static int ioclose_ret(void *context)
496 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
499 const char *xml_doc_str;
501 WRBUF wrbuf = wrbuf_alloc();
502 zebra_snippets *res =
503 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
504 zebra_snippet_word *w = zebra_snippets_list(res);
507 wrbuf_printf(wrbuf, "\'");
509 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
510 for (; w; w = w->next)
514 else if (ord != w->ord)
518 wrbuf_printf(wrbuf, "%s%s%s ",
521 w->match ? "*" : "");
524 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
526 (w->match ? "match='1'" : ""));
527 wrbuf_xmlputs(wrbuf, w->term);
528 wrbuf_printf(wrbuf, "</term>\n");
532 wrbuf_printf(wrbuf, "\'");
534 wrbuf_printf(wrbuf, "</snippet>\n");
536 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
538 zebra_snippets_destroy(res);
539 wrbuf_free(wrbuf, 1);
543 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
545 const char *esn = zebra_xslt_ns;
546 const char *params[10];
547 struct filter_info *tinfo = clientData;
550 struct filter_schema *schema;
551 int window_size = -1;
555 if (p->comp->which != Z_RecordComp_simple
556 || p->comp->u.simple->which != Z_ElementSetNames_generic)
558 p->diagnostic = YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP;
561 esn = p->comp->u.simple->u.generic;
563 schema = lookup_schema(tinfo, esn);
567 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
571 if (schema->include_snippet)
572 window_size = atoi(schema->include_snippet);
575 set_param_str(params, "schema", esn, p->odr);
577 set_param_str(params, "filename", p->fname, p->odr);
579 set_param_int(params, "score", p->score, p->odr);
580 set_param_int(params, "size", p->recordSize, p->odr);
581 set_param_int(params, "id", p->localno, p->odr);
583 if (window_size >= 0)
584 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
586 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
592 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
596 if (window_size >= 0)
598 xmlNodePtr node = xmlDocGetRootElement(doc);
599 const char *snippet_str = snippet_doc(p, 0, window_size);
600 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
601 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
603 if (!schema->stylesheet_xsp)
607 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
613 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
615 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
619 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
621 p->output_format = VAL_TEXT_XML;
622 p->rec_len = len_out;
623 p->rec_buf = odr_malloc(p->odr, p->rec_len);
624 memcpy(p->rec_buf, buf_out, p->rec_len);
628 else if (p->output_format == VAL_SUTRS)
632 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
634 p->output_format = VAL_SUTRS;
635 p->rec_len = len_out;
636 p->rec_buf = odr_malloc(p->odr, p->rec_len);
637 memcpy(p->rec_buf, buf_out, p->rec_len);
643 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
649 static struct recType filter_type_xslt = {
660 #ifdef IDZEBRA_STATIC_XSLT