1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <yaz/diagbib1.h>
26 #include <yaz/tpath.h>
27 #include <yaz/snprintf.h>
29 #include <libxml/xmlversion.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
32 #include <libxml/xmlIO.h>
33 #include <libxml/xmlreader.h>
34 #include <libxslt/transform.h>
35 #include <libxslt/xsltutils.h>
38 #include <libexslt/exslt.h>
41 #include <idzebra/util.h>
42 #include <idzebra/recctrl.h>
43 #include <yaz/oid_db.h>
45 /* DOM filter style indexing */
46 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
47 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
49 /* DOM filter style indexing */
50 #define ZEBRA_PI_NAME "zebra-2.0"
51 static const char *zebra_pi_name = ZEBRA_PI_NAME;
59 const char *stylesheet;
60 xsltStylesheetPtr stylesheet_xsp;
68 enum convert_type which;
70 struct convert_xslt xslt;
71 struct convert_meta meta;
73 struct convert_s *next;
76 struct filter_extract {
78 struct convert_s *convert;
82 struct convert_s *convert;
85 struct filter_retrieve {
87 const char *identifier;
88 struct convert_s *convert;
89 struct filter_retrieve *next;
92 #define DOM_INPUT_XMLREADER 1
93 #define DOM_INPUT_MARC 2
97 struct convert_s *convert;
101 xmlTextReaderPtr reader;
105 const char *input_charset;
110 struct filter_input *next;
116 const char *profile_path;
119 xmlDocPtr doc_config;
120 struct filter_extract *extract;
121 struct filter_retrieve *retrieve_list;
122 struct filter_input *input_list;
123 struct filter_store *store;
124 int record_info_invoked;
129 #define XML_STRCMP(a,b) strcmp((char*)a, b)
130 #define XML_STRLEN(a) strlen((char*)a)
133 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
135 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
136 const char *fmt, ...)
138 __attribute__ ((format (printf, 4, 5)))
142 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
143 const char *fmt, ...)
149 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
152 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
153 xmlGetLineNo(ptr), buf);
157 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
163 static void set_param_str(const char **params, const char *name,
164 const char *value, NMEM nmem)
166 char *quoted = nmem_malloc(nmem, 3 + strlen(value));
167 sprintf(quoted, "'%s'", value);
175 static void set_param_int(const char **params, const char *name,
176 zint value, NMEM nmem)
178 char *quoted = nmem_malloc(nmem, 30); /* 25 digits enough for 2^64 */
181 sprintf(quoted, "'" ZINT_FORMAT "'", value);
187 static void *filter_init(Res res, RecType recType)
189 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
191 tinfo->full_name = 0;
192 tinfo->profile_path = 0;
193 tinfo->nmem_record = nmem_create();
194 tinfo->nmem_config = nmem_create();
196 tinfo->retrieve_list = 0;
197 tinfo->input_list = 0;
199 tinfo->doc_config = 0;
200 tinfo->record_info_invoked = 0;
209 static int attr_content(struct _xmlAttr *attr, const char *name,
210 const char **dst_content)
212 if (!XML_STRCMP(attr->name, name) && attr->children
213 && attr->children->type == XML_TEXT_NODE)
215 *dst_content = (const char *)(attr->children->content);
221 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
222 const char **dst_content)
224 if (0 == XML_STRCMP(attr->name, name) && attr->children
225 && attr->children->type == XML_TEXT_NODE)
227 *dst_content = (const char *) (attr->children->content);
233 static void destroy_xsp(struct convert_s *c)
237 if (c->which == convert_xslt_type)
239 if (c->u.xslt.stylesheet_xsp)
240 xsltFreeStylesheet(c->u.xslt.stylesheet_xsp);
246 static void destroy_dom(struct filter_info *tinfo)
250 destroy_xsp(tinfo->extract->convert);
255 destroy_xsp(tinfo->store->convert);
258 if (tinfo->input_list)
260 struct filter_input *i_ptr;
261 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
265 case DOM_INPUT_XMLREADER:
266 if (i_ptr->u.xmlreader.reader)
267 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
270 yaz_iconv_close(i_ptr->u.marc.iconv);
271 yaz_marc_destroy(i_ptr->u.marc.handle);
274 destroy_xsp(i_ptr->convert);
276 tinfo->input_list = 0;
278 if (tinfo->retrieve_list)
280 struct filter_retrieve *r_ptr;
281 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
282 destroy_xsp(r_ptr->convert);
283 tinfo->retrieve_list = 0;
286 if (tinfo->doc_config)
288 xmlFreeDoc(tinfo->doc_config);
289 tinfo->doc_config = 0;
291 nmem_reset(tinfo->nmem_config);
294 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
295 struct convert_s **l)
298 FOR_EACH_ELEMENT(ptr) {
299 if (!XML_STRCMP(ptr->name, "xslt"))
301 struct _xmlAttr *attr;
302 struct convert_s *p = nmem_malloc(tinfo->nmem_config, sizeof(*p));
305 p->which = convert_xslt_type;
306 p->u.xslt.stylesheet = 0;
307 p->u.xslt.stylesheet_xsp = 0;
309 for (attr = ptr->properties; attr; attr = attr->next)
310 if (attr_content(attr, "stylesheet", &p->u.xslt.stylesheet))
314 dom_log(YLOG_WARN, tinfo, ptr,
315 "bad attribute @%s", attr->name);
317 if (p->u.xslt.stylesheet)
319 char tmp_xslt_full_name[1024];
320 if (!yaz_filepath_resolve(p->u.xslt.stylesheet,
325 dom_log(YLOG_WARN, tinfo, 0,
326 "stylesheet %s not found in "
328 p->u.xslt.stylesheet,
329 tinfo->profile_path);
333 p->u.xslt.stylesheet_xsp
334 = xsltParseStylesheetFile((const xmlChar*)
336 if (!p->u.xslt.stylesheet_xsp)
338 dom_log(YLOG_WARN, tinfo, 0,
339 "could not parse xslt stylesheet %s",
346 dom_log(YLOG_WARN, tinfo, ptr,
347 "missing attribute 'stylesheet'");
353 else if (!XML_STRCMP(ptr->name, "meta"))
355 struct _xmlAttr *attr;
356 struct convert_s *p = nmem_malloc(tinfo->nmem_config, sizeof(*p));
359 p->which = convert_meta_type;
361 for (attr = ptr->properties; attr; attr = attr->next)
362 dom_log(YLOG_WARN, tinfo, ptr,
363 "bad attribute @%s", attr->name);
369 dom_log(YLOG_WARN, tinfo, ptr,
370 "bad element '%s', expected <xslt>", ptr->name);
377 static int process_meta(struct filter_info *tinfo, xmlDocPtr doc, xmlNodePtr node,
378 struct recRetrieveCtrl *retctr)
381 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href &&
382 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
384 if (0 == XML_STRCMP(node->name, "meta"))
386 const char *element_set_name = 0;
388 struct _xmlAttr *attr;
389 for (attr = node->properties; attr; attr = attr->next)
391 if (attr_content_xml(attr, "element_set_name", &element_set_name))
395 dom_log(YLOG_WARN, tinfo, node,
396 "bad attribute @%s, expected @element_set_name",
400 if (element_set_name)
402 WRBUF result = wrbuf_alloc();
403 WRBUF addinfo = wrbuf_alloc();
404 const Odr_oid *input_format = yaz_oid_recsyn_xml;
405 const Odr_oid *output_format = 0;
408 ret = retctr->special_fetch(retctr->handle,
410 input_format, &output_format,
415 xmlParseMemory( wrbuf_buf(result), wrbuf_len(result));
418 xmlNodePtr t = xmlDocGetRootElement(sub_doc);
419 xmlAddChild(node, xmlCopyNode(t, 1));
423 wrbuf_destroy(result);
424 wrbuf_destroy(addinfo);
428 for (node = node->children; node; node = node->next)
429 process_meta(tinfo, doc, node, retctr);
433 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
434 struct recExtractCtrl *extctr,
435 struct recRetrieveCtrl *retctr,
436 struct convert_s *convert,
439 xsltStylesheetPtr *last_xsp)
441 for (; convert; convert = convert->next)
443 if (convert->which == convert_xslt_type)
445 xmlChar *buf_out = 0;
447 xmlDocPtr res_doc = xsltApplyStylesheet(convert->u.xslt.stylesheet_xsp,
450 *last_xsp = convert->u.xslt.stylesheet_xsp;
455 /* now saving into buffer and re-reading into DOM to avoid annoing
456 XSLT problem with thrown-out indentation text nodes */
457 xsltSaveResultToString(&buf_out, &len_out, res_doc,
458 convert->u.xslt.stylesheet_xsp);
463 *doc = xmlParseMemory((const char *) buf_out, len_out);
465 /* writing debug info out */
466 if (extctr && extctr->flagShowRecords)
467 yaz_log(YLOG_LOG, "%s: XSLT %s\n %.*s",
468 tinfo->fname ? tinfo->fname : "(none)",
469 convert->u.xslt.stylesheet,
474 else if (convert->which == convert_meta_type)
476 if (retctr) /* only execute meta on retrieval */
478 process_meta(tinfo, *doc, xmlDocGetRootElement(*doc), retctr);
480 /* last stylesheet absent */
489 static struct filter_input *new_input(struct filter_info *tinfo, int type)
491 struct filter_input *p;
492 struct filter_input **np = &tinfo->input_list;
493 for (;*np; np = &(*np)->next)
495 p = *np = nmem_malloc(tinfo->nmem_config, sizeof(*p));
504 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
505 const char *syntax, const char *name)
507 FOR_EACH_ELEMENT(ptr) {
508 if (!XML_STRCMP(ptr->name, "marc"))
510 yaz_iconv_t iconv = 0;
511 const char *input_charset = "marc-8";
512 struct _xmlAttr *attr;
514 for (attr = ptr->properties; attr; attr = attr->next)
516 if (attr_content(attr, "inputcharset", &input_charset))
520 dom_log(YLOG_WARN, tinfo, ptr,
521 "bad attribute @%s, expected @inputcharset",
525 iconv = yaz_iconv_open("utf-8", input_charset);
528 dom_log(YLOG_WARN, tinfo, ptr,
529 "unsupported @charset '%s'", input_charset);
534 struct filter_input *p
535 = new_input(tinfo, DOM_INPUT_MARC);
536 p->u.marc.handle = yaz_marc_create();
537 p->u.marc.iconv = iconv;
539 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
543 parse_convert(tinfo, ptr, &p->convert);
548 else if (!XML_STRCMP(ptr->name, "xmlreader"))
550 struct filter_input *p
551 = new_input(tinfo, DOM_INPUT_XMLREADER);
552 struct _xmlAttr *attr;
553 const char *level_str = 0;
555 p->u.xmlreader.split_level = 0;
556 p->u.xmlreader.reader = 0;
558 for (attr = ptr->properties; attr; attr = attr->next)
560 if (attr_content(attr, "level", &level_str))
564 dom_log(YLOG_WARN, tinfo, ptr,
565 "bad attribute @%s, expected @level",
570 p->u.xmlreader.split_level = atoi(level_str);
574 parse_convert(tinfo, ptr, &p->convert);
579 dom_log(YLOG_WARN, tinfo, ptr,
580 "bad element <%s>, expected <marc>|<xmlreader>",
588 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
590 char tmp_full_name[1024];
594 tinfo->fname = nmem_strdup(tinfo->nmem_config, fname);
596 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
597 NULL, tmp_full_name))
598 tinfo->full_name = nmem_strdup(tinfo->nmem_config, tmp_full_name);
600 tinfo->full_name = nmem_strdup(tinfo->nmem_config, tinfo->fname);
602 yaz_log(YLOG_LOG, "%s dom filter: "
603 "loading config file %s", tinfo->fname, tinfo->full_name);
605 doc = xmlParseFile(tinfo->full_name);
608 yaz_log(YLOG_WARN, "%s: dom filter: "
609 "failed to parse config file %s",
610 tinfo->fname, tinfo->full_name);
613 /* save because we store ptrs to the content */
614 tinfo->doc_config = doc;
616 ptr = xmlDocGetRootElement(doc);
617 if (!ptr || ptr->type != XML_ELEMENT_NODE
618 || XML_STRCMP(ptr->name, "dom"))
620 dom_log(YLOG_WARN, tinfo, ptr,
621 "bad root element <%s>, expected root element <dom>",
627 FOR_EACH_ELEMENT(ptr) {
628 if (!XML_STRCMP(ptr->name, "extract"))
631 <extract name="index">
632 <xslt stylesheet="first.xsl"/>
633 <xslt stylesheet="second.xsl"/>
636 struct _xmlAttr *attr;
637 struct filter_extract *f =
638 nmem_malloc(tinfo->nmem_config, sizeof(*f));
643 for (attr = ptr->properties; attr; attr = attr->next)
645 if (attr_content(attr, "name", &f->name))
649 dom_log(YLOG_WARN, tinfo, ptr,
650 "bad attribute @%s, expected @name",
654 parse_convert(tinfo, ptr->children, &f->convert);
656 else if (!XML_STRCMP(ptr->name, "retrieve"))
660 <xslt stylesheet="some.xsl"/>
661 <xslt stylesheet="some.xsl"/>
664 struct _xmlAttr *attr;
665 struct filter_retrieve **fp = &tinfo->retrieve_list;
666 struct filter_retrieve *f =
667 nmem_malloc(tinfo->nmem_config, sizeof(*f));
678 for (attr = ptr->properties; attr; attr = attr->next)
680 if (attr_content(attr, "identifier",
683 else if (attr_content(attr, "name", &f->name))
687 dom_log(YLOG_WARN, tinfo, ptr,
688 "bad attribute @%s, expected @identifier|@name",
692 parse_convert(tinfo, ptr->children, &f->convert);
694 else if (!XML_STRCMP(ptr->name, "store"))
698 <xslt stylesheet="some.xsl"/>
699 <xslt stylesheet="some.xsl"/>
702 struct filter_store *f =
703 nmem_malloc(tinfo->nmem_config, sizeof(*f));
707 parse_convert(tinfo, ptr->children, &f->convert);
709 else if (!XML_STRCMP(ptr->name, "input"))
713 <xmlreader level="1"/>
715 <input syntax="usmarc">
716 <marc inputcharset="marc-8"/>
719 struct _xmlAttr *attr;
720 const char *syntax = 0;
721 const char *name = 0;
722 for (attr = ptr->properties; attr; attr = attr->next)
724 if (attr_content(attr, "syntax", &syntax))
726 else if (attr_content(attr, "name", &name))
730 dom_log(YLOG_WARN, tinfo, ptr,
731 "bad attribute @%s, expected @syntax|@name",
735 parse_input(tinfo, ptr->children, syntax, name);
739 dom_log(YLOG_WARN, tinfo, ptr,
741 "expected <extract>|<input>|<retrieve>|<store>",
746 if (!tinfo->input_list)
748 struct filter_input *p
749 = new_input(tinfo, DOM_INPUT_XMLREADER);
750 p->u.xmlreader.split_level = 0;
751 p->u.xmlreader.reader = 0;
756 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
759 struct filter_retrieve *f = tinfo->retrieve_list;
761 /* return first schema if no est is provided */
764 for (; f; f = f->next)
766 /* find requested schema */
769 if (f->identifier && !strcmp(f->identifier, est))
771 if (f->name && !strcmp(f->name, est))
778 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
780 struct filter_info *tinfo = clientData;
783 yaz_log(YLOG_WARN, "dom filter: need config file");
787 if (tinfo->fname && !strcmp(args, tinfo->fname))
790 tinfo->profile_path = res_get(res, "profilePath");
793 return parse_dom(tinfo, args);
796 static void filter_destroy(void *clientData)
798 struct filter_info *tinfo = clientData;
800 nmem_destroy(tinfo->nmem_config);
801 nmem_destroy(tinfo->nmem_record);
805 static int ioread_ex(void *context, char *buffer, int len)
807 struct recExtractCtrl *p = context;
808 return p->stream->readf(p->stream, buffer, len);
811 static int ioclose_ex(void *context)
818 /* DOM filter style indexing */
819 static void index_value_of(struct filter_info *tinfo,
820 struct recExtractCtrl *extctr,
825 if (tinfo->record_info_invoked == 1)
827 xmlChar *text = xmlNodeGetContent(node);
828 size_t text_len = strlen((const char *)text);
830 /* if there is no text, we do not need to proceed */
833 const char *look = index_p;
840 /* assingning text to be indexed */
841 recword->term_buf = (const char *)text;
842 recword->term_len = text_len;
844 /* parsing all index name/type pairs */
845 /* may not start with ' ' or ':' */
846 while (*look && ' ' != *look && ':' != *look)
848 /* setting name and type to zero */
852 /* parsing one index name */
854 while (*look && ':' != *look && ' ' != *look)
859 strncpy((char *)index, (const char *)bval, eval - bval);
860 index[eval - bval] = '\0';
863 /* parsing one index type, if existing */
869 while (*look && ' ' != *look)
874 strncpy((char *)type, (const char *)bval, eval - bval);
875 type[eval - bval] = '\0';
878 /* actually indexing the text given */
880 recword->index_name = (const char *)index;
882 recword->index_type = (const char *) type;
884 /* writing debug out */
885 if (extctr->flagShowRecords)
886 dom_log(YLOG_LOG, tinfo, 0,
887 "INDEX '%s:%s' '%s'",
888 (const char *) index,
890 (const char *) text);
892 (extctr->tokenAdd)(recword);
894 /* eat whitespaces */
895 if (*look && ' ' == *look)
906 /* DOM filter style indexing */
907 static void set_record_info(struct filter_info *tinfo,
908 struct recExtractCtrl *extctr,
914 /* writing debug info out */
915 if (extctr && extctr->flagShowRecords)
916 dom_log(YLOG_LOG, tinfo, node,
917 "RECORD id=%s rank=%s type=%s",
918 id_p ? (const char *) id_p : "(null)",
919 rank_p ? (const char *) rank_p : "(null)",
920 type_p ? (const char *) type_p : "(null)");
924 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
926 if (rank_p && *rank_p)
927 extctr->staticrank = atozint((const char *)rank_p);
929 if (type_p && *type_p)
931 enum zebra_recctrl_action_t action = action_update;
932 if (!strcmp(type_p, "insert"))
933 action = action_insert;
934 else if (!strcmp(type_p, "delete"))
935 action = action_delete;
936 else if (!strcmp(type_p, "replace"))
937 action = action_replace;
938 else if (!strcmp(type_p, "update"))
939 action = action_update;
941 dom_log(YLOG_WARN, tinfo, node, "bad @type value: %s", type_p);
942 extctr->action = action;
945 if (tinfo->record_info_invoked == 1)
947 /* warn about multiple only once */
948 dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
950 tinfo->record_info_invoked++;
955 /* DOM filter style indexing */
956 static void process_xml_element_zebra_node(struct filter_info *tinfo,
957 struct recExtractCtrl *extctr,
961 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
962 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
964 if (0 == XML_STRCMP(node->name, "index"))
966 const char *index_p = 0;
968 struct _xmlAttr *attr;
969 for (attr = node->properties; attr; attr = attr->next)
971 if (attr_content_xml(attr, "name", &index_p))
973 index_value_of(tinfo, extctr, recword, node, index_p);
977 dom_log(YLOG_WARN, tinfo, node,
978 "bad attribute @%s, expected @name",
983 else if (0 == XML_STRCMP(node->name, "record"))
985 const char *id_p = 0;
986 const char *rank_p = 0;
987 const char *type_p = 0;
989 struct _xmlAttr *attr;
990 for (attr = node->properties; attr; attr = attr->next)
992 if (attr_content_xml(attr, "id", &id_p))
994 else if (attr_content_xml(attr, "rank", &rank_p))
996 else if (attr_content_xml(attr, "type", &type_p))
1000 dom_log(YLOG_WARN, tinfo, node,
1001 "bad attribute @%s, expected @id|@rank|@type",
1005 set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
1009 dom_log(YLOG_WARN, tinfo, node,
1011 " expected <record>|<index> in namespace '%s'",
1012 node->name, zebra_dom_ns);
1017 static int attr_content_pi(const char **c_ptr, const char *name,
1018 char *value, size_t value_max)
1020 size_t name_len = strlen(name);
1021 const char *look = *c_ptr;
1025 while (*look && ' ' == *look)
1027 if (strlen(look) > name_len)
1029 if (look[name_len] == '=' && !memcmp(look, name, name_len))
1033 while (*look && ' ' != *look)
1035 if (i < value_max-1)
1043 while (*look && ' ' == *look)
1049 /* DOM filter style indexing */
1050 static void process_xml_pi_node(struct filter_info *tinfo,
1051 struct recExtractCtrl *extctr,
1053 const char **index_pp)
1055 /* if right PI name, continue parsing PI */
1056 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
1058 xmlChar *pi_p = node->content;
1059 const char *look = (const char *) node->content;
1061 /* parsing PI record instructions */
1062 if (0 == strncmp((const char *)look, "record", 6))
1073 if (attr_content_pi(&look, "id", id, sizeof(id)))
1075 else if (attr_content_pi(&look, "rank", rank, sizeof(rank)))
1077 else if (attr_content_pi(&look, "type", type, sizeof(type)))
1079 dom_log(YLOG_WARN, tinfo, node,
1080 "content '%s', can not parse '%s'",
1084 set_record_info(tinfo, extctr, node, id, rank, type);
1086 /* parsing index instruction */
1087 else if (0 == strncmp((const char *)look, "index", 5))
1091 /* eat whitespace */
1092 while (*look && ' ' == *look)
1095 /* export index instructions to outside */
1100 dom_log(YLOG_WARN, tinfo, node,
1101 "content '%s', can not parse '%s'",
1107 /* DOM filter style indexing */
1108 static void process_xml_element_node(struct filter_info *tinfo,
1109 struct recExtractCtrl *extctr,
1113 /* remember indexing instruction from PI to next element node */
1114 const char *index_p = 0;
1116 /* check if we are an element node in the special zebra namespace
1117 and either set record data or index value-of node content*/
1118 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1120 /* loop through kid nodes */
1121 for (node = node->children; node; node = node->next)
1123 /* check and set PI record and index index instructions */
1124 if (node->type == XML_PI_NODE)
1126 process_xml_pi_node(tinfo, extctr, node, &index_p);
1128 else if (node->type == XML_ELEMENT_NODE)
1130 /* if there was a PI index instruction before this element */
1133 index_value_of(tinfo, extctr, recword, node, index_p);
1136 process_xml_element_node(tinfo, extctr, recword,node);
1144 /* DOM filter style indexing */
1145 static void extract_dom_doc_node(struct filter_info *tinfo,
1146 struct recExtractCtrl *extctr,
1149 /* only need to do the initialization once, reuse recword for all terms */
1151 (*extctr->init)(extctr, &recword);
1153 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1157 static int convert_extract_doc(struct filter_info *tinfo,
1158 struct filter_input *input,
1159 struct recExtractCtrl *p,
1164 const char *params[10];
1165 xsltStylesheetPtr last_xsp = 0;
1166 xmlDocPtr store_doc = 0;
1168 /* per default do not ingest record */
1169 tinfo->record_info_invoked = 0;
1171 /* exit if empty document given */
1173 return RECCTRL_EXTRACT_SKIP;
1175 /* we actuallu have a document which needs to be processed further */
1177 set_param_str(params, "schema", zebra_dom_ns, tinfo->nmem_record);
1179 if (p && p->flagShowRecords)
1184 FILE *outf = fopen("extract.xml", "w");
1185 xmlDocDumpMemory(doc, &buf_out, &len_out);
1186 fwrite(buf_out, 1, len_out, outf);
1188 yaz_log(YLOG_LOG, "Extract Doc: %.*s", len_out, buf_out);
1194 /* input conversion */
1195 perform_convert(tinfo, p, 0, input->convert, params, &doc, 0);
1199 /* store conversion */
1200 store_doc = xmlCopyDoc(doc, 1);
1201 perform_convert(tinfo, p, 0, tinfo->store->convert,
1202 params, &store_doc, &last_xsp);
1205 /* saving either store doc or original doc in case no store doc exists */
1207 xsltSaveResultToString(&buf_out, &len_out,
1208 store_doc ? store_doc : doc, last_xsp);
1210 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1212 if (p->setStoreData)
1213 (*p->setStoreData)(p, buf_out, len_out);
1217 xmlFreeDoc(store_doc);
1219 /* extract conversion */
1220 perform_convert(tinfo, p, 0, tinfo->extract->convert, params, &doc, 0);
1223 /* finally, do the indexing */
1225 extract_dom_doc_node(tinfo, p, doc);
1229 /* there was nothing to index, so there is no inserted/updated record */
1230 if (tinfo->record_info_invoked == 0)
1231 return RECCTRL_EXTRACT_SKIP;
1233 return RECCTRL_EXTRACT_OK;
1236 static int extract_xml_split(struct filter_info *tinfo,
1237 struct filter_input *input,
1238 struct recExtractCtrl *p)
1242 if (p->first_record)
1244 if (input->u.xmlreader.reader)
1245 xmlFreeTextReader(input->u.xmlreader.reader);
1246 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1247 p /* I/O handler */,
1254 if (!input->u.xmlreader.reader)
1255 return RECCTRL_EXTRACT_ERROR_GENERIC;
1257 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1260 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1261 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1263 if (type == XML_READER_TYPE_ELEMENT &&
1264 input->u.xmlreader.split_level == depth)
1268 /* per default do not ingest record */
1269 tinfo->record_info_invoked = 0;
1271 ptr = xmlTextReaderExpand(input->u.xmlreader.reader);
1274 /* we have a new document */
1276 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1277 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1279 xmlDocSetRootElement(doc, ptr2);
1281 /* writing debug info out */
1282 if (p->flagShowRecords)
1284 xmlChar *buf_out = 0;
1286 xmlDocDumpMemory(doc, &buf_out, &len_out);
1287 yaz_log(YLOG_LOG, "%s: XMLREADER level: %i\n%.*s",
1288 tinfo->fname ? tinfo->fname : "(none)",
1289 depth, len_out, buf_out);
1293 return convert_extract_doc(tinfo, input, p, doc);
1297 xmlFreeTextReader(input->u.xmlreader.reader);
1298 input->u.xmlreader.reader = 0;
1299 return RECCTRL_EXTRACT_ERROR_GENERIC;
1302 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1304 xmlFreeTextReader(input->u.xmlreader.reader);
1305 input->u.xmlreader.reader = 0;
1306 return RECCTRL_EXTRACT_EOF;
1309 static int extract_xml_full(struct filter_info *tinfo,
1310 struct filter_input *input,
1311 struct recExtractCtrl *p)
1313 if (p->first_record) /* only one record per stream */
1315 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1316 p /* I/O handler */,
1324 return RECCTRL_EXTRACT_ERROR_GENERIC;
1326 return convert_extract_doc(tinfo, input, p, doc);
1329 return RECCTRL_EXTRACT_EOF;
1332 static int extract_iso2709(struct filter_info *tinfo,
1333 struct filter_input *input,
1334 struct recExtractCtrl *p)
1340 if (p->stream->readf(p->stream, buf, 5) != 5)
1341 return RECCTRL_EXTRACT_EOF;
1342 while (*buf < '0' || *buf > '9')
1346 dom_log(YLOG_WARN, tinfo, 0,
1347 "MARC: Skipping bad byte %d (0x%02X)",
1348 *buf & 0xff, *buf & 0xff);
1349 for (i = 0; i<4; i++)
1352 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1353 return RECCTRL_EXTRACT_EOF;
1355 record_length = atoi_n (buf, 5);
1356 if (record_length < 25)
1358 dom_log(YLOG_WARN, tinfo, 0,
1359 "MARC record length < 25, is %d", record_length);
1360 return RECCTRL_EXTRACT_ERROR_GENERIC;
1362 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1363 if (read_bytes < record_length-5)
1365 dom_log(YLOG_WARN, tinfo, 0,
1366 "couldn't read whole MARC record");
1367 return RECCTRL_EXTRACT_ERROR_GENERIC;
1369 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1370 if (r < record_length)
1372 dom_log (YLOG_WARN, tinfo, 0,
1373 "parsing of MARC record failed r=%d length=%d",
1375 return RECCTRL_EXTRACT_ERROR_GENERIC;
1381 yaz_marc_write_xml(input->u.marc.handle, &root_ptr,
1382 "http://www.loc.gov/MARC21/slim", 0, 0);
1383 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1384 xmlDocSetRootElement(rdoc, root_ptr);
1385 return convert_extract_doc(tinfo, input, p, rdoc);
1387 return RECCTRL_EXTRACT_OK;
1390 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1392 struct filter_info *tinfo = clientData;
1393 struct filter_input *input = tinfo->input_list;
1396 return RECCTRL_EXTRACT_ERROR_GENERIC;
1398 nmem_reset(tinfo->nmem_record);
1400 if (p->setStoreData == 0)
1401 return extract_xml_full(tinfo, input, p);
1404 case DOM_INPUT_XMLREADER:
1405 if (input->u.xmlreader.split_level == 0)
1406 return extract_xml_full(tinfo, input, p);
1408 return extract_xml_split(tinfo, input, p);
1410 case DOM_INPUT_MARC:
1411 return extract_iso2709(tinfo, input, p);
1413 return RECCTRL_EXTRACT_ERROR_GENERIC;
1416 static int ioread_ret(void *context, char *buffer, int len)
1418 struct recRetrieveCtrl *p = context;
1419 return p->stream->readf(p->stream, buffer, len);
1422 static int ioclose_ret(void *context)
1427 static int filter_retrieve(void *clientData, struct recRetrieveCtrl *p)
1429 /* const char *esn = zebra_dom_ns; */
1430 const char *esn = 0;
1431 const char *params[32];
1432 struct filter_info *tinfo = clientData;
1434 struct filter_retrieve *retrieve;
1435 xsltStylesheetPtr last_xsp = 0;
1439 if (p->comp->which == Z_RecordComp_simple
1440 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1442 esn = p->comp->u.simple->u.generic;
1444 else if (p->comp->which == Z_RecordComp_complex
1445 && p->comp->u.complex->generic->elementSpec
1446 && p->comp->u.complex->generic->elementSpec->which ==
1447 Z_ElementSpec_elementSetName)
1449 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1452 retrieve = lookup_retrieve(tinfo, esn);
1456 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1457 p->addinfo = odr_strdup(p->odr, esn);
1462 set_param_int(params, "id", p->localno, p->odr->mem);
1464 set_param_str(params, "filename", p->fname, p->odr->mem);
1465 if (p->staticrank >= 0)
1466 set_param_int(params, "rank", p->staticrank, p->odr->mem);
1469 set_param_str(params, "schema", esn, p->odr->mem);
1472 set_param_str(params, "schema", retrieve->name, p->odr->mem);
1473 else if (retrieve->identifier)
1474 set_param_str(params, "schema", retrieve->identifier, p->odr->mem);
1476 set_param_str(params, "schema", "", p->odr->mem);
1479 set_param_int(params, "score", p->score, p->odr->mem);
1480 set_param_int(params, "size", p->recordSize, p->odr->mem);
1482 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1485 XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
1488 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1492 /* retrieve conversion */
1493 perform_convert(tinfo, 0, p, retrieve->convert, params, &doc, &last_xsp);
1496 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1498 else if (!p->input_format
1499 || !oid_oidcmp(p->input_format, yaz_oid_recsyn_xml))
1505 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1507 xmlDocDumpMemory(doc, &buf_out, &len_out);
1509 p->output_format = yaz_oid_recsyn_xml;
1510 p->rec_len = len_out;
1511 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1512 memcpy(p->rec_buf, buf_out, p->rec_len);
1515 else if (!oid_oidcmp(p->output_format, yaz_oid_recsyn_sutrs))
1521 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1523 xmlDocDumpMemory(doc, &buf_out, &len_out);
1525 p->output_format = yaz_oid_recsyn_sutrs;
1526 p->rec_len = len_out;
1527 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1528 memcpy(p->rec_buf, buf_out, p->rec_len);
1534 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1540 static struct recType filter_type = {
1551 #ifdef IDZEBRA_STATIC_DOM
1564 * indent-tabs-mode: nil
1566 * vim: shiftwidth=4 tabstop=8 expandtab