1 /* $Id: mod_dom.c,v 1.19 2007-02-23 11:35:08 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)
119 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
121 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
122 const char *fmt, ...)
124 __attribute__ ((format (printf, 4, 5)))
128 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
129 const char *fmt, ...)
133 xmlChar *node_path = 0;
136 node_path = xmlGetNodePath(ptr);
139 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
140 yaz_log(level, "%s: dom filter %s%s: %s",
141 tinfo->fname ? tinfo->fname : "none",
142 node_path ? "in " : "",
143 node_path ? (const char *) node_path : "", 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;
196 static int attr_content(struct _xmlAttr *attr, const char *name,
197 const char **dst_content)
199 if (!XML_STRCMP(attr->name, name) && attr->children
200 && attr->children->type == XML_TEXT_NODE)
202 *dst_content = (const char *)(attr->children->content);
208 static void destroy_xsp(struct convert_s *c)
212 if (c->stylesheet_xsp)
213 xsltFreeStylesheet(c->stylesheet_xsp);
218 static void destroy_dom(struct filter_info *tinfo)
222 destroy_xsp(tinfo->extract->convert);
227 destroy_xsp(tinfo->store->convert);
230 if (tinfo->input_list)
232 struct filter_input *i_ptr;
233 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
237 case DOM_INPUT_XMLREADER:
238 if (i_ptr->u.xmlreader.reader)
239 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
242 yaz_iconv_close(i_ptr->u.marc.iconv);
243 yaz_marc_destroy(i_ptr->u.marc.handle);
246 destroy_xsp(i_ptr->convert);
248 tinfo->input_list = 0;
250 if (tinfo->retrieve_list)
252 struct filter_retrieve *r_ptr;
253 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
254 destroy_xsp(r_ptr->convert);
255 tinfo->retrieve_list = 0;
258 if (tinfo->doc_config)
260 xmlFreeDoc(tinfo->doc_config);
261 tinfo->doc_config = 0;
263 odr_reset(tinfo->odr_config);
266 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
267 struct convert_s **l)
270 FOR_EACH_ELEMENT(ptr) {
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 element '%s', expected <xslt>", 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_EACH_ELEMENT(ptr) {
372 if (!XML_STRCMP(ptr->name, "marc"))
374 yaz_iconv_t iconv = 0;
375 const char *input_charset = "marc-8";
376 struct _xmlAttr *attr;
378 for (attr = ptr->properties; attr; attr = attr->next)
380 if (attr_content(attr, "inputcharset", &input_charset))
384 dom_log(YLOG_WARN, tinfo, ptr,
385 "bad attribute @%s, expected @inputcharset",
389 iconv = yaz_iconv_open("utf-8", input_charset);
392 dom_log(YLOG_WARN, tinfo, ptr,
393 "unsupported @charset '%s'", input_charset);
398 struct filter_input *p
399 = new_input(tinfo, DOM_INPUT_MARC);
400 p->u.marc.handle = yaz_marc_create();
401 p->u.marc.iconv = iconv;
403 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
407 parse_convert(tinfo, ptr, &p->convert);
412 else if (!XML_STRCMP(ptr->name, "xmlreader"))
414 struct filter_input *p
415 = new_input(tinfo, DOM_INPUT_XMLREADER);
416 struct _xmlAttr *attr;
417 const char *level_str = 0;
419 p->u.xmlreader.split_level = 0;
420 p->u.xmlreader.reader = 0;
422 for (attr = ptr->properties; attr; attr = attr->next)
424 if (attr_content(attr, "level", &level_str))
428 dom_log(YLOG_WARN, tinfo, ptr,
429 "bad attribute @%s, expected @level",
434 p->u.xmlreader.split_level = atoi(level_str);
438 parse_convert(tinfo, ptr, &p->convert);
443 dom_log(YLOG_WARN, tinfo, ptr,
444 "bad element <%s>, expected <marc>|<xmlreader>",
452 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
454 char tmp_full_name[1024];
458 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
460 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
461 NULL, tmp_full_name))
462 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
464 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
466 yaz_log(YLOG_LOG, "%s dom filter: "
467 "loading config file %s", tinfo->fname, tinfo->full_name);
469 doc = xmlParseFile(tinfo->full_name);
472 yaz_log(YLOG_WARN, "%s: dom filter: "
473 "failed to parse config file %s",
474 tinfo->fname, tinfo->full_name);
477 /* save because we store ptrs to the content */
478 tinfo->doc_config = doc;
480 ptr = xmlDocGetRootElement(doc);
481 if (!ptr || ptr->type != XML_ELEMENT_NODE
482 || XML_STRCMP(ptr->name, "dom"))
484 dom_log(YLOG_WARN, tinfo, ptr,
485 "bad root element <%s>, expected root element <dom>",
491 FOR_EACH_ELEMENT(ptr) {
492 if (!XML_STRCMP(ptr->name, "extract"))
495 <extract name="index">
496 <xslt stylesheet="first.xsl"/>
497 <xslt stylesheet="second.xsl"/>
500 struct _xmlAttr *attr;
501 struct filter_extract *f =
502 odr_malloc(tinfo->odr_config, sizeof(*f));
507 for (attr = ptr->properties; attr; attr = attr->next)
509 if (attr_content(attr, "name", &f->name))
513 dom_log(YLOG_WARN, tinfo, ptr,
514 "bad attribute @%s, expected @name",
518 parse_convert(tinfo, ptr->children, &f->convert);
520 else if (!XML_STRCMP(ptr->name, "retrieve"))
524 <xslt stylesheet="some.xsl"/>
525 <xslt stylesheet="some.xsl"/>
528 struct _xmlAttr *attr;
529 struct filter_retrieve **fp = &tinfo->retrieve_list;
530 struct filter_retrieve *f =
531 odr_malloc(tinfo->odr_config, sizeof(*f));
542 for (attr = ptr->properties; attr; attr = attr->next)
544 if (attr_content(attr, "identifier",
547 else if (attr_content(attr, "name", &f->name))
551 dom_log(YLOG_WARN, tinfo, ptr,
552 "bad attribute @%s, expected @identifier|@name",
556 parse_convert(tinfo, ptr->children, &f->convert);
558 else if (!XML_STRCMP(ptr->name, "store"))
562 <xslt stylesheet="some.xsl"/>
563 <xslt stylesheet="some.xsl"/>
566 struct filter_store *f =
567 odr_malloc(tinfo->odr_config, sizeof(*f));
571 parse_convert(tinfo, ptr->children, &f->convert);
573 else if (!XML_STRCMP(ptr->name, "input"))
577 <xmlreader level="1"/>
579 <input syntax="usmarc">
580 <marc inputcharset="marc-8"/>
583 struct _xmlAttr *attr;
584 const char *syntax = 0;
585 const char *name = 0;
586 for (attr = ptr->properties; attr; attr = attr->next)
588 if (attr_content(attr, "syntax", &syntax))
590 else if (attr_content(attr, "name", &name))
594 dom_log(YLOG_WARN, tinfo, ptr,
595 "bad attribute @%s, expected @syntax|@name",
599 parse_input(tinfo, ptr->children, syntax, name);
603 dom_log(YLOG_WARN, tinfo, ptr,
605 "expected <extract>|<input>|<retrieve>|<store>",
613 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
616 struct filter_retrieve *f = tinfo->retrieve_list;
618 /* return first schema if no est is provided */
621 for (; f; f = f->next)
623 /* find requested schema */
626 if (f->identifier && !strcmp(f->identifier, est))
628 if (f->name && !strcmp(f->name, est))
635 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
637 struct filter_info *tinfo = clientData;
640 yaz_log(YLOG_WARN, "dom filter: need config file");
644 if (tinfo->fname && !strcmp(args, tinfo->fname))
647 tinfo->profile_path = res_get(res, "profilePath");
650 return parse_dom(tinfo, args);
653 static void filter_destroy(void *clientData)
655 struct filter_info *tinfo = clientData;
657 odr_destroy(tinfo->odr_config);
658 odr_destroy(tinfo->odr_record);
662 static int ioread_ex(void *context, char *buffer, int len)
664 struct recExtractCtrl *p = context;
665 return p->stream->readf(p->stream, buffer, len);
668 static int ioclose_ex(void *context)
674 /* DOM filter style indexing */
675 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
676 xmlChar **dst_content)
678 if (0 == XML_STRCMP(attr->name, name) && attr->children
679 && attr->children->type == XML_TEXT_NODE)
681 *dst_content = (attr->children->content);
688 /* DOM filter style indexing */
689 static void index_value_of(struct filter_info *tinfo,
690 struct recExtractCtrl *extctr,
695 xmlChar *text = xmlNodeGetContent(node);
696 size_t text_len = strlen((const char *)text);
699 /* if there is no text, we do not need to proceed */
702 xmlChar *look = index_p;
709 /* assingning text to be indexed */
710 recword->term_buf = (const char *)text;
711 recword->term_len = text_len;
713 /* parsing all index name/type pairs */
714 /* may not start with ' ' or ':' */
715 while (*look && ' ' != *look && ':' != *look)
717 /* setting name and type to zero */
721 /* parsing one index name */
723 while (*look && ':' != *look && ' ' != *look)
728 strncpy((char *)index, (const char *)bval, eval - bval);
729 index[eval - bval] = '\0';
732 /* parsing one index type, if existing */
738 while (*look && ' ' != *look)
743 strncpy((char *)type, (const char *)bval, eval - bval);
744 type[eval - bval] = '\0';
747 /* actually indexing the text given */
748 dom_log(YLOG_DEBUG, tinfo, 0,
749 "INDEX '%s:%s' '%s'",
752 recword->index_name = (const char *)index;
754 recword->index_type = *type;
755 (extctr->tokenAdd)(recword);
757 /* eat whitespaces */
758 if (*look && ' ' == *look && *(look+1))
769 /* DOM filter style indexing */
770 static void set_record_info(struct filter_info *tinfo,
771 struct recExtractCtrl *extctr,
776 dom_log(YLOG_DEBUG, tinfo, 0,
777 "RECORD id=%s rank=%s type=%s",
778 id_p, rank_p, type_p);
781 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
784 extctr->staticrank = atozint((const char *)rank_p);
786 /* if (!strcmp("update", type_str)) */
787 /* index_node(tinfo, ctrl, ptr, recword); */
788 /* else if (!strcmp("delete", type_str)) */
789 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
791 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
797 /* DOM filter style indexing */
798 static void process_xml_element_zebra_node(struct filter_info *tinfo,
799 struct recExtractCtrl *extctr,
803 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
804 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
806 if (0 == XML_STRCMP(node->name, "index"))
808 xmlChar *index_p = 0;
810 struct _xmlAttr *attr;
811 for (attr = node->properties; attr; attr = attr->next)
813 if (attr_content_xml(attr, "name", &index_p))
815 index_value_of(tinfo, extctr, recword,node, index_p);
819 dom_log(YLOG_WARN, tinfo, node,
820 "bad attribute @%s, expected @name",
825 else if (0 == XML_STRCMP(node->name, "record"))
831 struct _xmlAttr *attr;
832 for (attr = node->properties; attr; attr = attr->next)
834 if (attr_content_xml(attr, "id", &id_p))
836 else if (attr_content_xml(attr, "rank", &rank_p))
838 else if (attr_content_xml(attr, "type", &type_p))
842 dom_log(YLOG_WARN, tinfo, node,
843 "bad attribute @%s, expected @id|@rank|@type",
847 if (type_p && 0 != strcmp("update", (const char *)type_p))
849 dom_log(YLOG_WARN, tinfo, node,
850 "attribute @%s, only implemented '@type='update'",
854 set_record_info(tinfo, extctr, id_p, rank_p, type_p);
858 dom_log(YLOG_WARN, tinfo, node,
860 " expected <record>|<index> in namespace '%s'",
861 node->name, zebra_dom_ns);
867 /* DOM filter style indexing */
868 static void process_xml_pi_node(struct filter_info *tinfo,
869 struct recExtractCtrl *extctr,
873 /* if right PI name, continue parsing PI */
874 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
876 xmlChar *pi_p = node->content;
877 xmlChar *look = pi_p;
882 /* parsing PI record instructions */
883 if (0 == strncmp((const char *)look, "record", 6))
896 while (*look && ' ' == *look && *(look+1))
899 /* parse possible id */
900 if (*look && 0 == strncmp((const char *)look, "id=", 3))
904 while (*look && ' ' != *look)
907 strncpy((char *)id, (const char *)bval, eval - bval);
908 id[eval - bval] = '\0';
912 while (*look && ' ' == *look && *(look+1))
915 /* parse possible rank */
916 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
920 while (*look && ' ' != *look)
923 strncpy((char *)rank, (const char *)bval, eval - bval);
924 rank[eval - bval] = '\0';
928 while (*look && ' ' == *look && *(look+1))
931 if (look && '\0' != *look)
933 dom_log(YLOG_WARN, tinfo, node,
934 "content '%s', can not parse '%s'",
938 set_record_info(tinfo, extctr, id, rank, 0);
941 /* parsing index instruction */
942 else if (0 == strncmp((const char *)look, "index", 5))
947 while (*look && ' ' == *look && *(look+1))
950 /* export index instructions to outside */
955 dom_log(YLOG_WARN, tinfo, node,
956 "content '%s', can not parse '%s'",
962 /* DOM filter style indexing */
963 static void process_xml_element_node(struct filter_info *tinfo,
964 struct recExtractCtrl *extctr,
968 /* remember indexing instruction from PI to next element node */
969 xmlChar *index_p = 0;
971 /* check if we are an element node in the special zebra namespace
972 and either set record data or index value-of node content*/
973 process_xml_element_zebra_node(tinfo, extctr, recword, node);
975 /* loop through kid nodes */
976 for (node = node->children; node; node = node->next)
978 /* check and set PI record and index index instructions */
979 if (node->type == XML_PI_NODE)
981 process_xml_pi_node(tinfo, extctr, node, &index_p);
983 else if (node->type == XML_ELEMENT_NODE)
985 /* if there was a PI index instruction before this element */
988 index_value_of(tinfo, extctr, recword, node, index_p);
991 process_xml_element_node(tinfo, extctr, recword,node);
999 /* DOM filter style indexing */
1000 static void extract_dom_doc_node(struct filter_info *tinfo,
1001 struct recExtractCtrl *extctr,
1007 /* only need to do the initialization once, reuse recword for all terms */
1009 (*extctr->init)(extctr, &recword);
1011 if (extctr->flagShowRecords)
1013 xmlDocDumpMemory(doc, &buf_out, &len_out);
1014 fwrite(buf_out, len_out, 1, stdout);
1018 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1024 static int convert_extract_doc(struct filter_info *tinfo,
1025 struct filter_input *input,
1026 struct recExtractCtrl *p,
1032 const char *params[10];
1033 xsltStylesheetPtr last_xsp = 0;
1034 xmlDocPtr store_doc = 0;
1037 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1039 /* input conversion */
1040 perform_convert(tinfo, input->convert, params, &doc, 0);
1044 /* store conversion */
1045 store_doc = xmlCopyDoc(doc, 1);
1046 perform_convert(tinfo, tinfo->store->convert,
1047 params, &store_doc, &last_xsp);
1051 xsltSaveResultToString(&buf_out, &len_out,
1052 store_doc ? store_doc : doc, last_xsp);
1054 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1055 if (p->flagShowRecords)
1056 fwrite(buf_out, len_out, 1, stdout);
1057 (*p->setStoreData)(p, buf_out, len_out);
1061 xmlFreeDoc(store_doc);
1063 /* extract conversion */
1064 perform_convert(tinfo, tinfo->extract->convert, params, &doc, 0);
1066 /* finally, do the indexing */
1069 extract_dom_doc_node(tinfo, p, doc);
1070 /* extract_doc_alvis(tinfo, p, doc); */
1074 return RECCTRL_EXTRACT_OK;
1077 static int extract_xml_split(struct filter_info *tinfo,
1078 struct filter_input *input,
1079 struct recExtractCtrl *p)
1083 if (p->first_record)
1085 if (input->u.xmlreader.reader)
1086 xmlFreeTextReader(input->u.xmlreader.reader);
1087 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1088 p /* I/O handler */,
1094 if (!input->u.xmlreader.reader)
1095 return RECCTRL_EXTRACT_ERROR_GENERIC;
1097 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1100 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1101 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1102 if (type == XML_READER_TYPE_ELEMENT &&
1103 input->u.xmlreader.split_level == depth)
1106 = xmlTextReaderExpand(input->u.xmlreader.reader);
1109 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1110 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1112 xmlDocSetRootElement(doc, ptr2);
1114 return convert_extract_doc(tinfo, input, p, doc);
1118 xmlFreeTextReader(input->u.xmlreader.reader);
1119 input->u.xmlreader.reader = 0;
1120 return RECCTRL_EXTRACT_ERROR_GENERIC;
1123 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1125 xmlFreeTextReader(input->u.xmlreader.reader);
1126 input->u.xmlreader.reader = 0;
1127 return RECCTRL_EXTRACT_EOF;
1130 static int extract_xml_full(struct filter_info *tinfo,
1131 struct filter_input *input,
1132 struct recExtractCtrl *p)
1134 if (p->first_record) /* only one record per stream */
1136 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1137 p /* I/O handler */,
1140 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1143 return RECCTRL_EXTRACT_ERROR_GENERIC;
1145 return convert_extract_doc(tinfo, input, p, doc);
1148 return RECCTRL_EXTRACT_EOF;
1151 static int extract_iso2709(struct filter_info *tinfo,
1152 struct filter_input *input,
1153 struct recExtractCtrl *p)
1159 if (p->stream->readf(p->stream, buf, 5) != 5)
1160 return RECCTRL_EXTRACT_EOF;
1161 while (*buf < '0' || *buf > '9')
1165 dom_log(YLOG_WARN, tinfo, 0,
1166 "MARC: Skipping bad byte %d (0x%02X)",
1167 *buf & 0xff, *buf & 0xff);
1168 for (i = 0; i<4; i++)
1171 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1172 return RECCTRL_EXTRACT_EOF;
1174 record_length = atoi_n (buf, 5);
1175 if (record_length < 25)
1177 dom_log(YLOG_WARN, tinfo, 0,
1178 "MARC record length < 25, is %d", record_length);
1179 return RECCTRL_EXTRACT_ERROR_GENERIC;
1181 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1182 if (read_bytes < record_length-5)
1184 dom_log(YLOG_WARN, tinfo, 0,
1185 "couldn't read whole MARC record");
1186 return RECCTRL_EXTRACT_ERROR_GENERIC;
1188 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1189 if (r < record_length)
1191 dom_log (YLOG_WARN, tinfo, 0,
1192 "parsing of MARC record failed r=%d length=%d",
1194 return RECCTRL_EXTRACT_ERROR_GENERIC;
1200 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1201 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1202 xmlDocSetRootElement(rdoc, root_ptr);
1203 return convert_extract_doc(tinfo, input, p, rdoc);
1205 return RECCTRL_EXTRACT_OK;
1208 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1210 struct filter_info *tinfo = clientData;
1211 struct filter_input *input = tinfo->input_list;
1214 return RECCTRL_EXTRACT_ERROR_GENERIC;
1216 odr_reset(tinfo->odr_record);
1219 case DOM_INPUT_XMLREADER:
1220 if (input->u.xmlreader.split_level == 0)
1221 return extract_xml_full(tinfo, input, p);
1223 return extract_xml_split(tinfo, input, p);
1225 case DOM_INPUT_MARC:
1226 return extract_iso2709(tinfo, input, p);
1228 return RECCTRL_EXTRACT_ERROR_GENERIC;
1231 static int ioread_ret(void *context, char *buffer, int len)
1233 struct recRetrieveCtrl *p = context;
1234 return p->stream->readf(p->stream, buffer, len);
1237 static int ioclose_ret(void *context)
1242 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1244 /* const char *esn = zebra_dom_ns; */
1245 const char *esn = 0;
1246 const char *params[32];
1247 struct filter_info *tinfo = clientData;
1249 struct filter_retrieve *retrieve;
1250 xsltStylesheetPtr last_xsp = 0;
1254 if (p->comp->which == Z_RecordComp_simple
1255 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1257 esn = p->comp->u.simple->u.generic;
1259 else if (p->comp->which == Z_RecordComp_complex
1260 && p->comp->u.complex->generic->elementSpec
1261 && p->comp->u.complex->generic->elementSpec->which ==
1262 Z_ElementSpec_elementSetName)
1264 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1267 retrieve = lookup_retrieve(tinfo, esn);
1271 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1276 set_param_int(params, "id", p->localno, p->odr);
1278 set_param_str(params, "filename", p->fname, p->odr);
1279 if (p->staticrank >= 0)
1280 set_param_int(params, "rank", p->staticrank, p->odr);
1283 set_param_str(params, "schema", esn, p->odr);
1286 set_param_str(params, "schema", retrieve->name, p->odr);
1287 else if (retrieve->identifier)
1288 set_param_str(params, "schema", retrieve->identifier, p->odr);
1290 set_param_str(params, "schema", "", p->odr);
1293 set_param_int(params, "score", p->score, p->odr);
1294 set_param_int(params, "size", p->recordSize, p->odr);
1296 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1299 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1302 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1306 /* retrieve conversion */
1307 perform_convert(tinfo, retrieve->convert, params, &doc, &last_xsp);
1310 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1312 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1318 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1320 xmlDocDumpMemory(doc, &buf_out, &len_out);
1322 p->output_format = VAL_TEXT_XML;
1323 p->rec_len = len_out;
1324 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1325 memcpy(p->rec_buf, buf_out, p->rec_len);
1328 else if (p->output_format == VAL_SUTRS)
1334 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1336 xmlDocDumpMemory(doc, &buf_out, &len_out);
1338 p->output_format = VAL_SUTRS;
1339 p->rec_len = len_out;
1340 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1341 memcpy(p->rec_buf, buf_out, p->rec_len);
1347 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1353 static struct recType filter_type = {
1364 #ifdef IDZEBRA_STATIC_DOM
1377 * indent-tabs-mode: nil
1379 * vim: shiftwidth=4 tabstop=8 expandtab