1 /* $Id: router_flexml.cpp,v 1.16 2006-01-19 09:41:01 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 std::string m_dl_path;
50 void parse_xml_config_dom(xmlDocPtr doc);
52 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
53 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
57 FactoryFilter *m_factory; // TODO shared_ptr
60 class RouterFleXML::Pos : public RoutePos {
62 virtual const filter::Base *move(const char *route);
63 virtual RoutePos *clone();
65 yp2::RouterFleXML::Rep *m_p;
68 RouterFleXML::Route>::iterator m_route_it;
69 std::list<boost::shared_ptr <const yp2::filter::Base> >::iterator m_filter_it;
73 void yp2::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
76 unsigned int filter_nr = 0;
77 while(node && yp2::xml::check_element_yp2(node, "filter"))
81 const struct _xmlAttr *attr;
83 std::string type_value;
84 for (attr = node->properties; attr; attr = attr->next)
86 std::string name = std::string((const char *) attr->name);
89 if (attr->children && attr->children->type == XML_TEXT_NODE)
90 value = std::string((const char *)attr->children->content);
94 else if (name == "type")
97 throw yp2::XMLError("Only attribute id or type allowed"
98 " in filter element. Got " + name);
101 if (!m_factory->exist(type_value))
103 std::cout << "about to load " << type_value << ", path=" <<
105 m_factory->add_creator_dl(type_value, m_dl_path);
107 yp2::filter::Base* filter_base = m_factory->create(type_value);
109 filter_base->configure(node);
111 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
112 throw yp2::XMLError("Filter " + id_value + " already defined");
114 m_id_filter_map[id_value] =
115 boost::shared_ptr<yp2::filter::Base>(filter_base);
117 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
121 void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
124 yp2::xml::check_element_yp2(node, "route");
126 unsigned int route_nr = 0;
127 while(yp2::xml::is_element_yp2(node, "route"))
131 const struct _xmlAttr *attr;
132 std::string id_value;
133 for (attr = node->properties; attr; attr = attr->next)
135 std::string name = std::string((const char *) attr->name);
138 if (attr->children && attr->children->type == XML_TEXT_NODE)
139 value = std::string((const char *)attr->children->content);
144 throw yp2::XMLError("Only attribute 'id' allowed for"
151 // process <filter> nodes in third level
152 const xmlNode* node3 = yp2::xml::jump_to_children(node, XML_ELEMENT_NODE);
154 unsigned int filter3_nr = 0;
155 while(node3 && yp2::xml::check_element_yp2(node3, "filter"))
159 const struct _xmlAttr *attr;
160 std::string refid_value;
161 std::string type_value;
162 for (attr = node3->properties; attr; attr = attr->next)
164 std::string name = std::string((const char *) attr->name);
167 if (attr->children && attr->children->type == XML_TEXT_NODE)
168 value = std::string((const char *)attr->children->content);
172 else if (name == "type")
175 throw yp2::XMLError("Only attribute 'refid' or 'type'"
176 " allowed for element 'filter'."
179 if (refid_value.length())
181 std::map<std::string,
182 boost::shared_ptr<const yp2::filter::Base > >::iterator it;
183 it = m_id_filter_map.find(refid_value);
184 if (it == m_id_filter_map.end())
185 throw yp2::XMLError("Unknown filter refid "
188 route.m_list.push_back(it->second);
190 else if (type_value.length())
192 if (!m_factory->exist(type_value))
194 std::cout << "about to load " << type_value << ", path=" <<
196 m_factory->add_creator_dl(type_value, m_dl_path);
198 yp2::filter::Base* filter_base = m_factory->create(type_value);
200 filter_base->configure(node3);
202 route.m_list.push_back(
203 boost::shared_ptr<yp2::filter::Base>(filter_base));
205 node3 = yp2::xml::jump_to_next(node3, XML_ELEMENT_NODE);
208 std::map<std::string,RouterFleXML::Route>::iterator it;
209 it = m_routes.find(id_value);
210 if (it != m_routes.end())
211 throw yp2::XMLError("Route id='" + id_value
212 + "' already exist");
214 m_routes[id_value] = route;
215 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
219 void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
222 throw yp2::XMLError("Empty XML Document");
224 const xmlNode* root = xmlDocGetRootElement(doc);
226 yp2::xml::check_element_yp2(root, "yp2");
228 const xmlNode* node = yp2::xml::jump_to_children(root, XML_ELEMENT_NODE);
230 if (yp2::xml::is_element_yp2(node, "dlpath"))
232 m_dl_path = yp2::xml::get_text(node);
233 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
235 // process <start> node which is expected first element node
236 if (yp2::xml::check_element_yp2(node, "start"))
238 const struct _xmlAttr *attr;
239 std::string id_value;
240 for (attr = node->properties; attr; attr = attr->next)
242 std::string name = std::string((const char *) attr->name);
245 if (attr->children && attr->children->type == XML_TEXT_NODE)
246 value = std::string((const char *)attr->children->content);
249 m_start_route = value;
251 throw yp2::XMLError("Only attribute start allowed"
252 " in element 'start'. Got " + name);
254 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
256 // process <filters> node if given
257 if (yp2::xml::is_element_yp2(node, "filters"))
259 parse_xml_filters(doc, yp2::xml::jump_to_children(node,
262 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
264 // process <routes> node which is expected third element node
265 yp2::xml::check_element_yp2(node, "routes");
267 parse_xml_routes(doc, yp2::xml::jump_to_children(node, XML_ELEMENT_NODE));
269 node = yp2::xml::jump_to_next(node, XML_ELEMENT_NODE);
272 throw yp2::XMLError("Unexpected element "
273 + std::string((const char *)node->name));
277 yp2::RouterFleXML::Rep::Rep() : m_xinclude(false)
281 void yp2::RouterFleXML::Rep::base(xmlDocPtr doc, yp2::FactoryFilter &factory)
283 m_factory = &factory;
284 parse_xml_config_dom(doc);
285 m_start_route = "start";
288 yp2::RouterFleXML::RouterFleXML(xmlDocPtr doc, yp2::FactoryFilter &factory)
291 m_p->base(doc, factory);
294 yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FactoryFilter &factory)
299 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
302 throw yp2::XMLError("xmlParseMemory failed");
305 m_p->base(doc, factory);
310 yp2::RouterFleXML::~RouterFleXML()
314 const yp2::filter::Base *yp2::RouterFleXML::Pos::move(const char *route)
318 std::cout << "move to " << route << "\n";
319 m_route_it = m_p->m_routes.find(route);
320 if (m_route_it == m_p->m_routes.end())
322 std::cout << "no such route " << route << "\n";
323 throw yp2::XMLError("bad route " + std::string(route));
325 m_filter_it = m_route_it->second.m_list.begin();
327 if (m_filter_it == m_route_it->second.m_list.end())
329 const yp2::filter::Base *f = (*m_filter_it).get();
334 yp2::RoutePos *yp2::RouterFleXML::createpos() const
336 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
338 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
339 if (p->m_route_it == m_p->m_routes.end())
344 p->m_filter_it = p->m_route_it->second.m_list.begin();
349 yp2::RoutePos *yp2::RouterFleXML::Pos::clone()
351 yp2::RouterFleXML::Pos *p = new yp2::RouterFleXML::Pos;
352 p->m_filter_it = m_filter_it;
353 p->m_route_it = m_route_it;
358 yp2::RouterFleXML::Pos::~Pos()
366 * indent-tabs-mode: nil
367 * c-file-style: "stroustrup"
369 * vim: shiftwidth=4 tabstop=8 expandtab