changed config file root element from <yp2 ... > to
[metaproxy-moved-to-github.git] / src / xmlutil.cpp
1 /* $Id: xmlutil.cpp,v 1.10 2006-11-29 13:00:54 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include <string.h>
8 #include "xmlutil.hpp"
9
10 namespace mp = metaproxy_1;
11 // Doxygen doesn't like mp::xml, so we use this instead
12 namespace mp_xml = metaproxy_1::xml;
13
14 static const std::string metaproxy_ns = "http://indexdata.com/metaproxy";
15
16 std::string mp_xml::get_text(const xmlNode *ptr)
17 {
18     std::string c;
19     for (ptr = ptr->children; ptr; ptr = ptr->next)
20         if (ptr->type == XML_TEXT_NODE)
21             c += std::string((const char *) (ptr->content));
22     return c;
23 }
24
25 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
26 {
27     if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
28     {
29         if (!strcmp((const char *) ptr->content, "true"))
30             return true;
31         else
32             return false;
33     }
34     return default_value;
35 }
36
37 int mp_xml::get_int(const xmlNode *ptr, int default_value)
38 {
39     if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
40     {
41         return atoi((const char *) ptr->content);
42     }
43     return default_value;
44 }
45
46 bool mp_xml::is_element(const xmlNode *ptr, 
47                           const std::string &ns,
48                           const std::string &name)
49 {
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))
53         return true;
54     return false;
55 }
56
57 bool mp_xml::is_element_mp(const xmlNode *ptr, 
58                            const std::string &name)
59 {
60     return mp::xml::is_element(ptr, metaproxy_ns, name);
61 }
62
63
64 bool mp_xml::check_element_mp(const xmlNode *ptr, 
65                               const std::string &name)
66 {
67     if (!mp::xml::is_element_mp(ptr, name))
68     {
69         std::string got_element = "<";
70         if (ptr && ptr->name)
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);
75             got_element += "\"";
76         }
77         got_element += ">";
78
79         throw mp::XMLError("Expected XML element <" + name 
80                            + " xmlns=\"" + metaproxy_ns + "\">"
81                            + ", not " + got_element);
82     }
83     return true;
84 }
85
86 std::string mp_xml::get_route(const xmlNode *node)
87 {
88     std::string route_value;
89     if (node)
90     {
91         const struct _xmlAttr *attr;
92         for (attr = node->properties; attr; attr = attr->next)
93         {
94             std::string name = std::string((const char *) attr->name);
95             std::string value;
96             
97             if (attr->children && attr->children->type == XML_TEXT_NODE)
98                 value = std::string((const char *)attr->children->content);
99             
100             if (name == "route")
101                 route_value = value;
102             else
103                 throw XMLError("Only attribute route allowed"
104                                " in " + std::string((const char *)node->name)
105                                + " element. Got " + std::string(name));
106         }
107     }
108     return route_value;
109 }
110
111
112 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
113                                           int xml_node_type)
114 {
115     node = node->children;
116     for (; node && node->type != xml_node_type; node = node->next)
117         ;
118     return node;
119 }
120
121 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
122                                       int xml_node_type)
123 {
124     node = node->next;
125     for (; node && node->type != xml_node_type; node = node->next)
126         ;
127     return node;
128 }
129
130 const xmlNode* mp_xml::jump_to(const xmlNode* node,
131                                  int xml_node_type)
132 {
133     for (; node && node->type != xml_node_type; node = node->next)
134         ;
135     return node;
136 }
137
138 void mp_xml::check_empty(const xmlNode *node)
139 {
140     if (node)
141     {
142         const xmlNode *n;
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));
147     }
148 }
149
150 /*
151  * Local variables:
152  * c-basic-offset: 4
153  * indent-tabs-mode: nil
154  * c-file-style: "stroustrup"
155  * End:
156  * vim: shiftwidth=4 tabstop=8 expandtab
157  */