e66b56c6148d44c4a96a1dfe71a5888f5c678821
[metaproxy-moved-to-github.git] / src / router_flexml.cpp
1 /* $Id: router_flexml.cpp,v 1.5 2005-12-08 15:34:08 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         bool m_xinclude;
31         IdFilterMap m_id_filter_map;
32         FilterIdList m_filter_id_list;
33         IdRouteMap m_id_route_map;
34         void xml_dom_error (const xmlNode* node, std::string msg);
35
36         void create_filter(std::string type, 
37                            const xmlDoc * xmldoc,
38                            std::string id = "");
39
40         void parse_xml_config_dom(xmlDocPtr doc);
41         
42         const xmlNode* jump_to(const xmlNode* node, int xml_node_type);
43
44         const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
45         
46         const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
47         void check_node_name(const xmlNode* node, std::string name);
48     };
49 }
50
51 void yp2::RouterFleXML::Rep::check_node_name(const xmlNode* node, std::string name)
52 {
53     if (std::string((const char *)node->name) 
54         !=  name)
55         xml_dom_error(node, "expected  <" + name + ">, got ");
56 }
57
58 const xmlNode* yp2::RouterFleXML::Rep::jump_to_children(const xmlNode* node, int xml_node_type)
59 {
60     node = node->children;
61     for (; node && node->type != xml_node_type; node = node->next)
62         ;
63     return node;
64 }
65         
66 const xmlNode* yp2::RouterFleXML::Rep::jump_to_next(const xmlNode* node, int xml_node_type)
67 {
68     node = node->next;
69     for (; node && node->type != xml_node_type; node = node->next)
70         ;
71     return node;
72 }
73         
74 const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node, int xml_node_type)
75 {
76     for (; node && node->type != xml_node_type; node = node->next)
77         ;
78     return node;
79 }
80
81 void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
82 {
83     if (!doc)
84     {
85         std::cerr << "XML configuration DOM pointer empty" << std::endl;
86         return;
87     }
88     
89     const xmlNode* root = xmlDocGetRootElement(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 yp2::RouterFleXML::Rep::Rep() : 
188     m_xinclude(false)
189 {
190 }
191
192 yp2::RouterFleXML::RouterFleXML(std::string xmlconf) 
193     : m_p(new Rep)
194 {            
195     LIBXML_TEST_VERSION;
196     
197     xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
198                                    xmlconf.size());
199     if (!doc)
200         throw XMLError("xmlParseMemory failed");
201     else
202     {
203         m_p->parse_xml_config_dom(doc);
204         xmlFreeDoc(doc);
205     }
206 }
207
208 yp2::RouterFleXML::~RouterFleXML()
209 {
210 }
211
212 const yp2::filter::Base *
213 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
214                         const yp2::Package *package) const 
215 {
216     return 0;
217 }
218         
219
220 /*
221  * Local variables:
222  * c-basic-offset: 4
223  * indent-tabs-mode: nil
224  * c-file-style: "stroustrup"
225  * End:
226  * vim: shiftwidth=4 tabstop=8 expandtab
227  */