1 /* $Id: alvis.c,v 1.11 2006-05-10 08:13:28 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 ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
254 struct filter_info *tinfo = clientData;
257 if (tinfo->fname && !strcmp(args, tinfo->fname))
259 destroy_schemas(tinfo);
260 create_schemas(tinfo, args);
264 static void filter_destroy(void *clientData)
266 struct filter_info *tinfo = clientData;
267 destroy_schemas(tinfo);
269 xmlFreeTextReader(tinfo->reader);
270 odr_destroy(tinfo->odr);
274 static int ioread_ex(void *context, char *buffer, int len)
276 struct recExtractCtrl *p = context;
277 return (*p->readf)(p->fh, buffer, len);
280 static int ioclose_ex(void *context)
285 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
286 xmlNodePtr ptr, RecWord *recWord)
288 for(; ptr; ptr = ptr->next)
290 index_cdata(tinfo, ctrl, ptr->children, recWord);
291 if (ptr->type != XML_TEXT_NODE)
293 recWord->term_buf = (const char *)ptr->content;
294 recWord->term_len = XML_STRLEN(ptr->content);
295 (*ctrl->tokenAdd)(recWord);
299 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
300 xmlNodePtr ptr, RecWord *recWord)
302 for(; ptr; ptr = ptr->next)
304 index_node(tinfo, ctrl, ptr->children, recWord);
305 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
306 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
308 if (!XML_STRCMP(ptr->name, "index"))
310 const char *name_str = 0;
311 const char *type_str = 0;
312 const char *xpath_str = 0;
313 struct _xmlAttr *attr;
314 for (attr = ptr->properties; attr; attr = attr->next)
316 attr_content(attr, "name", &name_str);
317 attr_content(attr, "xpath", &xpath_str);
318 attr_content(attr, "type", &type_str);
322 int prev_type = recWord->index_type; /* save default type */
324 if (type_str && *type_str)
325 recWord->index_type = *type_str; /* type was given */
326 recWord->index_name = name_str;
327 index_cdata(tinfo, ctrl, ptr->children, recWord);
329 recWord->index_type = prev_type; /* restore it again */
335 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
336 xmlNodePtr ptr, RecWord *recWord)
338 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
339 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
340 && !XML_STRCMP(ptr->name, "record"))
342 const char *type_str = "update";
343 const char *id_str = 0;
344 const char *rank_str = 0;
345 struct _xmlAttr *attr;
346 for (attr = ptr->properties; attr; attr = attr->next)
348 attr_content(attr, "type", &type_str);
349 attr_content(attr, "id", &id_str);
350 attr_content(attr, "rank", &rank_str);
353 sscanf(id_str, "%255s", ctrl->match_criteria);
356 ctrl->staticrank = atoi(rank_str);
357 yaz_log(YLOG_LOG, "rank=%d",ctrl->staticrank);
360 yaz_log(YLOG_LOG, "no rank");
364 index_node(tinfo, ctrl, ptr, recWord);
367 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
371 const char *params[10];
375 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
378 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
380 (*p->init)(p, &recWord);
382 if (schema && schema->stylesheet_xsp)
386 xsltApplyStylesheet(schema->stylesheet_xsp,
388 if (p->flagShowRecords)
390 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
391 fwrite(buf_out, len_out, 1, stdout);
394 root_ptr = xmlDocGetRootElement(resDoc);
396 index_record(tinfo, p, root_ptr, &recWord);
399 yaz_log(YLOG_WARN, "No root for index XML record."
400 " split_level=%s stylesheet=%s",
401 tinfo->split_level, schema->stylesheet);
405 xmlDocDumpMemory(doc, &buf_out, &len_out);
406 if (p->flagShowRecords)
407 fwrite(buf_out, len_out, 1, stdout);
408 (*p->setStoreData)(p, buf_out, len_out);
412 return RECCTRL_EXTRACT_OK;
415 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
422 xmlFreeTextReader(tinfo->reader);
423 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
430 return RECCTRL_EXTRACT_ERROR_GENERIC;
432 if (tinfo->split_level)
433 split_depth = atoi(tinfo->split_level);
434 ret = xmlTextReaderRead(tinfo->reader);
436 int type = xmlTextReaderNodeType(tinfo->reader);
437 int depth = xmlTextReaderDepth(tinfo->reader);
438 if (split_depth == 0 ||
440 type == XML_READER_TYPE_ELEMENT && split_depth == depth))
442 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
443 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
444 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
446 xmlDocSetRootElement(doc, ptr2);
448 return extract_doc(tinfo, p, doc);
450 ret = xmlTextReaderRead(tinfo->reader);
452 xmlFreeTextReader(tinfo->reader);
454 return RECCTRL_EXTRACT_EOF;
457 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
459 if (p->first_record) /* only one record per stream */
461 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
467 return RECCTRL_EXTRACT_ERROR_GENERIC;
469 return extract_doc(tinfo, p, doc);
472 return RECCTRL_EXTRACT_EOF;
475 static int filter_extract(void *clientData, struct recExtractCtrl *p)
477 struct filter_info *tinfo = clientData;
479 odr_reset(tinfo->odr);
481 if (tinfo->split_level == 0 && tinfo->split_path == 0)
482 return extract_full(tinfo, p);
485 return extract_split(tinfo, p);
489 static int ioread_ret(void *context, char *buffer, int len)
491 struct recRetrieveCtrl *p = context;
492 return (*p->readf)(p->fh, buffer, len);
495 static int ioclose_ret(void *context)
501 static const char *snippet_doc(struct recRetrieveCtrl *p, int text_mode,
504 const char *xml_doc_str;
506 WRBUF wrbuf = wrbuf_alloc();
507 zebra_snippets *res =
508 zebra_snippets_window(p->doc_snippet, p->hit_snippet, window_size);
509 zebra_snippet_word *w = zebra_snippets_list(res);
512 wrbuf_printf(wrbuf, "\'");
514 wrbuf_printf(wrbuf, "<snippet xmlns='%s'>\n", zebra_xslt_ns);
515 for (; w; w = w->next)
519 else if (ord != w->ord)
523 wrbuf_printf(wrbuf, "%s%s%s ",
526 w->match ? "*" : "");
529 wrbuf_printf(wrbuf, " <term ord='%d' seqno='" ZINT_FORMAT "' %s>",
531 (w->match ? "match='1'" : ""));
532 wrbuf_xmlputs(wrbuf, w->term);
533 wrbuf_printf(wrbuf, "</term>\n");
537 wrbuf_printf(wrbuf, "\'");
539 wrbuf_printf(wrbuf, "</snippet>\n");
541 xml_doc_str = odr_strdup(p->odr, wrbuf_buf(wrbuf));
543 zebra_snippets_destroy(res);
544 wrbuf_free(wrbuf, 1);
548 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
550 const char *esn = zebra_xslt_ns;
551 const char *params[20];
552 struct filter_info *tinfo = clientData;
555 struct filter_schema *schema;
556 int window_size = -1;
560 if (p->comp->which == Z_RecordComp_simple
561 && p->comp->u.simple->which == Z_ElementSetNames_generic)
563 esn = p->comp->u.simple->u.generic;
565 else if (p->comp->which == Z_RecordComp_complex
566 && p->comp->u.complex->generic->elementSpec
567 && p->comp->u.complex->generic->elementSpec->which ==
568 Z_ElementSpec_elementSetName)
570 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
573 schema = lookup_schema(tinfo, esn);
577 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
581 if (schema->include_snippet)
582 window_size = atoi(schema->include_snippet);
585 set_param_int(params, "id", p->localno, p->odr);
587 set_param_str(params, "filename", p->fname, p->odr);
588 if (p->staticrank >= 0)
589 set_param_int(params, "rank", p->staticrank, p->odr);
590 set_param_str(params, "schema", esn, p->odr);
592 set_param_int(params, "score", p->score, p->odr);
593 set_param_int(params, "size", p->recordSize, p->odr);
595 if (window_size >= 0)
596 set_param_xml(params, "snippet", snippet_doc(p, 1, window_size),
598 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
604 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
608 if (window_size >= 0)
610 xmlNodePtr node = xmlDocGetRootElement(doc);
611 const char *snippet_str = snippet_doc(p, 0, window_size);
612 xmlDocPtr snippet_doc = xmlParseMemory(snippet_str, strlen(snippet_str));
613 xmlAddChild(node, xmlDocGetRootElement(snippet_doc));
615 if (!schema->stylesheet_xsp)
619 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
625 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
627 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
631 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
633 p->output_format = VAL_TEXT_XML;
634 p->rec_len = len_out;
635 p->rec_buf = odr_malloc(p->odr, p->rec_len);
636 memcpy(p->rec_buf, buf_out, p->rec_len);
640 else if (p->output_format == VAL_SUTRS)
644 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
646 p->output_format = VAL_SUTRS;
647 p->rec_len = len_out;
648 p->rec_buf = odr_malloc(p->odr, p->rec_len);
649 memcpy(p->rec_buf, buf_out, p->rec_len);
655 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
661 static struct recType filter_type = {
672 #ifdef IDZEBRA_STATIC_ALVIS
685 * indent-tabs-mode: nil
687 * vim: shiftwidth=4 tabstop=8 expandtab