1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "xmlutil.hpp"
21 #include "router_flexml.hpp"
22 #include "factory_filter.hpp"
23 #include "factory_static.hpp"
29 #include <boost/shared_ptr.hpp>
31 #include <libxml/xmlversion.h>
32 #include <libxml/parser.h>
33 #include <libxml/tree.h>
35 namespace mp = metaproxy_1;
37 namespace metaproxy_1 {
38 class RouterFleXML::Route {
39 friend class RouterFleXML::Rep;
40 friend class RouterFleXML::Pos;
41 friend class RouterFleXML;
42 std::list<boost::shared_ptr<const mp::filter::Base> > m_list;
44 class RouterFleXML::Rep {
45 friend class RouterFleXML;
46 friend class RouterFleXML::Pos;
49 void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only);
51 typedef std::map<std::string,
52 boost::shared_ptr<const mp::filter::Base > >
55 IdFilterMap m_id_filter_map;
57 std::map<std::string,RouterFleXML::Route> m_routes;
59 std::string m_start_route;
61 std::string m_dl_path;
63 void parse_xml_config_dom(xmlDocPtr doc, bool test_only);
65 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
67 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
72 FactoryFilter *m_factory; // TODO shared_ptr
75 class RouterFleXML::Pos : public RoutePos {
77 virtual const filter::Base *move(const char *route);
78 virtual RoutePos *clone();
80 mp::RouterFleXML::Rep *m_p;
83 RouterFleXML::Route>::iterator m_route_it;
84 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
88 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, test_only);
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,
141 mp::xml::check_element_mp(node, "route");
143 unsigned int route_nr = 0;
144 while(mp::xml::is_element_mp(node, "route"))
148 const struct _xmlAttr *attr;
149 std::string id_value;
150 for (attr = node->properties; attr; attr = attr->next)
152 std::string name = std::string((const char *) attr->name);
155 if (attr->children && attr->children->type == XML_TEXT_NODE)
156 value = std::string((const char *)attr->children->content);
161 throw mp::XMLError("Only attribute 'id' allowed for"
168 // process <filter> nodes in third level
169 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
171 unsigned int filter3_nr = 0;
172 while(node3 && mp::xml::check_element_mp(node3, "filter"))
176 const struct _xmlAttr *attr;
177 std::string refid_value;
178 std::string type_value;
179 for (attr = node3->properties; attr; attr = attr->next)
181 std::string name = std::string((const char *) attr->name);
184 if (attr->children && attr->children->type == XML_TEXT_NODE)
185 value = std::string((const char *)attr->children->content);
189 else if (name == "type")
192 throw mp::XMLError("Only attribute 'refid' or 'type'"
193 " allowed for element 'filter'."
196 if (refid_value.length())
198 std::map<std::string,
199 boost::shared_ptr<const mp::filter::Base > >::iterator it;
200 it = m_id_filter_map.find(refid_value);
201 if (it == m_id_filter_map.end())
202 throw mp::XMLError("Unknown filter refid "
205 route.m_list.push_back(it->second);
207 else if (type_value.length())
209 if (!m_factory->exist(type_value))
211 std::cout << "about to load " << type_value << ", path=" <<
213 m_factory->add_creator_dl(type_value, m_dl_path);
215 mp::filter::Base* filter_base = m_factory->create(type_value);
217 filter_base->configure(node3, test_only);
219 route.m_list.push_back(
220 boost::shared_ptr<mp::filter::Base>(filter_base));
222 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
225 std::map<std::string,RouterFleXML::Route>::iterator it;
226 it = m_routes.find(id_value);
227 if (it != m_routes.end())
228 throw mp::XMLError("Route id='" + id_value
229 + "' already exist");
231 m_routes[id_value] = route;
232 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
236 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
240 throw mp::XMLError("Empty XML Document");
242 const xmlNode* root = xmlDocGetRootElement(doc);
244 mp::xml::check_element_mp(root, "metaproxy");
246 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
248 if (mp::xml::is_element_mp(node, "dlpath"))
250 m_dl_path = mp::xml::get_text(node);
251 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
253 // process <start> node which is expected first element node
254 if (mp::xml::check_element_mp(node, "start"))
256 const struct _xmlAttr *attr;
257 std::string id_value;
258 for (attr = node->properties; attr; attr = attr->next)
260 std::string name = std::string((const char *) attr->name);
263 if (attr->children && attr->children->type == XML_TEXT_NODE)
264 value = std::string((const char *)attr->children->content);
267 m_start_route = value;
269 throw mp::XMLError("Only attribute start allowed"
270 " in element 'start'. Got " + name);
272 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
274 // process <filters> node if given
275 if (mp::xml::is_element_mp(node, "filters"))
277 parse_xml_filters(doc, mp::xml::jump_to_children(node,
281 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
283 // process <routes> node which is expected third element node
284 mp::xml::check_element_mp(node, "routes");
286 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
289 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
292 throw mp::XMLError("Unexpected element "
293 + std::string((const char *)node->name));
297 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
301 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
304 m_factory = &factory;
305 parse_xml_config_dom(doc, test_only);
306 m_start_route = "start";
309 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
313 m_p->base(doc, factory, test_only);
316 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
322 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
325 throw mp::XMLError("xmlParseMemory failed");
328 m_p->base(doc, factory, test_only);
333 mp::RouterFleXML::~RouterFleXML()
337 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
341 //std::cout << "move to " << route << "\n";
342 m_route_it = m_p->m_routes.find(route);
343 if (m_route_it == m_p->m_routes.end())
345 std::cout << "no such route " << route << "\n";
346 throw mp::XMLError("bad route " + std::string(route));
348 m_filter_it = m_route_it->second.m_list.begin();
350 if (m_filter_it == m_route_it->second.m_list.end())
352 const mp::filter::Base *f = (*m_filter_it).get();
357 mp::RoutePos *mp::RouterFleXML::createpos() const
359 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
361 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
362 if (p->m_route_it == m_p->m_routes.end())
367 p->m_filter_it = p->m_route_it->second.m_list.begin();
372 mp::RoutePos *mp::RouterFleXML::Pos::clone()
374 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
375 p->m_filter_it = m_filter_it;
376 p->m_route_it = m_route_it;
381 mp::RouterFleXML::Pos::~Pos()
389 * indent-tabs-mode: nil
390 * c-file-style: "stroustrup"
392 * vim: shiftwidth=4 tabstop=8 expandtab