a4219301bb716c8d3873a475fec396fce4e58353
[metaproxy-moved-to-github.git] / src / router_flexml.cpp
1 /* $Id: router_flexml.cpp,v 1.4 2005-12-08 15:10:34 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include "router_flexml.hpp"
9
10 #include <iostream>
11 #include <map>
12 #include <list>
13
14 #include <boost/shared_ptr.hpp>
15
16 #include <libxml/xmlversion.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
19
20 namespace yp2 {
21     class RouterFleXML::Rep {
22         friend class RouterFleXML;
23         Rep();
24
25         typedef std::map<std::string, boost::shared_ptr<const yp2::filter::Base> >
26                 IdFilterMap ;
27         typedef std::list<std::string> FilterIdList;
28         typedef std::map<std::string, FilterIdList > IdRouteMap ;
29
30         std::string m_xmlconf;
31         bool m_xinclude;
32         xmlDoc * m_xmlconf_doc;
33         IdFilterMap m_id_filter_map;
34         FilterIdList m_filter_id_list;
35         IdRouteMap m_id_route_map;
36         void xml_dom_error (const xmlNode* node, std::string msg);
37
38         void create_filter(std::string type, 
39                            const xmlDoc * xmldoc,
40                            std::string id = "");
41
42         void parse_xml_config_dom();
43         
44         const xmlNode* jump_to(const xmlNode* node, int xml_node_type);
45
46         const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
47         
48         const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
49         void check_node_name(const xmlNode* node, std::string name);
50     };
51 }
52
53 void yp2::RouterFleXML::Rep::check_node_name(const xmlNode* node, std::string name)
54 {
55     if (std::string((const char *)node->name) 
56         !=  name)
57         xml_dom_error(node, "expected  <" + name + ">, got ");
58 }
59
60 const xmlNode* yp2::RouterFleXML::Rep::jump_to_children(const xmlNode* node, int xml_node_type)
61 {
62     node = node->children;
63     for (; node && node->type != xml_node_type; node = node->next)
64         ;
65     return node;
66 }
67         
68 const xmlNode* yp2::RouterFleXML::Rep::jump_to_next(const xmlNode* node, int xml_node_type)
69 {
70     node = node->next;
71     for (; node && node->type != xml_node_type; node = node->next)
72         ;
73     return node;
74 }
75         
76 const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node, int xml_node_type)
77 {
78     for (; node && node->type != xml_node_type; node = node->next)
79         ;
80     return node;
81 }
82
83 void yp2::RouterFleXML::Rep::parse_xml_config_dom()
84 {
85     if (!m_xmlconf_doc){    
86         std::cerr << "XML configuration DOM pointer empty" << std::endl;
87     }
88     
89     const xmlNode* root = xmlDocGetRootElement(m_xmlconf_doc);
90     
91     if ((std::string((const char *) root->name) != "yp2")
92         || (std::string((const char *)(root->ns->href)) 
93             != "http://indexdata.dk/yp2/config/1")
94         )
95         xml_dom_error(root, 
96                       "expected <yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">, got ");
97     
98     
99     for (const struct _xmlAttr *attr = root->properties; attr; attr = attr->next)
100     {
101         if (std::string((const char *)attr->name) == "xmlns")
102         {
103             const xmlNode *val = attr->children;
104             if (std::string((const char *)val->content) 
105                 !=  "http://indexdata.dk/yp2/config/1")
106                 xml_dom_error(root, 
107                               "expected  xmlns=\"http://indexdata.dk/yp2/config/1\", got ");
108         }  
109     }
110     std::cout << "processing /yp2" << std::endl;
111     
112     // process <start> node which is expected first element node
113     const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
114     //for (; node && node->type != XML_ELEMENT_NODE; node = node->next)
115     //    ;
116     
117     check_node_name(node, "start");
118     std::cout << "processing /yp2/start" << std::endl;
119     
120     // process <filters> node which is expected second element node
121     node = jump_to_next(node, XML_ELEMENT_NODE);
122     check_node_name(node, "filters");
123     std::cout << "processing /yp2/filters" << std::endl;
124     
125     // process <filter> nodes  in next level
126     const xmlNode* node2 = jump_to_children(node, XML_ELEMENT_NODE);
127     check_node_name(node2, "filter");
128     
129     unsigned int filter_nr = 0;
130     while(node2 && std::string((const char *)node2->name) ==  "filter"){
131         filter_nr++;
132         std::cout << "processing /yp2/filters/filter[" 
133                   << filter_nr << "]" << std::endl;
134         node2 = jump_to_next(node2, XML_ELEMENT_NODE);
135     }
136     
137     // process <routes> node which is expected third element node
138     node = jump_to_next(node, XML_ELEMENT_NODE);
139     check_node_name(node, "routes");
140     std::cout << "processing /yp2/routes" << std::endl;
141     
142     // process <route> nodes  in next level
143     node2 = jump_to_children(node, XML_ELEMENT_NODE);
144     check_node_name(node2, "route");
145     
146     unsigned int route_nr = 0;
147     while(node2 && std::string((const char *)node2->name) ==  "route"){
148         route_nr++;
149         std::cout << "processing /yp2/routes/route[" 
150                   << route_nr << "]" << std::endl;
151         
152         // process <filter> nodes in third level
153         const xmlNode* node3 
154             = jump_to_children(node2, XML_ELEMENT_NODE);
155         check_node_name(node3, "filter");
156         
157         unsigned int filter3_nr = 0;
158         while(node3 && std::string((const char *)node3->name) ==  "filter"){
159             filter3_nr++;
160             
161             std::cout << "processing /yp2/routes/route[" 
162                       << route_nr << "]/filter[" 
163                       << filter3_nr << "]" << std::endl;
164             
165             node3 = jump_to_next(node3, XML_ELEMENT_NODE);
166             
167         }
168         node2 = jump_to_next(node2, XML_ELEMENT_NODE);
169     }
170 }        
171
172 void yp2::RouterFleXML::Rep::create_filter(std::string type, 
173                                            const xmlDoc * xmldoc,
174                                            std::string id)
175 {
176     std::cout << "Created Filter type='" << type 
177               << "' id='" << id << "'" << std::endl;
178 }
179
180 void yp2::RouterFleXML::Rep::xml_dom_error (const xmlNode* node, std::string msg)
181 {
182     std::cerr << "ERROR: " << msg << " <"
183               << node->name << ">"
184               << std::endl;
185 }
186
187
188 yp2::RouterFleXML::Rep::Rep() : 
189     m_xmlconf(""), m_xinclude(false), m_xmlconf_doc(0)
190 {
191 }
192
193 yp2::RouterFleXML::RouterFleXML(std::string xmlconf) 
194     : m_p(new Rep)
195 {            
196     LIBXML_TEST_VERSION;
197     
198     m_p->m_xmlconf = xmlconf;
199     
200     m_p->m_xmlconf_doc = xmlParseMemory(m_p->m_xmlconf.c_str(),
201                                         m_p->m_xmlconf.size());
202     m_p->parse_xml_config_dom();
203 }
204
205 yp2::RouterFleXML::~RouterFleXML()
206 {
207     xmlFreeDoc(m_p->m_xmlconf_doc);
208 }
209
210 const yp2::filter::Base *
211 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
212                         const yp2::Package *package) const 
213 {
214     return 0;
215 }
216         
217
218 /*
219  * Local variables:
220  * c-basic-offset: 4
221  * indent-tabs-mode: nil
222  * c-file-style: "stroustrup"
223  * End:
224  * vim: shiftwidth=4 tabstop=8 expandtab
225  */