1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <metaproxy/xmlutil.hpp>
25 namespace mp = metaproxy_1;
26 // Doxygen doesn't like mp::xml, so we use this instead
27 namespace mp_xml = metaproxy_1::xml;
29 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
31 std::string mp_xml::get_text(const struct _xmlAttr *ptr)
33 return get_text(ptr->children);
36 std::string mp_xml::get_text(const xmlNode *ptr)
39 if (ptr && ptr->type != XML_TEXT_NODE)
41 for (; ptr; ptr = ptr->next)
42 if (ptr->type == XML_TEXT_NODE)
43 c += std::string((const char *) (ptr->content));
47 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
49 if (ptr && ptr->type != XML_TEXT_NODE)
51 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
53 if (!strcmp((const char *) ptr->content, "true")
54 || !strcmp((const char *) ptr->content, "1"))
62 int mp_xml::get_int(const xmlNode *ptr, int default_value)
64 if (ptr && ptr->type != XML_TEXT_NODE)
66 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
68 return atoi((const char *) ptr->content);
73 bool mp_xml::check_attribute(const _xmlAttr *ptr,
74 const std::string &ns,
75 const std::string &name)
77 if (!mp::xml::is_attribute(ptr, ns, name))
79 std::string got_attr = "'";
81 got_attr += std::string((const char *)ptr->name);
82 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
84 got_attr += std::string((const char *)ptr->ns->href);
88 throw mp::XMLError("Expected XML attribute '" + name
90 + ", not " + got_attr);
95 bool mp_xml::is_attribute(const _xmlAttr *ptr,
96 const std::string &ns,
97 const std::string &name)
99 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
103 && (!ptr->ns || !ptr->ns->href
104 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
111 bool mp_xml::is_element(const xmlNode *ptr,
112 const std::string &ns,
113 const std::string &name)
115 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
116 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
117 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
122 bool mp_xml::is_element_mp(const xmlNode *ptr,
123 const std::string &name)
125 return mp::xml::is_element(ptr, metaproxy_ns, name);
129 bool mp_xml::check_element_mp(const xmlNode *ptr,
130 const std::string &name)
132 if (!mp::xml::is_element_mp(ptr, name))
134 std::string got_element = "<";
135 if (ptr && ptr->name)
136 got_element += std::string((const char *)ptr->name);
137 if (ptr && ptr->ns && ptr->ns->href){
138 got_element += " xmlns=\"";
139 got_element += std::string((const char *)ptr->ns->href);
144 throw mp::XMLError("Expected XML element <" + name
145 + " xmlns=\"" + metaproxy_ns + "\">"
146 + ", not " + got_element);
151 void mp_xml::parse_attr(const xmlNode *node, const char **names,
155 for (i = 0; names[i]; i++)
160 const struct _xmlAttr *attr;
161 for (attr = node->properties; attr; attr = attr->next)
164 const char *name = (const char *) attr->name;
166 if (attr->children && attr->children->type == XML_TEXT_NODE)
167 value = std::string((const char *)attr->children->content);
168 for (i = 0; names[i]; i++)
169 if (!strcmp(name, names[i]))
176 throw XMLError("Unsupported attribute: '" +
179 std::string((const char *) node->name) + "'");
185 std::string mp_xml::get_route(const xmlNode *node, std::string &auth)
187 const char *names[3] = { "route", "auth", 0 };
188 std::string values[2];
190 parse_attr(node, names, values);
196 std::string mp_xml::get_route(const xmlNode *node)
198 const char *names[2] = { "route", 0 };
199 std::string values[1];
201 parse_attr(node, names, values);
206 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
209 node = node->children;
210 for (; node && node->type != xml_node_type; node = node->next)
215 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
219 for (; node && node->type != xml_node_type; node = node->next)
224 const xmlNode* mp_xml::jump_to(const xmlNode* node,
227 for (; node && node->type != xml_node_type; node = node->next)
232 void mp_xml::check_empty(const xmlNode *node)
237 const struct _xmlAttr *attr;
239 for (attr = node->properties; attr; attr = attr->next)
240 if (!strcmp((const char *) attr->name, "type"))
241 extra = " of type " + get_text(attr);
242 for (n = node->children; n; n = n->next)
243 if (n->type == XML_ELEMENT_NODE)
244 throw mp::XMLError("No child elements allowed inside element "
245 + std::string((const char *) node->name)
253 * c-file-style: "Stroustrup"
254 * indent-tabs-mode: nil
256 * vim: shiftwidth=4 tabstop=8 expandtab