1 /* $Id: xslt.c,v 1.21 2006-05-10 08:13:31 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 <yaz/tpath.h>
30 #include <libxml/xmlversion.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xmlIO.h>
34 #include <libxml/xmlreader.h>
35 #include <libxslt/transform.h>
37 #include <idzebra/util.h>
38 #include <idzebra/recctrl.h>
40 struct filter_xslt_schema {
42 const char *identifier;
43 const char *stylesheet;
44 struct filter_xslt_schema *next;
45 const char *default_schema;
46 const char *include_snippet;
47 xsltStylesheetPtr stylesheet_xsp;
50 struct filter_xslt_info {
54 const char *profile_path;
55 const char *split_level;
56 const char *split_path;
58 struct filter_xslt_schema *schemas;
59 xmlTextReaderPtr reader;
63 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
65 #define XML_STRCMP(a,b) strcmp((char*)a, b)
66 #define XML_STRLEN(a) strlen((char*)a)
68 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
70 static void set_param_xml(const char **params, const char *name,
71 const char *value, ODR odr)
80 static void set_param_str(const char **params, const char *name,
81 const char *value, ODR odr)
83 char *quoted = odr_malloc(odr, 3 + strlen(value));
84 sprintf(quoted, "'%s'", value);
92 static void set_param_int(const char **params, const char *name,
95 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
98 sprintf(quoted, "'" ZINT_FORMAT "'", value);
104 #define ENABLE_INPUT_CALLBACK 0
106 #if ENABLE_INPUT_CALLBACK
107 static int zebra_xmlInputMatchCallback (char const *filename)
109 yaz_log(YLOG_LOG, "match %s", filename);
113 static void * zebra_xmlInputOpenCallback (char const *filename)
118 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
123 static int zebra_xmlInputCloseCallback (void * context)
129 static void *filter_init(Res res, RecType recType)
131 struct filter_xslt_info *tinfo
132 = (struct filter_xslt_info *) xmalloc(sizeof(*tinfo));
135 tinfo->full_name = 0;
136 tinfo->profile_path = 0;
137 tinfo->split_level = 0;
138 tinfo->split_path = 0;
139 tinfo->odr = odr_createmem(ODR_ENCODE);
143 #if ENABLE_INPUT_CALLBACK
144 xmlRegisterDefaultInputCallbacks();
145 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
146 zebra_xmlInputOpenCallback,
147 zebra_xmlInputReadCallback,
148 zebra_xmlInputCloseCallback);
153 static int attr_content(struct _xmlAttr *attr, const char *name,
154 const char **dst_content)
156 if (!XML_STRCMP(attr->name, name) && attr->children &&
157 attr->children->type == XML_TEXT_NODE)
159 *dst_content = (const char *)(attr->children->content);
165 static void destroy_schemas(struct filter_xslt_info *tinfo)
167 struct filter_xslt_schema *schema = tinfo->schemas;
170 struct filter_xslt_schema *schema_next = schema->next;
171 if (schema->stylesheet_xsp)
172 xsltFreeStylesheet(schema->stylesheet_xsp);
174 schema = schema_next;
179 xmlFreeDoc(tinfo->doc);
183 static ZEBRA_RES create_schemas(struct filter_xslt_info *tinfo,
186 char tmp_full_name[1024];
188 tinfo->fname = xstrdup(fname);
190 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
191 NULL, tmp_full_name))
192 tinfo->full_name = xstrdup(tmp_full_name);
194 tinfo->full_name = xstrdup(tinfo->fname);
196 yaz_log(YLOG_LOG, "xslt filter: loading config file %s", tinfo->full_name);
198 tinfo->doc = xmlParseFile(tinfo->full_name);
200 yaz_log(YLOG_WARN, "xslt filter: could not parse config file %s",
205 ptr = xmlDocGetRootElement(tinfo->doc);
206 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
207 XML_STRCMP(ptr->name, "schemaInfo")){
209 "xslt filter: config file %s :"
210 " expected root element <schemaInfo>",
215 for (ptr = ptr->children; ptr; ptr = ptr->next)
217 if (ptr->type != XML_ELEMENT_NODE)
219 if (!XML_STRCMP(ptr->name, "schema"))
221 char tmp_xslt_full_name[1024];
222 struct _xmlAttr *attr;
223 struct filter_xslt_schema *schema = xmalloc(sizeof(*schema));
225 schema->identifier = 0;
226 schema->stylesheet = 0;
227 schema->default_schema = 0;
228 schema->next = tinfo->schemas;
229 schema->stylesheet_xsp = 0;
230 schema->include_snippet = 0;
231 tinfo->schemas = schema;
232 for (attr = ptr->properties; attr; attr = attr->next)
234 attr_content(attr, "identifier", &schema->identifier);
235 attr_content(attr, "name", &schema->name);
236 attr_content(attr, "stylesheet", &schema->stylesheet);
237 attr_content(attr, "default", &schema->default_schema);
238 attr_content(attr, "snippet", &schema->include_snippet);
240 if (schema->stylesheet){
241 yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
242 NULL, tmp_xslt_full_name);
243 schema->stylesheet_xsp
244 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
245 if (!schema->stylesheet_xsp)
247 "xslt filter: could not parse xslt stylesheet %s",
252 else if (!XML_STRCMP(ptr->name, "split"))
254 struct _xmlAttr *attr;
255 for (attr = ptr->properties; attr; attr = attr->next)
257 attr_content(attr, "level", &tinfo->split_level);
258 attr_content(attr, "path", &tinfo->split_path);
263 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
270 static struct filter_xslt_schema *lookup_schema(struct filter_xslt_info *tinfo,
273 struct filter_xslt_schema *schema;
274 for (schema = tinfo->schemas; schema; schema = schema->next)
278 if (schema->identifier && !strcmp(schema->identifier, est))
280 if (schema->name && !strcmp(schema->name, est))
283 if (schema->default_schema)
289 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
291 struct filter_xslt_info *tinfo = clientData;
292 if (!args || !*args){
293 yaz_log(YLOG_WARN, "xslt filter: need config file");
297 if (tinfo->fname && !strcmp(args, tinfo->fname))
301 /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
302 = res_get(res, "profilePath");
303 yaz_log(YLOG_LOG, "xslt filter: profilePath %s", tinfo->profile_path);
305 destroy_schemas(tinfo);
306 create_schemas(tinfo, args);
310 static void filter_destroy(void *clientData)
312 struct filter_xslt_info *tinfo = clientData;
313 destroy_schemas(tinfo);
315 xmlFreeTextReader(tinfo->reader);
316 odr_destroy(tinfo->odr);
320 static int ioread_ex(void *context, char *buffer, int len)
322 struct recExtractCtrl *p = context;
323 return (*p->readf)(p->fh, buffer, len);
326 static int ioclose_ex(void *context)
331 static void index_cdata(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
332 xmlNodePtr ptr, RecWord *recWord)
334 for(; ptr; ptr = ptr->next)
336 index_cdata(tinfo, ctrl, ptr->children, recWord);
337 if (ptr->type != XML_TEXT_NODE)
339 recWord->term_buf = (const char *)ptr->content;
340 recWord->term_len = XML_STRLEN(ptr->content);
341 (*ctrl->tokenAdd)(recWord);
345 static void index_node(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
346 xmlNodePtr ptr, RecWord *recWord)
348 for(; ptr; ptr = ptr->next)
350 index_node(tinfo, ctrl, ptr->children, recWord);
351 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
352 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
354 if (!XML_STRCMP(ptr->name, "index"))
356 const char *name_str = 0;
357 const char *type_str = 0;
358 const char *xpath_str = 0;
359 struct _xmlAttr *attr;
360 for (attr = ptr->properties; attr; attr = attr->next)
362 attr_content(attr, "name", &name_str);
363 attr_content(attr, "xpath", &xpath_str);
364 attr_content(attr, "type", &type_str);
368 int prev_type = recWord->index_type; /* save default type */
370 if (type_str && *type_str)
371 recWord->index_type = *type_str; /* type was given */
372 recWord->index_name = name_str;
373 index_cdata(tinfo, ctrl, ptr->children, recWord);
375 recWord->index_type = prev_type; /* restore it again */
381 static void index_record(struct filter_xslt_info *tinfo,struct recExtractCtrl *ctrl,
382 xmlNodePtr ptr, RecWord *recWord)
384 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
385 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
386 && !XML_STRCMP(ptr->name, "record"))
388 const char *type_str = "update";
389 const char *id_str = 0;
390 const char *rank_str = 0;
391 struct _xmlAttr *attr;
392 for (attr = ptr->properties; attr; attr = attr->next)
394 attr_content(attr, "type", &type_str);
395 attr_content(attr, "id", &id_str);
396 attr_content(attr, "rank", &rank_str);
399 sscanf(id_str, "%255s", ctrl->match_criteria);
402 ctrl->staticrank = atoi(rank_str);
403 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
406 yaz_log(YLOG_LOG, "no rank");
410 index_node(tinfo, ctrl, ptr, recWord);
413 static int extract_doc(struct filter_xslt_info *tinfo, struct recExtractCtrl *p,
417 const char *params[10];
421 struct filter_xslt_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
424 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
426 (*p->init)(p, &recWord);
428 if (schema && schema->stylesheet_xsp)
432 xsltApplyStylesheet(schema->stylesheet_xsp,
434 if (p->flagShowRecords)
436 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
437 fwrite(buf_out, len_out, 1, stdout);
440 root_ptr = xmlDocGetRootElement(resDoc);
442 index_record(tinfo, p, root_ptr, &recWord);
445 yaz_log(YLOG_WARN, "No root for index XML record."
446 " split_level=%s stylesheet=%s",
447 tinfo->split_level, schema->stylesheet);
451 xmlDocDumpMemory(doc, &buf_out, &len_out);
452 if (p->flagShowRecords)
453 fwrite(buf_out, len_out, 1, stdout);
454 (*p->setStoreData)(p, buf_out, len_out);
458 return RECCTRL_EXTRACT_OK;
461 static int extract_split(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
468 xmlFreeTextReader(tinfo->reader);
469 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
476 return RECCTRL_EXTRACT_ERROR_GENERIC;
478 if (tinfo->split_level)
479 split_depth = atoi(tinfo->split_level);
480 ret = xmlTextReaderRead(tinfo->reader);
482 int type = xmlTextReaderNodeType(tinfo->reader);
483 int depth = xmlTextReaderDepth(tinfo->reader);
484 if (split_depth == 0 ||
486 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
488 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
489 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
490 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
492 xmlDocSetRootElement(doc, ptr2);
494 return extract_doc(tinfo, p, doc);
496 ret = xmlTextReaderRead(tinfo->reader);
498 xmlFreeTextReader(tinfo->reader);
500 return RECCTRL_EXTRACT_EOF;
503 static int extract_full(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
505 if (p->first_record) /* only one record per stream */
507 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
513 return RECCTRL_EXTRACT_ERROR_GENERIC;
515 return extract_doc(tinfo, p, doc);
518 return RECCTRL_EXTRACT_EOF;
521 static int filter_extract(void *clientData, struct recExtractCtrl *p)
523 struct filter_xslt_info *tinfo = clientData;
525 odr_reset(tinfo->odr);
527 if (tinfo->split_level == 0 && tinfo->split_path == 0)
528 return extract_full(tinfo, p);
531 return extract_split(tinfo, p);
535 static int ioread_ret(void *context, char *buffer, int len)
537 struct recRetrieveCtrl *p = context;
538 return (*p->readf)(p->fh, buffer, len);
541 static int ioclose_ret(void *context)
547 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
550 const char *xml_doc_str;
552 WRBUF wrbuf = wrbuf_alloc();
553 zebra_snippets *res =
554 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
555 zebra_snippet_word *w = zebra_snippets_list(res);
558 wrbuf_printf(wrbuf, "\'");
560 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
561 for (; w; w = w->next)
565 else if (ord != w->ord)
569 wrbuf_printf(wrbuf, "%s%s%s ",
572 w->match ? "*" : "");
575 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
577 (w->match ? "match='1'" : ""));
578 wrbuf_xmlputs(wrbuf, w->term);
579 wrbuf_printf(wrbuf, "</term>\n");
583 wrbuf_printf(wrbuf, "\'");
585 wrbuf_printf(wrbuf, "</snippet>\n");
587 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
589 zebra_snippets_destroy(res);
590 wrbuf_free(wrbuf, 1);
594 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
596 const char *esn = zebra_xslt_ns;
597 const char *params[20];
598 struct filter_xslt_info *tinfo = clientData;
601 struct filter_xslt_schema *schema;
602 int window_size = -1;
606 if (p->comp->which == Z_RecordComp_simple
607 && p->comp->u.simple->which == Z_ElementSetNames_generic)
609 esn = p->comp->u.simple->u.generic;
611 else if (p->comp->which == Z_RecordComp_complex
612 && p->comp->u.complex->generic->elementSpec
613 && p->comp->u.complex->generic->elementSpec->which ==
614 Z_ElementSpec_elementSetName)
616 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
619 schema = lookup_schema(tinfo, esn);
623 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
627 if (schema->include_snippet)
628 window_size = atoi(schema->include_snippet);
631 set_param_int(params, "id", p->localno, p->odr);
633 set_param_str(params, "filename", p->fname, p->odr);
634 if (p->staticrank >= 0)
635 set_param_int(params, "rank", p->staticrank, p->odr);
636 set_param_str(params, "schema", esn, p->odr);
638 set_param_int(params, "score", p->score, p->odr);
639 set_param_int(params, "size", p->recordSize, p->odr);
641 if (window_size >= 0)
642 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
644 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
650 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
654 if (window_size >= 0)
656 xmlNodePtr node = xmlDocGetRootElement(doc);
657 const char *snippet_str = snippet_doc(p, 0, window_size);
658 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
659 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
661 if (!schema->stylesheet_xsp)
665 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
671 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
673 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
677 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
679 p->output_format = VAL_TEXT_XML;
680 p->rec_len = len_out;
681 p->rec_buf = odr_malloc(p->odr, p->rec_len);
682 memcpy(p->rec_buf, buf_out, p->rec_len);
686 else if (p->output_format == VAL_SUTRS)
690 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
692 p->output_format = VAL_SUTRS;
693 p->rec_len = len_out;
694 p->rec_buf = odr_malloc(p->odr, p->rec_len);
695 memcpy(p->rec_buf, buf_out, p->rec_len);
701 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
707 static struct recType filter_type = {
718 #ifdef IDZEBRA_STATIC_XSLT
731 * indent-tabs-mode: nil
733 * vim: shiftwidth=4 tabstop=8 expandtab