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