1 /* $Id: router_flexml.cpp,v 1.11 2006-01-09 13:43:59 adam Exp $
2 Copyright (c) 2005, Index Data.
9 #include "router_flexml.hpp"
10 #include "factory_filter.hpp"
11 #include "factory_static.hpp"
17 #include <boost/shared_ptr.hpp>
19 #include <libxml/xmlversion.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
25 class RouterFleXML::Route {
26 friend class RouterFleXML::Rep;
27 friend class RouterFleXML::Pos;
28 friend class RouterFleXML;
29 std::list<boost::shared_ptr<const yp2::filter::Base> > m_list;
31 class RouterFleXML::Rep {
32 friend class RouterFleXML;
33 friend class RouterFleXML::Pos;
36 void base(xmlDocPtr doc, yp2::FactoryFilter &factory);
38 typedef std::map<std::string,
39 boost::shared_ptr<const yp2::filter::Base > >
42 IdFilterMap m_id_filter_map;
44 std::map<std::string,RouterFleXML::Route> m_routes;
46 std::string m_start_route;
51 RouterFleXML::Route>::iterator m_cur_route_it;
53 std::list<boost::shared_ptr <const yp2::filter::Base> >::iterator m_cur_filter_it;
55 void parse_xml_config_dom(xmlDocPtr doc);
57 bool check_element_yp2(const xmlNode *ptr,
58 const std::string &name);
60 const xmlNode* jump_to(const xmlNode* node, int xml_node_type);
62 const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
64 const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
65 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
66 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
70 FactoryFilter *m_factory; // TODO shared_ptr
74 class RouterFleXML::Pos : public RoutePos {
76 virtual const filter::Base *move();
77 virtual RoutePos *clone();
79 yp2::RouterFleXML::Rep *m_p;
82 RouterFleXML::Route>::iterator m_route_it;
83 std::list<boost::shared_ptr <const yp2::filter::Base> >::iterator m_filter_it;
88 const xmlNode* yp2::RouterFleXML::Rep::jump_to_children(const xmlNode* node,
91 node = node->children;
92 for (; node && node->type != xml_node_type; node = node->next)
97 const xmlNode* yp2::RouterFleXML::Rep::jump_to_next(const xmlNode* node,
101 for (; node && node->type != xml_node_type; node = node->next)
106 const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node,
109 for (; node && node->type != xml_node_type; node = node->next)
114 bool yp2::RouterFleXML::Rep::check_element_yp2(const xmlNode *ptr,
115 const std::string &name)
117 if (!yp2::xml::is_element_yp2(ptr, name))
118 throw XMLError("Expected element name " + name);
123 void yp2::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
126 unsigned int filter_nr = 0;
127 while(node && check_element_yp2(node, "filter"))
131 const struct _xmlAttr *attr;
132 std::string id_value;
133 std::string type_value;
134 for (attr = node->properties; attr; attr = attr->next)
136 std::string name = std::string((const char *) attr->name);
139 if (attr->children && attr->children->type == XML_TEXT_NODE)
140 value = std::string((const char *)attr->children->content);
144 else if (name == "type")
147 throw XMLError("Only attribute id or type allowed"
148 " in filter element. Got " + name);
151 yp2::filter::Base* filter_base = m_factory->create(type_value);
153 filter_base->configure(node);
155 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
156 throw XMLError("Filter " + id_value + " already defined");
158 m_id_filter_map[id_value] =
159 boost::shared_ptr<yp2::filter::Base>(filter_base);
161 node = jump_to_next(node, XML_ELEMENT_NODE);
165 void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
168 check_element_yp2(node, "route");
170 unsigned int route_nr = 0;
171 while(yp2::xml::is_element_yp2(node, "route"))
175 const struct _xmlAttr *attr;
176 std::string id_value;
177 for (attr = node->properties; attr; attr = attr->next)
179 std::string name = std::string((const char *) attr->name);
182 if (attr->children && attr->children->type == XML_TEXT_NODE)
183 value = std::string((const char *)attr->children->content);
188 throw XMLError("Only attribute 'id' allowed for element"
195 // process <filter> nodes in third level
196 const xmlNode* node3 = jump_to_children(node, XML_ELEMENT_NODE);
198 unsigned int filter3_nr = 0;
199 while(node3 && check_element_yp2(node3, "filter"))
203 const struct _xmlAttr *attr;
204 std::string refid_value;
205 std::string type_value;
206 for (attr = node3->properties; attr; attr = attr->next)
208 std::string name = std::string((const char *) attr->name);
211 if (attr->children && attr->children->type == XML_TEXT_NODE)
212 value = std::string((const char *)attr->children->content);
216 else if (name == "type")
219 throw XMLError("Only attribute 'refid' or 'type'"
220 " allowed for element 'filter'."
223 if (refid_value.length())
225 std::map<std::string,
226 boost::shared_ptr<const yp2::filter::Base > >::iterator it;
227 it = m_id_filter_map.find(refid_value);
228 if (it == m_id_filter_map.end())
229 throw XMLError("Unknown filter refid " + refid_value);
231 route.m_list.push_back(it->second);
233 else if (type_value.length())
235 yp2::filter::Base* filter_base = m_factory->create(type_value);
237 filter_base->configure(node3);
239 route.m_list.push_back(
240 boost::shared_ptr<yp2::filter::Base>(filter_base));
242 node3 = jump_to_next(node3, XML_ELEMENT_NODE);
245 std::map<std::string,RouterFleXML::Route>::iterator it;
246 it = m_routes.find(id_value);
247 if (it != m_routes.end())
248 throw XMLError("Route id='" + id_value + "' already exist");
250 m_routes[id_value] = route;
251 node = jump_to_next(node, XML_ELEMENT_NODE);
255 void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
258 throw XMLError("Empty XML Document");
260 const xmlNode* root = xmlDocGetRootElement(doc);
262 check_element_yp2(root, "yp2");
264 // process <start> node which is expected first element node
265 const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
266 if (check_element_yp2(node, "start"))
268 const struct _xmlAttr *attr;
269 std::string id_value;
270 for (attr = node->properties; attr; attr = attr->next)
272 std::string name = std::string((const char *) attr->name);
275 if (attr->children && attr->children->type == XML_TEXT_NODE)
276 value = std::string((const char *)attr->children->content);
279 m_start_route = value;
281 throw XMLError("Only attribute start allowed"
282 " in element 'start'. Got " + name);
284 node = jump_to_next(node, XML_ELEMENT_NODE);
286 // process <filters> node which is expected second element node
287 check_element_yp2(node, "filters");
289 parse_xml_filters(doc, jump_to_children(node, XML_ELEMENT_NODE));
291 // process <routes> node which is expected third element node
292 node = jump_to_next(node, XML_ELEMENT_NODE);
293 check_element_yp2(node, "routes");
295 parse_xml_routes(doc, jump_to_children(node, XML_ELEMENT_NODE));
298 yp2::RouterFleXML::Rep::Rep() : m_xinclude(false)
302 void yp2::RouterFleXML::Rep::base(xmlDocPtr doc, yp2::FactoryFilter &factory)
304 m_factory = &factory;
305 parse_xml_config_dom(doc);
306 m_start_route = "start";
309 yp2::RouterFleXML::RouterFleXML(xmlDocPtr doc, yp2::FactoryFilter &factory)
312 m_p->base(doc, factory);
315 yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FactoryFilter &factory)
320 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
323 throw XMLError("xmlParseMemory failed");
326 m_p->base(doc, factory);
331 yp2::RouterFleXML::~RouterFleXML()
336 const yp2::filter::Base *yp2::RouterFleXML::Pos::move()
338 if (m_filter_it == m_route_it->second.m_list.end())
340 const yp2::filter::Base *f = (*m_filter_it).get();
345 yp2::RoutePos *yp2::RouterFleXML::createpos() const
347 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
349 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
350 if (p->m_route_it == m_p->m_routes.end())
355 p->m_filter_it = p->m_route_it->second.m_list.begin();
360 yp2::RoutePos *yp2::RouterFleXML::Pos::clone()
362 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
363 p->m_filter_it = m_filter_it;
364 p->m_route_it = m_route_it;
369 yp2::RouterFleXML::Pos::~Pos()
374 const yp2::filter::Base *
375 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
376 const yp2::Package *package) const
379 { // Initial move. find start route
380 m_p->m_cur_route_it = m_p->m_routes.find("start");
381 if (m_p->m_cur_route_it == m_p->m_routes.end())
383 m_p->m_cur_filter_it = m_p->m_cur_route_it->second.m_list.begin();
387 const yp2::filter::Base *f = (*m_p->m_cur_filter_it).get();
389 (m_p->m_cur_filter_it)++;
392 // TOTO: should search all routes (not only start)!
393 m_p->m_cur_filter_it = m_p->m_cur_route_it->second.m_list.begin();
394 while (m_p->m_cur_filter_it !=
395 m_p->m_cur_route_it->second.m_list.end())
397 const yp2::filter::Base *f = (*m_p->m_cur_filter_it).get();
398 (m_p->m_cur_filter_it)++;
404 if (m_p->m_cur_filter_it == m_p->m_cur_route_it->second.m_list.end())
406 return (*m_p->m_cur_filter_it).get();
413 * indent-tabs-mode: nil
414 * c-file-style: "stroustrup"
416 * vim: shiftwidth=4 tabstop=8 expandtab