29#include <yaz/diagbib1.h>
31#include <yaz/snprintf.h>
33#include <libxml/xmlversion.h>
34#include <libxml/parser.h>
35#include <libxml/tree.h>
36#include <libxml/xmlIO.h>
37#include <libxml/xmlreader.h>
38#include <libxslt/transform.h>
39#include <libxslt/xsltutils.h>
42#include <libexslt/exslt.h>
47#include <yaz/oid_db.h>
50#define ZEBRA_DOM_NS "http://indexdata.com/zebra-2.0"
54#define ZEBRA_PI_NAME "zebra-2.0"
96#define DOM_INPUT_XMLREADER 1
97#define DOM_INPUT_MARC 2
133#define XML_STRCMP(a,b) strcmp((char*)a, b)
134#define XML_STRLEN(a) strlen((char*)a)
137#define FOR_EACH_ELEMENT(ptr) for (; ptr; ptr = ptr->next) if (ptr->type == XML_ELEMENT_NODE)
140 const char *fmt, ...)
142 __attribute__ ((format (printf, 4, 5)))
147 const char *fmt, ...)
153 yaz_vsnprintf(buf,
sizeof(buf)-1, fmt, ap);
156 yaz_log(level,
"%s:%ld: %s", tinfo->
fname ? tinfo->
fname :
"none",
157 xmlGetLineNo(ptr), buf);
161 yaz_log(level,
"%s: %s", tinfo->
fname ? tinfo->
fname :
"none", buf);
168 const char *value, NMEM nmem)
170 char *quoted = nmem_malloc(nmem, 3 + strlen(value));
171 yaz_snprintf(quoted, 3 + strlen(value),
"'%s'", value);
180 zint value, NMEM nmem)
182 char *quoted = nmem_malloc(nmem, 30);
185 yaz_snprintf(quoted, 30,
"'" ZINT_FORMAT "'", value);
214 const char **dst_content)
216 if (!
XML_STRCMP(attr->name, name) && attr->children
217 && attr->children->type == XML_TEXT_NODE)
219 *dst_content = (
const char *)(attr->children->content);
293 struct _xmlAttr *attr;
301 for (attr = ptr->properties; attr; attr = attr->next)
307 "bad attribute @%s", attr->name);
311 char tmp_xslt_full_name[1024];
318 "stylesheet %s not found in "
326 = xsltParseStylesheetFile((
const xmlChar*)
331 "could not parse xslt stylesheet %s",
339 "missing attribute 'stylesheet'");
345 else if (!
XML_STRCMP(ptr->name,
"process-meta"))
347 struct _xmlAttr *attr;
353 for (attr = ptr->properties; attr; attr = attr->next)
355 "bad attribute @%s", attr->name);
362 "bad element '%s', expected <xslt>", ptr->name);
373 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href &&
378 const char *element_set_name = 0;
380 struct _xmlAttr *attr;
381 for (attr = node->properties; attr; attr = attr->next)
387 dom_log(YLOG_WARN, tinfo, node,
388 "bad attribute @%s, expected @name", attr->name);
391 if (element_set_name)
393 WRBUF result = wrbuf_alloc();
394 WRBUF addinfo = wrbuf_alloc();
395 const Odr_oid *input_format = yaz_oid_recsyn_xml;
396 const Odr_oid *output_format = 0;
401 input_format, &output_format,
406 xmlParseMemory(wrbuf_buf(result), wrbuf_len(result));
409 xmlNodePtr t = xmlDocGetRootElement(sub_doc);
410 xmlReplaceNode(node, xmlCopyNode(t, 1));
414 wrbuf_destroy(result);
415 wrbuf_destroy(addinfo);
419 for (node = node->children; node; node = node->next)
430 xsltStylesheetPtr *last_xsp)
432 for (; convert; convert = convert->
next)
436 xmlChar *buf_out = 0;
448 xsltSaveResultToString(&buf_out, &len_out, res_doc,
454 *doc = xmlParseMemory((
const char *) buf_out, len_out);
458 yaz_log(YLOG_LOG,
"%s: XSLT %s\n %.*s",
469 process_meta(tinfo, *doc, xmlDocGetRootElement(*doc), retctr);
484 for (;*np; np = &(*np)->
next)
486 p = *np = nmem_malloc(tinfo->
nmem_config,
sizeof(*p));
501 yaz_iconv_t
iconv = 0;
503 struct _xmlAttr *attr;
505 for (attr = ptr->properties; attr; attr = attr->next)
512 "bad attribute @%s, expected @inputcharset",
516 iconv = yaz_iconv_open(
"utf-8", input_charset);
520 "unsupported @charset '%s'", input_charset);
543 struct _xmlAttr *attr;
544 const char *level_str = 0;
549 for (attr = ptr->properties; attr; attr = attr->next)
556 "bad attribute @%s, expected @level",
571 "bad element <%s>, expected <marc>|<xmlreader>",
581 char tmp_full_name[1024];
588 NULL, tmp_full_name))
593 yaz_log(YLOG_LOG,
"%s dom filter: "
599 yaz_log(YLOG_WARN,
"%s: dom filter: "
600 "failed to parse config file %s",
607 ptr = xmlDocGetRootElement(doc);
608 if (!ptr || ptr->type != XML_ELEMENT_NODE
612 "bad root element <%s>, expected root element <dom>",
627 struct _xmlAttr *attr;
634 for (attr = ptr->properties; attr; attr = attr->next)
641 "bad attribute @%s, expected @name",
655 struct _xmlAttr *attr;
669 for (attr = ptr->properties; attr; attr = attr->next)
679 "bad attribute @%s, expected @identifier|@name",
710 struct _xmlAttr *attr;
711 const char *syntax = 0;
712 const char *name = 0;
713 for (attr = ptr->properties; attr; attr = attr->next)
722 "bad attribute @%s, expected @syntax|@name",
732 "expected <extract>|<input>|<retrieve>|<store>",
755 for (; f; f = f->
next)
762 if (f->
name && !strcmp(f->
name, est))
774 yaz_log(YLOG_WARN,
"dom filter: need config file");
778 if (tinfo->
fname && !strcmp(args, tinfo->
fname))
796static int ioread_ex(
void *context,
char *buffer,
int len)
818 xmlChar *text = xmlNodeGetContent(node);
819 size_t text_len = strlen((
const char *)text);
832 const char *look = index_p;
840 recword->
term_buf = (
const char *)text;
845 while (*look &&
' ' != *look &&
':' != *look)
853 while (*look &&
':' != *look &&
' ' != *look)
858 strncpy((
char *)index, (
const char *)bval, eval - bval);
859 index[eval - bval] =
'\0';
868 while (*look &&
' ' != *look)
873 strncpy((
char *)type, (
const char *)bval, eval - bval);
874 type[eval - bval] =
'\0';
879 recword->
seqno = seqno_base;
887 "INDEX '%s:%s' '%s'",
888 (
const char *) index,
890 (
const char *) text);
894 if (seqno_max < recword->seqno)
895 seqno_max = recword->
seqno;
898 if (*look &&
' ' == *look)
903 recword->
seqno = seqno_max;
921 "RECORD id=%s rank=%s type=%s",
922 id_p ? (
const char *) id_p :
"(null)",
923 rank_p ? (
const char *) rank_p :
"(null)",
924 type_p ? (
const char *) type_p :
"(null)");
929 size_t l = strlen(id_p);
936 if (rank_p && *rank_p)
939 if (type_p && *type_p)
942 if (!strcmp(type_p,
"insert"))
944 else if (!strcmp(type_p,
"delete"))
946 else if (!strcmp(type_p,
"replace"))
948 else if (!strcmp(type_p,
"update"))
950 else if (!strcmp(type_p,
"adelete"))
953 dom_log(YLOG_WARN, tinfo, node,
"bad @type value: %s", type_p);
960 dom_log(YLOG_WARN, tinfo, node,
"multiple record elements");
978 if (node->type == XML_ELEMENT_NODE && node->ns && node->ns->href
983 const char *index_p = 0;
985 struct _xmlAttr *attr;
986 for (attr = node->properties; attr; attr = attr->next)
994 dom_log(YLOG_WARN, tinfo, node,
995 "bad attribute @%s, expected @name",
1000 else if (0 ==
XML_STRCMP(node->name,
"group"))
1002 const char *unit_p =
"element";
1004 struct _xmlAttr *attr;
1005 for (attr = node->properties; attr; attr = attr->next)
1011 dom_log(YLOG_WARN, tinfo, node,
1012 "bad attribute @%s, expected @unit",
1018 WRBUF w = wrbuf_alloc();
1020 wrbuf_puts(w, unit_p);
1028 "INDEX '%s:%s' '%s'",
1034 for (node = node->children; node; node = node->next)
1043 "INDEX '%s:%s' '%s'",
1051 else if (0 ==
XML_STRCMP(node->name,
"record"))
1053 const char *id_p = 0;
1054 const char *rank_p = 0;
1055 const char *type_p = 0;
1057 struct _xmlAttr *attr;
1058 for (attr = node->properties; attr; attr = attr->next)
1068 dom_log(YLOG_WARN, tinfo, node,
1069 "bad attribute @%s, expected @id|@rank|@type",
1077 dom_log(YLOG_WARN, tinfo, node,
1079 " expected <record>|<index> in namespace '%s'",
1086 char *value,
size_t value_max)
1088 size_t name_len = strlen(name);
1089 const char *look = *c_ptr;
1092 if (strlen(look) > name_len)
1094 if (look[name_len] ==
'=' && !memcmp(look, name, name_len))
1098 while (*look &&
' ' != *look)
1100 if (i < value_max-1)
1116 const char **index_pp)
1121 xmlChar *pi_p = node->content;
1122 const char *look = (
const char *) node->content;
1125 if (0 == strncmp((
const char *)look,
"record", 6))
1138 while (
' ' == *look)
1150 dom_log(YLOG_WARN, tinfo, node,
1151 "content '%s', can not parse '%s'",
1159 else if (0 == strncmp((
const char *)look,
"index", 5))
1164 while (*look &&
' ' == *look)
1172 dom_log(YLOG_WARN, tinfo, node,
1173 "content '%s', can not parse '%s'",
1186 const char *index_p = 0;
1193 for (node = node->children; node; node = node->next)
1196 if (node->type == XML_PI_NODE)
1200 else if (node->type == XML_ELEMENT_NODE)
1223 (*extctr->
init)(extctr, &recword);
1236 const char *params[10];
1237 xsltStylesheetPtr last_xsp = 0;
1254 xmlDocDumpMemory(doc, &buf_out, &len_out);
1256 FILE *
outf = fopen(
"extract.xml",
"w");
1257 fwrite(buf_out, 1, len_out,
outf);
1260 yaz_log(YLOG_LOG,
"Extract Doc: %.*s", len_out, buf_out);
1265 xmlDocPtr store_doc = 0;
1273 store_doc = xmlCopyDoc(doc, 1);
1275 params, &store_doc, &last_xsp);
1280 xsltSaveResultToString(&buf_out, &len_out,
1281 store_doc ? store_doc : doc, last_xsp);
1283 xmlDocDumpMemory(store_doc ? store_doc : doc, &buf_out, &len_out);
1289 xmlFreeDoc(store_doc);
1337 if (type == XML_READER_TYPE_ELEMENT &&
1350 xmlNodePtr ptr2 = xmlCopyNode(ptr, 1);
1351 xmlDocPtr doc = xmlNewDoc((
const xmlChar*)
"1.0");
1353 xmlDocSetRootElement(doc, ptr2);
1358 xmlChar *buf_out = 0;
1360 xmlDocDumpMemory(doc, &buf_out, &len_out);
1361 yaz_log(YLOG_LOG,
"%s: XMLREADER level: %i\n%.*s",
1363 depth, len_out, buf_out);
1416 while (*buf <
'0' || *buf >
'9')
1421 "MARC: Skipping bad byte %d (0x%02X)",
1422 *buf & 0xff, *buf & 0xff);
1423 for (i = 0; i < 4; i++)
1429 record_length = atoi_n (buf, 5);
1430 if (record_length < 25)
1433 "MARC record length < 25, is %d", record_length);
1437 if (read_bytes < record_length-5)
1440 "couldn't read whole MARC record");
1443 r = yaz_marc_read_iso2709(input->
u.
marc.
handle, buf, record_length);
1444 if (r < record_length)
1447 "parsing of MARC record failed r=%d length=%d",
1455 yaz_marc_write_xml(input->
u.
marc.
handle, &root_ptr,
1456 "http://www.loc.gov/MARC21/slim", 0, 0);
1457 rdoc = xmlNewDoc((
const xmlChar*)
"1.0");
1458 xmlDocSetRootElement(rdoc, root_ptr);
1505 const char *esn = 0;
1506 const char *params[32];
1510 xsltStylesheetPtr last_xsp = 0;
1514 if (p->
comp->which == Z_RecordComp_simple
1515 && p->
comp->u.simple->which == Z_ElementSetNames_generic)
1517 esn = p->
comp->u.simple->u.generic;
1519 else if (p->
comp->which == Z_RecordComp_complex
1520 && p->
comp->u.complex->generic->elementSpec
1521 && p->
comp->u.complex->generic->elementSpec->which ==
1522 Z_ElementSpec_elementSetName)
1524 esn = p->
comp->u.complex->generic->elementSpec->u.elementSetName;
1531 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
1560 XML_PARSE_XINCLUDE | XML_PARSE_NOENT | XML_PARSE_NONET);
1563 p->
diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1571 p->
diagnostic = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1580 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1582 xmlDocDumpMemory(doc, &buf_out, &len_out);
1590 else if (!oid_oidcmp(p->
output_format, yaz_oid_recsyn_sutrs))
1596 xsltSaveResultToString(&buf_out, &len_out, doc, last_xsp);
1598 xmlDocDumpMemory(doc, &buf_out, &len_out);
1609 p->
diagnostic = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
1626#if IDZEBRA_STATIC_DOM
static void filter_destroy(void *clientData)
static void * filter_init(Res res, RecType recType)
static void destroy_dom(struct filter_info *tinfo)
static int extract_xml_split(struct filter_info *tinfo, struct filter_input *input, struct recExtractCtrl *p)
#define DOM_INPUT_XMLREADER
static ZEBRA_RES parse_dom(struct filter_info *tinfo, const char *fname)
static ZEBRA_RES perform_convert(struct filter_info *tinfo, struct recExtractCtrl *extctr, struct recRetrieveCtrl *retctr, struct convert_s *convert, const char **params, xmlDocPtr *doc, xsltStylesheetPtr *last_xsp)
static void process_xml_element_zebra_node(struct filter_info *tinfo, struct recExtractCtrl *extctr, RecWord *recword, xmlNodePtr node)
static ZEBRA_RES parse_input(struct filter_info *tinfo, xmlNodePtr ptr, const char *syntax, const char *name)
static int extract_iso2709(struct filter_info *tinfo, struct filter_input *input, struct recExtractCtrl *p)
static int ioclose_ret(void *context)
#define FOR_EACH_ELEMENT(ptr)
static void process_xml_pi_node(struct filter_info *tinfo, struct recExtractCtrl *extctr, xmlNodePtr node, const char **index_pp)
static const char * zebra_pi_name
static void process_xml_element_node(struct filter_info *tinfo, struct recExtractCtrl *extctr, RecWord *recword, xmlNodePtr node)
static struct recType filter_type
static int ioclose_ex(void *context)
static void extract_dom_doc_node(struct filter_info *tinfo, struct recExtractCtrl *extctr, xmlDocPtr doc)
static ZEBRA_RES parse_convert(struct filter_info *tinfo, xmlNodePtr ptr, struct convert_s **l)
static void index_value_of(struct filter_info *tinfo, struct recExtractCtrl *extctr, RecWord *recword, xmlNodePtr node, const char *index_p)
static int process_meta(struct filter_info *tinfo, xmlDocPtr doc, xmlNodePtr node, struct recRetrieveCtrl *retctr)
static void set_record_info(struct filter_info *tinfo, struct recExtractCtrl *extctr, xmlNodePtr node, const char *id_p, const char *rank_p, const char *type_p)
static int convert_extract_doc(struct filter_info *tinfo, struct filter_input *input, struct recExtractCtrl *p, xmlDocPtr doc)
static const char * zebra_dom_ns
static int ioread_ret(void *context, char *buffer, int len)
static int attr_content(struct _xmlAttr *attr, const char *name, const char **dst_content)
static int attr_content_pi(const char **c_ptr, const char *name, char *value, size_t value_max)
static void dom_log(int level, struct filter_info *tinfo, xmlNodePtr ptr, const char *fmt,...)
static int ioread_ex(void *context, char *buffer, int len)
static void set_param_str(const char **params, const char *name, const char *value, NMEM nmem)
static void destroy_xsp(struct convert_s *c)
static void set_param_int(const char **params, const char *name, zint value, NMEM nmem)
static struct filter_input * new_input(struct filter_info *tinfo, int type)
static struct filter_retrieve * lookup_retrieve(struct filter_info *tinfo, const char *est)
static int extract_xml_full(struct filter_info *tinfo, struct filter_input *input, struct recExtractCtrl *p)
static ZEBRA_RES filter_config(void *clientData, Res res, const char *args)
#define RECCTRL_EXTRACT_EOF
#define ZEBRA_GROUP_INDEX_NAME
#define RECCTRL_EXTRACT_ERROR_GENERIC
#define RECCTRL_EXTRACT_OK
#define RECCTRL_EXTRACT_SKIP
const char * res_get(Res r, const char *name)
int(* readf)(struct ZebraRecStream *s, char *buf, size_t count)
read function
xsltStylesheetPtr stylesheet_xsp
struct filter_retrieve * retrieve_list
struct filter_extract * extract
struct filter_store * store
struct filter_input * input_list
const char * profile_path
struct filter_retrieve * next
struct convert_s * convert
struct convert_s * convert
const Odr_oid * input_format
int(* special_fetch)(void *handle, const char *esn, const Odr_oid *input_format, const Odr_oid **output_format, WRBUF result, WRBUF addinfo)
Z_RecordComposition * comp
struct ZebraRecStream * stream
const Odr_oid * output_format
zint atozint(const char *src)
short ZEBRA_RES
Common return type for Zebra API.