1 /* $Id: mod_dom.c,v 1.25 2007-03-01 10:35:46 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;
111 int record_info_invoked;
116 #define XML_STRCMP(a,b) strcmp((char*)a, b)
117 #define XML_STRLEN(a) strlen((char*)a)
120 #define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
122 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
123 const char *fmt, ...)
125 __attribute__ ((format (printf, 4, 5)))
129 static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr,
130 const char *fmt, ...)
136 yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap);
139 yaz_log(level, "%s:%ld: %s", tinfo->fname ? tinfo->fname : "none",
140 xmlGetLineNo(ptr), buf);
144 yaz_log(level, "%s: %s", tinfo->fname ? tinfo->fname : "none", buf);
150 static void set_param_str(const char **params, const char *name,
151 const char *value, ODR odr)
153 char *quoted = odr_malloc(odr, 3 + strlen(value));
154 sprintf(quoted, "'%s'", value);
162 static void set_param_int(const char **params, const char *name,
165 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
168 sprintf(quoted, "'" ZINT_FORMAT "'", value);
174 static void *filter_init(Res res, RecType recType)
176 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
178 tinfo->full_name = 0;
179 tinfo->profile_path = 0;
180 tinfo->odr_record = odr_createmem(ODR_ENCODE);
181 tinfo->odr_config = odr_createmem(ODR_ENCODE);
183 tinfo->retrieve_list = 0;
184 tinfo->input_list = 0;
186 tinfo->doc_config = 0;
187 tinfo->record_info_invoked = 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 recExtractCtrl *extctr,
337 struct convert_s *convert,
340 xsltStylesheetPtr *last_xsp)
342 for (; convert; convert = convert->next)
344 xmlChar *buf_out = 0;
346 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
349 *last_xsp = convert->stylesheet_xsp;
353 /* now saving into buffer and re-reading into DOM to avoid annoing
354 XSLT problem with thrown-out indentation text nodes */
356 xsltSaveResultToString(&buf_out, &len_out, res_doc,
357 convert->stylesheet_xsp);
362 *doc = xmlParseDoc(buf_out);
364 /* writing debug info out */
365 if (extctr->flagShowRecords)
366 yaz_log(YLOG_LOG, "%s: XSLT %s \n %s",
367 tinfo->fname ? tinfo->fname : "(none)",
376 static struct filter_input *new_input(struct filter_info *tinfo, int type)
378 struct filter_input *p;
379 struct filter_input **np = &tinfo->input_list;
380 for (;*np; np = &(*np)->next)
382 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
391 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
392 const char *syntax, const char *name)
394 FOR_EACH_ELEMENT(ptr) {
395 if (!XML_STRCMP(ptr->name, "marc"))
397 yaz_iconv_t iconv = 0;
398 const char *input_charset = "marc-8";
399 struct _xmlAttr *attr;
401 for (attr = ptr->properties; attr; attr = attr->next)
403 if (attr_content(attr, "inputcharset", &input_charset))
407 dom_log(YLOG_WARN, tinfo, ptr,
408 "bad attribute @%s, expected @inputcharset",
412 iconv = yaz_iconv_open("utf-8", input_charset);
415 dom_log(YLOG_WARN, tinfo, ptr,
416 "unsupported @charset '%s'", input_charset);
421 struct filter_input *p
422 = new_input(tinfo, DOM_INPUT_MARC);
423 p->u.marc.handle = yaz_marc_create();
424 p->u.marc.iconv = iconv;
426 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
430 parse_convert(tinfo, ptr, &p->convert);
435 else if (!XML_STRCMP(ptr->name, "xmlreader"))
437 struct filter_input *p
438 = new_input(tinfo, DOM_INPUT_XMLREADER);
439 struct _xmlAttr *attr;
440 const char *level_str = 0;
442 p->u.xmlreader.split_level = 0;
443 p->u.xmlreader.reader = 0;
445 for (attr = ptr->properties; attr; attr = attr->next)
447 if (attr_content(attr, "level", &level_str))
451 dom_log(YLOG_WARN, tinfo, ptr,
452 "bad attribute @%s, expected @level",
457 p->u.xmlreader.split_level = atoi(level_str);
461 parse_convert(tinfo, ptr, &p->convert);
466 dom_log(YLOG_WARN, tinfo, ptr,
467 "bad element <%s>, expected <marc>|<xmlreader>",
475 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
477 char tmp_full_name[1024];
481 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
483 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
484 NULL, tmp_full_name))
485 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
487 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
489 yaz_log(YLOG_LOG, "%s dom filter: "
490 "loading config file %s", tinfo->fname, tinfo->full_name);
492 doc = xmlParseFile(tinfo->full_name);
495 yaz_log(YLOG_WARN, "%s: dom filter: "
496 "failed to parse config file %s",
497 tinfo->fname, tinfo->full_name);
500 /* save because we store ptrs to the content */
501 tinfo->doc_config = doc;
503 ptr = xmlDocGetRootElement(doc);
504 if (!ptr || ptr->type != XML_ELEMENT_NODE
505 || XML_STRCMP(ptr->name, "dom"))
507 dom_log(YLOG_WARN, tinfo, ptr,
508 "bad root element <%s>, expected root element <dom>",
514 FOR_EACH_ELEMENT(ptr) {
515 if (!XML_STRCMP(ptr->name, "extract"))
518 <extract name="index">
519 <xslt stylesheet="first.xsl"/>
520 <xslt stylesheet="second.xsl"/>
523 struct _xmlAttr *attr;
524 struct filter_extract *f =
525 odr_malloc(tinfo->odr_config, sizeof(*f));
530 for (attr = ptr->properties; attr; attr = attr->next)
532 if (attr_content(attr, "name", &f->name))
536 dom_log(YLOG_WARN, tinfo, ptr,
537 "bad attribute @%s, expected @name",
541 parse_convert(tinfo, ptr->children, &f->convert);
543 else if (!XML_STRCMP(ptr->name, "retrieve"))
547 <xslt stylesheet="some.xsl"/>
548 <xslt stylesheet="some.xsl"/>
551 struct _xmlAttr *attr;
552 struct filter_retrieve **fp = &tinfo->retrieve_list;
553 struct filter_retrieve *f =
554 odr_malloc(tinfo->odr_config, sizeof(*f));
565 for (attr = ptr->properties; attr; attr = attr->next)
567 if (attr_content(attr, "identifier",
570 else if (attr_content(attr, "name", &f->name))
574 dom_log(YLOG_WARN, tinfo, ptr,
575 "bad attribute @%s, expected @identifier|@name",
579 parse_convert(tinfo, ptr->children, &f->convert);
581 else if (!XML_STRCMP(ptr->name, "store"))
585 <xslt stylesheet="some.xsl"/>
586 <xslt stylesheet="some.xsl"/>
589 struct filter_store *f =
590 odr_malloc(tinfo->odr_config, sizeof(*f));
594 parse_convert(tinfo, ptr->children, &f->convert);
596 else if (!XML_STRCMP(ptr->name, "input"))
600 <xmlreader level="1"/>
602 <input syntax="usmarc">
603 <marc inputcharset="marc-8"/>
606 struct _xmlAttr *attr;
607 const char *syntax = 0;
608 const char *name = 0;
609 for (attr = ptr->properties; attr; attr = attr->next)
611 if (attr_content(attr, "syntax", &syntax))
613 else if (attr_content(attr, "name", &name))
617 dom_log(YLOG_WARN, tinfo, ptr,
618 "bad attribute @%s, expected @syntax|@name",
622 parse_input(tinfo, ptr->children, syntax, name);
626 dom_log(YLOG_WARN, tinfo, ptr,
628 "expected <extract>|<input>|<retrieve>|<store>",
636 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
639 struct filter_retrieve *f = tinfo->retrieve_list;
641 /* return first schema if no est is provided */
644 for (; f; f = f->next)
646 /* find requested schema */
649 if (f->identifier && !strcmp(f->identifier, est))
651 if (f->name && !strcmp(f->name, est))
658 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
660 struct filter_info *tinfo = clientData;
663 yaz_log(YLOG_WARN, "dom filter: need config file");
667 if (tinfo->fname && !strcmp(args, tinfo->fname))
670 tinfo->profile_path = res_get(res, "profilePath");
673 return parse_dom(tinfo, args);
676 static void filter_destroy(void *clientData)
678 struct filter_info *tinfo = clientData;
680 odr_destroy(tinfo->odr_config);
681 odr_destroy(tinfo->odr_record);
685 static int ioread_ex(void *context, char *buffer, int len)
687 struct recExtractCtrl *p = context;
688 return p->stream->readf(p->stream, buffer, len);
691 static int ioclose_ex(void *context)
697 /* DOM filter style indexing */
698 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
699 xmlChar **dst_content)
701 if (0 == XML_STRCMP(attr->name, name) && attr->children
702 && attr->children->type == XML_TEXT_NODE)
704 *dst_content = (attr->children->content);
711 /* DOM filter style indexing */
712 static void index_value_of(struct filter_info *tinfo,
713 struct recExtractCtrl *extctr,
718 if (tinfo->record_info_invoked == 1)
720 xmlChar *text = xmlNodeGetContent(node);
721 size_t text_len = strlen((const char *)text);
723 yaz_log(YLOG_LOG, "Indexing :%.*s:", text_len, text);
725 /* if there is no text, we do not need to proceed */
728 xmlChar *look = index_p;
735 /* assingning text to be indexed */
736 recword->term_buf = (const char *)text;
737 recword->term_len = text_len;
739 /* parsing all index name/type pairs */
740 /* may not start with ' ' or ':' */
741 while (*look && ' ' != *look && ':' != *look)
743 /* setting name and type to zero */
747 /* parsing one index name */
749 while (*look && ':' != *look && ' ' != *look)
754 strncpy((char *)index, (const char *)bval, eval - bval);
755 index[eval - bval] = '\0';
758 /* parsing one index type, if existing */
764 while (*look && ' ' != *look)
769 strncpy((char *)type, (const char *)bval, eval - bval);
770 type[eval - bval] = '\0';
773 /* actually indexing the text given */
774 dom_log(YLOG_DEBUG, tinfo, 0,
775 "INDEX '%s:%s' '%s'",
776 index ? (const char *) index : "null",
777 type ? (const char *) type : "null",
778 text ? (const char *) text : "null");
780 recword->index_name = (const char *)index;
782 recword->index_type = *type;
784 /* writing debug out */
785 if (extctr->flagShowRecords)
786 dom_log(YLOG_LOG, tinfo, 0,
787 "INDEX '%s:%s' '%s'",
788 index ? (const char *) index : "null",
789 type ? (const char *) type : "null",
790 text ? (const char *) text : "null");
792 /* actually indexing the text given */
793 recword->index_name = (const char *)index;
795 recword->index_type = *type;
796 (extctr->tokenAdd)(recword);
798 /* eat whitespaces */
799 if (*look && ' ' == *look && *(look+1))
810 /* DOM filter style indexing */
811 static void set_record_info(struct filter_info *tinfo,
812 struct recExtractCtrl *extctr,
819 /* writing debug info out */
820 if (extctr->flagShowRecords)
821 dom_log(YLOG_LOG, tinfo, 0,
822 "RECORD id=%s rank=%s type=%s",
823 id_p ? (const char *) id_p : "(null)",
824 rank_p ? (const char *) rank_p : "(null)",
825 type_p ? (const char *) type_p : "(null)");
829 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
832 extctr->staticrank = atozint((const char *)rank_p);
834 /* if (!strcmp("update", type_str)) */
835 /* index_node(tinfo, ctrl, ptr, recword); */
836 /* else if (!strcmp("delete", type_str)) */
837 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter delete: to be implemented"); */
839 /* dom_log(YLOG_WARN, tinfo, ptr, "dom filter: unknown record type '%s'", */
841 if (tinfo->record_info_invoked == 1)
843 /* warn about multiple only once */
844 dom_log(YLOG_WARN, tinfo, node, "multiple record elements");
846 tinfo->record_info_invoked++;
851 /* DOM filter style indexing */
852 static void process_xml_element_zebra_node(struct filter_info *tinfo,
853 struct recExtractCtrl *extctr,
857 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
858 && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns))
860 if (0 == XML_STRCMP(node->name, "index"))
862 xmlChar *index_p = 0;
864 struct _xmlAttr *attr;
865 for (attr = node->properties; attr; attr = attr->next)
867 if (attr_content_xml(attr, "name", &index_p))
869 index_value_of(tinfo, extctr, recword,node, index_p);
873 dom_log(YLOG_WARN, tinfo, node,
874 "bad attribute @%s, expected @name",
879 else if (0 == XML_STRCMP(node->name, "record"))
885 struct _xmlAttr *attr;
886 for (attr = node->properties; attr; attr = attr->next)
888 if (attr_content_xml(attr, "id", &id_p))
890 else if (attr_content_xml(attr, "rank", &rank_p))
892 else if (attr_content_xml(attr, "type", &type_p))
896 dom_log(YLOG_WARN, tinfo, node,
897 "bad attribute @%s, expected @id|@rank|@type",
901 if (type_p && 0 != strcmp("update", (const char *)type_p))
903 dom_log(YLOG_WARN, tinfo, node,
904 "attribute @%s, only implemented '@type='update'",
908 set_record_info(tinfo, extctr, node, id_p, rank_p, type_p);
912 dom_log(YLOG_WARN, tinfo, node,
914 " expected <record>|<index> in namespace '%s'",
915 node->name, zebra_dom_ns);
921 /* DOM filter style indexing */
922 static void process_xml_pi_node(struct filter_info *tinfo,
923 struct recExtractCtrl *extctr,
927 /* if right PI name, continue parsing PI */
928 if (0 == strcmp(zebra_pi_name, (const char *)node->name))
930 xmlChar *pi_p = node->content;
931 xmlChar *look = pi_p;
936 /* parsing PI record instructions */
937 if (0 == strncmp((const char *)look, "record", 6))
950 while (*look && ' ' == *look && *(look+1))
953 /* parse possible id */
954 if (*look && 0 == strncmp((const char *)look, "id=", 3))
958 while (*look && ' ' != *look)
961 strncpy((char *)id, (const char *)bval, eval - bval);
962 id[eval - bval] = '\0';
966 while (*look && ' ' == *look && *(look+1))
969 /* parse possible rank */
970 if (*look && 0 == strncmp((const char *)look, "rank=", 5))
974 while (*look && ' ' != *look)
977 strncpy((char *)rank, (const char *)bval, eval - bval);
978 rank[eval - bval] = '\0';
982 while (*look && ' ' == *look && *(look+1))
985 if (look && '\0' != *look)
987 dom_log(YLOG_WARN, tinfo, node,
988 "content '%s', can not parse '%s'",
992 set_record_info(tinfo, extctr, node, id, rank, 0);
995 /* parsing index instruction */
996 else if (0 == strncmp((const char *)look, "index", 5))
1000 /* eat whitespace */
1001 while (*look && ' ' == *look && *(look+1))
1004 /* export index instructions to outside */
1009 dom_log(YLOG_WARN, tinfo, node,
1010 "content '%s', can not parse '%s'",
1016 /* DOM filter style indexing */
1017 static void process_xml_element_node(struct filter_info *tinfo,
1018 struct recExtractCtrl *extctr,
1022 /* remember indexing instruction from PI to next element node */
1023 xmlChar *index_p = 0;
1025 /* check if we are an element node in the special zebra namespace
1026 and either set record data or index value-of node content*/
1027 process_xml_element_zebra_node(tinfo, extctr, recword, node);
1029 /* loop through kid nodes */
1030 for (node = node->children; node; node = node->next)
1032 /* check and set PI record and index index instructions */
1033 if (node->type == XML_PI_NODE)
1035 process_xml_pi_node(tinfo, extctr, node, &index_p);
1037 else if (node->type == XML_ELEMENT_NODE)
1039 /* if there was a PI index instruction before this element */
1042 index_value_of(tinfo, extctr, recword, node, index_p);
1045 process_xml_element_node(tinfo, extctr, recword,node);
1053 /* DOM filter style indexing */
1054 static void extract_dom_doc_node(struct filter_info *tinfo,
1055 struct recExtractCtrl *extctr,
1058 /* only need to do the initialization once, reuse recword for all terms */
1060 (*extctr->init)(extctr, &recword);
1063 if (extctr->flagShowRecords)
1065 xmlDocDumpMemory(doc, &buf_out, &len_out);
1066 fwrite(buf_out, len_out, 1, stdout);
1070 tinfo->record_info_invoked = 0;
1071 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
1077 static int convert_extract_doc(struct filter_info *tinfo,
1078 struct filter_input *input,
1079 struct recExtractCtrl *p,
1085 const char *params[10];
1086 xsltStylesheetPtr last_xsp = 0;
1087 xmlDocPtr store_doc = 0;
1090 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1092 /* input conversion */
1093 perform_convert(tinfo, p, input->convert, params, &doc, 0);
1097 /* store conversion */
1098 store_doc = xmlCopyDoc(doc, 1);
1099 perform_convert(tinfo, p, tinfo->store->convert,
1100 params, &store_doc, &last_xsp);
1104 xsltSaveResultToString(&buf_out, &len_out,
1105 store_doc ? store_doc : doc, last_xsp);
1107 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1109 /* if (p->flagShowRecords)
1110 fwrite(buf_out, len_out, 1, stdout); */
1112 (*p->setStoreData)(p, buf_out, len_out);
1116 xmlFreeDoc(store_doc);
1118 /* extract conversion */
1119 perform_convert(tinfo, p, tinfo->extract->convert, params, &doc, 0);
1121 /* finally, do the indexing */
1123 extract_dom_doc_node(tinfo, p, doc);
1128 if (tinfo->record_info_invoked == 0)
1129 return RECCTRL_EXTRACT_SKIP;
1130 return RECCTRL_EXTRACT_OK;
1133 static int extract_xml_split(struct filter_info *tinfo,
1134 struct filter_input *input,
1135 struct recExtractCtrl *p)
1139 if (p->first_record)
1141 if (input->u.xmlreader.reader)
1142 xmlFreeTextReader(input->u.xmlreader.reader);
1143 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1144 p /* I/O handler */,
1150 if (!input->u.xmlreader.reader)
1151 return RECCTRL_EXTRACT_ERROR_GENERIC;
1153 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1156 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1157 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1158 if (type == XML_READER_TYPE_ELEMENT &&
1159 input->u.xmlreader.split_level == depth)
1162 = xmlTextReaderExpand(input->u.xmlreader.reader);
1165 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1166 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1168 xmlDocSetRootElement(doc, ptr2);
1170 /* writing debug info out */
1171 if (p->flagShowRecords){
1172 xmlChar *buf_out = 0;
1174 xmlDocDumpMemory(doc, &buf_out, &len_out);
1175 yaz_log(YLOG_LOG, "%s: XMLREADER depth: %i\n%s",
1176 tinfo->fname ? tinfo->fname : "(none)",
1182 return convert_extract_doc(tinfo, input, p, doc);
1186 xmlFreeTextReader(input->u.xmlreader.reader);
1187 input->u.xmlreader.reader = 0;
1188 return RECCTRL_EXTRACT_ERROR_GENERIC;
1191 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1193 xmlFreeTextReader(input->u.xmlreader.reader);
1194 input->u.xmlreader.reader = 0;
1195 return RECCTRL_EXTRACT_EOF;
1198 static int extract_xml_full(struct filter_info *tinfo,
1199 struct filter_input *input,
1200 struct recExtractCtrl *p)
1202 if (p->first_record) /* only one record per stream */
1204 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1205 p /* I/O handler */,
1208 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1211 return RECCTRL_EXTRACT_ERROR_GENERIC;
1213 return convert_extract_doc(tinfo, input, p, doc);
1216 return RECCTRL_EXTRACT_EOF;
1219 static int extract_iso2709(struct filter_info *tinfo,
1220 struct filter_input *input,
1221 struct recExtractCtrl *p)
1227 if (p->stream->readf(p->stream, buf, 5) != 5)
1228 return RECCTRL_EXTRACT_EOF;
1229 while (*buf < '0' || *buf > '9')
1233 dom_log(YLOG_WARN, tinfo, 0,
1234 "MARC: Skipping bad byte %d (0x%02X)",
1235 *buf & 0xff, *buf & 0xff);
1236 for (i = 0; i<4; i++)
1239 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1240 return RECCTRL_EXTRACT_EOF;
1242 record_length = atoi_n (buf, 5);
1243 if (record_length < 25)
1245 dom_log(YLOG_WARN, tinfo, 0,
1246 "MARC record length < 25, is %d", record_length);
1247 return RECCTRL_EXTRACT_ERROR_GENERIC;
1249 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1250 if (read_bytes < record_length-5)
1252 dom_log(YLOG_WARN, tinfo, 0,
1253 "couldn't read whole MARC record");
1254 return RECCTRL_EXTRACT_ERROR_GENERIC;
1256 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1257 if (r < record_length)
1259 dom_log (YLOG_WARN, tinfo, 0,
1260 "parsing of MARC record failed r=%d length=%d",
1262 return RECCTRL_EXTRACT_ERROR_GENERIC;
1268 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1269 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1270 xmlDocSetRootElement(rdoc, root_ptr);
1271 return convert_extract_doc(tinfo, input, p, rdoc);
1273 return RECCTRL_EXTRACT_OK;
1276 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1278 struct filter_info *tinfo = clientData;
1279 struct filter_input *input = tinfo->input_list;
1282 return RECCTRL_EXTRACT_ERROR_GENERIC;
1284 odr_reset(tinfo->odr_record);
1287 case DOM_INPUT_XMLREADER:
1288 if (input->u.xmlreader.split_level == 0)
1289 return extract_xml_full(tinfo, input, p);
1291 return extract_xml_split(tinfo, input, p);
1293 case DOM_INPUT_MARC:
1294 return extract_iso2709(tinfo, input, p);
1296 return RECCTRL_EXTRACT_ERROR_GENERIC;
1299 static int ioread_ret(void *context, char *buffer, int len)
1301 struct recRetrieveCtrl *p = context;
1302 return p->stream->readf(p->stream, buffer, len);
1305 static int ioclose_ret(void *context)
1310 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1312 /* const char *esn = zebra_dom_ns; */
1313 const char *esn = 0;
1314 const char *params[32];
1315 struct filter_info *tinfo = clientData;
1317 struct filter_retrieve *retrieve;
1318 xsltStylesheetPtr last_xsp = 0;
1322 if (p->comp->which == Z_RecordComp_simple
1323 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1325 esn = p->comp->u.simple->u.generic;
1327 else if (p->comp->which == Z_RecordComp_complex
1328 && p->comp->u.complex->generic->elementSpec
1329 && p->comp->u.complex->generic->elementSpec->which ==
1330 Z_ElementSpec_elementSetName)
1332 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1335 retrieve = lookup_retrieve(tinfo, esn);
1339 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1344 set_param_int(params, "id", p->localno, p->odr);
1346 set_param_str(params, "filename", p->fname, p->odr);
1347 if (p->staticrank >= 0)
1348 set_param_int(params, "rank", p->staticrank, p->odr);
1351 set_param_str(params, "schema", esn, p->odr);
1354 set_param_str(params, "schema", retrieve->name, p->odr);
1355 else if (retrieve->identifier)
1356 set_param_str(params, "schema", retrieve->identifier, p->odr);
1358 set_param_str(params, "schema", "", p->odr);
1361 set_param_int(params, "score", p->score, p->odr);
1362 set_param_int(params, "size", p->recordSize, p->odr);
1364 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1367 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1370 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1374 /* retrieve conversion */
1375 perform_convert(tinfo, 0, retrieve->convert, params, &doc, &last_xsp);
1378 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1380 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1386 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1388 xmlDocDumpMemory(doc, &buf_out, &len_out);
1390 p->output_format = VAL_TEXT_XML;
1391 p->rec_len = len_out;
1392 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1393 memcpy(p->rec_buf, buf_out, p->rec_len);
1396 else if (p->output_format == VAL_SUTRS)
1402 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1404 xmlDocDumpMemory(doc, &buf_out, &len_out);
1406 p->output_format = VAL_SUTRS;
1407 p->rec_len = len_out;
1408 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1409 memcpy(p->rec_buf, buf_out, p->rec_len);
1415 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1421 static struct recType filter_type = {
1432 #ifdef IDZEBRA_STATIC_DOM
1445 * indent-tabs-mode: nil
1447 * vim: shiftwidth=4 tabstop=8 expandtab