Implemented yp2::xml::check_empty that serves as default
[metaproxy-moved-to-github.git] / src / xmlutil.cpp
1 /* $Id: xmlutil.cpp,v 1.4 2006-01-11 14:58:28 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "xmlutil.hpp"
8
9 std::string yp2::xml::get_text(const xmlNode *ptr)
10 {
11     std::string c;
12     for (ptr = ptr->children; ptr; ptr = ptr->next)
13         if (ptr->type == XML_TEXT_NODE)
14             c += std::string((const char *) (ptr->content));
15     return c;
16 }
17
18
19 bool yp2::xml::is_element(const xmlNode *ptr, 
20                           const std::string &ns,
21                           const std::string &name)
22 {
23     if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href 
24         && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
25         && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
26         return true;
27     return false;
28 }
29
30 bool yp2::xml::is_element_yp2(const xmlNode *ptr, 
31                               const std::string &name)
32 {
33     return yp2::xml::is_element(ptr, "http://indexdata.dk/yp2/config/1", name);
34 }
35
36
37 bool yp2::xml::check_element_yp2(const xmlNode *ptr, 
38                                  const std::string &name)
39 {
40     if (!yp2::xml::is_element_yp2(ptr, name))
41         throw yp2::XMLError("Expected element name " + name);
42     return true;
43 }
44
45 std::string yp2::xml::get_route(const xmlNode *node)
46 {
47     std::string route_value;
48     if (node)
49     {
50         const struct _xmlAttr *attr;
51         for (attr = node->properties; attr; attr = attr->next)
52         {
53             std::string name = std::string((const char *) attr->name);
54             std::string value;
55             
56             if (attr->children && attr->children->type == XML_TEXT_NODE)
57                 value = std::string((const char *)attr->children->content);
58             
59             if (name == "route")
60                 route_value = value;
61             else
62                 throw XMLError("Only attribute route allowed"
63                                " in " + std::string((const char *)node->name)
64                                + " element. Got " + std::string(name));
65         }
66     }
67     return route_value;
68 }
69
70
71 const xmlNode* yp2::xml::jump_to_children(const xmlNode* node,
72                                           int xml_node_type)
73 {
74     node = node->children;
75     for (; node && node->type != xml_node_type; node = node->next)
76         ;
77     return node;
78 }
79
80 const xmlNode* yp2::xml::jump_to_next(const xmlNode* node,
81                                       int xml_node_type)
82 {
83     node = node->next;
84     for (; node && node->type != xml_node_type; node = node->next)
85         ;
86     return node;
87 }
88
89 const xmlNode* yp2::xml::jump_to(const xmlNode* node,
90                                  int xml_node_type)
91 {
92     for (; node && node->type != xml_node_type; node = node->next)
93         ;
94     return node;
95 }
96
97 void yp2::xml::check_empty(const xmlNode *node)
98 {
99     if (node)
100     {
101         const xmlNode *n;
102         for (n = node->children; n; n = n->next)
103             if (n->type == XML_ELEMENT_NODE)
104                 throw yp2::XMLError("No child elements allowed inside element "
105                                     + std::string((const char *) node->name));
106     }
107 }
108
109 /*
110  * Local variables:
111  * c-basic-offset: 4
112  * indent-tabs-mode: nil
113  * c-file-style: "stroustrup"
114  * End:
115  * vim: shiftwidth=4 tabstop=8 expandtab
116  */