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