1 /* $Id: router_flexml.cpp,v 1.14 2006-01-11 13:13:49 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;
48 void parse_xml_config_dom(xmlDocPtr doc);
50 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
51 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
55 FactoryFilter *m_factory; // TODO shared_ptr
58 class RouterFleXML::Pos : public RoutePos {
60 virtual const filter::Base *move(const char *route);
61 virtual RoutePos *clone();
63 yp2::RouterFleXML::Rep *m_p;
66 RouterFleXML::Route>::iterator m_route_it;
67 std::list<boost::shared_ptr <const yp2::filter::Base> >::iterator m_filter_it;
71 void yp2::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
74 unsigned int filter_nr = 0;
75 while(node && yp2::xml::check_element_yp2(node, "filter"))
79 const struct _xmlAttr *attr;
81 std::string type_value;
82 for (attr = node->properties; attr; attr = attr->next)
84 std::string name = std::string((const char *) attr->name);
87 if (attr->children && attr->children->type == XML_TEXT_NODE)
88 value = std::string((const char *)attr->children->content);
92 else if (name == "type")
95 throw yp2::XMLError("Only attribute id or type allowed"
96 " in filter element. Got " + name);
99 yp2::filter::Base* filter_base = m_factory->create(type_value);
101 filter_base->configure(node);
103 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
104 throw yp2::XMLError("Filter " + id_value + " already defined");
106 m_id_filter_map[id_value] =
107 boost::shared_ptr<yp2::filter::Base>(filter_base);
109 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
113 void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
116 yp2::xml::check_element_yp2(node, "route");
118 unsigned int route_nr = 0;
119 while(yp2::xml::is_element_yp2(node, "route"))
123 const struct _xmlAttr *attr;
124 std::string id_value;
125 for (attr = node->properties; attr; attr = attr->next)
127 std::string name = std::string((const char *) attr->name);
130 if (attr->children && attr->children->type == XML_TEXT_NODE)
131 value = std::string((const char *)attr->children->content);
136 throw yp2::XMLError("Only attribute 'id' allowed for"
143 // process <filter> nodes in third level
144 const xmlNode* node3 = yp2::xml::jump_to_children(node, XML_ELEMENT_NODE);
146 unsigned int filter3_nr = 0;
147 while(node3 && yp2::xml::check_element_yp2(node3, "filter"))
151 const struct _xmlAttr *attr;
152 std::string refid_value;
153 std::string type_value;
154 for (attr = node3->properties; attr; attr = attr->next)
156 std::string name = std::string((const char *) attr->name);
159 if (attr->children && attr->children->type == XML_TEXT_NODE)
160 value = std::string((const char *)attr->children->content);
164 else if (name == "type")
167 throw yp2::XMLError("Only attribute 'refid' or 'type'"
168 " allowed for element 'filter'."
171 if (refid_value.length())
173 std::map<std::string,
174 boost::shared_ptr<const yp2::filter::Base > >::iterator it;
175 it = m_id_filter_map.find(refid_value);
176 if (it == m_id_filter_map.end())
177 throw yp2::XMLError("Unknown filter refid "
180 route.m_list.push_back(it->second);
182 else if (type_value.length())
184 yp2::filter::Base* filter_base = m_factory->create(type_value);
186 filter_base->configure(node3);
188 route.m_list.push_back(
189 boost::shared_ptr<yp2::filter::Base>(filter_base));
191 node3 = yp2::xml::jump_to_next(node3, XML_ELEMENT_NODE);
194 std::map<std::string,RouterFleXML::Route>::iterator it;
195 it = m_routes.find(id_value);
196 if (it != m_routes.end())
197 throw yp2::XMLError("Route id='" + id_value
198 + "' already exist");
200 m_routes[id_value] = route;
201 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
205 void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
208 throw yp2::XMLError("Empty XML Document");
210 const xmlNode* root = xmlDocGetRootElement(doc);
212 yp2::xml::check_element_yp2(root, "yp2");
214 // process <start> node which is expected first element node
215 const xmlNode* node = yp2::xml::jump_to_children(root, XML_ELEMENT_NODE);
216 if (yp2::xml::check_element_yp2(node, "start"))
218 const struct _xmlAttr *attr;
219 std::string id_value;
220 for (attr = node->properties; attr; attr = attr->next)
222 std::string name = std::string((const char *) attr->name);
225 if (attr->children && attr->children->type == XML_TEXT_NODE)
226 value = std::string((const char *)attr->children->content);
229 m_start_route = value;
231 throw yp2::XMLError("Only attribute start allowed"
232 " in element 'start'. Got " + name);
234 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
236 // process <filters> node which is expected second element node
237 yp2::xml::check_element_yp2(node, "filters");
239 parse_xml_filters(doc, yp2::xml::jump_to_children(node, XML_ELEMENT_NODE));
241 // process <routes> node which is expected third element node
242 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
243 yp2::xml::check_element_yp2(node, "routes");
245 parse_xml_routes(doc, yp2::xml::jump_to_children(node, XML_ELEMENT_NODE));
247 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
250 throw yp2::XMLError("Unexpected element "
251 + std::string((const char *)node->name));
255 yp2::RouterFleXML::Rep::Rep() : m_xinclude(false)
259 void yp2::RouterFleXML::Rep::base(xmlDocPtr doc, yp2::FactoryFilter &factory)
261 m_factory = &factory;
262 parse_xml_config_dom(doc);
263 m_start_route = "start";
266 yp2::RouterFleXML::RouterFleXML(xmlDocPtr doc, yp2::FactoryFilter &factory)
269 m_p->base(doc, factory);
272 yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FactoryFilter &factory)
277 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
280 throw yp2::XMLError("xmlParseMemory failed");
283 m_p->base(doc, factory);
288 yp2::RouterFleXML::~RouterFleXML()
292 const yp2::filter::Base *yp2::RouterFleXML::Pos::move(const char *route)
296 std::cout << "move to " << route << "\n";
297 m_route_it = m_p->m_routes.find(route);
298 if (m_route_it == m_p->m_routes.end())
300 std::cout << "no such route " << route << "\n";
301 throw yp2::XMLError("bad route " + std::string(route));
303 m_filter_it = m_route_it->second.m_list.begin();
305 if (m_filter_it == m_route_it->second.m_list.end())
307 const yp2::filter::Base *f = (*m_filter_it).get();
312 yp2::RoutePos *yp2::RouterFleXML::createpos() const
314 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
316 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
317 if (p->m_route_it == m_p->m_routes.end())
322 p->m_filter_it = p->m_route_it->second.m_list.begin();
327 yp2::RoutePos *yp2::RouterFleXML::Pos::clone()
329 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
330 p->m_filter_it = m_filter_it;
331 p->m_route_it = m_route_it;
336 yp2::RouterFleXML::Pos::~Pos()
344 * indent-tabs-mode: nil
345 * c-file-style: "stroustrup"
347 * vim: shiftwidth=4 tabstop=8 expandtab