1 /* $Id: router_flexml.cpp,v 1.22 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
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
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
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
23 #include "xmlutil.hpp"
24 #include "router_flexml.hpp"
25 #include "factory_filter.hpp"
26 #include "factory_static.hpp"
32 #include <boost/shared_ptr.hpp>
34 #include <libxml/xmlversion.h>
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
38 namespace mp = metaproxy_1;
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;
47 class RouterFleXML::Rep {
48 friend class RouterFleXML;
49 friend class RouterFleXML::Pos;
52 void base(xmlDocPtr doc, mp::FactoryFilter &factory);
54 typedef std::map<std::string,
55 boost::shared_ptr<const mp::filter::Base > >
58 IdFilterMap m_id_filter_map;
60 std::map<std::string,RouterFleXML::Route> m_routes;
62 std::string m_start_route;
64 std::string m_dl_path;
66 void parse_xml_config_dom(xmlDocPtr doc);
68 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
69 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
73 FactoryFilter *m_factory; // TODO shared_ptr
76 class RouterFleXML::Pos : public RoutePos {
78 virtual const filter::Base *move(const char *route);
79 virtual RoutePos *clone();
81 mp::RouterFleXML::Rep *m_p;
84 RouterFleXML::Route>::iterator m_route_it;
85 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
89 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
92 unsigned int filter_nr = 0;
93 while(node && mp::xml::check_element_mp(node, "filter"))
97 const struct _xmlAttr *attr;
99 std::string type_value;
100 for (attr = node->properties; attr; attr = attr->next)
102 std::string name = std::string((const char *) attr->name);
105 if (attr->children && attr->children->type == XML_TEXT_NODE)
106 value = std::string((const char *)attr->children->content);
110 else if (name == "type")
113 throw mp::XMLError("Only attribute id or type allowed"
114 " in filter element. Got " + name);
117 if (!m_factory->exist(type_value))
119 std::cout << "about to load " << type_value << ", path=" <<
121 m_factory->add_creator_dl(type_value, m_dl_path);
123 mp::filter::Base* filter_base = m_factory->create(type_value);
125 filter_base->configure(node);
127 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
128 throw mp::XMLError("Filter " + id_value + " already defined");
130 m_id_filter_map[id_value] =
131 boost::shared_ptr<mp::filter::Base>(filter_base);
133 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
137 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
140 mp::xml::check_element_mp(node, "route");
142 unsigned int route_nr = 0;
143 while(mp::xml::is_element_mp(node, "route"))
147 const struct _xmlAttr *attr;
148 std::string id_value;
149 for (attr = node->properties; attr; attr = attr->next)
151 std::string name = std::string((const char *) attr->name);
154 if (attr->children && attr->children->type == XML_TEXT_NODE)
155 value = std::string((const char *)attr->children->content);
160 throw mp::XMLError("Only attribute 'id' allowed for"
167 // process <filter> nodes in third level
168 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
170 unsigned int filter3_nr = 0;
171 while(node3 && mp::xml::check_element_mp(node3, "filter"))
175 const struct _xmlAttr *attr;
176 std::string refid_value;
177 std::string type_value;
178 for (attr = node3->properties; attr; attr = attr->next)
180 std::string name = std::string((const char *) attr->name);
183 if (attr->children && attr->children->type == XML_TEXT_NODE)
184 value = std::string((const char *)attr->children->content);
188 else if (name == "type")
191 throw mp::XMLError("Only attribute 'refid' or 'type'"
192 " allowed for element 'filter'."
195 if (refid_value.length())
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 "
204 route.m_list.push_back(it->second);
206 else if (type_value.length())
208 if (!m_factory->exist(type_value))
210 std::cout << "about to load " << type_value << ", path=" <<
212 m_factory->add_creator_dl(type_value, m_dl_path);
214 mp::filter::Base* filter_base = m_factory->create(type_value);
216 filter_base->configure(node3);
218 route.m_list.push_back(
219 boost::shared_ptr<mp::filter::Base>(filter_base));
221 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
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");
230 m_routes[id_value] = route;
231 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
235 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
238 throw mp::XMLError("Empty XML Document");
240 const xmlNode* root = xmlDocGetRootElement(doc);
242 mp::xml::check_element_mp(root, "metaproxy");
244 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
246 if (mp::xml::is_element_mp(node, "dlpath"))
248 m_dl_path = mp::xml::get_text(node);
249 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
251 // process <start> node which is expected first element node
252 if (mp::xml::check_element_mp(node, "start"))
254 const struct _xmlAttr *attr;
255 std::string id_value;
256 for (attr = node->properties; attr; attr = attr->next)
258 std::string name = std::string((const char *) attr->name);
261 if (attr->children && attr->children->type == XML_TEXT_NODE)
262 value = std::string((const char *)attr->children->content);
265 m_start_route = value;
267 throw mp::XMLError("Only attribute start allowed"
268 " in element 'start'. Got " + name);
270 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
272 // process <filters> node if given
273 if (mp::xml::is_element_mp(node, "filters"))
275 parse_xml_filters(doc, mp::xml::jump_to_children(node,
278 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
280 // process <routes> node which is expected third element node
281 mp::xml::check_element_mp(node, "routes");
283 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE));
285 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
288 throw mp::XMLError("Unexpected element "
289 + std::string((const char *)node->name));
293 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
297 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory)
299 m_factory = &factory;
300 parse_xml_config_dom(doc);
301 m_start_route = "start";
304 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory)
307 m_p->base(doc, factory);
310 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory)
315 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
318 throw mp::XMLError("xmlParseMemory failed");
321 m_p->base(doc, factory);
326 mp::RouterFleXML::~RouterFleXML()
330 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
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())
338 std::cout << "no such route " << route << "\n";
339 throw mp::XMLError("bad route " + std::string(route));
341 m_filter_it = m_route_it->second.m_list.begin();
343 if (m_filter_it == m_route_it->second.m_list.end())
345 const mp::filter::Base *f = (*m_filter_it).get();
350 mp::RoutePos *mp::RouterFleXML::createpos() const
352 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
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())
360 p->m_filter_it = p->m_route_it->second.m_list.begin();
365 mp::RoutePos *mp::RouterFleXML::Pos::clone()
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;
374 mp::RouterFleXML::Pos::~Pos()
382 * indent-tabs-mode: nil
383 * c-file-style: "stroustrup"
385 * vim: shiftwidth=4 tabstop=8 expandtab