21#include <metaproxy/xmlutil.hpp>
28namespace mp_xml = metaproxy_1::xml;
30static const std::string
metaproxy_ns =
"http://indexdata.com/metaproxy";
32std::string mp_xml::get_text(
const struct _xmlAttr *ptr)
34 return get_text(ptr->children);
37std::string mp_xml::get_text(
const xmlNode *ptr)
40 if (ptr && ptr->type != XML_TEXT_NODE)
42 for (; ptr; ptr = ptr->next)
43 if (ptr->type == XML_TEXT_NODE)
44 c += std::string((
const char *) (ptr->content));
48bool mp_xml::get_bool(
const xmlNode *ptr,
bool default_value)
50 if (ptr && ptr->type != XML_TEXT_NODE)
52 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
54 if (!strcmp((
const char *) ptr->content,
"true")
55 || !strcmp((
const char *) ptr->content,
"1"))
63int mp_xml::get_int(
const xmlNode *ptr,
int default_value)
65 if (ptr && ptr->type != XML_TEXT_NODE)
67 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
69 return atoi((
const char *) ptr->content);
74bool mp_xml::check_attribute(
const _xmlAttr *ptr,
75 const std::string &ns,
76 const std::string &name)
78 if (!mp::xml::is_attribute(ptr, ns, name))
80 std::string got_attr =
"'";
82 got_attr += std::string((
const char *)ptr->name);
83 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
85 got_attr += std::string((
const char *)ptr->ns->href);
89 throw mp::XMLError(
"Expected XML attribute '" + name
91 +
", not " + got_attr);
96bool mp_xml::is_attribute(
const _xmlAttr *ptr,
97 const std::string &ns,
98 const std::string &name)
100 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
104 && (!ptr->ns || !ptr->ns->href
105 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
112bool mp_xml::is_element(
const xmlNode *ptr,
113 const std::string &ns,
114 const std::string &name)
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))
123bool mp_xml::is_element_mp(
const xmlNode *ptr,
124 const std::string &name)
130bool mp_xml::check_element_mp(
const xmlNode *ptr,
131 const std::string &name)
133 if (!mp::xml::is_element_mp(ptr, name))
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);
145 throw mp::XMLError(
"Expected XML element <" + name
147 +
", not " + got_element);
152void mp_xml::parse_attr(
const xmlNode *node,
const char **names,
156 for (i = 0; names[i]; i++)
161 const struct _xmlAttr *attr;
162 for (attr = node->properties; attr; attr = attr->next)
165 const char *name = (
const char *) attr->name;
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]))
177 throw XMLError(
"Unsupported attribute: '" +
180 std::string((
const char *) node->name) +
"'");
186std::string mp_xml::get_route(
const xmlNode *node, std::string &auth)
188 const char *names[3] = {
"route",
"auth", 0 };
189 std::string values[2];
191 parse_attr(node, names, values);
197std::string mp_xml::get_route(
const xmlNode *node)
199 const char *names[2] = {
"route", 0 };
200 std::string values[1];
202 parse_attr(node, names, values);
207const xmlNode* mp_xml::jump_to_children(
const xmlNode* node,
210 node = node->children;
211 for (; node && node->type != xml_node_type; node = node->next)
216const xmlNode* mp_xml::jump_to_next(
const xmlNode* node,
220 for (; node && node->type != xml_node_type; node = node->next)
225const xmlNode* mp_xml::jump_to(
const xmlNode* node,
228 for (; node && node->type != xml_node_type; node = node->next)
233void mp_xml::check_empty(
const xmlNode *node)
238 const struct _xmlAttr *attr;
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)
static const std::string metaproxy_ns