1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 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 "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 && ptr->content)
50 if (!strcmp((const char *) ptr->content, "true"))
58 int mp_xml::get_int(const xmlNode *ptr, int default_value)
60 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
62 return atoi((const char *) ptr->content);
67 bool mp_xml::check_attribute(const _xmlAttr *ptr,
68 const std::string &ns,
69 const std::string &name)
72 if (!mp::xml::is_attribute(ptr, ns, name))
74 std::string got_attr = "'";
76 got_attr += std::string((const char *)ptr->name);
77 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
79 got_attr += std::string((const char *)ptr->ns->href);
83 throw mp::XMLError("Expected XML attribute '" + name
85 + ", not " + got_attr);
90 bool mp_xml::is_attribute(const _xmlAttr *ptr,
91 const std::string &ns,
92 const std::string &name)
94 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
98 && (!ptr->ns || !ptr->ns->href
99 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
106 bool mp_xml::is_element(const xmlNode *ptr,
107 const std::string &ns,
108 const std::string &name)
110 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
111 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
112 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
117 bool mp_xml::is_element_mp(const xmlNode *ptr,
118 const std::string &name)
120 return mp::xml::is_element(ptr, metaproxy_ns, name);
124 bool mp_xml::check_element_mp(const xmlNode *ptr,
125 const std::string &name)
127 if (!mp::xml::is_element_mp(ptr, name))
129 std::string got_element = "<";
130 if (ptr && ptr->name)
131 got_element += std::string((const char *)ptr->name);
132 if (ptr && ptr->ns && ptr->ns->href){
133 got_element += " xmlns=\"";
134 got_element += std::string((const char *)ptr->ns->href);
139 throw mp::XMLError("Expected XML element <" + name
140 + " xmlns=\"" + metaproxy_ns + "\">"
141 + ", not " + got_element);
146 std::string mp_xml::get_route(const xmlNode *node)
148 std::string route_value;
151 const struct _xmlAttr *attr;
152 for (attr = node->properties; attr; attr = attr->next)
154 std::string name = std::string((const char *) attr->name);
157 if (attr->children && attr->children->type == XML_TEXT_NODE)
158 value = std::string((const char *)attr->children->content);
163 throw XMLError("Only attribute route allowed"
164 " in " + std::string((const char *)node->name)
165 + " element. Got " + std::string(name));
172 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
175 node = node->children;
176 for (; node && node->type != xml_node_type; node = node->next)
181 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
185 for (; node && node->type != xml_node_type; node = node->next)
190 const xmlNode* mp_xml::jump_to(const xmlNode* node,
193 for (; node && node->type != xml_node_type; node = node->next)
198 void mp_xml::check_empty(const xmlNode *node)
203 for (n = node->children; n; n = n->next)
204 if (n->type == XML_ELEMENT_NODE)
205 throw mp::XMLError("No child elements allowed inside element "
206 + std::string((const char *) node->name));
213 * indent-tabs-mode: nil
214 * c-file-style: "stroustrup"
216 * vim: shiftwidth=4 tabstop=8 expandtab