metaproxy 1.22.1
xmlutil.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "config.hpp"
20
21#include <metaproxy/xmlutil.hpp>
22
23#include <string.h>
24#include <stdlib.h>
25
26namespace mp = metaproxy_1;
27// Doxygen doesn't like mp::xml, so we use this instead
28namespace mp_xml = metaproxy_1::xml;
29
30static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
31
32std::string mp_xml::get_text(const struct _xmlAttr *ptr)
33{
34 return get_text(ptr->children);
35}
36
37std::string mp_xml::get_text(const xmlNode *ptr)
38{
39 std::string c;
40 if (ptr && ptr->type != XML_TEXT_NODE)
41 ptr = ptr->children;
42 for (; ptr; ptr = ptr->next)
43 if (ptr->type == XML_TEXT_NODE)
44 c += std::string((const char *) (ptr->content));
45 return c;
46}
47
48bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
49{
50 if (ptr && ptr->type != XML_TEXT_NODE)
51 ptr = ptr->children;
52 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
53 {
54 if (!strcmp((const char *) ptr->content, "true")
55 || !strcmp((const char *) ptr->content, "1"))
56 return true;
57 else
58 return false;
59 }
60 return default_value;
61}
62
63int mp_xml::get_int(const xmlNode *ptr, int default_value)
64{
65 if (ptr && ptr->type != XML_TEXT_NODE)
66 ptr = ptr->children;
67 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
68 {
69 return atoi((const char *) ptr->content);
70 }
71 return default_value;
72}
73
74bool mp_xml::check_attribute(const _xmlAttr *ptr,
75 const std::string &ns,
76 const std::string &name)
77{
78 if (!mp::xml::is_attribute(ptr, ns, name))
79 {
80 std::string got_attr = "'";
81 if (ptr && ptr->name)
82 got_attr += std::string((const char *)ptr->name);
83 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
84 got_attr += " ";
85 got_attr += std::string((const char *)ptr->ns->href);
86 }
87 got_attr += "'";
88
89 throw mp::XMLError("Expected XML attribute '" + name
90 + " " + ns + "'"
91 + ", not " + got_attr);
92 }
93 return true;
94}
95
96bool mp_xml::is_attribute(const _xmlAttr *ptr,
97 const std::string &ns,
98 const std::string &name)
99{
100 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
101 return false;
102
103 if (ns.size()
104 && (!ptr->ns || !ptr->ns->href
105 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
106 return false;
107
108 return true;
109}
110
111
112bool mp_xml::is_element(const xmlNode *ptr,
113 const std::string &ns,
114 const std::string &name)
115{
116 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
117 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
118 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
119 return true;
120 return false;
121}
122
123bool mp_xml::is_element_mp(const xmlNode *ptr,
124 const std::string &name)
125{
126 return mp::xml::is_element(ptr, metaproxy_ns, name);
127}
128
129
130bool mp_xml::check_element_mp(const xmlNode *ptr,
131 const std::string &name)
132{
133 if (!mp::xml::is_element_mp(ptr, name))
134 {
135 std::string got_element = "<";
136 if (ptr && ptr->name)
137 got_element += std::string((const char *)ptr->name);
138 if (ptr && ptr->ns && ptr->ns->href){
139 got_element += " xmlns=\"";
140 got_element += std::string((const char *)ptr->ns->href);
141 got_element += "\"";
142 }
143 got_element += ">";
144
145 throw mp::XMLError("Expected XML element <" + name
146 + " xmlns=\"" + metaproxy_ns + "\">"
147 + ", not " + got_element);
148 }
149 return true;
150}
151
152void mp_xml::parse_attr(const xmlNode *node, const char **names,
153 std::string *values)
154{
155 size_t i;
156 for (i = 0; names[i]; i++)
157 values[i].clear();
158
159 if (node)
160 {
161 const struct _xmlAttr *attr;
162 for (attr = node->properties; attr; attr = attr->next)
163 {
164 std::string value;
165 const char *name = (const char *) attr->name;
166
167 if (attr->children && attr->children->type == XML_TEXT_NODE)
168 value = std::string((const char *)attr->children->content);
169 for (i = 0; names[i]; i++)
170 if (!strcmp(name, names[i]))
171 {
172 values[i] = value;
173 break;
174 }
175 if (!names[i])
176 {
177 throw XMLError("Unsupported attribute: '" +
178 std::string(name) +
179 "' in element '" +
180 std::string((const char *) node->name) + "'");
181 }
182 }
183 }
184}
185
186std::string mp_xml::get_route(const xmlNode *node, std::string &auth)
187{
188 const char *names[3] = { "route", "auth", 0 };
189 std::string values[2];
190
191 parse_attr(node, names, values);
192
193 auth = values[1];
194 return values[0];
195}
196
197std::string mp_xml::get_route(const xmlNode *node)
198{
199 const char *names[2] = { "route", 0 };
200 std::string values[1];
201
202 parse_attr(node, names, values);
203
204 return values[0];
205}
206
207const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
208 int xml_node_type)
209{
210 node = node->children;
211 for (; node && node->type != xml_node_type; node = node->next)
212 ;
213 return node;
214}
215
216const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
217 int xml_node_type)
218{
219 node = node->next;
220 for (; node && node->type != xml_node_type; node = node->next)
221 ;
222 return node;
223}
224
225const xmlNode* mp_xml::jump_to(const xmlNode* node,
226 int xml_node_type)
227{
228 for (; node && node->type != xml_node_type; node = node->next)
229 ;
230 return node;
231}
232
233void mp_xml::check_empty(const xmlNode *node)
234{
235 if (node)
236 {
237 const xmlNode *n;
238 const struct _xmlAttr *attr;
239 std::string extra;
240 for (attr = node->properties; attr; attr = attr->next)
241 if (!strcmp((const char *) attr->name, "type"))
242 extra = " of type " + get_text(attr);
243 for (n = node->children; n; n = n->next)
244 if (n->type == XML_ELEMENT_NODE)
245 throw mp::XMLError("No child elements allowed inside element "
246 + std::string((const char *) node->name)
247 + extra);
248 }
249}
250
251/*
252 * Local variables:
253 * c-basic-offset: 4
254 * c-file-style: "Stroustrup"
255 * indent-tabs-mode: nil
256 * End:
257 * vim: shiftwidth=4 tabstop=8 expandtab
258 */
259
static const std::string metaproxy_ns
Definition xmlutil.cpp:30