1 /* $Id: xslt.c,v 1.25 2006-05-24 18:31:33 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)
276 /* find requested schema */
279 if (schema->identifier && !strcmp(schema->identifier, est))
282 if (schema->name && !strcmp(schema->name, est))
285 /* or return default schema if defined */
286 else if (schema->default_schema)
290 /* return first schema if no default schema defined */
292 return tinfo->schemas;
297 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
299 struct filter_xslt_info *tinfo = clientData;
300 if (!args || !*args){
301 yaz_log(YLOG_WARN, "xslt filter: need config file");
305 if (tinfo->fname && !strcmp(args, tinfo->fname))
309 /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
310 = res_get(res, "profilePath");
311 yaz_log(YLOG_LOG, "xslt filter: profilePath %s", tinfo->profile_path);
313 destroy_schemas(tinfo);
314 create_schemas(tinfo, args);
318 static void filter_destroy(void *clientData)
320 struct filter_xslt_info *tinfo = clientData;
321 destroy_schemas(tinfo);
323 xmlFreeTextReader(tinfo->reader);
324 odr_destroy(tinfo->odr);
328 static int ioread_ex(void *context, char *buffer, int len)
330 struct recExtractCtrl *p = context;
331 return (*p->readf)(p->fh, buffer, len);
334 static int ioclose_ex(void *context)
339 static void index_cdata(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
340 xmlNodePtr ptr, RecWord *recWord)
342 for(; ptr; ptr = ptr->next)
344 index_cdata(tinfo, ctrl, ptr->children, recWord);
345 if (ptr->type != XML_TEXT_NODE)
347 recWord->term_buf = (const char *)ptr->content;
348 recWord->term_len = XML_STRLEN(ptr->content);
349 (*ctrl->tokenAdd)(recWord);
353 static void index_node(struct filter_xslt_info *tinfo, struct recExtractCtrl *ctrl,
354 xmlNodePtr ptr, RecWord *recWord)
356 for(; ptr; ptr = ptr->next)
358 index_node(tinfo, ctrl, ptr->children, recWord);
359 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
360 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
362 if (!XML_STRCMP(ptr->name, "index"))
364 const char *name_str = 0;
365 const char *type_str = 0;
366 const char *xpath_str = 0;
367 struct _xmlAttr *attr;
368 for (attr = ptr->properties; attr; attr = attr->next)
370 attr_content(attr, "name", &name_str);
371 attr_content(attr, "xpath", &xpath_str);
372 attr_content(attr, "type", &type_str);
376 int prev_type = recWord->index_type; /* save default type */
378 if (type_str && *type_str)
379 recWord->index_type = *type_str; /* type was given */
380 recWord->index_name = name_str;
381 index_cdata(tinfo, ctrl, ptr->children, recWord);
383 recWord->index_type = prev_type; /* restore it again */
389 static void index_record(struct filter_xslt_info *tinfo,struct recExtractCtrl *ctrl,
390 xmlNodePtr ptr, RecWord *recWord)
392 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
393 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
394 && !XML_STRCMP(ptr->name, "record"))
396 const char *type_str = "update";
397 const char *id_str = 0;
398 const char *rank_str = 0;
399 struct _xmlAttr *attr;
400 for (attr = ptr->properties; attr; attr = attr->next)
402 attr_content(attr, "type", &type_str);
403 attr_content(attr, "id", &id_str);
404 attr_content(attr, "rank", &rank_str);
407 sscanf(id_str, "%255s", ctrl->match_criteria);
410 ctrl->staticrank = atoi(rank_str);
411 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
414 yaz_log(YLOG_LOG, "no rank");
418 index_node(tinfo, ctrl, ptr, recWord);
421 static int extract_doc(struct filter_xslt_info *tinfo, struct recExtractCtrl *p,
425 const char *params[10];
429 struct filter_xslt_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
432 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
434 (*p->init)(p, &recWord);
436 if (schema && schema->stylesheet_xsp)
440 xsltApplyStylesheet(schema->stylesheet_xsp,
442 if (p->flagShowRecords)
444 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
445 fwrite(buf_out, len_out, 1, stdout);
448 root_ptr = xmlDocGetRootElement(resDoc);
450 index_record(tinfo, p, root_ptr, &recWord);
453 yaz_log(YLOG_WARN, "No root for index XML record."
454 " split_level=%s stylesheet=%s",
455 tinfo->split_level, schema->stylesheet);
459 xmlDocDumpMemory(doc, &buf_out, &len_out);
460 if (p->flagShowRecords)
461 fwrite(buf_out, len_out, 1, stdout);
462 (*p->setStoreData)(p, buf_out, len_out);
466 return RECCTRL_EXTRACT_OK;
469 static int extract_split(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
476 xmlFreeTextReader(tinfo->reader);
477 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
484 return RECCTRL_EXTRACT_ERROR_GENERIC;
486 if (tinfo->split_level)
487 split_depth = atoi(tinfo->split_level);
488 ret = xmlTextReaderRead(tinfo->reader);
490 int type = xmlTextReaderNodeType(tinfo->reader);
491 int depth = xmlTextReaderDepth(tinfo->reader);
492 if (split_depth == 0 ||
494 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
496 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
497 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
498 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
500 xmlDocSetRootElement(doc, ptr2);
502 return extract_doc(tinfo, p, doc);
504 ret = xmlTextReaderRead(tinfo->reader);
506 xmlFreeTextReader(tinfo->reader);
508 return RECCTRL_EXTRACT_EOF;
511 static int extract_full(struct filter_xslt_info *tinfo, struct recExtractCtrl *p)
513 if (p->first_record) /* only one record per stream */
515 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
521 return RECCTRL_EXTRACT_ERROR_GENERIC;
523 return extract_doc(tinfo, p, doc);
526 return RECCTRL_EXTRACT_EOF;
529 static int filter_extract(void *clientData, struct recExtractCtrl *p)
531 struct filter_xslt_info *tinfo = clientData;
533 odr_reset(tinfo->odr);
535 if (tinfo->split_level == 0 && tinfo->split_path == 0)
536 return extract_full(tinfo, p);
539 return extract_split(tinfo, p);
543 static int ioread_ret(void *context, char *buffer, int len)
545 struct recRetrieveCtrl *p = context;
546 return (*p->readf)(p->fh, buffer, len);
549 static int ioclose_ret(void *context)
555 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
558 const char *xml_doc_str;
560 WRBUF wrbuf = wrbuf_alloc();
561 zebra_snippets *res =
562 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
563 zebra_snippet_word *w = zebra_snippets_list(res);
566 wrbuf_printf(wrbuf, "\'");
568 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
569 for (; w; w = w->next)
573 else if (ord != w->ord)
577 wrbuf_printf(wrbuf, "%s%s%s ",
580 w->match ? "*" : "");
583 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
585 (w->match ? "match='1'" : ""));
586 wrbuf_xmlputs(wrbuf, w->term);
587 wrbuf_printf(wrbuf, "</term>\n");
591 wrbuf_printf(wrbuf, "\'");
593 wrbuf_printf(wrbuf, "</snippet>\n");
595 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
597 zebra_snippets_destroy(res);
598 wrbuf_free(wrbuf, 1);
602 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
605 const char *params[32];
606 struct filter_xslt_info *tinfo = clientData;
609 struct filter_xslt_schema *schema;
610 int window_size = -1;
614 if (p->comp->which == Z_RecordComp_simple
615 && p->comp->u.simple->which == Z_ElementSetNames_generic)
617 esn = p->comp->u.simple->u.generic;
619 else if (p->comp->which == Z_RecordComp_complex
620 && p->comp->u.complex->generic->elementSpec
621 && p->comp->u.complex->generic->elementSpec->which ==
622 Z_ElementSpec_elementSetName)
624 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
627 schema = lookup_schema(tinfo, esn);
631 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
635 if (schema->include_snippet)
636 window_size = atoi(schema->include_snippet);
639 set_param_int(params, "id", p->localno, p->odr);
641 set_param_str(params, "filename", p->fname, p->odr);
642 if (p->staticrank >= 0)
643 set_param_int(params, "rank", p->staticrank, p->odr);
646 set_param_str(params, "schema", esn, p->odr);
649 set_param_str(params, "schema", schema->name, p->odr);
650 else if (schema->identifier)
651 set_param_str(params, "schema", schema->identifier, p->odr);
653 set_param_str(params, "schema", "", p->odr);
656 set_param_int(params, "score", p->score, p->odr);
657 set_param_int(params, "size", p->recordSize, p->odr);
659 if (window_size >= 0)
660 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
662 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
668 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
672 if (window_size >= 0)
674 xmlNodePtr node = xmlDocGetRootElement(doc);
675 const char *snippet_str = snippet_doc(p, 0, window_size);
676 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
677 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
679 if (!schema->stylesheet_xsp)
683 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
689 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
691 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
696 xsltSaveResultToString(&buf_out, &len_out, resDoc,
697 schema->stylesheet_xsp);
699 p->output_format = VAL_TEXT_XML;
700 p->rec_len = len_out;
701 p->rec_buf = odr_malloc(p->odr, p->rec_len);
702 memcpy(p->rec_buf, buf_out, p->rec_len);
706 else if (p->output_format == VAL_SUTRS)
711 xsltSaveResultToString(&buf_out, &len_out, resDoc,
712 schema->stylesheet_xsp);
714 p->output_format = VAL_SUTRS;
715 p->rec_len = len_out;
716 p->rec_buf = odr_malloc(p->odr, p->rec_len);
717 memcpy(p->rec_buf, buf_out, p->rec_len);
723 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
729 static struct recType filter_type = {
740 #ifdef IDZEBRA_STATIC_XSLT
753 * indent-tabs-mode: nil
755 * vim: shiftwidth=4 tabstop=8 expandtab