1 /* $Id: xmlutil.cpp,v 1.13 2007-01-25 14:05:54 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
12 namespace mp = metaproxy_1;
13 // Doxygen doesn't like mp::xml, so we use this instead
14 namespace mp_xml = metaproxy_1::xml;
16 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
18 std::string mp_xml::get_text(const struct _xmlAttr *ptr)
20 return get_text(ptr->children);
23 std::string mp_xml::get_text(const xmlNode *ptr)
26 if (ptr && ptr->type != XML_TEXT_NODE)
28 for (; ptr; ptr = ptr->next)
29 if (ptr->type == XML_TEXT_NODE)
30 c += std::string((const char *) (ptr->content));
34 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
36 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
38 if (!strcmp((const char *) ptr->content, "true"))
46 int mp_xml::get_int(const xmlNode *ptr, int default_value)
48 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
50 return atoi((const char *) ptr->content);
55 bool mp_xml::check_attribute(const _xmlAttr *ptr,
56 const std::string &ns,
57 const std::string &name)
60 if (!mp::xml::is_attribute(ptr, ns, name))
62 std::string got_attr = "'";
64 got_attr += std::string((const char *)ptr->name);
65 if (ns.size() && ptr && ptr->ns && ptr->ns->href){
67 got_attr += std::string((const char *)ptr->ns->href);
71 throw mp::XMLError("Expected XML attribute '" + name
73 + ", not " + got_attr);
78 bool mp_xml::is_attribute(const _xmlAttr *ptr,
79 const std::string &ns,
80 const std::string &name)
82 if (0 != xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
86 && (!ptr->ns || !ptr->ns->href
87 || 0 != xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)))
94 bool mp_xml::is_element(const xmlNode *ptr,
95 const std::string &ns,
96 const std::string &name)
98 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
99 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
100 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
105 bool mp_xml::is_element_mp(const xmlNode *ptr,
106 const std::string &name)
108 return mp::xml::is_element(ptr, metaproxy_ns, name);
112 bool mp_xml::check_element_mp(const xmlNode *ptr,
113 const std::string &name)
115 if (!mp::xml::is_element_mp(ptr, name))
117 std::string got_element = "<";
118 if (ptr && ptr->name)
119 got_element += std::string((const char *)ptr->name);
120 if (ptr && ptr->ns && ptr->ns->href){
121 got_element += " xmlns=\"";
122 got_element += std::string((const char *)ptr->ns->href);
127 throw mp::XMLError("Expected XML element <" + name
128 + " xmlns=\"" + metaproxy_ns + "\">"
129 + ", not " + got_element);
134 std::string mp_xml::get_route(const xmlNode *node)
136 std::string route_value;
139 const struct _xmlAttr *attr;
140 for (attr = node->properties; attr; attr = attr->next)
142 std::string name = std::string((const char *) attr->name);
145 if (attr->children && attr->children->type == XML_TEXT_NODE)
146 value = std::string((const char *)attr->children->content);
151 throw XMLError("Only attribute route allowed"
152 " in " + std::string((const char *)node->name)
153 + " element. Got " + std::string(name));
160 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
163 node = node->children;
164 for (; node && node->type != xml_node_type; node = node->next)
169 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
173 for (; node && node->type != xml_node_type; node = node->next)
178 const xmlNode* mp_xml::jump_to(const xmlNode* node,
181 for (; node && node->type != xml_node_type; node = node->next)
186 void mp_xml::check_empty(const xmlNode *node)
191 for (n = node->children; n; n = n->next)
192 if (n->type == XML_ELEMENT_NODE)
193 throw mp::XMLError("No child elements allowed inside element "
194 + std::string((const char *) node->name));
201 * indent-tabs-mode: nil
202 * c-file-style: "stroustrup"
204 * vim: shiftwidth=4 tabstop=8 expandtab