1 /* $Id: alvis.c,v 1.19 2007-08-31 07:02:24 adam Exp $
2 Copyright (C) 1995-2007
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>
29 #include <yaz/oid_db.h>
31 #include <libxml/xmlversion.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
34 #include <libxml/xmlIO.h>
35 #include <libxml/xmlreader.h>
36 #include <libxslt/transform.h>
37 #include <libxslt/xsltutils.h>
40 #include <libexslt/exslt.h>
43 #include <idzebra/util.h>
44 #include <idzebra/recctrl.h>
46 struct filter_schema {
48 const char *identifier;
49 const char *stylesheet;
50 struct filter_schema *next;
51 const char *default_schema;
52 /* char default_schema; */
53 xsltStylesheetPtr stylesheet_xsp;
60 const char *profile_path;
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);
209 yaz_log(YLOG_WARN, "alvis filter: could not parse config file %s",
215 ptr = xmlDocGetRootElement(tinfo->doc);
216 if (!ptr || ptr->type != XML_ELEMENT_NODE
217 || XML_STRCMP(ptr->name, "schemaInfo"))
220 "alvis filter: config file %s :"
221 " expected root element <schemaInfo>",
226 for (ptr = ptr->children; ptr; ptr = ptr->next)
228 if (ptr->type != XML_ELEMENT_NODE)
230 if (!XML_STRCMP(ptr->name, "schema"))
232 struct _xmlAttr *attr;
233 struct filter_schema *schema = xmalloc(sizeof(*schema));
235 schema->identifier = 0;
236 schema->stylesheet = 0;
237 schema->default_schema = 0;
238 schema->next = tinfo->schemas;
239 schema->stylesheet_xsp = 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);
248 /*yaz_log(YLOG_LOG, "XSLT add %s %s %s",
249 schema->name, schema->identifier, schema->stylesheet); */
251 /* find requested schema */
253 if (schema->stylesheet)
255 char tmp_xslt_full_name[1024];
256 if (!yaz_filepath_resolve(schema->stylesheet, tinfo->profile_path,
257 NULL, tmp_xslt_full_name))
260 "alvis filter: stylesheet %s not found in path %s",
261 schema->stylesheet, tinfo->profile_path);
264 schema->stylesheet_xsp
265 = xsltParseStylesheetFile((const xmlChar*) tmp_xslt_full_name);
266 if (!schema->stylesheet_xsp)
269 "alvis filter: could not parse xslt stylesheet %s",
275 else if (!XML_STRCMP(ptr->name, "split"))
277 struct _xmlAttr *attr;
278 for (attr = ptr->properties; attr; attr = attr->next)
280 const char *split_level_str = 0;
281 attr_content(attr, "level", &split_level_str);
283 split_level_str ? atoi(split_level_str) : 0;
288 yaz_log(YLOG_WARN, "Bad element %s in %s", ptr->name, fname);
295 static struct filter_schema *lookup_schema(struct filter_info *tinfo,
298 struct filter_schema *schema;
300 for (schema = tinfo->schemas; schema; schema = schema->next)
302 /* find requested schema */
305 if (schema->identifier && !strcmp(schema->identifier, est))
308 if (schema->name && !strcmp(schema->name, est))
311 /* or return default schema if defined */
312 else if (schema->default_schema)
316 /* return first schema if no default schema defined */
318 return tinfo->schemas;
323 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
325 struct filter_info *tinfo = clientData;
328 yaz_log(YLOG_WARN, "alvis filter: need config file");
332 if (tinfo->fname && !strcmp(args, tinfo->fname))
335 tinfo->profile_path = res_get(res, "profilePath");
336 yaz_log(YLOG_LOG, "alvis filter: profilePath %s", tinfo->profile_path);
338 destroy_schemas(tinfo);
339 return create_schemas(tinfo, args);
342 static void filter_destroy(void *clientData)
344 struct filter_info *tinfo = clientData;
345 destroy_schemas(tinfo);
346 xfree(tinfo->full_name);
348 xmlFreeTextReader(tinfo->reader);
349 odr_destroy(tinfo->odr);
353 static int ioread_ex(void *context, char *buffer, int len)
355 struct recExtractCtrl *p = context;
356 return p->stream->readf(p->stream, buffer, len);
359 static int ioclose_ex(void *context)
364 static void index_cdata(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
365 xmlNodePtr ptr, RecWord *recWord)
367 for(; ptr; ptr = ptr->next)
369 index_cdata(tinfo, ctrl, ptr->children, recWord);
370 if (ptr->type != XML_TEXT_NODE)
372 recWord->term_buf = (const char *)ptr->content;
373 recWord->term_len = XML_STRLEN(ptr->content);
374 (*ctrl->tokenAdd)(recWord);
378 static void index_node(struct filter_info *tinfo, struct recExtractCtrl *ctrl,
379 xmlNodePtr ptr, RecWord *recWord)
381 for(; ptr; ptr = ptr->next)
383 index_node(tinfo, ctrl, ptr->children, recWord);
384 if (ptr->type != XML_ELEMENT_NODE || !ptr->ns ||
385 XML_STRCMP(ptr->ns->href, zebra_xslt_ns))
387 if (!XML_STRCMP(ptr->name, "index"))
389 const char *name_str = 0;
390 const char *type_str = 0;
391 const char *xpath_str = 0;
392 struct _xmlAttr *attr;
393 for (attr = ptr->properties; attr; attr = attr->next)
395 attr_content(attr, "name", &name_str);
396 attr_content(attr, "xpath", &xpath_str);
397 attr_content(attr, "type", &type_str);
401 int prev_type = recWord->index_type; /* save default type */
403 if (type_str && *type_str)
404 recWord->index_type = *type_str; /* type was given */
405 recWord->index_name = name_str;
406 index_cdata(tinfo, ctrl, ptr->children, recWord);
408 recWord->index_type = prev_type; /* restore it again */
414 static void index_record(struct filter_info *tinfo,struct recExtractCtrl *ctrl,
415 xmlNodePtr ptr, RecWord *recWord)
417 const char *type_str = "update";
419 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns &&
420 !XML_STRCMP(ptr->ns->href, zebra_xslt_ns)
421 && !XML_STRCMP(ptr->name, "record"))
423 const char *id_str = 0;
424 const char *rank_str = 0;
425 struct _xmlAttr *attr;
426 for (attr = ptr->properties; attr; attr = attr->next)
428 attr_content(attr, "type", &type_str);
429 attr_content(attr, "id", &id_str);
430 attr_content(attr, "rank", &rank_str);
433 sscanf(id_str, "%255s", ctrl->match_criteria);
436 ctrl->staticrank = atozint(rank_str);
440 if (!strcmp("update", type_str))
441 index_node(tinfo, ctrl, ptr, recWord);
442 else if (!strcmp("delete", type_str))
443 yaz_log(YLOG_WARN, "alvis filter delete: to be implemented");
445 yaz_log(YLOG_WARN, "alvis filter: unknown record type '%s'",
449 static int extract_doc(struct filter_info *tinfo, struct recExtractCtrl *p,
453 const char *params[10];
457 struct filter_schema *schema = lookup_schema(tinfo, zebra_xslt_ns);
460 set_param_str(params, "schema", zebra_xslt_ns, tinfo->odr);
462 (*p->init)(p, &recWord);
464 if (schema && schema->stylesheet_xsp)
468 xsltApplyStylesheet(schema->stylesheet_xsp,
470 if (p->flagShowRecords)
472 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
473 fwrite(buf_out, len_out, 1, stdout);
476 root_ptr = xmlDocGetRootElement(resDoc);
478 index_record(tinfo, p, root_ptr, &recWord);
481 yaz_log(YLOG_WARN, "No root for index XML record."
482 " split_level=%d stylesheet=%s",
483 tinfo->split_level, schema->stylesheet);
487 xmlDocDumpMemory(doc, &buf_out, &len_out);
488 if (p->flagShowRecords)
489 fwrite(buf_out, len_out, 1, stdout);
491 (*p->setStoreData)(p, buf_out, len_out);
495 return RECCTRL_EXTRACT_OK;
498 static int extract_split(struct filter_info *tinfo, struct recExtractCtrl *p)
505 xmlFreeTextReader(tinfo->reader);
506 tinfo->reader = xmlReaderForIO(ioread_ex, ioclose_ex,
515 return RECCTRL_EXTRACT_ERROR_GENERIC;
517 ret = xmlTextReaderRead(tinfo->reader);
520 int type = xmlTextReaderNodeType(tinfo->reader);
521 int depth = xmlTextReaderDepth(tinfo->reader);
522 if (type == XML_READER_TYPE_ELEMENT && tinfo->split_level == depth)
524 xmlNodePtr ptr = xmlTextReaderExpand(tinfo->reader);
527 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
528 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
530 xmlDocSetRootElement(doc, ptr2);
532 return extract_doc(tinfo, p, doc);
536 xmlFreeTextReader(tinfo->reader);
538 return RECCTRL_EXTRACT_ERROR_GENERIC;
541 ret = xmlTextReaderRead(tinfo->reader);
543 xmlFreeTextReader(tinfo->reader);
545 return RECCTRL_EXTRACT_EOF;
548 static int extract_full(struct filter_info *tinfo, struct recExtractCtrl *p)
550 if (p->first_record) /* only one record per stream */
552 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex, p /* I/O handler */,
559 return RECCTRL_EXTRACT_ERROR_GENERIC;
561 xmlNodePtr root = xmlDocGetRootElement(doc);
563 return RECCTRL_EXTRACT_ERROR_GENERIC;
566 return extract_doc(tinfo, p, doc);
569 return RECCTRL_EXTRACT_EOF;
572 static int filter_extract(void *clientData, struct recExtractCtrl *p)
574 struct filter_info *tinfo = clientData;
576 odr_reset(tinfo->odr);
577 if (tinfo->split_level == 0 || p->setStoreData == 0)
578 return extract_full(tinfo, p);
580 return extract_split(tinfo, p);
583 static int ioread_ret(void *context, char *buffer, int len)
585 struct recRetrieveCtrl *p = context;
586 return p->stream->readf(p->stream, buffer, len);
589 static int ioclose_ret(void *context)
594 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
596 /* const char *esn = zebra_xslt_ns; */
598 const char *params[32];
599 struct filter_info *tinfo = clientData;
602 struct filter_schema *schema;
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_;
628 set_param_int(params, "id", p->localno, p->odr);
630 set_param_str(params, "filename", p->fname, p->odr);
631 if (p->staticrank >= 0)
632 set_param_int(params, "rank", p->staticrank, p->odr);
635 set_param_str(params, "schema", esn, p->odr);
638 set_param_str(params, "schema", schema->name, p->odr);
639 else if (schema->identifier)
640 set_param_str(params, "schema", schema->identifier, p->odr);
642 set_param_str(params, "schema", "", p->odr);
645 set_param_int(params, "score", p->score, p->odr);
646 set_param_int(params, "size", p->recordSize, p->odr);
648 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
651 XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
654 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
658 if (!schema->stylesheet_xsp)
662 resDoc = xsltApplyStylesheet(schema->stylesheet_xsp,
668 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
670 else if (!p->input_format
671 || !oid_oidcmp(p->input_format, yaz_oid_recsyn_xml))
676 if (schema->stylesheet_xsp)
677 xsltSaveResultToString(&buf_out, &len_out, resDoc,
678 schema->stylesheet_xsp);
680 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
682 p->output_format = yaz_oid_recsyn_xml;
683 p->rec_len = len_out;
684 p->rec_buf = odr_malloc(p->odr, p->rec_len);
685 memcpy(p->rec_buf, buf_out, p->rec_len);
688 else if (!oid_oidcmp(p->output_format, yaz_oid_recsyn_sutrs))
693 if (schema->stylesheet_xsp)
694 xsltSaveResultToString(&buf_out, &len_out, resDoc,
695 schema->stylesheet_xsp);
697 xmlDocDumpMemory(resDoc, &buf_out, &len_out);
699 p->output_format = yaz_oid_recsyn_sutrs;
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);
708 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
714 static struct recType filter_type = {
725 #ifdef IDZEBRA_STATIC_ALVIS
738 * indent-tabs-mode: nil
740 * vim: shiftwidth=4 tabstop=8 expandtab