YAZ 5.37.0
xml_match.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
9#if HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <yaz/srw.h>
14#if YAZ_HAVE_XML2
15#include <yaz/nmem_xml.h>
16#include "sru-p.h"
17
18int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem)
19{
20 if (ptr && ptr->type == XML_ELEMENT_NODE &&
21 !xmlStrcmp(ptr->name, BAD_CAST elem))
22 {
23 return 1;
24 }
25 return 0;
26}
27
28#define CHECK_TYPE 0
29
30int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem,
31 char **val, int *len)
32{
33#if CHECK_TYPE
34 struct _xmlAttr *attr;
35#endif
36 if (!yaz_match_xsd_element(ptr, elem))
37 return 0;
38#if CHECK_TYPE
39 for (attr = ptr->properties; attr; attr = attr->next)
40 if (!strcmp(attr->name, "type") &&
41 attr->children && attr->children->type == XML_TEXT_NODE)
42 {
43 const char *t = strchr(attr->children->content, ':');
44 if (t)
45 t = t + 1;
46 else
47 t = attr->children->content;
48 if (!strcmp(t, "string"))
49 break;
50 }
51 if (!attr)
52 return 0;
53#endif
54 ptr = ptr->children;
55 if (!ptr || ptr->type != XML_TEXT_NODE)
56 {
57 *val = "";
58 return 1;
59 }
60 *val = nmem_strdup(nmem, (const char *) ptr->content);
61 if (len)
62 *len = xmlStrlen(ptr->content);
63 return 1;
64}
65
66int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o,
67 char **val, int *len)
68{
69 return yaz_match_xsd_string_n_nmem(ptr, elem, o->mem, val, len);
70}
71
72int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val)
73{
74 return yaz_match_xsd_string_n(ptr, elem, o, val, 0);
75}
76
77int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o,
78 char **val, int *len, int fixup_root)
79{
80 xmlBufferPtr buf;
81 int no_root_nodes = 0;
82
83 if (!yaz_match_xsd_element(ptr, elem))
84 return 0;
85
86 buf = xmlBufferCreate();
87
88 /* Copy each element nodes at top.
89 In most cases there is only one root node.. At least one server
90 http://www.theeuropeanlibrary.org/sru/sru.pl
91 has multiple root nodes in recordData.
92 */
93 for (ptr = ptr->children; ptr; ptr = ptr->next)
94 {
95 if (ptr->type == XML_ELEMENT_NODE)
96 {
97 /* copy node to get NS right (bug #740). */
98 xmlNode *tmp = xmlCopyNode(ptr, 1);
99
100 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
101
102 xmlFreeNode(tmp);
103 no_root_nodes++;
104 }
105 }
106 if (no_root_nodes != 1 && fixup_root)
107 {
108 /* does not appear to be an XML document. Make it so */
109 xmlBufferAddHead(buf, (const xmlChar *) "<yaz_record>", -1);
110 xmlBufferAdd(buf, (const xmlChar *) "</yaz_record>", -1);
111 }
112 *val = nmem_from_xml_buffer(odr_getmem(o), buf, len);
113 xmlBufferFree(buf);
114
115 return 1;
116}
117
118int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o,
119 char **val, int *len)
120{
121 return yaz_match_xsd_XML_n2(ptr, elem, o, val, len, 0);
122}
123
124int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o,
125 Odr_int **val)
126{
127#if CHECK_TYPE
128 struct _xmlAttr *attr;
129#endif
130 if (!yaz_match_xsd_element(ptr, elem))
131 return 0;
132#if CHECK_TYPE
133 for (attr = ptr->properties; attr; attr = attr->next)
134 if (!strcmp(attr->name, "type") &&
135 attr->children && attr->children->type == XML_TEXT_NODE)
136 {
137 const char *t = strchr(attr->children->content, ':');
138 if (t)
139 t = t + 1;
140 else
141 t = attr->children->content;
142 if (!strcmp(t, "integer"))
143 break;
144 }
145 if (!attr)
146 return 0;
147#endif
148 ptr = ptr->children;
149 if (!ptr || ptr->type != XML_TEXT_NODE)
150 return 0;
151 *val = odr_intdup(o, odr_atoi((const char *) ptr->content));
152 return 1;
153}
154
155
156#endif
157
158
159/*
160 * Local variables:
161 * c-basic-offset: 4
162 * c-file-style: "Stroustrup"
163 * indent-tabs-mode: nil
164 * End:
165 * vim: shiftwidth=4 tabstop=8 expandtab
166 */
167
struct nmem_control * NMEM
NMEM handle (an opaque pointer to memory).
Definition nmem.h:44
char * nmem_from_xml_buffer(NMEM nmem, const xmlBufferPtr buf, int *ret_len)
copies xmlBuffer data to NMEM
Definition nmem_xml.c:36
Header for Nibble Memory functions + Libxml2 specific stuff.
char * nmem_strdup(NMEM mem, const char *src)
allocates string on NMEM handle (similar strdup)
Definition nmemsdup.c:19
#define odr_getmem(o)
Definition odr.h:216
struct odr * ODR
Definition odr.h:121
nmem_int_t Odr_int
Definition odr.h:47
Odr_int * odr_intdup(ODR o, Odr_int v)
Definition odr_mem.c:51
Odr_int odr_atoi(const char *s)
Definition odr_mem.c:146
SRU private header.
Header for SRW/SRU.
NMEM mem
Definition odr.h:130
int yaz_match_xsd_element(xmlNodePtr ptr, const char *elem)
Definition xml_match.c:18
int yaz_match_xsd_string_n_nmem(xmlNodePtr ptr, const char *elem, NMEM nmem, char **val, int *len)
Definition xml_match.c:30
int yaz_match_xsd_XML_n2(xmlNodePtr ptr, const char *elem, ODR o, char **val, int *len, int fixup_root)
Definition xml_match.c:77
int yaz_match_xsd_XML_n(xmlNodePtr ptr, const char *elem, ODR o, char **val, int *len)
Definition xml_match.c:118
int yaz_match_xsd_string_n(xmlNodePtr ptr, const char *elem, ODR o, char **val, int *len)
Definition xml_match.c:66
int yaz_match_xsd_string(xmlNodePtr ptr, const char *elem, ODR o, char **val)
Definition xml_match.c:72
int yaz_match_xsd_integer(xmlNodePtr ptr, const char *elem, ODR o, Odr_int **val)
Definition xml_match.c:124