1 /* $Id: router_flexml.cpp,v 1.17 2006-03-16 10:40:59 adam Exp $
2 Copyright (c) 2005-2006, 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>
23 namespace mp = metaproxy_1;
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;
32 class RouterFleXML::Rep {
33 friend class RouterFleXML;
34 friend class RouterFleXML::Pos;
37 void base(xmlDocPtr doc, mp::FactoryFilter &factory);
39 typedef std::map<std::string,
40 boost::shared_ptr<const mp::filter::Base > >
43 IdFilterMap m_id_filter_map;
45 std::map<std::string,RouterFleXML::Route> m_routes;
47 std::string m_start_route;
49 std::string m_dl_path;
51 void parse_xml_config_dom(xmlDocPtr doc);
53 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
54 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
58 FactoryFilter *m_factory; // TODO shared_ptr
61 class RouterFleXML::Pos : public RoutePos {
63 virtual const filter::Base *move(const char *route);
64 virtual RoutePos *clone();
66 mp::RouterFleXML::Rep *m_p;
69 RouterFleXML::Route>::iterator m_route_it;
70 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
74 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
77 unsigned int filter_nr = 0;
78 while(node && mp::xml::check_element_yp2(node, "filter"))
82 const struct _xmlAttr *attr;
84 std::string type_value;
85 for (attr = node->properties; attr; attr = attr->next)
87 std::string name = std::string((const char *) attr->name);
90 if (attr->children && attr->children->type == XML_TEXT_NODE)
91 value = std::string((const char *)attr->children->content);
95 else if (name == "type")
98 throw mp::XMLError("Only attribute id or type allowed"
99 " in filter element. Got " + name);
102 if (!m_factory->exist(type_value))
104 std::cout << "about to load " << type_value << ", path=" <<
106 m_factory->add_creator_dl(type_value, m_dl_path);
108 mp::filter::Base* filter_base = m_factory->create(type_value);
110 filter_base->configure(node);
112 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
113 throw mp::XMLError("Filter " + id_value + " already defined");
115 m_id_filter_map[id_value] =
116 boost::shared_ptr<mp::filter::Base>(filter_base);
118 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
122 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
125 mp::xml::check_element_yp2(node, "route");
127 unsigned int route_nr = 0;
128 while(mp::xml::is_element_yp2(node, "route"))
132 const struct _xmlAttr *attr;
133 std::string id_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);
145 throw mp::XMLError("Only attribute 'id' allowed for"
152 // process <filter> nodes in third level
153 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
155 unsigned int filter3_nr = 0;
156 while(node3 && mp::xml::check_element_yp2(node3, "filter"))
160 const struct _xmlAttr *attr;
161 std::string refid_value;
162 std::string type_value;
163 for (attr = node3->properties; attr; attr = attr->next)
165 std::string name = std::string((const char *) attr->name);
168 if (attr->children && attr->children->type == XML_TEXT_NODE)
169 value = std::string((const char *)attr->children->content);
173 else if (name == "type")
176 throw mp::XMLError("Only attribute 'refid' or 'type'"
177 " allowed for element 'filter'."
180 if (refid_value.length())
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 "
189 route.m_list.push_back(it->second);
191 else if (type_value.length())
193 if (!m_factory->exist(type_value))
195 std::cout << "about to load " << type_value << ", path=" <<
197 m_factory->add_creator_dl(type_value, m_dl_path);
199 mp::filter::Base* filter_base = m_factory->create(type_value);
201 filter_base->configure(node3);
203 route.m_list.push_back(
204 boost::shared_ptr<mp::filter::Base>(filter_base));
206 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
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");
215 m_routes[id_value] = route;
216 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
220 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
223 throw mp::XMLError("Empty XML Document");
225 const xmlNode* root = xmlDocGetRootElement(doc);
227 mp::xml::check_element_yp2(root, "yp2");
229 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
231 if (mp::xml::is_element_yp2(node, "dlpath"))
233 m_dl_path = mp::xml::get_text(node);
234 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
236 // process <start> node which is expected first element node
237 if (mp::xml::check_element_yp2(node, "start"))
239 const struct _xmlAttr *attr;
240 std::string id_value;
241 for (attr = node->properties; attr; attr = attr->next)
243 std::string name = std::string((const char *) attr->name);
246 if (attr->children && attr->children->type == XML_TEXT_NODE)
247 value = std::string((const char *)attr->children->content);
250 m_start_route = value;
252 throw mp::XMLError("Only attribute start allowed"
253 " in element 'start'. Got " + name);
255 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
257 // process <filters> node if given
258 if (mp::xml::is_element_yp2(node, "filters"))
260 parse_xml_filters(doc, mp::xml::jump_to_children(node,
263 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
265 // process <routes> node which is expected third element node
266 mp::xml::check_element_yp2(node, "routes");
268 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE));
270 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
273 throw mp::XMLError("Unexpected element "
274 + std::string((const char *)node->name));
278 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
282 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory)
284 m_factory = &factory;
285 parse_xml_config_dom(doc);
286 m_start_route = "start";
289 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory)
292 m_p->base(doc, factory);
295 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory)
300 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
303 throw mp::XMLError("xmlParseMemory failed");
306 m_p->base(doc, factory);
311 mp::RouterFleXML::~RouterFleXML()
315 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
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())
323 std::cout << "no such route " << route << "\n";
324 throw mp::XMLError("bad route " + std::string(route));
326 m_filter_it = m_route_it->second.m_list.begin();
328 if (m_filter_it == m_route_it->second.m_list.end())
330 const mp::filter::Base *f = (*m_filter_it).get();
335 mp::RoutePos *mp::RouterFleXML::createpos() const
337 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
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())
345 p->m_filter_it = p->m_route_it->second.m_list.begin();
350 mp::RoutePos *mp::RouterFleXML::Pos::clone()
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;
359 mp::RouterFleXML::Pos::~Pos()
367 * indent-tabs-mode: nil
368 * c-file-style: "stroustrup"
370 * vim: shiftwidth=4 tabstop=8 expandtab