1 /* $Id: router_flexml.cpp,v 1.23 2008-02-20 15:07:52 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, bool test_only);
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, bool test_only);
68 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
70 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
75 FactoryFilter *m_factory; // TODO shared_ptr
78 class RouterFleXML::Pos : public RoutePos {
80 virtual const filter::Base *move(const char *route);
81 virtual RoutePos *clone();
83 mp::RouterFleXML::Rep *m_p;
86 RouterFleXML::Route>::iterator m_route_it;
87 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
91 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
95 unsigned int filter_nr = 0;
96 while(node && mp::xml::check_element_mp(node, "filter"))
100 const struct _xmlAttr *attr;
101 std::string id_value;
102 std::string type_value;
103 for (attr = node->properties; attr; attr = attr->next)
105 std::string name = std::string((const char *) attr->name);
108 if (attr->children && attr->children->type == XML_TEXT_NODE)
109 value = std::string((const char *)attr->children->content);
113 else if (name == "type")
116 throw mp::XMLError("Only attribute id or type allowed"
117 " in filter element. Got " + name);
120 if (!m_factory->exist(type_value))
122 std::cout << "about to load " << type_value << ", path=" <<
124 m_factory->add_creator_dl(type_value, m_dl_path);
126 mp::filter::Base* filter_base = m_factory->create(type_value);
128 filter_base->configure(node, test_only);
130 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
131 throw mp::XMLError("Filter " + id_value + " already defined");
133 m_id_filter_map[id_value] =
134 boost::shared_ptr<mp::filter::Base>(filter_base);
136 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
140 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
144 mp::xml::check_element_mp(node, "route");
146 unsigned int route_nr = 0;
147 while(mp::xml::is_element_mp(node, "route"))
151 const struct _xmlAttr *attr;
152 std::string id_value;
153 for (attr = node->properties; attr; attr = attr->next)
155 std::string name = std::string((const char *) attr->name);
158 if (attr->children && attr->children->type == XML_TEXT_NODE)
159 value = std::string((const char *)attr->children->content);
164 throw mp::XMLError("Only attribute 'id' allowed for"
171 // process <filter> nodes in third level
172 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
174 unsigned int filter3_nr = 0;
175 while(node3 && mp::xml::check_element_mp(node3, "filter"))
179 const struct _xmlAttr *attr;
180 std::string refid_value;
181 std::string type_value;
182 for (attr = node3->properties; attr; attr = attr->next)
184 std::string name = std::string((const char *) attr->name);
187 if (attr->children && attr->children->type == XML_TEXT_NODE)
188 value = std::string((const char *)attr->children->content);
192 else if (name == "type")
195 throw mp::XMLError("Only attribute 'refid' or 'type'"
196 " allowed for element 'filter'."
199 if (refid_value.length())
201 std::map<std::string,
202 boost::shared_ptr<const mp::filter::Base > >::iterator it;
203 it = m_id_filter_map.find(refid_value);
204 if (it == m_id_filter_map.end())
205 throw mp::XMLError("Unknown filter refid "
208 route.m_list.push_back(it->second);
210 else if (type_value.length())
212 if (!m_factory->exist(type_value))
214 std::cout << "about to load " << type_value << ", path=" <<
216 m_factory->add_creator_dl(type_value, m_dl_path);
218 mp::filter::Base* filter_base = m_factory->create(type_value);
220 filter_base->configure(node3, test_only);
222 route.m_list.push_back(
223 boost::shared_ptr<mp::filter::Base>(filter_base));
225 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
228 std::map<std::string,RouterFleXML::Route>::iterator it;
229 it = m_routes.find(id_value);
230 if (it != m_routes.end())
231 throw mp::XMLError("Route id='" + id_value
232 + "' already exist");
234 m_routes[id_value] = route;
235 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
239 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
243 throw mp::XMLError("Empty XML Document");
245 const xmlNode* root = xmlDocGetRootElement(doc);
247 mp::xml::check_element_mp(root, "metaproxy");
249 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
251 if (mp::xml::is_element_mp(node, "dlpath"))
253 m_dl_path = mp::xml::get_text(node);
254 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
256 // process <start> node which is expected first element node
257 if (mp::xml::check_element_mp(node, "start"))
259 const struct _xmlAttr *attr;
260 std::string id_value;
261 for (attr = node->properties; attr; attr = attr->next)
263 std::string name = std::string((const char *) attr->name);
266 if (attr->children && attr->children->type == XML_TEXT_NODE)
267 value = std::string((const char *)attr->children->content);
270 m_start_route = value;
272 throw mp::XMLError("Only attribute start allowed"
273 " in element 'start'. Got " + name);
275 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
277 // process <filters> node if given
278 if (mp::xml::is_element_mp(node, "filters"))
280 parse_xml_filters(doc, mp::xml::jump_to_children(node,
284 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
286 // process <routes> node which is expected third element node
287 mp::xml::check_element_mp(node, "routes");
289 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
292 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
295 throw mp::XMLError("Unexpected element "
296 + std::string((const char *)node->name));
300 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
304 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
307 m_factory = &factory;
308 parse_xml_config_dom(doc, test_only);
309 m_start_route = "start";
312 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
316 m_p->base(doc, factory, test_only);
319 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
325 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
328 throw mp::XMLError("xmlParseMemory failed");
331 m_p->base(doc, factory, test_only);
336 mp::RouterFleXML::~RouterFleXML()
340 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
344 //std::cout << "move to " << route << "\n";
345 m_route_it = m_p->m_routes.find(route);
346 if (m_route_it == m_p->m_routes.end())
348 std::cout << "no such route " << route << "\n";
349 throw mp::XMLError("bad route " + std::string(route));
351 m_filter_it = m_route_it->second.m_list.begin();
353 if (m_filter_it == m_route_it->second.m_list.end())
355 const mp::filter::Base *f = (*m_filter_it).get();
360 mp::RoutePos *mp::RouterFleXML::createpos() const
362 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
364 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
365 if (p->m_route_it == m_p->m_routes.end())
370 p->m_filter_it = p->m_route_it->second.m_list.begin();
375 mp::RoutePos *mp::RouterFleXML::Pos::clone()
377 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
378 p->m_filter_it = m_filter_it;
379 p->m_route_it = m_route_it;
384 mp::RouterFleXML::Pos::~Pos()
392 * indent-tabs-mode: nil
393 * c-file-style: "stroustrup"
395 * vim: shiftwidth=4 tabstop=8 expandtab