1 /* $Id: mod_dom.c,v 1.15 2007-02-15 15:08:41 marc 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
27 #include <yaz/diagbib1.h>
28 #include <yaz/tpath.h>
30 #include <libxml/xmlversion.h>
31 #include <libxml/parser.h>
32 #include <libxml/tree.h>
33 #include <libxml/xmlIO.h>
34 #include <libxml/xmlreader.h>
35 #include <libxslt/transform.h>
36 #include <libxslt/xsltutils.h>
39 #include <libexslt/exslt.h>
42 #include <idzebra/util.h>
43 #include <idzebra/recctrl.h>
45 /* DOM filter style indexing */
46 #define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
47 static const char *zebra_dom_ns = ZEBRA_DOM_NS;
49 /* DOM filter style indexing */
50 #define ZEBRA_PI_NAME "zebra-2.0"
51 static const char *zebra_pi_name = ZEBRA_PI_NAME;
56 const char *stylesheet;
57 xsltStylesheetPtr stylesheet_xsp;
58 struct convert_s *next;
61 struct filter_extract {
63 struct convert_s *convert;
67 struct convert_s *convert;
70 struct filter_retrieve {
72 const char *identifier;
73 struct convert_s *convert;
74 struct filter_retrieve *next;
77 #define DOM_INPUT_XMLREADER 1
78 #define DOM_INPUT_MARC 2
82 struct convert_s *convert;
86 const char *input_charset;
91 xmlTextReaderPtr reader;
95 struct filter_input *next;
101 const char *profile_path;
104 xmlDocPtr doc_config;
105 struct filter_extract *extract;
106 struct filter_retrieve *retrieve_list;
107 struct filter_input *input_list;
108 struct filter_store *store;
111 #define XML_STRCMP(a,b) strcmp((char*)a, b)
112 #define XML_STRLEN(a) strlen((char*)a)
117 static void set_param_str(const char **params, const char *name,
118 const char *value, ODR odr)
120 char *quoted = odr_malloc(odr, 3 + strlen(value));
121 sprintf(quoted, "'%s'", value);
129 static void set_param_int(const char **params, const char *name,
132 char *quoted = odr_malloc(odr, 30); /* 25 digits enough for 2^64 */
135 sprintf(quoted, "'" ZINT_FORMAT "'", value);
141 static void *filter_init(Res res, RecType recType)
143 struct filter_info *tinfo = (struct filter_info *) xmalloc(sizeof(*tinfo));
145 tinfo->full_name = 0;
146 tinfo->profile_path = 0;
147 tinfo->odr_record = odr_createmem(ODR_ENCODE);
148 tinfo->odr_config = odr_createmem(ODR_ENCODE);
150 tinfo->retrieve_list = 0;
151 tinfo->input_list = 0;
153 tinfo->doc_config = 0;
162 static int attr_content(struct _xmlAttr *attr, const char *name,
163 const char **dst_content)
165 if (!XML_STRCMP(attr->name, name) && attr->children
166 && attr->children->type == XML_TEXT_NODE)
168 *dst_content = (const char *)(attr->children->content);
174 static void destroy_xsp(struct convert_s *c)
178 if (c->stylesheet_xsp)
179 xsltFreeStylesheet(c->stylesheet_xsp);
184 static void destroy_dom(struct filter_info *tinfo)
188 destroy_xsp(tinfo->extract->convert);
193 destroy_xsp(tinfo->store->convert);
196 if (tinfo->input_list)
198 struct filter_input *i_ptr;
199 for (i_ptr = tinfo->input_list; i_ptr; i_ptr = i_ptr->next)
203 case DOM_INPUT_XMLREADER:
204 if (i_ptr->u.xmlreader.reader)
205 xmlFreeTextReader(i_ptr->u.xmlreader.reader);
208 yaz_iconv_close(i_ptr->u.marc.iconv);
209 yaz_marc_destroy(i_ptr->u.marc.handle);
212 destroy_xsp(i_ptr->convert);
214 tinfo->input_list = 0;
216 if (tinfo->retrieve_list)
218 struct filter_retrieve *r_ptr;
219 for (r_ptr = tinfo->retrieve_list; r_ptr; r_ptr = r_ptr->next)
220 destroy_xsp(r_ptr->convert);
221 tinfo->retrieve_list = 0;
224 if (tinfo->doc_config)
226 xmlFreeDoc(tinfo->doc_config);
227 tinfo->doc_config = 0;
229 odr_reset(tinfo->odr_config);
232 static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr,
233 struct convert_s **l)
236 for(; ptr; ptr = ptr->next)
238 if (ptr->type != XML_ELEMENT_NODE)
240 if (!XML_STRCMP(ptr->name, "xslt"))
242 struct _xmlAttr *attr;
244 = odr_malloc(tinfo->odr_config, sizeof(*p));
248 p->stylesheet_xsp = 0;
250 for (attr = ptr->properties; attr; attr = attr->next)
251 if (attr_content(attr, "stylesheet", &p->stylesheet))
254 yaz_log(YLOG_WARN, "%s: dom filter: "
255 "%s bad attribute @%s, "
256 "expected @stylesheet",
258 xmlGetNodePath(ptr), attr->name);
261 char tmp_xslt_full_name[1024];
262 if (!yaz_filepath_resolve(p->stylesheet,
267 yaz_log(YLOG_WARN, "%s: dom filter: "
268 "stylesheet %s not found in "
272 tinfo->profile_path);
277 = xsltParseStylesheetFile((const xmlChar*)
279 if (!p->stylesheet_xsp)
281 yaz_log(YLOG_WARN, "%s: dom filter: "
282 "could not parse xslt "
284 tinfo->fname, tmp_xslt_full_name);
290 yaz_log(YLOG_WARN, "%s: dom filter: "
291 "%s missing attribute 'stylesheet' ",
292 tinfo->fname, xmlGetNodePath(ptr));
303 tinfo->fname, xmlGetNodePath(ptr), ptr->name);
311 static ZEBRA_RES perform_convert(struct filter_info *tinfo,
312 struct convert_s *convert,
315 xsltStylesheetPtr *last_xsp)
317 for (; convert; convert = convert->next)
319 xmlDocPtr res_doc = xsltApplyStylesheet(convert->stylesheet_xsp,
322 *last_xsp = convert->stylesheet_xsp;
329 static struct filter_input *new_input(struct filter_info *tinfo, int type)
331 struct filter_input *p;
332 struct filter_input **np = &tinfo->input_list;
333 for (;*np; np = &(*np)->next)
335 p = *np = odr_malloc(tinfo->odr_config, sizeof(*p));
344 static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr,
348 for (; ptr; ptr = ptr->next)
350 if (ptr->type != XML_ELEMENT_NODE)
352 if (!XML_STRCMP(ptr->name, "marc"))
354 yaz_iconv_t iconv = 0;
355 const char *input_charset = "marc-8";
356 struct _xmlAttr *attr;
358 for (attr = ptr->properties; attr; attr = attr->next)
360 if (attr_content(attr, "charset", &input_charset))
363 yaz_log(YLOG_WARN, "%s: dom filter: "
364 "%s bad attribute @%s,"
365 " expected @charset",
367 xmlGetNodePath(ptr), attr->name);
369 iconv = yaz_iconv_open("utf-8", input_charset);
372 yaz_log(YLOG_WARN, "%s: dom filter: "
373 "%s unsupported @charset '%s'",
374 tinfo->fname, xmlGetNodePath(ptr),
380 struct filter_input *p
381 = new_input(tinfo, DOM_INPUT_MARC);
382 p->u.marc.handle = yaz_marc_create();
383 p->u.marc.iconv = iconv;
385 yaz_marc_iconv(p->u.marc.handle, p->u.marc.iconv);
389 parse_convert(tinfo, ptr, &p->convert);
394 else if (!XML_STRCMP(ptr->name, "xmlreader"))
396 struct filter_input *p
397 = new_input(tinfo, DOM_INPUT_XMLREADER);
398 struct _xmlAttr *attr;
399 const char *level_str = 0;
401 p->u.xmlreader.split_level = 0;
402 p->u.xmlreader.reader = 0;
404 for (attr = ptr->properties; attr; attr = attr->next)
406 if (attr_content(attr, "level", &level_str))
409 yaz_log(YLOG_WARN, "%s: dom filter: "
410 "%s bad attribute @%s,"
412 tinfo->fname, xmlGetNodePath(ptr),
416 p->u.xmlreader.split_level = atoi(level_str);
420 parse_convert(tinfo, ptr, &p->convert);
425 yaz_log(YLOG_WARN, "%s: dom filter: "
426 "%s bad element <%s>,"
427 " expected <marc>|<xmlreader>",
428 tinfo->fname, xmlGetNodePath(ptr), ptr->name);
435 static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
437 char tmp_full_name[1024];
441 tinfo->fname = odr_strdup(tinfo->odr_config, fname);
443 if (yaz_filepath_resolve(tinfo->fname, tinfo->profile_path,
444 NULL, tmp_full_name))
445 tinfo->full_name = odr_strdup(tinfo->odr_config, tmp_full_name);
447 tinfo->full_name = odr_strdup(tinfo->odr_config, tinfo->fname);
449 yaz_log(YLOG_LOG, "%s dom filter: "
450 "loading config file %s", tinfo->fname, tinfo->full_name);
452 doc = xmlParseFile(tinfo->full_name);
455 yaz_log(YLOG_WARN, "%s: dom filter: "
456 "failed to parse config file %s",
457 tinfo->fname, tinfo->full_name);
460 /* save because we store ptrs to the content */
461 tinfo->doc_config = doc;
463 ptr = xmlDocGetRootElement(doc);
464 if (!ptr || ptr->type != XML_ELEMENT_NODE
465 || XML_STRCMP(ptr->name, "dom"))
467 yaz_log(YLOG_WARN, "%s: dom filter: "
468 "%s bad root element <%s>,"
469 " expected root element <dom>",
470 tinfo->fname, xmlGetNodePath(ptr), ptr->name);
474 for (ptr = ptr->children; ptr; ptr = ptr->next)
476 if (ptr->type != XML_ELEMENT_NODE)
478 if (!XML_STRCMP(ptr->name, "extract"))
481 <extract name="index">
482 <xslt stylesheet="first.xsl"/>
483 <xslt stylesheet="second.xsl"/>
486 struct _xmlAttr *attr;
487 struct filter_extract *f =
488 odr_malloc(tinfo->odr_config, sizeof(*f));
493 for (attr = ptr->properties; attr; attr = attr->next)
495 if (attr_content(attr, "name", &f->name))
498 yaz_log(YLOG_WARN, "%s: dom filter: "
499 "%s bad attribute @%s"
502 xmlGetNodePath(ptr),attr->name);
505 parse_convert(tinfo, ptr->children, &f->convert);
507 else if (!XML_STRCMP(ptr->name, "retrieve"))
511 <xslt stylesheet="some.xsl"/>
512 <xslt stylesheet="some.xsl"/>
515 struct _xmlAttr *attr;
516 struct filter_retrieve **fp = &tinfo->retrieve_list;
517 struct filter_retrieve *f =
518 odr_malloc(tinfo->odr_config, sizeof(*f));
529 for (attr = ptr->properties; attr; attr = attr->next)
531 if (attr_content(attr, "identifier",
534 else if (attr_content(attr, "name", &f->name))
537 yaz_log(YLOG_WARN, "%s: dom filter: "
538 "%s bad attribute @%s"
539 " expected @identifier|@name",
541 xmlGetNodePath(ptr),attr->name);
543 parse_convert(tinfo, ptr->children, &f->convert);
545 else if (!XML_STRCMP(ptr->name, "store"))
549 <xslt stylesheet="some.xsl"/>
550 <xslt stylesheet="some.xsl"/>
553 struct filter_store *f =
554 odr_malloc(tinfo->odr_config, sizeof(*f));
558 parse_convert(tinfo, ptr->children, &f->convert);
560 else if (!XML_STRCMP(ptr->name, "input"))
564 <xmlreader level="1"/>
566 <input syntax="usmarc">
567 <marc inputcharset="marc-8"/>
570 struct _xmlAttr *attr;
571 const char *syntax = 0;
572 const char *name = 0;
573 for (attr = ptr->properties; attr; attr = attr->next)
575 if (attr_content(attr, "syntax", &syntax))
577 else if (attr_content(attr, "name", &name))
580 yaz_log(YLOG_WARN, "%s: dom filter: "
581 "%s bad attribute @%s"
582 " expected @syntax|@name",
584 xmlGetNodePath(ptr),attr->name);
586 parse_input(tinfo, ptr->children, syntax, name);
590 yaz_log(YLOG_WARN, "%s: dom filter: "
591 "%s bad element <%s>,"
592 " expected <extract>|<input>|<retrieve>|<store>",
593 tinfo->fname, xmlGetNodePath(ptr), ptr->name);
600 static struct filter_retrieve *lookup_retrieve(struct filter_info *tinfo,
603 struct filter_retrieve *f = tinfo->retrieve_list;
605 /* return first schema if no est is provided */
608 for (; f; f = f->next)
610 /* find requested schema */
613 if (f->identifier && !strcmp(f->identifier, est))
615 if (f->name && !strcmp(f->name, est))
622 static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
624 struct filter_info *tinfo = clientData;
627 yaz_log(YLOG_WARN, "dom filter: need config file");
631 if (tinfo->fname && !strcmp(args, tinfo->fname))
634 tinfo->profile_path = res_get(res, "profilePath");
637 return parse_dom(tinfo, args);
640 static void filter_destroy(void *clientData)
642 struct filter_info *tinfo = clientData;
644 odr_destroy(tinfo->odr_config);
645 odr_destroy(tinfo->odr_record);
649 static int ioread_ex(void *context, char *buffer, int len)
651 struct recExtractCtrl *p = context;
652 return p->stream->readf(p->stream, buffer, len);
655 static int ioclose_ex(void *context)
661 /* DOM filter style indexing */
662 static int attr_content_xml(struct _xmlAttr *attr, const char *name,
663 xmlChar **dst_content)
665 if (0 == XML_STRCMP(attr->name, name) && attr->children
666 && attr->children->type == XML_TEXT_NODE)
668 *dst_content = (attr->children->content);
675 /* DOM filter style indexing */
676 static void index_value_of(struct filter_info *tinfo,
677 struct recExtractCtrl *extctr,
682 xmlChar *text = xmlNodeGetContent(node);
683 size_t text_len = strlen((const char *)text);
686 /* if there is no text, we do not need to proceed */
689 xmlChar *look = index_p;
696 /* assingning text to be indexed */
697 recword->term_buf = (const char *)text;
698 recword->term_len = text_len;
700 /* parsing all index name/type pairs */
701 /* may not start with ' ' or ':' */
702 while (*look && ' ' != *look && ':' != *look){
704 /* setting name and type to zero */
708 /* parsing one index name */
710 while (*look && ':' != *look && ' ' != *look){
714 strncpy((char *)index, (const char *)bval, eval - bval);
715 index[eval - bval] = '\0';
718 /* parsing one index type, if existing */
723 while (*look && ' ' != *look){
727 strncpy((char *)type, (const char *)bval, eval - bval);
728 type[eval - bval] = '\0';
731 /* actually indexing the text given */
732 yaz_log(YLOG_DEBUG, "%s dom filter: "
733 "INDEX '%s:%s' '%s'",
734 tinfo->fname, index, type, text);
736 recword->index_name = (const char *)index;
738 recword->index_type = *type;
739 (extctr->tokenAdd)(recword);
741 /* eat whitespaces */
742 if (*look && ' ' == *look && *(look+1)){
752 /* DOM filter style indexing */
753 static void set_record_info(struct filter_info *tinfo,
754 struct recExtractCtrl *extctr,
759 yaz_log(YLOG_DEBUG, "%s dom filter: "
760 "RECORD id=%s rank=%s type=%s",
761 tinfo->fname, id_p, rank_p, type_p);
764 sscanf((const char *)id_p, "%255s", extctr->match_criteria);
767 extctr->staticrank = atozint((const char *)rank_p);
769 /* if (!strcmp("update", type_str)) */
770 /* index_node(tinfo, ctrl, ptr, recword); */
771 /* else if (!strcmp("delete", type_str)) */
772 /* yaz_log(YLOG_WARN, "dom filter delete: to be implemented"); */
774 /* yaz_log(YLOG_WARN, "dom filter: unknown record type '%s'", */
780 /* DOM filter style indexing */
781 static void process_xml_element_zebra_node(struct filter_info *tinfo,
782 struct recExtractCtrl *extctr,
786 if (node->type == XML_ELEMENT_NODE
787 && node->ns && 0 == XML_STRCMP(node->ns->href, zebra_dom_ns)){
789 if (0 == XML_STRCMP(node->name, "index")){
790 xmlChar *index_p = 0;
792 struct _xmlAttr *attr;
793 for (attr = node->properties; attr; attr = attr->next){
794 if (attr_content_xml(attr, "name", &index_p)){
795 index_value_of(tinfo, extctr, recword,node, index_p);
798 yaz_log(YLOG_WARN,"%s dom filter: "
799 "%s bad attribute @%s, expected @name",
800 tinfo->fname, xmlGetNodePath(node), attr->name);
803 else if (0 == XML_STRCMP(node->name, "record")){
808 struct _xmlAttr *attr;
809 for (attr = node->properties; attr; attr = attr->next){
810 if (attr_content_xml(attr, "id", &id_p))
812 else if (attr_content_xml(attr, "rank", &rank_p))
814 else if (attr_content_xml(attr, "type", &type_p))
817 yaz_log(YLOG_WARN,"%s dom filter: "
818 "%s bad attribute @%s,"
819 " expected @id|@rank|@type",
820 tinfo->fname, xmlGetNodePath(node), attr->name);
822 if (type_p && 0 != strcmp("update", (const char *)type_p))
823 yaz_log(YLOG_WARN,"%s dom filter: "
825 " only implemented '@type='update'",
826 tinfo->fname, xmlGetNodePath(node), attr->name);
830 set_record_info(tinfo, extctr, id_p, rank_p, type_p);
832 yaz_log(YLOG_WARN,"%s dom filter: "
833 "%s bad element <%s>,"
834 " expected <record>|<index> in namespace '%s'",
835 tinfo->fname, xmlGetNodePath(node),
836 node->name, zebra_dom_ns);
843 /* DOM filter style indexing */
844 static void process_xml_pi_node(struct filter_info *tinfo,
845 struct recExtractCtrl *extctr,
850 /* yaz_log(YLOG_DEBUG,"PI %s\n", xmlGetNodePath(node)); */
852 /* if right PI name, continue parsing PI */
853 if (0 == strcmp(zebra_pi_name, (const char *)node->name)){
854 xmlChar *pi_p = node->content;
855 xmlChar *look = pi_p;
860 /* parsing PI record instructions */
861 if (0 == strncmp((const char *)look, "record", 6)){
873 while (*look && ' ' == *look && *(look+1))
876 /* parse possible id */
877 if (*look && 0 == strncmp((const char *)look, "id=", 3)){
880 while (*look && ' ' != *look)
883 strncpy((char *)id, (const char *)bval, eval - bval);
884 id[eval - bval] = '\0';
888 while (*look && ' ' == *look && *(look+1))
891 /* parse possible rank */
892 if (*look && 0 == strncmp((const char *)look, "rank=", 5)){
895 while (*look && ' ' != *look)
898 strncpy((char *)rank, (const char *)bval, eval - bval);
899 rank[eval - bval] = '\0';
903 while (*look && ' ' == *look && *(look+1))
906 if (look && '\0' != *look)
907 yaz_log(YLOG_WARN,"%s dom filter: "
908 "%s content '%s', can not parse '%s'",
909 tinfo->fname, xmlGetNodePath(node), pi_p, look);
911 set_record_info(tinfo, extctr, id, rank, 0);
915 /* parsing index instruction */
916 else if (0 == strncmp((const char *)look, "index", 5)){
920 while (*look && ' ' == *look && *(look+1))
923 /* export index instructions to outside */
927 yaz_log(YLOG_WARN,"%s dom filter: "
928 "%s content '%s', can not parse '%s'",
929 tinfo->fname, xmlGetNodePath(node), pi_p, look);
933 /* DOM filter style indexing */
934 static void process_xml_element_node(struct filter_info *tinfo,
935 struct recExtractCtrl *extctr,
939 /* remember indexing instruction from PI to next element node */
940 xmlChar *index_p = 0;
942 /* check if we are an element node in the special zebra namespace
943 and either set record data or index value-of node content*/
944 process_xml_element_zebra_node(tinfo, extctr, recword, node);
946 /* loop through kid nodes */
947 for (node = node->children; node; node = node->next)
949 /* check and set PI record and index index instructions */
950 if (node->type == XML_PI_NODE){
951 process_xml_pi_node(tinfo, extctr, node, &index_p);
953 else if (node->type == XML_ELEMENT_NODE){
954 /* if there was a PI index instruction before this element */
956 index_value_of(tinfo, extctr, recword, node, index_p);
959 process_xml_element_node(tinfo, extctr, recword,node);
967 /* DOM filter style indexing */
968 static void extract_dom_doc_node(struct filter_info *tinfo,
969 struct recExtractCtrl *extctr,
975 /* only need to do the initialization once, reuse recword for all terms */
977 (*extctr->init)(extctr, &recword);
979 if (extctr->flagShowRecords){
980 xmlDocDumpMemory(doc, &buf_out, &len_out);
981 fwrite(buf_out, len_out, 1, stdout);
985 process_xml_element_node(tinfo, extctr, &recword, (xmlNodePtr)doc);
991 static int convert_extract_doc(struct filter_info *tinfo,
992 struct filter_input *input,
993 struct recExtractCtrl *p,
999 const char *params[10];
1000 xsltStylesheetPtr last_xsp = 0;
1001 xmlDocPtr store_doc = 0;
1004 set_param_str(params, "schema", zebra_dom_ns, tinfo->odr_record);
1006 /* input conversion */
1007 perform_convert(tinfo, input->convert, params, &doc, 0);
1011 /* store conversion */
1012 store_doc = xmlCopyDoc(doc, 1);
1013 perform_convert(tinfo, tinfo->store->convert,
1014 params, &store_doc, &last_xsp);
1018 xsltSaveResultToString(&buf_out, &len_out,
1019 store_doc ? store_doc : doc, last_xsp);
1021 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1022 if (p->flagShowRecords)
1023 fwrite(buf_out, len_out, 1, stdout);
1024 (*p->setStoreData)(p, buf_out, len_out);
1028 xmlFreeDoc(store_doc);
1030 /* extract conversion */
1031 perform_convert(tinfo, tinfo->extract->convert, params, &doc, 0);
1033 /* finally, do the indexing */
1035 extract_dom_doc_node(tinfo, p, doc);
1036 /* extract_doc_alvis(tinfo, p, doc); */
1040 return RECCTRL_EXTRACT_OK;
1043 static int extract_xml_split(struct filter_info *tinfo,
1044 struct filter_input *input,
1045 struct recExtractCtrl *p)
1049 if (p->first_record)
1051 if (input->u.xmlreader.reader)
1052 xmlFreeTextReader(input->u.xmlreader.reader);
1053 input->u.xmlreader.reader = xmlReaderForIO(ioread_ex, ioclose_ex,
1054 p /* I/O handler */,
1060 if (!input->u.xmlreader.reader)
1061 return RECCTRL_EXTRACT_ERROR_GENERIC;
1063 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1066 int type = xmlTextReaderNodeType(input->u.xmlreader.reader);
1067 int depth = xmlTextReaderDepth(input->u.xmlreader.reader);
1068 if (type == XML_READER_TYPE_ELEMENT &&
1069 input->u.xmlreader.split_level == depth)
1072 = xmlTextReaderExpand(input->u.xmlreader.reader);
1075 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1076 xmlDocPtr doc = xmlNewDoc((const xmlChar*) "1.0");
1078 xmlDocSetRootElement(doc, ptr2);
1080 return convert_extract_doc(tinfo, input, p, doc);
1084 xmlFreeTextReader(input->u.xmlreader.reader);
1085 input->u.xmlreader.reader = 0;
1086 return RECCTRL_EXTRACT_ERROR_GENERIC;
1089 ret = xmlTextReaderRead(input->u.xmlreader.reader);
1091 xmlFreeTextReader(input->u.xmlreader.reader);
1092 input->u.xmlreader.reader = 0;
1093 return RECCTRL_EXTRACT_EOF;
1096 static int extract_xml_full(struct filter_info *tinfo,
1097 struct filter_input *input,
1098 struct recExtractCtrl *p)
1100 if (p->first_record) /* only one record per stream */
1102 xmlDocPtr doc = xmlReadIO(ioread_ex, ioclose_ex,
1103 p /* I/O handler */,
1106 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1109 return RECCTRL_EXTRACT_ERROR_GENERIC;
1111 return convert_extract_doc(tinfo, input, p, doc);
1114 return RECCTRL_EXTRACT_EOF;
1117 static int extract_iso2709(struct filter_info *tinfo,
1118 struct filter_input *input,
1119 struct recExtractCtrl *p)
1125 if (p->stream->readf(p->stream, buf, 5) != 5)
1126 return RECCTRL_EXTRACT_EOF;
1127 while (*buf < '0' || *buf > '9')
1131 yaz_log(YLOG_WARN, "%s dom filter: "
1132 "MARC: Skipping bad byte %d (0x%02X)",
1133 tinfo->fname, *buf & 0xff, *buf & 0xff);
1134 for (i = 0; i<4; i++)
1137 if (p->stream->readf(p->stream, buf+4, 1) != 1)
1138 return RECCTRL_EXTRACT_EOF;
1140 record_length = atoi_n (buf, 5);
1141 if (record_length < 25)
1143 yaz_log (YLOG_WARN, "%s dom filter: "
1144 "MARC record length < 25, is %d",
1145 tinfo->fname, record_length);
1146 return RECCTRL_EXTRACT_ERROR_GENERIC;
1148 read_bytes = p->stream->readf(p->stream, buf+5, record_length-5);
1149 if (read_bytes < record_length-5)
1151 yaz_log (YLOG_WARN, "%s dom filter: "
1152 "Couldn't read whole MARC record",
1154 return RECCTRL_EXTRACT_ERROR_GENERIC;
1156 r = yaz_marc_read_iso2709(input->u.marc.handle, buf, record_length);
1157 if (r < record_length)
1159 yaz_log (YLOG_WARN, "%s dom filter: "
1160 "Parsing of MARC record failed r=%d length=%d",
1161 tinfo->fname, r, record_length);
1162 return RECCTRL_EXTRACT_ERROR_GENERIC;
1168 yaz_marc_write_xml(input->u.marc.handle, &root_ptr, 0, 0, 0);
1169 rdoc = xmlNewDoc((const xmlChar*) "1.0");
1170 xmlDocSetRootElement(rdoc, root_ptr);
1171 return convert_extract_doc(tinfo, input, p, rdoc);
1173 return RECCTRL_EXTRACT_OK;
1176 static int filter_extract(void *clientData, struct recExtractCtrl *p)
1178 struct filter_info *tinfo = clientData;
1179 struct filter_input *input = tinfo->input_list;
1182 return RECCTRL_EXTRACT_ERROR_GENERIC;
1184 odr_reset(tinfo->odr_record);
1187 case DOM_INPUT_XMLREADER:
1188 if (input->u.xmlreader.split_level == 0)
1189 return extract_xml_full(tinfo, input, p);
1191 return extract_xml_split(tinfo, input, p);
1193 case DOM_INPUT_MARC:
1194 return extract_iso2709(tinfo, input, p);
1196 return RECCTRL_EXTRACT_ERROR_GENERIC;
1199 static int ioread_ret(void *context, char *buffer, int len)
1201 struct recRetrieveCtrl *p = context;
1202 return p->stream->readf(p->stream, buffer, len);
1205 static int ioclose_ret(void *context)
1210 static int filter_retrieve (void *clientData, struct recRetrieveCtrl *p)
1212 /* const char *esn = zebra_dom_ns; */
1213 const char *esn = 0;
1214 const char *params[32];
1215 struct filter_info *tinfo = clientData;
1217 struct filter_retrieve *retrieve;
1218 xsltStylesheetPtr last_xsp = 0;
1222 if (p->comp->which == Z_RecordComp_simple
1223 && p->comp->u.simple->which == Z_ElementSetNames_generic)
1225 esn = p->comp->u.simple->u.generic;
1227 else if (p->comp->which == Z_RecordComp_complex
1228 && p->comp->u.complex->generic->elementSpec
1229 && p->comp->u.complex->generic->elementSpec->which ==
1230 Z_ElementSpec_elementSetName)
1232 esn = p->comp->u.complex->generic->elementSpec->u.elementSetName;
1235 retrieve = lookup_retrieve(tinfo, esn);
1239 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1244 set_param_int(params, "id", p->localno, p->odr);
1246 set_param_str(params, "filename", p->fname, p->odr);
1247 if (p->staticrank >= 0)
1248 set_param_int(params, "rank", p->staticrank, p->odr);
1251 set_param_str(params, "schema", esn, p->odr);
1254 set_param_str(params, "schema", retrieve->name, p->odr);
1255 else if (retrieve->identifier)
1256 set_param_str(params, "schema", retrieve->identifier, p->odr);
1258 set_param_str(params, "schema", "", p->odr);
1261 set_param_int(params, "score", p->score, p->odr);
1262 set_param_int(params, "size", p->recordSize, p->odr);
1264 doc = xmlReadIO(ioread_ret, ioclose_ret, p /* I/O handler */,
1267 XML_PARSE_XINCLUDE|XML_PARSE_NOENT);
1270 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1274 /* retrieve conversion */
1275 perform_convert(tinfo, retrieve->convert, params, &doc, &last_xsp);
1278 p->diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1280 else if (p->input_format == VAL_NONE || p->input_format == VAL_TEXT_XML)
1286 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1288 xmlDocDumpMemory(doc, &buf_out, &len_out);
1290 p->output_format = VAL_TEXT_XML;
1291 p->rec_len = len_out;
1292 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1293 memcpy(p->rec_buf, buf_out, p->rec_len);
1296 else if (p->output_format == VAL_SUTRS)
1302 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1304 xmlDocDumpMemory(doc, &buf_out, &len_out);
1306 p->output_format = VAL_SUTRS;
1307 p->rec_len = len_out;
1308 p->rec_buf = odr_malloc(p->odr, p->rec_len);
1309 memcpy(p->rec_buf, buf_out, p->rec_len);
1315 p->diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1321 static struct recType filter_type = {
1332 #ifdef IDZEBRA_STATIC_DOM
1345 * indent-tabs-mode: nil
1347 * vim: shiftwidth=4 tabstop=8 expandtab