2 /* $Id: mod_dom.c,v 1.28 2007-03-06 08:48:57 adam Exp $
3 Copyright (C) 1995-2007
6 This file is part of the Zebra server.
8 Zebra is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <yaz/diagbib1.h>
30 #include <yaz/tpath.h>
31 #include <yaz/snprintf.h>
33 #include <libxml/xmlversion.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
36 #include <libxml/xmlIO.h>
37 #include <libxml/xmlreader.h>
38 #include <libxslt/transform.h>
39 #include <libxslt/xsltutils.h>
42 #include <libexslt/exslt.h>
45 #include <idzebra/util.h>
46 #include <idzebra/recctrl.h>
48 /* DOM filter style indexing */
49 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
50 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
52 /* DOM filter style indexing */
53 #define ZEBRA_PI_NAME "zebra-2.0"
54 static const char *zebra_pi_name = ZEBRA_PI_NAME;
59 const char *stylesheet;
60 xsltStylesheetPtr stylesheet_xsp;
61 struct convert_s *next;
64 struct filter_extract {
66 struct convert_s *convert;
70 struct convert_s *convert;
73 struct filter_retrieve {
75 const char *identifier;
76 struct convert_s *convert;
77 struct filter_retrieve *next;
80 #define DOM_INPUT_XMLREADER 1
81 #define DOM_INPUT_MARC 2
85 struct convert_s *convert;
89 const char *input_charset;
94 xmlTextReaderPtr reader;
98 struct filter_input *next;
104 const char *profile_path;
107 xmlDocPtr doc_config;
108 struct filter_extract *extract;
109 struct filter_retrieve *retrieve_list;
110 struct filter_input *input_list;
111 struct filter_store *store;
112 int record_info_invoked;
117 #define XML_STRCMP(a,b) strcmp((char*)a, b)
118 #define XML_STRLEN(a) strlen((char*)a)
121 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
123 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
124 const char *fmt, ...)
126 __attribute__ ((format (printf, 4, 5)))
130 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
131 const char *fmt, ...)
137 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
140 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
141 xmlGetLineNo(ptr), buf);
145 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
151 static void set_param_str(const char **params, const char *name,
152 const char *value, ODR odr)
154 char *quoted = odr_malloc(odr, 3 + strlen(value));
155 sprintf(quoted, "'%s'", value);
163 static void set_param_int(const char **params, const char *name,
166 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
169 sprintf(quoted, "'" ZINT_FORMAT "'", value);
175 static void *filter_init(Res res, RecType recType)
177 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
179 tinfo->full_name = 0;
180 tinfo->profile_path = 0;
181 tinfo->odr_record = odr_createmem(ODR_ENCODE);
182 tinfo->odr_config = odr_createmem(ODR_ENCODE);
184 tinfo->retrieve_list = 0;
185 tinfo->input_list = 0;
187 tinfo->doc_config = 0;
188 tinfo->record_info_invoked = 0;
197 static int attr_content(struct _xmlAttr *attr, const char *name,
198 const char **dst_content)
200 if (!XML_STRCMP(attr->name, name) && attr->children
201 && attr->children->type == XML_TEXT_NODE)
203 *dst_content = (const char *)(attr->children->content);
209 static void destroy_xsp(struct convert_s *c)
213 if (c->stylesheet_xsp)
214 xsltFreeStylesheet(c->stylesheet_xsp);
219 static void destroy_dom(struct filter_info *tinfo)
223 destroy_xsp(tinfo->extract->convert);
228 destroy_xsp(tinfo->store->convert);
231 if (tinfo->input_list)
233 struct filter_input *i_ptr;
234 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
238 case DOM_INPUT_XMLREADER:
239 if (i_ptr->u.xmlreader.reader)
240 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
243 yaz_iconv_close(i_ptr->u.marc.iconv);
244 yaz_marc_destroy(i_ptr->u.marc.handle);
247 destroy_xsp(i_ptr->convert);
249 tinfo->input_list = 0;
251 if (tinfo->retrieve_list)
253 struct filter_retrieve *r_ptr;
254 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
255 destroy_xsp(r_ptr->convert);
256 tinfo->retrieve_list = 0;
259 if (tinfo->doc_config)
261 xmlFreeDoc(tinfo->doc_config);
262 tinfo->doc_config = 0;
264 odr_reset(tinfo->odr_config);
267 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
268 struct convert_s **l)
271 FOR_EACH_ELEMENT(ptr) {
272 if (!XML_STRCMP(ptr->name, "xslt"))
274 struct _xmlAttr *attr;
276 = odr_malloc(tinfo->odr_config, sizeof(*p));
280 p->stylesheet_xsp = 0;
282 for (attr = ptr->properties; attr; attr = attr->next)
283 if (attr_content(attr, "stylesheet", &p->stylesheet))
287 dom_log(YLOG_WARN, tinfo, ptr,
288 "bad attribute @%s", attr->name);
292 char tmp_xslt_full_name[1024];
293 if (!yaz_filepath_resolve(p->stylesheet,
298 dom_log(YLOG_WARN, tinfo, 0,
299 "stylesheet %s not found in "
302 tinfo->profile_path);
307 = xsltParseStylesheetFile((const xmlChar*)
309 if (!p->stylesheet_xsp)
311 dom_log(YLOG_WARN, tinfo, 0,
312 "could not parse xslt stylesheet %s",
319 dom_log(YLOG_WARN, tinfo, ptr,
320 "missing attribute 'stylesheet' ");
328 dom_log(YLOG_WARN, tinfo, ptr,
329 "bad element '%s', expected <xslt>", ptr->name);
336 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
337 struct recExtractCtrl *extctr,
338 struct convert_s *convert,
341 xsltStylesheetPtr *last_xsp)
343 for (; convert; convert = convert->next)
345 xmlChar *buf_out = 0;
347 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
350 *last_xsp = convert->stylesheet_xsp;
355 /* now saving into buffer and re-reading into DOM to avoid annoing
356 XSLT problem with thrown-out indentation text nodes */
357 xsltSaveResultToString(&buf_out, &len_out, res_doc,
358 convert->stylesheet_xsp);
363 *doc = xmlParseMemory((const char *) buf_out, len_out);
365 /* writing debug info out */
366 if (extctr && extctr->flagShowRecords)
367 yaz_log(YLOG_LOG, "%s: XSLT %s\n %.*s",
368 tinfo->fname ? tinfo->fname : "(none)",
377 static struct filter_input *new_input(struct filter_info *tinfo, int type)
379 struct filter_input *p;
380 struct filter_input **np = &tinfo->input_list;
381 for (;*np; np = &(*np)->next)
383 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
392 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
393 const char *syntax, const char *name)
395 FOR_EACH_ELEMENT(ptr) {
396 if (!XML_STRCMP(ptr->name, "marc"))
398 yaz_iconv_t iconv = 0;
399 const char *input_charset = "marc-8";
400 struct _xmlAttr *attr;
402 for (attr = ptr->properties; attr; attr = attr->next)
404 if (attr_content(attr, "inputcharset", &input_charset))
408 dom_log(YLOG_WARN, tinfo, ptr,
409 "bad attribute @%s, expected @inputcharset",
413 iconv = yaz_iconv_open("utf-8", input_charset);
416 dom_log(YLOG_WARN, tinfo, ptr,
417 "unsupported @charset '%s'", input_charset);
422 struct filter_input *p
423 = new_input(tinfo, DOM_INPUT_MARC);
424 p->u.marc.handle = yaz_marc_create();
425 p->u.marc.iconv = iconv;
427 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
431 parse_convert(tinfo, ptr, &p->convert);
436 else if (!XML_STRCMP(ptr->name, "xmlreader"))
438 struct filter_input *p
439 = new_input(tinfo, DOM_INPUT_XMLREADER);
440 struct _xmlAttr *attr;
441 const char *level_str = 0;
443 p->u.xmlreader.split_level = 0;
444 p->u.xmlreader.reader = 0;
446 for (attr = ptr->properties; attr; attr = attr->next)
448 if (attr_content(attr, "level", &level_str))
452 dom_log(YLOG_WARN, tinfo, ptr,
453 "bad attribute @%s, expected @level",
458 p->u.xmlreader.split_level = atoi(level_str);
462 parse_convert(tinfo, ptr, &p->convert);
467 dom_log(YLOG_WARN, tinfo, ptr,
468 "bad element <%s>, expected <marc>|<xmlreader>",
476 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
478 char tmp_full_name[1024];
482 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
484 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
485 NULL, tmp_full_name))
486 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
488 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
490 yaz_log(YLOG_LOG, "%s dom filter: "
491 "loading config file %s", tinfo->fname, tinfo->full_name);
493 doc = xmlParseFile(tinfo->full_name);
496 yaz_log(YLOG_WARN, "%s: dom filter: "
497 "failed to parse config file %s",
498 tinfo->fname, tinfo->full_name);
501 /* save because we store ptrs to the content */
502 tinfo->doc_config = doc;
504 ptr = xmlDocGetRootElement(doc);
505 if (!ptr || ptr->type != XML_ELEMENT_NODE
506 || XML_STRCMP(ptr->name, "dom"))
508 dom_log(YLOG_WARN, tinfo, ptr,
509 "bad root element <%s>, expected root element <dom>",
515 FOR_EACH_ELEMENT(ptr) {
516 if (!XML_STRCMP(ptr->name, "extract"))
519 <extract name="index">
520 <xslt stylesheet="first.xsl"/>
521 <xslt stylesheet="second.xsl"/>
524 struct _xmlAttr *attr;
525 struct filter_extract *f =
526 odr_malloc(tinfo->odr_config, sizeof(*f));
531 for (attr = ptr->properties; attr; attr = attr->next)
533 if (attr_content(attr, "name", &f->name))
537 dom_log(YLOG_WARN, tinfo, ptr,
538 "bad attribute @%s, expected @name",
542 parse_convert(tinfo, ptr->children, &f->convert);
544 else if (!XML_STRCMP(ptr->name, "retrieve"))
548 <xslt stylesheet="some.xsl"/>
549 <xslt stylesheet="some.xsl"/>
552 struct _xmlAttr *attr;
553 struct filter_retrieve **fp = &tinfo->retrieve_list;
554 struct filter_retrieve *f =
555 odr_malloc(tinfo->odr_config, sizeof(*f));
566 for (attr = ptr->properties; attr; attr = attr->next)
568 if (attr_content(attr, "identifier",
571 else if (attr_content(attr, "name", &f->name))
575 dom_log(YLOG_WARN, tinfo, ptr,
576 "bad attribute @%s, expected @identifier|@name",
580 parse_convert(tinfo, ptr->children, &f->convert);
582 else if (!XML_STRCMP(ptr->name, "store"))
586 <xslt stylesheet="some.xsl"/>
587 <xslt stylesheet="some.xsl"/>
590 struct filter_store *f =
591 odr_malloc(tinfo->odr_config, sizeof(*f));
595 parse_convert(tinfo, ptr->children, &f->convert);
597 else if (!XML_STRCMP(ptr->name, "input"))
601 <xmlreader level="1"/>
603 <input syntax="usmarc">
604 <marc inputcharset="marc-8"/>
607 struct _xmlAttr *attr;
608 const char *syntax = 0;
609 const char *name = 0;
610 for (attr = ptr->properties; attr; attr = attr->next)
612 if (attr_content(attr, "syntax", &syntax))
614 else if (attr_content(attr, "name", &name))
618 dom_log(YLOG_WARN, tinfo, ptr,
619 "bad attribute @%s, expected @syntax|@name",
623 parse_input(tinfo, ptr->children, syntax, name);
627 dom_log(YLOG_WARN, tinfo, ptr,
629 "expected <extract>|<input>|<retrieve>|<store>",
637 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
640 struct filter_retrieve *f = tinfo->retrieve_list;
642 /* return first schema if no est is provided */
645 for (; f; f = f->next)
647 /* find requested schema */
650 if (f->identifier && !strcmp(f->identifier, est))
652 if (f->name && !strcmp(f->name, est))
659 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
661 struct filter_info *tinfo = clientData;
664 yaz_log(YLOG_WARN, "dom filter: need config file");
668 if (tinfo->fname && !strcmp(args, tinfo->fname))
671 tinfo->profile_path = res_get(res, "profilePath");
674 return parse_dom(tinfo, args);
677 static void filter_destroy(void *clientData)
679 struct filter_info *tinfo = clientData;
681 odr_destroy(tinfo->odr_config);
682 odr_destroy(tinfo->odr_record);
686 static int ioread_ex(void *context, char *buffer, int len)
688 struct recExtractCtrl *p = context;
689 return p->stream->readf(p->stream, buffer, len);
692 static int ioclose_ex(void *context)
698 /* DOM filter style indexing */
699 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
700 xmlChar **dst_content)
702 if (0 == XML_STRCMP(attr->name, name) && attr->children
703 && attr->children->type == XML_TEXT_NODE)
705 *dst_content = (attr->children->content);
712 /* DOM filter style indexing */
713 static void index_value_of(struct filter_info *tinfo,
714 struct recExtractCtrl *extctr,
719 if (tinfo->record_info_invoked == 1)
721 xmlChar *text = xmlNodeGetContent(node);
722 size_t text_len = strlen((const char *)text);
724 /* if there is no text, we do not need to proceed */
727 xmlChar *look = index_p;
734 /* assingning text to be indexed */
735 recword->term_buf = (const char *)text;
736 recword->term_len = text_len;
738 /* parsing all index name/type pairs */
739 /* may not start with ' ' or ':' */
740 while (*look && ' ' != *look && ':' != *look)
742 /* setting name and type to zero */
746 /* parsing one index name */
748 while (*look && ':' != *look && ' ' != *look)
753 strncpy((char *)index, (const char *)bval, eval - bval);
754 index[eval - bval] = '\0';
757 /* parsing one index type, if existing */
763 while (*look && ' ' != *look)
768 strncpy((char *)type, (const char *)bval, eval - bval);
769 type[eval - bval] = '\0';
772 /* actually indexing the text given */
773 dom_log(YLOG_DEBUG, tinfo, 0,
774 "INDEX '%s:%s' '%s'",
775 index ? (const char *) index : "null",
776 type ? (const char *) type : "null",
777 text ? (const char *) text : "null");
779 recword->index_name = (const char *)index;
781 recword->index_type = *type;
783 /* writing debug out */
784 if (extctr->flagShowRecords)
785 dom_log(YLOG_LOG, tinfo, 0,
786 "INDEX '%s:%s' '%s'",
787 index ? (const char *) index : "null",
788 type ? (const char *) type : "null",
789 text ? (const char *) text : "null");
791 /* actually indexing the text given */
792 recword->index_name = (const char *)index;
794 recword->index_type = *type;
795 (extctr->tokenAdd)(recword);
797 /* eat whitespaces */
798 if (*look && ' ' == *look && *(look+1))
809 /* DOM filter style indexing */
810 static void set_record_info(struct filter_info *tinfo,
811 struct recExtractCtrl *extctr,
818 /* writing debug info out */
819 if (extctr->flagShowRecords)
820 dom_log(YLOG_LOG, tinfo, 0,
821 "RECORD id=%s rank=%s type=%s",
822 id_p ? (const char *) id_p : "(null)",
823 rank_p ? (const char *) rank_p : "(null)",
824 type_p ? (const char *) type_p : "(null)");
828 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
831 extctr->staticrank = atozint((const char *)rank_p);
833 /* if (!strcmp("update", type_str)) */
834 /* index_node(tinfo, ctrl, ptr, recword); */
835 /* else if (!strcmp("delete", type_str)) */
836 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
838 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
840 if (tinfo->record_info_invoked == 1)
842 /* warn about multiple only once */
843 dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
845 tinfo->record_info_invoked++;
850 /* DOM filter style indexing */
851 static void process_xml_element_zebra_node(struct filter_info *tinfo,
852 struct recExtractCtrl *extctr,
856 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
857 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
859 if (0 == XML_STRCMP(node->name, "index"))
861 xmlChar *index_p = 0;
863 struct _xmlAttr *attr;
864 for (attr = node->properties; attr; attr = attr->next)
866 if (attr_content_xml(attr, "name", &index_p))
868 index_value_of(tinfo, extctr, recword,node, index_p);
872 dom_log(YLOG_WARN, tinfo, node,
873 "bad attribute @%s, expected @name",
878 else if (0 == XML_STRCMP(node->name, "record"))
884 struct _xmlAttr *attr;
885 for (attr = node->properties; attr; attr = attr->next)
887 if (attr_content_xml(attr, "id", &id_p))
889 else if (attr_content_xml(attr, "rank", &rank_p))
891 else if (attr_content_xml(attr, "type", &type_p))
895 dom_log(YLOG_WARN, tinfo, node,
896 "bad attribute @%s, expected @id|@rank|@type",
900 if (type_p && 0 != strcmp("update", (const char *)type_p))
902 dom_log(YLOG_WARN, tinfo, node,
903 "attribute @%s, only implemented '@type='update'",
907 set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
911 dom_log(YLOG_WARN, tinfo, node,
913 " expected <record>|<index> in namespace '%s'",
914 node->name, zebra_dom_ns);
920 /* DOM filter style indexing */
921 static void process_xml_pi_node(struct filter_info *tinfo,
922 struct recExtractCtrl *extctr,
926 /* if right PI name, continue parsing PI */
927 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
929 xmlChar *pi_p = node->content;
930 xmlChar *look = pi_p;
935 /* parsing PI record instructions */
936 if (0 == strncmp((const char *)look, "record", 6))
949 while (*look && ' ' == *look && *(look+1))
952 /* parse possible id */
953 if (*look && 0 == strncmp((const char *)look, "id=", 3))
957 while (*look && ' ' != *look)
960 strncpy((char *)id, (const char *)bval, eval - bval);
961 id[eval - bval] = '\0';
965 while (*look && ' ' == *look && *(look+1))
968 /* parse possible rank */
969 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
973 while (*look && ' ' != *look)
976 strncpy((char *)rank, (const char *)bval, eval - bval);
977 rank[eval - bval] = '\0';
981 while (*look && ' ' == *look && *(look+1))
984 if (look && '\0' != *look)
986 dom_log(YLOG_WARN, tinfo, node,
987 "content '%s', can not parse '%s'",
991 set_record_info(tinfo, extctr, node, id, rank, 0);
994 /* parsing index instruction */
995 else if (0 == strncmp((const char *)look, "index", 5))
1000 while (*look && ' ' == *look && *(look+1))
1003 /* export index instructions to outside */
1008 dom_log(YLOG_WARN, tinfo, node,
1009 "content '%s', can not parse '%s'",
1015 /* DOM filter style indexing */
1016 static void process_xml_element_node(struct filter_info *tinfo,
1017 struct recExtractCtrl *extctr,
1021 /* remember indexing instruction from PI to next element node */
1022 xmlChar *index_p = 0;
1024 /* check if we are an element node in the special zebra namespace
1025 and either set record data or index value-of node content*/
1026 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1028 /* loop through kid nodes */
1029 for (node = node->children; node; node = node->next)
1031 /* check and set PI record and index index instructions */
1032 if (node->type == XML_PI_NODE)
1034 process_xml_pi_node(tinfo, extctr, node, &index_p);
1036 else if (node->type == XML_ELEMENT_NODE)
1038 /* if there was a PI index instruction before this element */
1041 index_value_of(tinfo, extctr, recword, node, index_p);
1044 process_xml_element_node(tinfo, extctr, recword,node);
1052 /* DOM filter style indexing */
1053 static void extract_dom_doc_node(struct filter_info *tinfo,
1054 struct recExtractCtrl *extctr,
1057 /* only need to do the initialization once, reuse recword for all terms */
1059 (*extctr->init)(extctr, &recword);
1061 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1067 static int convert_extract_doc(struct filter_info *tinfo,
1068 struct filter_input *input,
1069 struct recExtractCtrl *p,
1075 const char *params[10];
1076 xsltStylesheetPtr last_xsp = 0;
1077 xmlDocPtr store_doc = 0;
1079 /* per default do not ingest record */
1080 tinfo->record_info_invoked = 0;
1082 /* exit if empty document given */
1084 return RECCTRL_EXTRACT_SKIP;
1086 /* we actuallu have a document which needs to be processed further */
1088 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1090 /* input conversion */
1091 perform_convert(tinfo, p, input->convert, params, &doc, 0);
1095 /* store conversion */
1096 store_doc = xmlCopyDoc(doc, 1);
1097 perform_convert(tinfo, p, tinfo->store->convert,
1098 params, &store_doc, &last_xsp);
1101 /* saving either store doc or original doc in case no store doc exists */
1103 xsltSaveResultToString(&buf_out, &len_out,
1104 store_doc ? store_doc : doc, last_xsp);
1106 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1108 (*p->setStoreData)(p, buf_out, len_out);
1112 xmlFreeDoc(store_doc);
1114 /* extract conversion */
1115 perform_convert(tinfo, p, tinfo->extract->convert, params, &doc, 0);
1118 /* finally, do the indexing */
1120 extract_dom_doc_node(tinfo, p, doc);
1124 /* there was nothing to index, so there is no inserted/updated record */
1125 if (tinfo->record_info_invoked == 0)
1126 return RECCTRL_EXTRACT_SKIP;
1128 return RECCTRL_EXTRACT_OK;
1131 static int extract_xml_split(struct filter_info *tinfo,
1132 struct filter_input *input,
1133 struct recExtractCtrl *p)
1137 if (p->first_record)
1139 if (input->u.xmlreader.reader)
1140 xmlFreeTextReader(input->u.xmlreader.reader);
1141 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1142 p /* I/O handler */,
1148 if (!input->u.xmlreader.reader)
1149 return RECCTRL_EXTRACT_ERROR_GENERIC;
1151 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1154 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1155 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1157 if (type == XML_READER_TYPE_ELEMENT &&
1158 input->u.xmlreader.split_level == depth)
1160 /* per default do not ingest record */
1161 tinfo->record_info_invoked = 0;
1164 = xmlTextReaderExpand(input->u.xmlreader.reader);
1168 /* we have a new document */
1170 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1171 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1173 xmlDocSetRootElement(doc, ptr2);
1175 /* writing debug info out */
1176 if (p->flagShowRecords)
1178 xmlChar *buf_out = 0;
1180 xmlDocDumpMemory(doc, &buf_out, &len_out);
1181 yaz_log(YLOG_LOG, "%s: XMLREADER depth: %i\n%.*s",
1182 tinfo->fname ? tinfo->fname : "(none)",
1183 depth, len_out, buf_out);
1187 return convert_extract_doc(tinfo, input, p, doc);
1191 xmlFreeTextReader(input->u.xmlreader.reader);
1192 input->u.xmlreader.reader = 0;
1193 return RECCTRL_EXTRACT_ERROR_GENERIC;
1196 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1198 xmlFreeTextReader(input->u.xmlreader.reader);
1199 input->u.xmlreader.reader = 0;
1200 return RECCTRL_EXTRACT_EOF;
1203 static int extract_xml_full(struct filter_info *tinfo,
1204 struct filter_input *input,
1205 struct recExtractCtrl *p)
1207 if (p->first_record) /* only one record per stream */
1209 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1210 p /* I/O handler */,
1213 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1216 return RECCTRL_EXTRACT_ERROR_GENERIC;
1218 return convert_extract_doc(tinfo, input, p, doc);
1221 return RECCTRL_EXTRACT_EOF;
1224 static int extract_iso2709(struct filter_info *tinfo,
1225 struct filter_input *input,
1226 struct recExtractCtrl *p)
1232 if (p->stream->readf(p->stream, buf, 5) != 5)
1233 return RECCTRL_EXTRACT_EOF;
1234 while (*buf < '0' || *buf > '9')
1238 dom_log(YLOG_WARN, tinfo, 0,
1239 "MARC: Skipping bad byte %d (0x%02X)",
1240 *buf & 0xff, *buf & 0xff);
1241 for (i = 0; i<4; i++)
1244 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1245 return RECCTRL_EXTRACT_EOF;
1247 record_length = atoi_n (buf, 5);
1248 if (record_length < 25)
1250 dom_log(YLOG_WARN, tinfo, 0,
1251 "MARC record length < 25, is %d", record_length);
1252 return RECCTRL_EXTRACT_ERROR_GENERIC;
1254 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1255 if (read_bytes < record_length-5)
1257 dom_log(YLOG_WARN, tinfo, 0,
1258 "couldn't read whole MARC record");
1259 return RECCTRL_EXTRACT_ERROR_GENERIC;
1261 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1262 if (r < record_length)
1264 dom_log (YLOG_WARN, tinfo, 0,
1265 "parsing of MARC record failed r=%d length=%d",
1267 return RECCTRL_EXTRACT_ERROR_GENERIC;
1273 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1274 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1275 xmlDocSetRootElement(rdoc, root_ptr);
1276 return convert_extract_doc(tinfo, input, p, rdoc);
1278 return RECCTRL_EXTRACT_OK;
1281 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1283 struct filter_info *tinfo = clientData;
1284 struct filter_input *input = tinfo->input_list;
1287 return RECCTRL_EXTRACT_ERROR_GENERIC;
1289 odr_reset(tinfo->odr_record);
1292 case DOM_INPUT_XMLREADER:
1293 if (input->u.xmlreader.split_level == 0)
1294 return extract_xml_full(tinfo, input, p);
1296 return extract_xml_split(tinfo, input, p);
1298 case DOM_INPUT_MARC:
1299 return extract_iso2709(tinfo, input, p);
1301 return RECCTRL_EXTRACT_ERROR_GENERIC;
1304 static int ioread_ret(void *context, char *buffer, int len)
1306 struct recRetrieveCtrl *p = context;
1307 return p->stream->readf(p->stream, buffer, len);
1310 static int ioclose_ret(void *context)
1315 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1317 /* const char *esn = zebra_dom_ns; */
1318 const char *esn = 0;
1319 const char *params[32];
1320 struct filter_info *tinfo = clientData;
1322 struct filter_retrieve *retrieve;
1323 xsltStylesheetPtr last_xsp = 0;
1327 if (p->comp->which == Z_RecordComp_simple
1328 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1330 esn = p->comp->u.simple->u.generic;
1332 else if (p->comp->which == Z_RecordComp_complex
1333 && p->comp->u.complex->generic->elementSpec
1334 && p->comp->u.complex->generic->elementSpec->which ==
1335 Z_ElementSpec_elementSetName)
1337 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1340 retrieve = lookup_retrieve(tinfo, esn);
1344 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1349 set_param_int(params, "id", p->localno, p->odr);
1351 set_param_str(params, "filename", p->fname, p->odr);
1352 if (p->staticrank >= 0)
1353 set_param_int(params, "rank", p->staticrank, p->odr);
1356 set_param_str(params, "schema", esn, p->odr);
1359 set_param_str(params, "schema", retrieve->name, p->odr);
1360 else if (retrieve->identifier)
1361 set_param_str(params, "schema", retrieve->identifier, p->odr);
1363 set_param_str(params, "schema", "", p->odr);
1366 set_param_int(params, "score", p->score, p->odr);
1367 set_param_int(params, "size", p->recordSize, p->odr);
1369 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1372 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1375 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1379 /* retrieve conversion */
1380 perform_convert(tinfo, 0, retrieve->convert, params, &doc, &last_xsp);
1383 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1385 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1391 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1393 xmlDocDumpMemory(doc, &buf_out, &len_out);
1395 p->output_format = VAL_TEXT_XML;
1396 p->rec_len = len_out;
1397 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1398 memcpy(p->rec_buf, buf_out, p->rec_len);
1401 else if (p->output_format == VAL_SUTRS)
1407 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1409 xmlDocDumpMemory(doc, &buf_out, &len_out);
1411 p->output_format = VAL_SUTRS;
1412 p->rec_len = len_out;
1413 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1414 memcpy(p->rec_buf, buf_out, p->rec_len);
1420 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1426 static struct recType filter_type = {
1437 #ifdef IDZEBRA_STATIC_DOM
1450 * indent-tabs-mode: nil
1452 * vim: shiftwidth=4 tabstop=8 expandtab