1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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)
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);
96 bool 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)))
112 bool 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))
123 bool mp_xml::is_element_mp(const xmlNode *ptr,
124 const std::string &name)
126 return mp::xml::is_element(ptr, metaproxy_ns, name);
130 bool 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
146 + " xmlns=\"" + metaproxy_ns + "\">"
147 + ", not " + got_element);
152 std::string mp_xml::get_route(const xmlNode *node)
154 std::string route_value;
157 const struct _xmlAttr *attr;
158 for (attr = node->properties; attr; attr = attr->next)
160 std::string name = std::string((const char *) attr->name);
163 if (attr->children && attr->children->type == XML_TEXT_NODE)
164 value = std::string((const char *)attr->children->content);
169 throw XMLError("Only attribute route allowed"
170 " in " + std::string((const char *)node->name)
171 + " element. Got " + std::string(name));
178 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
181 node = node->children;
182 for (; node && node->type != xml_node_type; node = node->next)
187 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
191 for (; node && node->type != xml_node_type; node = node->next)
196 const xmlNode* mp_xml::jump_to(const xmlNode* node,
199 for (; node && node->type != xml_node_type; node = node->next)
204 void mp_xml::check_empty(const xmlNode *node)
209 const struct _xmlAttr *attr;
211 for (attr = node->properties; attr; attr = attr->next)
212 if (!strcmp((const char *) attr->name, "type"))
213 extra = " of type " + get_text(attr);
214 for (n = node->children; n; n = n->next)
215 if (n->type == XML_ELEMENT_NODE)
216 throw mp::XMLError("No child elements allowed inside element "
217 + std::string((const char *) node->name)
225 * c-file-style: "Stroustrup"
226 * indent-tabs-mode: nil
228 * vim: shiftwidth=4 tabstop=8 expandtab