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