Implement basic configuration of session_shared.
[metaproxy-moved-to-github.git] / src / xmlutil.cpp
1 /* $Id: xmlutil.cpp,v 1.9 2006-06-21 09:16:54 adam 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 std::string mp_xml::get_text(const xmlNode *ptr)
15 {
16     std::string c;
17     for (ptr = ptr->children; ptr; ptr = ptr->next)
18         if (ptr->type == XML_TEXT_NODE)
19             c += std::string((const char *) (ptr->content));
20     return c;
21 }
22
23 bool mp_xml::get_bool(const xmlNode *ptr, bool default_value)
24 {
25     if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
26     {
27         if (!strcmp((const char *) ptr->content, "true"))
28             return true;
29         else
30             return false;
31     }
32     return default_value;
33 }
34
35 int mp_xml::get_int(const xmlNode *ptr, int default_value)
36 {
37     if (ptr && ptr->type == XML_TEXT_NODE && ptr->content)
38     {
39         return atoi((const char *) ptr->content);
40     }
41     return default_value;
42 }
43
44 bool mp_xml::is_element(const xmlNode *ptr, 
45                           const std::string &ns,
46                           const std::string &name)
47 {
48     if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href 
49         && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
50         && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
51         return true;
52     return false;
53 }
54
55 bool mp_xml::is_element_yp2(const xmlNode *ptr, 
56                               const std::string &name)
57 {
58     return mp::xml::is_element(ptr, "http://indexdata.dk/yp2/config/1", name);
59 }
60
61
62 bool mp_xml::check_element_yp2(const xmlNode *ptr, 
63                                  const std::string &name)
64 {
65     if (!mp::xml::is_element_yp2(ptr, name))
66         throw mp::XMLError("Expected element name " + name);
67     return true;
68 }
69
70 std::string mp_xml::get_route(const xmlNode *node)
71 {
72     std::string route_value;
73     if (node)
74     {
75         const struct _xmlAttr *attr;
76         for (attr = node->properties; attr; attr = attr->next)
77         {
78             std::string name = std::string((const char *) attr->name);
79             std::string value;
80             
81             if (attr->children && attr->children->type == XML_TEXT_NODE)
82                 value = std::string((const char *)attr->children->content);
83             
84             if (name == "route")
85                 route_value = value;
86             else
87                 throw XMLError("Only attribute route allowed"
88                                " in " + std::string((const char *)node->name)
89                                + " element. Got " + std::string(name));
90         }
91     }
92     return route_value;
93 }
94
95
96 const xmlNode* mp_xml::jump_to_children(const xmlNode* node,
97                                           int xml_node_type)
98 {
99     node = node->children;
100     for (; node && node->type != xml_node_type; node = node->next)
101         ;
102     return node;
103 }
104
105 const xmlNode* mp_xml::jump_to_next(const xmlNode* node,
106                                       int xml_node_type)
107 {
108     node = node->next;
109     for (; node && node->type != xml_node_type; node = node->next)
110         ;
111     return node;
112 }
113
114 const xmlNode* mp_xml::jump_to(const xmlNode* node,
115                                  int xml_node_type)
116 {
117     for (; node && node->type != xml_node_type; node = node->next)
118         ;
119     return node;
120 }
121
122 void mp_xml::check_empty(const xmlNode *node)
123 {
124     if (node)
125     {
126         const xmlNode *n;
127         for (n = node->children; n; n = n->next)
128             if (n->type == XML_ELEMENT_NODE)
129                 throw mp::XMLError("No child elements allowed inside element "
130                                     + std::string((const char *) node->name));
131     }
132 }
133
134 /*
135  * Local variables:
136  * c-basic-offset: 4
137  * indent-tabs-mode: nil
138  * c-file-style: "stroustrup"
139  * End:
140  * vim: shiftwidth=4 tabstop=8 expandtab
141  */