1 /* $Id: mod_dom.c,v 1.18 2007-02-23 11:16:39 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
28 #include <yaz/diagbib1.h>
29 #include <yaz/tpath.h>
30 #include <yaz/snprintf.h>
32 #include <libxml/xmlversion.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
35 #include <libxml/xmlIO.h>
36 #include <libxml/xmlreader.h>
37 #include <libxslt/transform.h>
38 #include <libxslt/xsltutils.h>
41 #include <libexslt/exslt.h>
44 #include <idzebra/util.h>
45 #include <idzebra/recctrl.h>
47 /* DOM filter style indexing */
48 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
49 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
51 /* DOM filter style indexing */
52 #define ZEBRA_PI_NAME "zebra-2.0"
53 static const char *zebra_pi_name = ZEBRA_PI_NAME;
58 const char *stylesheet;
59 xsltStylesheetPtr stylesheet_xsp;
60 struct convert_s *next;
63 struct filter_extract {
65 struct convert_s *convert;
69 struct convert_s *convert;
72 struct filter_retrieve {
74 const char *identifier;
75 struct convert_s *convert;
76 struct filter_retrieve *next;
79 #define DOM_INPUT_XMLREADER 1
80 #define DOM_INPUT_MARC 2
84 struct convert_s *convert;
88 const char *input_charset;
93 xmlTextReaderPtr reader;
97 struct filter_input *next;
103 const char *profile_path;
106 xmlDocPtr doc_config;
107 struct filter_extract *extract;
108 struct filter_retrieve *retrieve_list;
109 struct filter_input *input_list;
110 struct filter_store *store;
115 #define XML_STRCMP(a,b) strcmp((char*)a, b)
116 #define XML_STRLEN(a) strlen((char*)a)
118 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
119 const char *fmt, ...)
121 __attribute__ ((format (printf, 4, 5)))
125 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
126 const char *fmt, ...)
130 xmlChar *node_path = 0;
133 node_path = xmlGetNodePath(ptr);
136 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
137 yaz_log(level, "%s: dom filter %s%s: %s",
138 tinfo->fname ? tinfo->fname : "none",
139 node_path ? "in " : "",
140 node_path ? (const char *) node_path : "", buf);
148 static void set_param_str(const char **params, const char *name,
149 const char *value, ODR odr)
151 char *quoted = odr_malloc(odr, 3 + strlen(value));
152 sprintf(quoted, "'%s'", value);
160 static void set_param_int(const char **params, const char *name,
163 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
166 sprintf(quoted, "'" ZINT_FORMAT "'", value);
172 static void *filter_init(Res res, RecType recType)
174 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
176 tinfo->full_name = 0;
177 tinfo->profile_path = 0;
178 tinfo->odr_record = odr_createmem(ODR_ENCODE);
179 tinfo->odr_config = odr_createmem(ODR_ENCODE);
181 tinfo->retrieve_list = 0;
182 tinfo->input_list = 0;
184 tinfo->doc_config = 0;
193 static int attr_content(struct _xmlAttr *attr, const char *name,
194 const char **dst_content)
196 if (!XML_STRCMP(attr->name, name) && attr->children
197 && attr->children->type == XML_TEXT_NODE)
199 *dst_content = (const char *)(attr->children->content);
205 static void destroy_xsp(struct convert_s *c)
209 if (c->stylesheet_xsp)
210 xsltFreeStylesheet(c->stylesheet_xsp);
215 static void destroy_dom(struct filter_info *tinfo)
219 destroy_xsp(tinfo->extract->convert);
224 destroy_xsp(tinfo->store->convert);
227 if (tinfo->input_list)
229 struct filter_input *i_ptr;
230 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
234 case DOM_INPUT_XMLREADER:
235 if (i_ptr->u.xmlreader.reader)
236 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
239 yaz_iconv_close(i_ptr->u.marc.iconv);
240 yaz_marc_destroy(i_ptr->u.marc.handle);
243 destroy_xsp(i_ptr->convert);
245 tinfo->input_list = 0;
247 if (tinfo->retrieve_list)
249 struct filter_retrieve *r_ptr;
250 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
251 destroy_xsp(r_ptr->convert);
252 tinfo->retrieve_list = 0;
255 if (tinfo->doc_config)
257 xmlFreeDoc(tinfo->doc_config);
258 tinfo->doc_config = 0;
260 odr_reset(tinfo->odr_config);
263 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
264 struct convert_s **l)
267 for(; ptr; ptr = ptr->next)
269 if (ptr->type != XML_ELEMENT_NODE)
271 if (!XML_STRCMP(ptr->name, "xslt"))
273 struct _xmlAttr *attr;
275 = odr_malloc(tinfo->odr_config, sizeof(*p));
279 p->stylesheet_xsp = 0;
281 for (attr = ptr->properties; attr; attr = attr->next)
282 if (attr_content(attr, "stylesheet", &p->stylesheet))
286 dom_log(YLOG_WARN, tinfo, ptr,
287 "bad attribute @%s", attr->name);
291 char tmp_xslt_full_name[1024];
292 if (!yaz_filepath_resolve(p->stylesheet,
297 dom_log(YLOG_WARN, tinfo, 0,
298 "stylesheet %s not found in "
301 tinfo->profile_path);
306 = xsltParseStylesheetFile((const xmlChar*)
308 if (!p->stylesheet_xsp)
310 dom_log(YLOG_WARN, tinfo, 0,
311 "could not parse xslt stylesheet %s",
318 dom_log(YLOG_WARN, tinfo, ptr,
319 "missing attribute 'stylesheet' ");
327 dom_log(YLOG_WARN, tinfo, ptr,
328 "bad node '%s'", ptr->name);
335 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
336 struct convert_s *convert,
339 xsltStylesheetPtr *last_xsp)
341 for (; convert; convert = convert->next)
343 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
346 *last_xsp = convert->stylesheet_xsp;
353 static struct filter_input *new_input(struct filter_info *tinfo, int type)
355 struct filter_input *p;
356 struct filter_input **np = &tinfo->input_list;
357 for (;*np; np = &(*np)->next)
359 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
368 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
369 const char *syntax, const char *name)
371 for (; ptr; ptr = ptr->next)
373 if (ptr->type != XML_ELEMENT_NODE)
375 if (!XML_STRCMP(ptr->name, "marc"))
377 yaz_iconv_t iconv = 0;
378 const char *input_charset = "marc-8";
379 struct _xmlAttr *attr;
381 for (attr = ptr->properties; attr; attr = attr->next)
383 if (attr_content(attr, "inputcharset", &input_charset))
387 dom_log(YLOG_WARN, tinfo, ptr,
388 "bad attribute @%s, expected @inputcharset",
392 iconv = yaz_iconv_open("utf-8", input_charset);
395 dom_log(YLOG_WARN, tinfo, ptr,
396 "unsupported @charset '%s'", input_charset);
401 struct filter_input *p
402 = new_input(tinfo, DOM_INPUT_MARC);
403 p->u.marc.handle = yaz_marc_create();
404 p->u.marc.iconv = iconv;
406 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
410 parse_convert(tinfo, ptr, &p->convert);
415 else if (!XML_STRCMP(ptr->name, "xmlreader"))
417 struct filter_input *p
418 = new_input(tinfo, DOM_INPUT_XMLREADER);
419 struct _xmlAttr *attr;
420 const char *level_str = 0;
422 p->u.xmlreader.split_level = 0;
423 p->u.xmlreader.reader = 0;
425 for (attr = ptr->properties; attr; attr = attr->next)
427 if (attr_content(attr, "level", &level_str))
431 dom_log(YLOG_WARN, tinfo, ptr,
432 "bad attribute @%s, expected @level",
437 p->u.xmlreader.split_level = atoi(level_str);
441 parse_convert(tinfo, ptr, &p->convert);
446 dom_log(YLOG_WARN, tinfo, ptr,
447 "bad element <%s>, expected <marc>|<xmlreader>",
455 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
457 char tmp_full_name[1024];
461 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
463 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
464 NULL, tmp_full_name))
465 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
467 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
469 yaz_log(YLOG_LOG, "%s dom filter: "
470 "loading config file %s", tinfo->fname, tinfo->full_name);
472 doc = xmlParseFile(tinfo->full_name);
475 yaz_log(YLOG_WARN, "%s: dom filter: "
476 "failed to parse config file %s",
477 tinfo->fname, tinfo->full_name);
480 /* save because we store ptrs to the content */
481 tinfo->doc_config = doc;
483 ptr = xmlDocGetRootElement(doc);
484 if (!ptr || ptr->type != XML_ELEMENT_NODE
485 || XML_STRCMP(ptr->name, "dom"))
487 dom_log(YLOG_WARN, tinfo, ptr,
488 "bad root element <%s>, expected root element <dom>",
493 for (ptr = ptr->children; ptr; ptr = ptr->next)
495 if (ptr->type != XML_ELEMENT_NODE)
497 if (!XML_STRCMP(ptr->name, "extract"))
500 <extract name="index">
501 <xslt stylesheet="first.xsl"/>
502 <xslt stylesheet="second.xsl"/>
505 struct _xmlAttr *attr;
506 struct filter_extract *f =
507 odr_malloc(tinfo->odr_config, sizeof(*f));
512 for (attr = ptr->properties; attr; attr = attr->next)
514 if (attr_content(attr, "name", &f->name))
518 dom_log(YLOG_WARN, tinfo, ptr,
519 "bad attribute @%s, expected @name",
523 parse_convert(tinfo, ptr->children, &f->convert);
525 else if (!XML_STRCMP(ptr->name, "retrieve"))
529 <xslt stylesheet="some.xsl"/>
530 <xslt stylesheet="some.xsl"/>
533 struct _xmlAttr *attr;
534 struct filter_retrieve **fp = &tinfo->retrieve_list;
535 struct filter_retrieve *f =
536 odr_malloc(tinfo->odr_config, sizeof(*f));
547 for (attr = ptr->properties; attr; attr = attr->next)
549 if (attr_content(attr, "identifier",
552 else if (attr_content(attr, "name", &f->name))
556 dom_log(YLOG_WARN, tinfo, ptr,
557 "bad attribute @%s, expected @identifier|@name",
561 parse_convert(tinfo, ptr->children, &f->convert);
563 else if (!XML_STRCMP(ptr->name, "store"))
567 <xslt stylesheet="some.xsl"/>
568 <xslt stylesheet="some.xsl"/>
571 struct filter_store *f =
572 odr_malloc(tinfo->odr_config, sizeof(*f));
576 parse_convert(tinfo, ptr->children, &f->convert);
578 else if (!XML_STRCMP(ptr->name, "input"))
582 <xmlreader level="1"/>
584 <input syntax="usmarc">
585 <marc inputcharset="marc-8"/>
588 struct _xmlAttr *attr;
589 const char *syntax = 0;
590 const char *name = 0;
591 for (attr = ptr->properties; attr; attr = attr->next)
593 if (attr_content(attr, "syntax", &syntax))
595 else if (attr_content(attr, "name", &name))
599 dom_log(YLOG_WARN, tinfo, ptr,
600 "bad attribute @%s, expected @syntax|@name",
604 parse_input(tinfo, ptr->children, syntax, name);
608 dom_log(YLOG_WARN, tinfo, ptr,
610 "expected <extract>|<input>|<retrieve>|<store>",
618 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
621 struct filter_retrieve *f = tinfo->retrieve_list;
623 /* return first schema if no est is provided */
626 for (; f; f = f->next)
628 /* find requested schema */
631 if (f->identifier && !strcmp(f->identifier, est))
633 if (f->name && !strcmp(f->name, est))
640 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
642 struct filter_info *tinfo = clientData;
645 yaz_log(YLOG_WARN, "dom filter: need config file");
649 if (tinfo->fname && !strcmp(args, tinfo->fname))
652 tinfo->profile_path = res_get(res, "profilePath");
655 return parse_dom(tinfo, args);
658 static void filter_destroy(void *clientData)
660 struct filter_info *tinfo = clientData;
662 odr_destroy(tinfo->odr_config);
663 odr_destroy(tinfo->odr_record);
667 static int ioread_ex(void *context, char *buffer, int len)
669 struct recExtractCtrl *p = context;
670 return p->stream->readf(p->stream, buffer, len);
673 static int ioclose_ex(void *context)
679 /* DOM filter style indexing */
680 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
681 xmlChar **dst_content)
683 if (0 == XML_STRCMP(attr->name, name) && attr->children
684 && attr->children->type == XML_TEXT_NODE)
686 *dst_content = (attr->children->content);
693 /* DOM filter style indexing */
694 static void index_value_of(struct filter_info *tinfo,
695 struct recExtractCtrl *extctr,
700 xmlChar *text = xmlNodeGetContent(node);
701 size_t text_len = strlen((const char *)text);
704 /* if there is no text, we do not need to proceed */
707 xmlChar *look = index_p;
714 /* assingning text to be indexed */
715 recword->term_buf = (const char *)text;
716 recword->term_len = text_len;
718 /* parsing all index name/type pairs */
719 /* may not start with ' ' or ':' */
720 while (*look && ' ' != *look && ':' != *look)
722 /* setting name and type to zero */
726 /* parsing one index name */
728 while (*look && ':' != *look && ' ' != *look)
733 strncpy((char *)index, (const char *)bval, eval - bval);
734 index[eval - bval] = '\0';
737 /* parsing one index type, if existing */
743 while (*look && ' ' != *look)
748 strncpy((char *)type, (const char *)bval, eval - bval);
749 type[eval - bval] = '\0';
752 /* actually indexing the text given */
753 dom_log(YLOG_DEBUG, tinfo, 0,
754 "INDEX '%s:%s' '%s'",
757 recword->index_name = (const char *)index;
759 recword->index_type = *type;
760 (extctr->tokenAdd)(recword);
762 /* eat whitespaces */
763 if (*look && ' ' == *look && *(look+1))
774 /* DOM filter style indexing */
775 static void set_record_info(struct filter_info *tinfo,
776 struct recExtractCtrl *extctr,
781 dom_log(YLOG_DEBUG, tinfo, 0,
782 "RECORD id=%s rank=%s type=%s",
783 id_p, rank_p, type_p);
786 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
789 extctr->staticrank = atozint((const char *)rank_p);
791 /* if (!strcmp("update", type_str)) */
792 /* index_node(tinfo, ctrl, ptr, recword); */
793 /* else if (!strcmp("delete", type_str)) */
794 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
796 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
802 /* DOM filter style indexing */
803 static void process_xml_element_zebra_node(struct filter_info *tinfo,
804 struct recExtractCtrl *extctr,
808 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
809 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
811 if (0 == XML_STRCMP(node->name, "index"))
813 xmlChar *index_p = 0;
815 struct _xmlAttr *attr;
816 for (attr = node->properties; attr; attr = attr->next)
818 if (attr_content_xml(attr, "name", &index_p))
820 index_value_of(tinfo, extctr, recword,node, index_p);
824 dom_log(YLOG_WARN, tinfo, node,
825 "bad attribute @%s, expected @name",
830 else if (0 == XML_STRCMP(node->name, "record"))
836 struct _xmlAttr *attr;
837 for (attr = node->properties; attr; attr = attr->next)
839 if (attr_content_xml(attr, "id", &id_p))
841 else if (attr_content_xml(attr, "rank", &rank_p))
843 else if (attr_content_xml(attr, "type", &type_p))
847 dom_log(YLOG_WARN, tinfo, node,
848 "bad attribute @%s, expected @id|@rank|@type",
852 if (type_p && 0 != strcmp("update", (const char *)type_p))
854 dom_log(YLOG_WARN, tinfo, node,
855 "attribute @%s, only implemented '@type='update'",
859 set_record_info(tinfo, extctr, id_p, rank_p, type_p);
863 dom_log(YLOG_WARN, tinfo, node,
865 " expected <record>|<index> in namespace '%s'",
866 node->name, zebra_dom_ns);
872 /* DOM filter style indexing */
873 static void process_xml_pi_node(struct filter_info *tinfo,
874 struct recExtractCtrl *extctr,
878 /* if right PI name, continue parsing PI */
879 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
881 xmlChar *pi_p = node->content;
882 xmlChar *look = pi_p;
887 /* parsing PI record instructions */
888 if (0 == strncmp((const char *)look, "record", 6))
901 while (*look && ' ' == *look && *(look+1))
904 /* parse possible id */
905 if (*look && 0 == strncmp((const char *)look, "id=", 3))
909 while (*look && ' ' != *look)
912 strncpy((char *)id, (const char *)bval, eval - bval);
913 id[eval - bval] = '\0';
917 while (*look && ' ' == *look && *(look+1))
920 /* parse possible rank */
921 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
925 while (*look && ' ' != *look)
928 strncpy((char *)rank, (const char *)bval, eval - bval);
929 rank[eval - bval] = '\0';
933 while (*look && ' ' == *look && *(look+1))
936 if (look && '\0' != *look)
938 dom_log(YLOG_WARN, tinfo, node,
939 "content '%s', can not parse '%s'",
943 set_record_info(tinfo, extctr, id, rank, 0);
946 /* parsing index instruction */
947 else if (0 == strncmp((const char *)look, "index", 5))
952 while (*look && ' ' == *look && *(look+1))
955 /* export index instructions to outside */
960 dom_log(YLOG_WARN, tinfo, node,
961 "content '%s', can not parse '%s'",
967 /* DOM filter style indexing */
968 static void process_xml_element_node(struct filter_info *tinfo,
969 struct recExtractCtrl *extctr,
973 /* remember indexing instruction from PI to next element node */
974 xmlChar *index_p = 0;
976 /* check if we are an element node in the special zebra namespace
977 and either set record data or index value-of node content*/
978 process_xml_element_zebra_node(tinfo, extctr, recword, node);
980 /* loop through kid nodes */
981 for (node = node->children; node; node = node->next)
983 /* check and set PI record and index index instructions */
984 if (node->type == XML_PI_NODE)
986 process_xml_pi_node(tinfo, extctr, node, &index_p);
988 else if (node->type == XML_ELEMENT_NODE)
990 /* if there was a PI index instruction before this element */
993 index_value_of(tinfo, extctr, recword, node, index_p);
996 process_xml_element_node(tinfo, extctr, recword,node);
1004 /* DOM filter style indexing */
1005 static void extract_dom_doc_node(struct filter_info *tinfo,
1006 struct recExtractCtrl *extctr,
1012 /* only need to do the initialization once, reuse recword for all terms */
1014 (*extctr->init)(extctr, &recword);
1016 if (extctr->flagShowRecords)
1018 xmlDocDumpMemory(doc, &buf_out, &len_out);
1019 fwrite(buf_out, len_out, 1, stdout);
1023 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1029 static int convert_extract_doc(struct filter_info *tinfo,
1030 struct filter_input *input,
1031 struct recExtractCtrl *p,
1037 const char *params[10];
1038 xsltStylesheetPtr last_xsp = 0;
1039 xmlDocPtr store_doc = 0;
1042 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1044 /* input conversion */
1045 perform_convert(tinfo, input->convert, params, &doc, 0);
1049 /* store conversion */
1050 store_doc = xmlCopyDoc(doc, 1);
1051 perform_convert(tinfo, tinfo->store->convert,
1052 params, &store_doc, &last_xsp);
1056 xsltSaveResultToString(&buf_out, &len_out,
1057 store_doc ? store_doc : doc, last_xsp);
1059 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1060 if (p->flagShowRecords)
1061 fwrite(buf_out, len_out, 1, stdout);
1062 (*p->setStoreData)(p, buf_out, len_out);
1066 xmlFreeDoc(store_doc);
1068 /* extract conversion */
1069 perform_convert(tinfo, tinfo->extract->convert, params, &doc, 0);
1071 /* finally, do the indexing */
1074 extract_dom_doc_node(tinfo, p, doc);
1075 /* extract_doc_alvis(tinfo, p, doc); */
1079 return RECCTRL_EXTRACT_OK;
1082 static int extract_xml_split(struct filter_info *tinfo,
1083 struct filter_input *input,
1084 struct recExtractCtrl *p)
1088 if (p->first_record)
1090 if (input->u.xmlreader.reader)
1091 xmlFreeTextReader(input->u.xmlreader.reader);
1092 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1093 p /* I/O handler */,
1099 if (!input->u.xmlreader.reader)
1100 return RECCTRL_EXTRACT_ERROR_GENERIC;
1102 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1105 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1106 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1107 if (type == XML_READER_TYPE_ELEMENT &&
1108 input->u.xmlreader.split_level == depth)
1111 = xmlTextReaderExpand(input->u.xmlreader.reader);
1114 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1115 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1117 xmlDocSetRootElement(doc, ptr2);
1119 return convert_extract_doc(tinfo, input, p, doc);
1123 xmlFreeTextReader(input->u.xmlreader.reader);
1124 input->u.xmlreader.reader = 0;
1125 return RECCTRL_EXTRACT_ERROR_GENERIC;
1128 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1130 xmlFreeTextReader(input->u.xmlreader.reader);
1131 input->u.xmlreader.reader = 0;
1132 return RECCTRL_EXTRACT_EOF;
1135 static int extract_xml_full(struct filter_info *tinfo,
1136 struct filter_input *input,
1137 struct recExtractCtrl *p)
1139 if (p->first_record) /* only one record per stream */
1141 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1142 p /* I/O handler */,
1145 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1148 return RECCTRL_EXTRACT_ERROR_GENERIC;
1150 return convert_extract_doc(tinfo, input, p, doc);
1153 return RECCTRL_EXTRACT_EOF;
1156 static int extract_iso2709(struct filter_info *tinfo,
1157 struct filter_input *input,
1158 struct recExtractCtrl *p)
1164 if (p->stream->readf(p->stream, buf, 5) != 5)
1165 return RECCTRL_EXTRACT_EOF;
1166 while (*buf < '0' || *buf > '9')
1170 dom_log(YLOG_WARN, tinfo, 0,
1171 "MARC: Skipping bad byte %d (0x%02X)",
1172 *buf & 0xff, *buf & 0xff);
1173 for (i = 0; i<4; i++)
1176 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1177 return RECCTRL_EXTRACT_EOF;
1179 record_length = atoi_n (buf, 5);
1180 if (record_length < 25)
1182 dom_log(YLOG_WARN, tinfo, 0,
1183 "MARC record length < 25, is %d", record_length);
1184 return RECCTRL_EXTRACT_ERROR_GENERIC;
1186 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1187 if (read_bytes < record_length-5)
1189 dom_log(YLOG_WARN, tinfo, 0,
1190 "couldn't read whole MARC record");
1191 return RECCTRL_EXTRACT_ERROR_GENERIC;
1193 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1194 if (r < record_length)
1196 dom_log (YLOG_WARN, tinfo, 0,
1197 "parsing of MARC record failed r=%d length=%d",
1199 return RECCTRL_EXTRACT_ERROR_GENERIC;
1205 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1206 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1207 xmlDocSetRootElement(rdoc, root_ptr);
1208 return convert_extract_doc(tinfo, input, p, rdoc);
1210 return RECCTRL_EXTRACT_OK;
1213 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1215 struct filter_info *tinfo = clientData;
1216 struct filter_input *input = tinfo->input_list;
1219 return RECCTRL_EXTRACT_ERROR_GENERIC;
1221 odr_reset(tinfo->odr_record);
1224 case DOM_INPUT_XMLREADER:
1225 if (input->u.xmlreader.split_level == 0)
1226 return extract_xml_full(tinfo, input, p);
1228 return extract_xml_split(tinfo, input, p);
1230 case DOM_INPUT_MARC:
1231 return extract_iso2709(tinfo, input, p);
1233 return RECCTRL_EXTRACT_ERROR_GENERIC;
1236 static int ioread_ret(void *context, char *buffer, int len)
1238 struct recRetrieveCtrl *p = context;
1239 return p->stream->readf(p->stream, buffer, len);
1242 static int ioclose_ret(void *context)
1247 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1249 /* const char *esn = zebra_dom_ns; */
1250 const char *esn = 0;
1251 const char *params[32];
1252 struct filter_info *tinfo = clientData;
1254 struct filter_retrieve *retrieve;
1255 xsltStylesheetPtr last_xsp = 0;
1259 if (p->comp->which == Z_RecordComp_simple
1260 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1262 esn = p->comp->u.simple->u.generic;
1264 else if (p->comp->which == Z_RecordComp_complex
1265 && p->comp->u.complex->generic->elementSpec
1266 && p->comp->u.complex->generic->elementSpec->which ==
1267 Z_ElementSpec_elementSetName)
1269 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1272 retrieve = lookup_retrieve(tinfo, esn);
1276 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1281 set_param_int(params, "id", p->localno, p->odr);
1283 set_param_str(params, "filename", p->fname, p->odr);
1284 if (p->staticrank >= 0)
1285 set_param_int(params, "rank", p->staticrank, p->odr);
1288 set_param_str(params, "schema", esn, p->odr);
1291 set_param_str(params, "schema", retrieve->name, p->odr);
1292 else if (retrieve->identifier)
1293 set_param_str(params, "schema", retrieve->identifier, p->odr);
1295 set_param_str(params, "schema", "", p->odr);
1298 set_param_int(params, "score", p->score, p->odr);
1299 set_param_int(params, "size", p->recordSize, p->odr);
1301 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1304 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1307 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1311 /* retrieve conversion */
1312 perform_convert(tinfo, retrieve->convert, params, &doc, &last_xsp);
1315 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1317 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1323 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1325 xmlDocDumpMemory(doc, &buf_out, &len_out);
1327 p->output_format = VAL_TEXT_XML;
1328 p->rec_len = len_out;
1329 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1330 memcpy(p->rec_buf, buf_out, p->rec_len);
1333 else if (p->output_format == VAL_SUTRS)
1339 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1341 xmlDocDumpMemory(doc, &buf_out, &len_out);
1343 p->output_format = VAL_SUTRS;
1344 p->rec_len = len_out;
1345 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1346 memcpy(p->rec_buf, buf_out, p->rec_len);
1352 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1358 static struct recType filter_type = {
1369 #ifdef IDZEBRA_STATIC_DOM
1382 * indent-tabs-mode: nil
1384 * vim: shiftwidth=4 tabstop=8 expandtab