1 /* $Id: alvis.c,v 1.4 2006-11-03 23:17:08 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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>
36 #include <libxslt/xsltutils.h>
39 #include <libexslt/exslt.h>
42 #include <idzebra/util.h>
43 #include <idzebra/recctrl.h>
45 struct filter_schema {
47 const char *identifier;
48 const char *stylesheet;
49 struct filter_schema *next;
50 const char *default_schema;
51 /* char default_schema; */
52 const char *include_snippet;
53 xsltStylesheetPtr stylesheet_xsp;
60 const char *profile_path;
61 const char *split_level;
62 const char *split_path;
64 struct filter_schema *schemas;
65 xmlTextReaderPtr reader;
68 #define ZEBRA_SCHEMA_XSLT_NS "http://indexdata.dk/zebra/xslt/1"
70 #define XML_STRCMP(a,b) strcmp((char*)a, b)
71 #define XML_STRLEN(a) strlen((char*)a)
73 static const char *zebra_xslt_ns = ZEBRA_SCHEMA_XSLT_NS;
75 static void set_param_xml(const char **params, const char *name,
76 const char *value, ODR odr)
85 static void set_param_str(const char **params, const char *name,
86 const char *value, ODR odr)
88 char *quoted = odr_malloc(odr, 3 + strlen(value));
89 sprintf(quoted, "'%s'", value);
97 static void set_param_int(const char **params, const char *name,
100 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
103 sprintf(quoted, "'" ZINT_FORMAT "'", value);
109 #define ENABLE_INPUT_CALLBACK 0
111 #if ENABLE_INPUT_CALLBACK
112 static int zebra_xmlInputMatchCallback (char const *filename)
114 yaz_log(YLOG_LOG, "match %s", filename);
118 static void * zebra_xmlInputOpenCallback (char const *filename)
123 static int zebra_xmlInputReadCallback (void * context, char * buffer, int len)
128 static int zebra_xmlInputCloseCallback (void * context)
134 static void *filter_init(Res res, RecType recType)
136 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
139 tinfo->full_name = 0;
140 tinfo->profile_path = 0;
141 tinfo->split_level = 0;
142 tinfo->split_path = 0;
143 tinfo->odr = odr_createmem(ODR_ENCODE);
151 #if ENABLE_INPUT_CALLBACK
152 xmlRegisterDefaultInputCallbacks();
153 xmlRegisterInputCallbacks(zebra_xmlInputMatchCallback,
154 zebra_xmlInputOpenCallback,
155 zebra_xmlInputReadCallback,
156 zebra_xmlInputCloseCallback);
161 static int attr_content(struct _xmlAttr *attr, const char *name,
162 const char **dst_content)
164 if (!XML_STRCMP(attr->name, name) && attr->children &&
165 attr->children->type == XML_TEXT_NODE)
167 *dst_content = (const char *)(attr->children->content);
173 static void destroy_schemas(struct filter_info *tinfo)
175 struct filter_schema *schema = tinfo->schemas;
178 struct filter_schema *schema_next = schema->next;
179 if (schema->stylesheet_xsp)
180 xsltFreeStylesheet(schema->stylesheet_xsp);
182 schema = schema_next;
187 xmlFreeDoc(tinfo->doc);
191 static ZEBRA_RES create_schemas(struct filter_info *tinfo, const char *fname)
193 char tmp_full_name[1024];
195 tinfo->fname = xstrdup(fname);
197 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
198 NULL, tmp_full_name))
199 tinfo->full_name = xstrdup(tmp_full_name);
201 tinfo->full_name = xstrdup(tinfo->fname);
203 yaz_log(YLOG_LOG, "alvis filter: loading config file %s", tinfo->full_name);
205 tinfo->doc = xmlParseFile(tinfo->full_name);
208 yaz_log(YLOG_WARN, "alvis filter: could not parse config file %s",
214 ptr = xmlDocGetRootElement(tinfo->doc);
215 if (!ptr || ptr->type != XML_ELEMENT_NODE ||
216 XML_STRCMP(ptr->name, "schemaInfo")){
218 "alvis filter: config file %s :"
219 " expected root element <schemaInfo>",
224 for (ptr = ptr->children; ptr; ptr = ptr->next)
226 if (ptr->type != XML_ELEMENT_NODE)
228 if (!XML_STRCMP(ptr->name, "schema"))
230 char tmp_xslt_full_name[1024];
231 struct _xmlAttr *attr;
232 struct filter_schema *schema = xmalloc(sizeof(*schema));
234 schema->identifier = 0;
235 schema->stylesheet = 0;
236 schema->default_schema = 0;
237 schema->next = tinfo->schemas;
238 schema->stylesheet_xsp = 0;
239 schema->include_snippet = 0;
240 tinfo->schemas = schema;
241 for (attr = ptr->properties; attr; attr = attr->next)
243 attr_content(attr, "identifier", &schema->identifier);
244 attr_content(attr, "name", &schema->name);
245 attr_content(attr, "stylesheet", &schema->stylesheet);
246 attr_content(attr, "default", &schema->default_schema);
247 attr_content(attr, "snippet", &schema->include_snippet);
249 /*yaz_log(YLOG_LOG, "XSLT add %s %s %s",
250 schema->name, schema->identifier, schema->stylesheet); */
252 /* find requested schema */
254 if (schema->stylesheet){
255 yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
256 NULL, tmp_xslt_full_name);
257 schema->stylesheet_xsp
258 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
259 if (!schema->stylesheet_xsp)
261 "alvis filter: could not parse xslt stylesheet %s",
267 else if (!XML_STRCMP(ptr->name, "split"))
269 struct _xmlAttr *attr;
270 for (attr = ptr->properties; attr; attr = attr->next)
272 attr_content(attr, "level", &tinfo->split_level);
273 attr_content(attr, "path", &tinfo->split_path);
278 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
285 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
288 struct filter_schema *schema;
290 for (schema = tinfo->schemas; schema; schema = schema->next)
292 /* find requested schema */
295 if (schema->identifier && !strcmp(schema->identifier, est))
298 if (schema->name && !strcmp(schema->name, est))
301 /* or return default schema if defined */
302 else if (schema->default_schema)
306 /* return first schema if no default schema defined */
308 return tinfo->schemas;
313 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
315 struct filter_info *tinfo = clientData;
316 if (!args || !*args){
317 yaz_log(YLOG_WARN, "alvis filter: need config file");
321 if (tinfo->fname && !strcmp(args, tinfo->fname))
325 /* = res_get_def(res, "profilePath", DEFAULT_PROFILE_PATH); */
326 = res_get(res, "profilePath");
327 yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
329 destroy_schemas(tinfo);
330 create_schemas(tinfo, args);
334 static void filter_destroy(void *clientData)
336 struct filter_info *tinfo = clientData;
337 destroy_schemas(tinfo);
339 xmlFreeTextReader(tinfo->reader);
340 odr_destroy(tinfo->odr);
344 static int ioread_ex(void *context, char *buffer, int len)
346 struct recExtractCtrl *p = context;
347 return p->stream->readf(p->stream, buffer, len);
350 static int ioclose_ex(void *context)
355 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
356 xmlNodePtr ptr, RecWord *recWord)
358 for(; ptr; ptr = ptr->next)
360 index_cdata(tinfo, ctrl, ptr->children, recWord);
361 if (ptr->type != XML_TEXT_NODE)
363 recWord->term_buf = (const char *)ptr->content;
364 recWord->term_len = XML_STRLEN(ptr->content);
365 (*ctrl->tokenAdd)(recWord);
369 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
370 xmlNodePtr ptr, RecWord *recWord)
372 for(; ptr; ptr = ptr->next)
374 index_node(tinfo, ctrl, ptr->children, recWord);
375 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
376 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
378 if (!XML_STRCMP(ptr->name, "index"))
380 const char *name_str = 0;
381 const char *type_str = 0;
382 const char *xpath_str = 0;
383 struct _xmlAttr *attr;
384 for (attr = ptr->properties; attr; attr = attr->next)
386 attr_content(attr, "name", &name_str);
387 attr_content(attr, "xpath", &xpath_str);
388 attr_content(attr, "type", &type_str);
392 int prev_type = recWord->index_type; /* save default type */
394 if (type_str && *type_str)
395 recWord->index_type = *type_str; /* type was given */
396 recWord->index_name = name_str;
397 index_cdata(tinfo, ctrl, ptr->children, recWord);
399 recWord->index_type = prev_type; /* restore it again */
405 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
406 xmlNodePtr ptr, RecWord *recWord)
408 const char *type_str = "update";
410 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
411 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
412 && !XML_STRCMP(ptr->name, "record"))
414 const char *id_str = 0;
415 const char *rank_str = 0;
416 struct _xmlAttr *attr;
417 for (attr = ptr->properties; attr; attr = attr->next)
419 attr_content(attr, "type", &type_str);
420 attr_content(attr, "id", &id_str);
421 attr_content(attr, "rank", &rank_str);
424 sscanf(id_str, "%255s", ctrl->match_criteria);
427 ctrl->staticrank = atoi(rank_str);
432 if (!strcmp("update", type_str))
433 index_node(tinfo, ctrl, ptr, recWord);
434 else if (!strcmp("delete", type_str))
435 yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
437 yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'",
441 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
445 const char *params[10];
449 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
452 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
454 (*p->init)(p, &recWord);
456 if (schema && schema->stylesheet_xsp)
460 xsltApplyStylesheet(schema->stylesheet_xsp,
462 if (p->flagShowRecords)
464 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
465 fwrite(buf_out, len_out, 1, stdout);
468 root_ptr = xmlDocGetRootElement(resDoc);
470 index_record(tinfo, p, root_ptr, &recWord);
473 yaz_log(YLOG_WARN, "No root for index XML record."
474 " split_level=%s stylesheet=%s",
475 tinfo->split_level, schema->stylesheet);
479 xmlDocDumpMemory(doc, &buf_out, &len_out);
480 if (p->flagShowRecords)
481 fwrite(buf_out, len_out, 1, stdout);
482 (*p->setStoreData)(p, buf_out, len_out);
486 return RECCTRL_EXTRACT_OK;
489 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
496 xmlFreeTextReader(tinfo->reader);
497 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
504 return RECCTRL_EXTRACT_ERROR_GENERIC;
506 if (tinfo->split_level)
507 split_depth = atoi(tinfo->split_level);
508 ret = xmlTextReaderRead(tinfo->reader);
510 int type = xmlTextReaderNodeType(tinfo->reader);
511 int depth = xmlTextReaderDepth(tinfo->reader);
512 if (split_depth == 0 ||
514 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
516 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
517 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
518 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
520 xmlDocSetRootElement(doc, ptr2);
522 return extract_doc(tinfo, p, doc);
524 ret = xmlTextReaderRead(tinfo->reader);
526 xmlFreeTextReader(tinfo->reader);
528 return RECCTRL_EXTRACT_EOF;
531 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
533 if (p->first_record) /* only one record per stream */
535 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
541 return RECCTRL_EXTRACT_ERROR_GENERIC;
543 return extract_doc(tinfo, p, doc);
546 return RECCTRL_EXTRACT_EOF;
549 static int filter_extract(void *clientData, struct recExtractCtrl *p)
551 struct filter_info *tinfo = clientData;
553 odr_reset(tinfo->odr);
555 if (tinfo->split_level == 0 && tinfo->split_path == 0)
556 return extract_full(tinfo, p);
559 return extract_split(tinfo, p);
563 static int ioread_ret(void *context, char *buffer, int len)
565 struct recRetrieveCtrl *p = context;
566 return p->stream->readf(p->stream, buffer, len);
569 static int ioclose_ret(void *context)
575 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
578 const char *xml_doc_str;
580 WRBUF wrbuf = wrbuf_alloc();
581 zebra_snippets *res =
582 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
583 zebra_snippet_word *w = zebra_snippets_list(res);
586 wrbuf_printf(wrbuf, "\'");
588 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
589 for (; w; w = w->next)
593 else if (ord != w->ord)
597 wrbuf_printf(wrbuf, "%s%s%s ",
600 w->match ? "*" : "");
603 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
605 (w->match ? "match='1'" : ""));
606 wrbuf_xmlputs(wrbuf, w->term);
607 wrbuf_printf(wrbuf, "</term>\n");
611 wrbuf_printf(wrbuf, "\'");
613 wrbuf_printf(wrbuf, "</snippet>\n");
615 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
617 zebra_snippets_destroy(res);
618 wrbuf_free(wrbuf, 1);
622 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
624 /* const char *esn = zebra_xslt_ns; */
626 const char *params[32];
627 struct filter_info *tinfo = clientData;
630 struct filter_schema *schema;
631 int window_size = -1;
635 if (p->comp->which == Z_RecordComp_simple
636 && p->comp->u.simple->which == Z_ElementSetNames_generic)
638 esn = p->comp->u.simple->u.generic;
640 else if (p->comp->which == Z_RecordComp_complex
641 && p->comp->u.complex->generic->elementSpec
642 && p->comp->u.complex->generic->elementSpec->which ==
643 Z_ElementSpec_elementSetName)
645 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
648 schema = lookup_schema(tinfo, esn);
652 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
656 if (schema->include_snippet)
657 window_size = atoi(schema->include_snippet);
660 set_param_int(params, "id", p->localno, p->odr);
662 set_param_str(params, "filename", p->fname, p->odr);
663 if (p->staticrank >= 0)
664 set_param_int(params, "rank", p->staticrank, p->odr);
667 set_param_str(params, "schema", esn, p->odr);
670 set_param_str(params, "schema", schema->name, p->odr);
671 else if (schema->identifier)
672 set_param_str(params, "schema", schema->identifier, p->odr);
674 set_param_str(params, "schema", "", p->odr);
677 set_param_int(params, "score", p->score, p->odr);
678 set_param_int(params, "size", p->recordSize, p->odr);
680 if (window_size >= 0)
681 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
683 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
689 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
693 if (window_size >= 0)
695 xmlNodePtr node = xmlDocGetRootElement(doc);
696 const char *snippet_str = snippet_doc(p, 0, window_size);
697 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
698 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
700 if (!schema->stylesheet_xsp)
704 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
710 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
712 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
717 xsltSaveResultToString(&buf_out, &len_out, resDoc,
718 schema->stylesheet_xsp);
720 p->output_format = VAL_TEXT_XML;
721 p->rec_len = len_out;
722 p->rec_buf = odr_malloc(p->odr, p->rec_len);
723 memcpy(p->rec_buf, buf_out, p->rec_len);
726 else if (p->output_format == VAL_SUTRS)
731 xsltSaveResultToString(&buf_out, &len_out, resDoc,
732 schema->stylesheet_xsp);
734 p->output_format = VAL_SUTRS;
735 p->rec_len = len_out;
736 p->rec_buf = odr_malloc(p->odr, p->rec_len);
737 memcpy(p->rec_buf, buf_out, p->rec_len);
743 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
749 static struct recType filter_type = {
760 #ifdef IDZEBRA_STATIC_ALVIS
773 * indent-tabs-mode: nil
775 * vim: shiftwidth=4 tabstop=8 expandtab