From 23d3217defe73218069ac17be28742b39637a144 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 10 Sep 2013 10:39:40 +0200 Subject: [PATCH] Refactor yaz_match-routines to separate source --- src/Makefile.am | 2 +- src/sru-p.h | 6 ++ src/srw.c | 137 -------------------------------------------- src/xml_match.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ win/makefile | 1 + 5 files changed, 179 insertions(+), 138 deletions(-) create mode 100644 src/xml_match.c diff --git a/src/Makefile.am b/src/Makefile.am index b5bdd97..87898b5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -92,7 +92,7 @@ libyaz_la_SOURCES=base64.c version.c options.c log.c \ zoom-event.c \ record_render.c zoom-socket.c zoom-opt.c zoom-p.h sru_facet.c sru-p.h \ grs1disp.c zgdu.c soap.c srw.c srwutil.c uri.c solr.c diag_map.c \ - opac_to_xml.c xml_to_opac.c \ + opac_to_xml.c xml_match.c xml_to_opac.c \ cclfind.c ccltoken.c cclerrms.c cclqual.c cclptree.c cclp.h \ cclqfile.c cclstr.c cclxmlconfig.c ccl_stop_words.c \ cql.y cqlstdio.c cqltransform.c cqlutil.c xcqlutil.c cqlstring.c \ diff --git a/src/sru-p.h b/src/sru-p.h index 09bd4ce..def26e8 100644 --- a/src/sru-p.h +++ b/src/sru-p.h @@ -53,6 +53,12 @@ int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, char **val, int *len); int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem); +int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len, int fixup_root); + +int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len); + xmlNodePtr add_xsd_string(xmlNodePtr ptr, const char *elem, const char *val); void add_xsd_integer(xmlNodePtr ptr, const char *elem, const Odr_int *val); diff --git a/src/srw.c b/src/srw.c index 33452e2..386100c 100644 --- a/src/srw.c +++ b/src/srw.c @@ -79,143 +79,6 @@ void add_xsd_integer(xmlNodePtr ptr, const char *elem, } } -int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem) -{ - if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem)) - { - return 1; - } - return 0; -} - -#define CHECK_TYPE 0 - -int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, - char **val, int *len) -{ -#if CHECK_TYPE - struct _xmlAttr *attr; -#endif - if (!yaz_match_xsd_element(ptr, elem)) - return 0; -#if CHECK_TYPE - for (attr = ptr->properties; attr; attr = attr->next) - if (!strcmp(attr->name, "type") && - attr->children && attr->children->type == XML_TEXT_NODE) - { - const char *t = strchr(attr->children->content, ':'); - if (t) - t = t + 1; - else - t = attr->children->content; - if (!strcmp(t, "string")) - break; - } - if (!attr) - return 0; -#endif - ptr = ptr->children; - if (!ptr || ptr->type != XML_TEXT_NODE) - { - *val = ""; - return 1; - } - *val = odr_strdup(o, (const char *) ptr->content); - if (len) - *len = xmlStrlen(ptr->content); - return 1; -} - - -int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val) -{ - return yaz_match_xsd_string_n(ptr, elem, o, val, 0); -} - -static int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, - char **val, int *len, int fixup_root) -{ - xmlBufferPtr buf; - int no_root_nodes = 0; - - if (!yaz_match_xsd_element(ptr, elem)) - return 0; - - buf = xmlBufferCreate(); - - /* Copy each element nodes at top. - In most cases there is only one root node.. At least one server - http://www.theeuropeanlibrary.org/sru/sru.pl - has multiple root nodes in recordData. - */ - for (ptr = ptr->children; ptr; ptr = ptr->next) - { - if (ptr->type == XML_ELEMENT_NODE) - { - /* copy node to get NS right (bug #740). */ - xmlNode *tmp = xmlCopyNode(ptr, 1); - - xmlNodeDump(buf, tmp->doc, tmp, 0, 0); - - xmlFreeNode(tmp); - no_root_nodes++; - } - } - if (no_root_nodes != 1 && fixup_root) - { - /* does not appear to be an XML document. Make it so */ - xmlBufferAddHead(buf, (const xmlChar *) "", -1); - xmlBufferAdd(buf, (const xmlChar *) "", -1); - } - *val = (char *) odr_malloc(o, buf->use + 1); - memcpy(*val, buf->content, buf->use); - (*val)[buf->use] = '\0'; - - if (len) - *len = buf->use; - - xmlBufferFree(buf); - - return 1; -} - -static int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o, - char **val, int *len) -{ - return yaz_match_xsd_XML_n2(ptr, elem, o, val, len, 0); -} - -int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, - Odr_int **val) -{ -#if CHECK_TYPE - struct _xmlAttr *attr; -#endif - if (!yaz_match_xsd_element(ptr, elem)) - return 0; -#if CHECK_TYPE - for (attr = ptr->properties; attr; attr = attr->next) - if (!strcmp(attr->name, "type") && - attr->children && attr->children->type == XML_TEXT_NODE) - { - const char *t = strchr(attr->children->content, ':'); - if (t) - t = t + 1; - else - t = attr->children->content; - if (!strcmp(t, "integer")) - break; - } - if (!attr) - return 0; -#endif - ptr = ptr->children; - if (!ptr || ptr->type != XML_TEXT_NODE) - return 0; - *val = odr_intdup(o, odr_atoi((const char *) ptr->content)); - return 1; -} - char *yaz_negotiate_sru_version(char *input_ver) { if (!input_ver) diff --git a/src/xml_match.c b/src/xml_match.c new file mode 100644 index 0000000..ab0e4b1 --- /dev/null +++ b/src/xml_match.c @@ -0,0 +1,171 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2013 Index Data + * See the file LICENSE for details. + */ +/** + * \file xml_match.c + * \brief XML node inspection utilities + */ +#if HAVE_CONFIG_H +#include +#endif + +#include + +#include +#include +#if YAZ_HAVE_XML2 +#include +#include +#include "sru-p.h" + +int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem) +{ + if (ptr->type == XML_ELEMENT_NODE && !xmlStrcmp(ptr->name, BAD_CAST elem)) + { + return 1; + } + return 0; +} + +#define CHECK_TYPE 0 + +int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len) +{ +#if CHECK_TYPE + struct _xmlAttr *attr; +#endif + if (!yaz_match_xsd_element(ptr, elem)) + return 0; +#if CHECK_TYPE + for (attr = ptr->properties; attr; attr = attr->next) + if (!strcmp(attr->name, "type") && + attr->children && attr->children->type == XML_TEXT_NODE) + { + const char *t = strchr(attr->children->content, ':'); + if (t) + t = t + 1; + else + t = attr->children->content; + if (!strcmp(t, "string")) + break; + } + if (!attr) + return 0; +#endif + ptr = ptr->children; + if (!ptr || ptr->type != XML_TEXT_NODE) + { + *val = ""; + return 1; + } + *val = odr_strdup(o, (const char *) ptr->content); + if (len) + *len = xmlStrlen(ptr->content); + return 1; +} + + +int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val) +{ + return yaz_match_xsd_string_n(ptr, elem, o, val, 0); +} + +int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len, int fixup_root) +{ + xmlBufferPtr buf; + int no_root_nodes = 0; + + if (!yaz_match_xsd_element(ptr, elem)) + return 0; + + buf = xmlBufferCreate(); + + /* Copy each element nodes at top. + In most cases there is only one root node.. At least one server + http://www.theeuropeanlibrary.org/sru/sru.pl + has multiple root nodes in recordData. + */ + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + if (ptr->type == XML_ELEMENT_NODE) + { + /* copy node to get NS right (bug #740). */ + xmlNode *tmp = xmlCopyNode(ptr, 1); + + xmlNodeDump(buf, tmp->doc, tmp, 0, 0); + + xmlFreeNode(tmp); + no_root_nodes++; + } + } + if (no_root_nodes != 1 && fixup_root) + { + /* does not appear to be an XML document. Make it so */ + xmlBufferAddHead(buf, (const xmlChar *) "", -1); + xmlBufferAdd(buf, (const xmlChar *) "", -1); + } + *val = (char *) odr_malloc(o, buf->use + 1); + memcpy(*val, buf->content, buf->use); + (*val)[buf->use] = '\0'; + + if (len) + *len = buf->use; + + xmlBufferFree(buf); + + return 1; +} + +int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o, + char **val, int *len) +{ + return yaz_match_xsd_XML_n2(ptr, elem, o, val, len, 0); +} + +int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, + Odr_int **val) +{ +#if CHECK_TYPE + struct _xmlAttr *attr; +#endif + if (!yaz_match_xsd_element(ptr, elem)) + return 0; +#if CHECK_TYPE + for (attr = ptr->properties; attr; attr = attr->next) + if (!strcmp(attr->name, "type") && + attr->children && attr->children->type == XML_TEXT_NODE) + { + const char *t = strchr(attr->children->content, ':'); + if (t) + t = t + 1; + else + t = attr->children->content; + if (!strcmp(t, "integer")) + break; + } + if (!attr) + return 0; +#endif + ptr = ptr->children; + if (!ptr || ptr->type != XML_TEXT_NODE) + return 0; + *val = odr_intdup(o, odr_atoi((const char *) ptr->content)); + return 1; +} + + +#endif + + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/win/makefile b/win/makefile index e04c57e..b88e2a9 100644 --- a/win/makefile +++ b/win/makefile @@ -490,6 +490,7 @@ MISC_OBJS= \ $(OBJDIR)\charneg.obj \ $(OBJDIR)\grs1disp.obj \ $(OBJDIR)\opac_to_xml.obj \ + $(OBJDIR)\xml_match.obj \ $(OBJDIR)\xml_to_opac.obj \ $(OBJDIR)\zgdu.obj \ $(OBJDIR)\soap.obj \ -- 1.7.10.4