1 /* $Id: xmlutil.cpp,v 1.14 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "xmlutil.hpp"
27 namespace mp = metaproxy_1;
28 // Doxygen doesn't like mp::xml, so we use this instead
29 namespace mp_xml = metaproxy_1::xml;
31 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
33 std::string mp_xml::get_text(const struct _xmlAttr *ptr)
35 return get_text(ptr->children);
38 std::string mp_xml::get_text(const xmlNode *ptr)
41 if (ptr && ptr->type != XML_TEXT_NODE)
43 for (; ptr; ptr = ptr->next)
44 if (ptr->type == XML_TEXT_NODE)
45 c += std::string((const char *) (ptr->content));
49 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
51 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
53 if (!strcmp((const char *) ptr->content, "true"))
61 int mp_xml::get_int(const xmlNode *ptr, int default_value)
63 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
65 return atoi((const char *) ptr->content);
70 bool mp_xml::check_attribute(const _xmlAttr *ptr,
71 const std::string &ns,
72 const std::string &name)
75 if (!mp::xml::is_attribute(ptr, ns, name))
77 std::string got_attr = "'";
79 got_attr += std::string((const char *)ptr->name);
80 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
82 got_attr += std::string((const char *)ptr->ns->href);
86 throw mp::XMLError("Expected XML attribute '" + name
88 + ", not " + got_attr);
93 bool mp_xml::is_attribute(const _xmlAttr *ptr,
94 const std::string &ns,
95 const std::string &name)
97 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
101 && (!ptr->ns || !ptr->ns->href
102 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
109 bool mp_xml::is_element(const xmlNode *ptr,
110 const std::string &ns,
111 const std::string &name)
113 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
114 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
115 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
120 bool mp_xml::is_element_mp(const xmlNode *ptr,
121 const std::string &name)
123 return mp::xml::is_element(ptr, metaproxy_ns, name);
127 bool mp_xml::check_element_mp(const xmlNode *ptr,
128 const std::string &name)
130 if (!mp::xml::is_element_mp(ptr, name))
132 std::string got_element = "<";
133 if (ptr && ptr->name)
134 got_element += std::string((const char *)ptr->name);
135 if (ptr && ptr->ns && ptr->ns->href){
136 got_element += " xmlns=\"";
137 got_element += std::string((const char *)ptr->ns->href);
142 throw mp::XMLError("Expected XML element <" + name
143 + " xmlns=\"" + metaproxy_ns + "\">"
144 + ", not " + got_element);
149 std::string mp_xml::get_route(const xmlNode *node)
151 std::string route_value;
154 const struct _xmlAttr *attr;
155 for (attr = node->properties; attr; attr = attr->next)
157 std::string name = std::string((const char *) attr->name);
160 if (attr->children && attr->children->type == XML_TEXT_NODE)
161 value = std::string((const char *)attr->children->content);
166 throw XMLError("Only attribute route allowed"
167 " in " + std::string((const char *)node->name)
168 + " element. Got " + std::string(name));
175 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
178 node = node->children;
179 for (; node && node->type != xml_node_type; node = node->next)
184 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
188 for (; node && node->type != xml_node_type; node = node->next)
193 const xmlNode* mp_xml::jump_to(const xmlNode* node,
196 for (; node && node->type != xml_node_type; node = node->next)
201 void mp_xml::check_empty(const xmlNode *node)
206 for (n = node->children; n; n = n->next)
207 if (n->type == XML_ELEMENT_NODE)
208 throw mp::XMLError("No child elements allowed inside element "
209 + std::string((const char *) node->name));
216 * indent-tabs-mode: nil
217 * c-file-style: "stroustrup"
219 * vim: shiftwidth=4 tabstop=8 expandtab