b6bafa9249f2c7e077d7ed3ef84be420ac987ab9
[metaproxy-moved-to-github.git] / src / router_flexml.cpp
1 /* $Id: router_flexml.cpp,v 1.19 2006-11-29 13:00:54 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8 #include "xmlutil.hpp"
9 #include "router_flexml.hpp"
10 #include "factory_filter.hpp"
11 #include "factory_static.hpp"
12
13 #include <iostream>
14 #include <map>
15 #include <list>
16
17 #include <boost/shared_ptr.hpp>
18
19 #include <libxml/xmlversion.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
22
23 namespace mp = metaproxy_1;
24
25 namespace metaproxy_1 {
26     class RouterFleXML::Route {
27         friend class RouterFleXML::Rep;
28         friend class RouterFleXML::Pos;
29         friend class RouterFleXML;
30         std::list<boost::shared_ptr<const mp::filter::Base> > m_list;
31     };
32     class RouterFleXML::Rep {
33         friend class RouterFleXML;
34         friend class RouterFleXML::Pos;
35         Rep();
36
37         void base(xmlDocPtr doc, mp::FactoryFilter &factory);
38
39         typedef std::map<std::string,
40                          boost::shared_ptr<const mp::filter::Base > >
41                          IdFilterMap ;
42
43         IdFilterMap m_id_filter_map;
44
45         std::map<std::string,RouterFleXML::Route> m_routes;
46
47         std::string m_start_route;
48
49         std::string m_dl_path;
50
51         void parse_xml_config_dom(xmlDocPtr doc);
52
53         void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
54         void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
55
56         bool m_xinclude;
57     private:
58         FactoryFilter *m_factory; // TODO shared_ptr
59     };
60
61     class RouterFleXML::Pos : public RoutePos {
62     public:
63         virtual const filter::Base *move(const char *route);
64         virtual RoutePos *clone();
65         virtual ~Pos();
66         mp::RouterFleXML::Rep *m_p;
67
68         std::map<std::string, 
69                  RouterFleXML::Route>::iterator m_route_it;
70         std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
71     };
72 }
73
74 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
75                                                const xmlNode *node)
76 {
77     unsigned int filter_nr = 0;
78     while(node && mp::xml::check_element_mp(node, "filter"))
79     {
80         filter_nr++;
81
82         const struct _xmlAttr *attr;
83         std::string id_value;
84         std::string type_value;
85         for (attr = node->properties; attr; attr = attr->next)
86         {
87             std::string name = std::string((const char *) attr->name);
88             std::string value;
89
90             if (attr->children && attr->children->type == XML_TEXT_NODE)
91                 value = std::string((const char *)attr->children->content);
92
93             if (name == "id")
94                 id_value = value;
95             else if (name == "type")
96                 type_value = value;
97             else
98                 throw mp::XMLError("Only attribute id or type allowed"
99                                     " in filter element. Got " + name);
100         }
101
102         if (!m_factory->exist(type_value))
103         {
104             std::cout << "about to load " << type_value << ", path=" << 
105                 m_dl_path << "\n";
106             m_factory->add_creator_dl(type_value, m_dl_path);
107         }
108         mp::filter::Base* filter_base = m_factory->create(type_value);
109
110         filter_base->configure(node);
111
112         if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
113             throw mp::XMLError("Filter " + id_value + " already defined");
114
115         m_id_filter_map[id_value] =
116             boost::shared_ptr<mp::filter::Base>(filter_base);
117
118         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
119     }
120 }
121
122 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
123                                               const xmlNode *node)
124 {
125     mp::xml::check_element_mp(node, "route");
126
127     unsigned int route_nr = 0;
128     while(mp::xml::is_element_mp(node, "route"))
129     {
130         route_nr++;
131
132         const struct _xmlAttr *attr;
133         std::string id_value;
134         for (attr = node->properties; attr; attr = attr->next)
135         {
136             std::string name = std::string((const char *) attr->name);
137             std::string value;
138             
139             if (attr->children && attr->children->type == XML_TEXT_NODE)
140                 value = std::string((const char *)attr->children->content);
141             
142             if (name == "id")
143                 id_value = value;
144             else
145                 throw mp::XMLError("Only attribute 'id' allowed for"
146                                     " element 'route'."
147                                     " Got " + name);
148         }
149
150         Route route;
151
152         // process <filter> nodes in third level
153         const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
154
155         unsigned int filter3_nr = 0;
156         while(node3 && mp::xml::check_element_mp(node3, "filter"))
157         {
158             filter3_nr++;
159             
160             const struct _xmlAttr *attr;
161             std::string refid_value;
162             std::string type_value;
163             for (attr = node3->properties; attr; attr = attr->next)
164             {
165                 std::string name = std::string((const char *) attr->name);
166                 std::string value;
167                 
168                 if (attr->children && attr->children->type == XML_TEXT_NODE)
169                     value = std::string((const char *)attr->children->content);
170                 
171                 if (name == "refid")
172                     refid_value = value;
173                 else if (name == "type")
174                     type_value = value;
175                 else
176                     throw mp::XMLError("Only attribute 'refid' or 'type'"
177                                         " allowed for element 'filter'."
178                                         " Got " + name);
179             }
180             if (refid_value.length())
181             {
182                 std::map<std::string,
183                     boost::shared_ptr<const mp::filter::Base > >::iterator it;
184                 it = m_id_filter_map.find(refid_value);
185                 if (it == m_id_filter_map.end())
186                     throw mp::XMLError("Unknown filter refid "
187                                         + refid_value);
188                 else
189                     route.m_list.push_back(it->second);
190             }
191             else if (type_value.length())
192             {
193                 if (!m_factory->exist(type_value))
194                 {
195                     std::cout << "about to load " << type_value << ", path=" << 
196                         m_dl_path << "\n";
197                     m_factory->add_creator_dl(type_value, m_dl_path);
198                 }
199                 mp::filter::Base* filter_base = m_factory->create(type_value);
200
201                 filter_base->configure(node3);
202                 
203                 route.m_list.push_back(
204                     boost::shared_ptr<mp::filter::Base>(filter_base));
205             }
206             node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
207             
208         }
209         std::map<std::string,RouterFleXML::Route>::iterator it;
210         it = m_routes.find(id_value);
211         if (it != m_routes.end())
212             throw mp::XMLError("Route id='" + id_value
213                                 + "' already exist");
214         else
215             m_routes[id_value] = route;
216         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
217     }
218 }
219
220 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
221 {
222     if (!doc)
223         throw mp::XMLError("Empty XML Document");
224     
225     const xmlNode* root = xmlDocGetRootElement(doc);
226     
227     mp::xml::check_element_mp(root,  "metaproxy");
228
229     const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
230
231     if (mp::xml::is_element_mp(node, "dlpath"))
232     {
233         m_dl_path = mp::xml::get_text(node);
234         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
235     }
236     // process <start> node which is expected first element node
237     if (mp::xml::check_element_mp(node, "start"))
238     {
239         const struct _xmlAttr *attr;
240         std::string id_value;
241         for (attr = node->properties; attr; attr = attr->next)
242         {
243             std::string name = std::string((const char *) attr->name);
244             std::string value;
245
246             if (attr->children && attr->children->type == XML_TEXT_NODE)
247                 value = std::string((const char *)attr->children->content);
248
249             if (name == "route")
250                 m_start_route = value;
251             else
252                 throw mp::XMLError("Only attribute start allowed"
253                                     " in element 'start'. Got " + name);
254         }
255         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
256     }
257     // process <filters> node if given
258     if (mp::xml::is_element_mp(node, "filters"))
259     {
260         parse_xml_filters(doc, mp::xml::jump_to_children(node,
261                                                           XML_ELEMENT_NODE));
262                       
263         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
264     }
265     // process <routes> node which is expected third element node
266     mp::xml::check_element_mp(node, "routes");
267     
268     parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE));
269
270     node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
271     if (node)
272     {
273         throw mp::XMLError("Unexpected element " 
274                             + std::string((const char *)node->name));
275     }
276 }        
277
278 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
279 {
280 }
281
282 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory)
283 {
284     m_factory = &factory;
285     parse_xml_config_dom(doc);
286     m_start_route = "start";
287 }
288
289 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory)
290     : m_p(new Rep)
291 {
292     m_p->base(doc, factory);
293 }
294
295 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory) 
296     : m_p(new Rep)
297 {            
298     LIBXML_TEST_VERSION;
299     
300     xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
301                                    xmlconf.size());
302     if (!doc)
303         throw mp::XMLError("xmlParseMemory failed");
304     else
305     {
306         m_p->base(doc, factory);
307         xmlFreeDoc(doc);
308     }
309 }
310
311 mp::RouterFleXML::~RouterFleXML()
312 {
313 }
314
315 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
316 {
317     if (route && *route)
318     {
319         std::cout << "move to " << route << "\n";
320         m_route_it = m_p->m_routes.find(route);
321         if (m_route_it == m_p->m_routes.end())
322         {
323             std::cout << "no such route " << route << "\n";
324             throw mp::XMLError("bad route " + std::string(route));
325         }
326         m_filter_it = m_route_it->second.m_list.begin();
327     }
328     if (m_filter_it == m_route_it->second.m_list.end())
329         return 0;
330     const mp::filter::Base *f = (*m_filter_it).get();
331     m_filter_it++;
332     return f;
333 }
334
335 mp::RoutePos *mp::RouterFleXML::createpos() const
336 {
337     mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
338
339     p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
340     if (p->m_route_it == m_p->m_routes.end())
341     {
342         delete p;
343         return 0;
344     }
345     p->m_filter_it = p->m_route_it->second.m_list.begin();
346     p->m_p = m_p.get();
347     return p;
348 }
349
350 mp::RoutePos *mp::RouterFleXML::Pos::clone()
351 {
352     mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
353     p->m_filter_it = m_filter_it;
354     p->m_route_it = m_route_it;
355     p->m_p = m_p;
356     return p;
357 }
358
359 mp::RouterFleXML::Pos::~Pos()
360 {
361 }
362
363
364 /*
365  * Local variables:
366  * c-basic-offset: 4
367  * indent-tabs-mode: nil
368  * c-file-style: "stroustrup"
369  * End:
370  * vim: shiftwidth=4 tabstop=8 expandtab
371  */