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