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
19 #include <metaproxy/xmlutil.hpp>
24 namespace mp = metaproxy_1;
25 // Doxygen doesn't like mp::xml, so we use this instead
26 namespace mp_xml = metaproxy_1::xml;
28 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
30 std::string mp_xml::get_text(const struct _xmlAttr *ptr)
32 return get_text(ptr->children);
35 std::string mp_xml::get_text(const xmlNode *ptr)
38 if (ptr && ptr->type != XML_TEXT_NODE)
40 for (; ptr; ptr = ptr->next)
41 if (ptr->type == XML_TEXT_NODE)
42 c += std::string((const char *) (ptr->content));
46 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
48 if (ptr && ptr->type != XML_TEXT_NODE)
50 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
52 if (!strcmp((const char *) ptr->content, "true")
53 || !strcmp((const char *) ptr->content, "1"))
61 int mp_xml::get_int(const xmlNode *ptr, int default_value)
63 if (ptr && ptr->type != XML_TEXT_NODE)
65 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
67 return atoi((const char *) ptr->content);
72 bool mp_xml::check_attribute(const _xmlAttr *ptr,
73 const std::string &ns,
74 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 std::string mp_xml::get_route(const xmlNode *node)
153 std::string route_value;
156 const struct _xmlAttr *attr;
157 for (attr = node->properties; attr; attr = attr->next)
159 std::string name = std::string((const char *) attr->name);
162 if (attr->children && attr->children->type == XML_TEXT_NODE)
163 value = std::string((const char *)attr->children->content);
168 throw XMLError("Only attribute route allowed"
169 " in " + std::string((const char *)node->name)
170 + " element. Got " + std::string(name));
177 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
180 node = node->children;
181 for (; node && node->type != xml_node_type; node = node->next)
186 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
190 for (; node && node->type != xml_node_type; node = node->next)
195 const xmlNode* mp_xml::jump_to(const xmlNode* node,
198 for (; node && node->type != xml_node_type; node = node->next)
203 void mp_xml::check_empty(const xmlNode *node)
208 for (n = node->children; n; n = n->next)
209 if (n->type == XML_ELEMENT_NODE)
210 throw mp::XMLError("No child elements allowed inside element "
211 + std::string((const char *) node->name));
218 * c-file-style: "Stroustrup"
219 * indent-tabs-mode: nil
221 * vim: shiftwidth=4 tabstop=8 expandtab