1 /* $Id: xmlutil.cpp,v 1.10 2006-11-29 13:00:54 marc Exp $
2 Copyright (c) 2005-2006, Index Data.
4 See the LICENSE file for details
10 namespace mp = metaproxy_1;
11 // Doxygen doesn't like mp::xml, so we use this instead
12 namespace mp_xml = metaproxy_1::xml;
14 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
16 std::string mp_xml::get_text(const xmlNode *ptr)
19 for (ptr = ptr->children; ptr; ptr = ptr->next)
20 if (ptr->type == XML_TEXT_NODE)
21 c += std::string((const char *) (ptr->content));
25 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
27 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
29 if (!strcmp((const char *) ptr->content, "true"))
37 int mp_xml::get_int(const xmlNode *ptr, int default_value)
39 if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
41 return atoi((const char *) ptr->content);
46 bool mp_xml::is_element(const xmlNode *ptr,
47 const std::string &ns,
48 const std::string &name)
50 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
51 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
52 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
57 bool mp_xml::is_element_mp(const xmlNode *ptr,
58 const std::string &name)
60 return mp::xml::is_element(ptr, metaproxy_ns, name);
64 bool mp_xml::check_element_mp(const xmlNode *ptr,
65 const std::string &name)
67 if (!mp::xml::is_element_mp(ptr, name))
69 std::string got_element = "<";
71 got_element += std::string((const char *)ptr->name);
72 if (ptr && ptr->ns && ptr->ns->href){
73 got_element += " xmlns=\"";
74 got_element += std::string((const char *)ptr->ns->href);
79 throw mp::XMLError("Expected XML element <" + name
80 + " xmlns=\"" + metaproxy_ns + "\">"
81 + ", not " + got_element);
86 std::string mp_xml::get_route(const xmlNode *node)
88 std::string route_value;
91 const struct _xmlAttr *attr;
92 for (attr = node->properties; attr; attr = attr->next)
94 std::string name = std::string((const char *) attr->name);
97 if (attr->children && attr->children->type == XML_TEXT_NODE)
98 value = std::string((const char *)attr->children->content);
103 throw XMLError("Only attribute route allowed"
104 " in " + std::string((const char *)node->name)
105 + " element. Got " + std::string(name));
112 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
115 node = node->children;
116 for (; node && node->type != xml_node_type; node = node->next)
121 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
125 for (; node && node->type != xml_node_type; node = node->next)
130 const xmlNode* mp_xml::jump_to(const xmlNode* node,
133 for (; node && node->type != xml_node_type; node = node->next)
138 void mp_xml::check_empty(const xmlNode *node)
143 for (n = node->children; n; n = n->next)
144 if (n->type == XML_ELEMENT_NODE)
145 throw mp::XMLError("No child elements allowed inside element "
146 + std::string((const char *) node->name));
153 * indent-tabs-mode: nil
154 * c-file-style: "stroustrup"
156 * vim: shiftwidth=4 tabstop=8 expandtab