1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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 <metaproxy/xmlutil.hpp>
21 #include "router_flexml.hpp"
22 #include "factory_filter.hpp"
23 #include "factory_static.hpp"
30 #include <boost/shared_ptr.hpp>
32 #include <libxml/xmlversion.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
36 namespace mp = metaproxy_1;
38 namespace metaproxy_1 {
39 class RouterFleXML::Route {
40 friend class RouterFleXML::Rep;
41 friend class RouterFleXML::Pos;
42 friend class RouterFleXML;
43 std::list<boost::shared_ptr<const mp::filter::Base> > m_list;
45 class RouterFleXML::Rep {
46 friend class RouterFleXML;
47 friend class RouterFleXML::Pos;
50 void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only);
52 typedef std::map<std::string,
53 boost::shared_ptr<const mp::filter::Base > >
56 IdFilterMap m_id_filter_map;
58 std::map<std::string,RouterFleXML::Route> m_routes;
60 std::string m_start_route;
62 std::string m_dl_path;
64 void parse_xml_config_dom(xmlDocPtr doc, bool test_only);
66 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
68 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,
93 unsigned int filter_nr = 0;
94 while(node && mp::xml::check_element_mp(node, "filter"))
98 const struct _xmlAttr *attr;
100 std::string type_value;
101 for (attr = node->properties; attr; attr = attr->next)
103 std::string name = std::string((const char *) attr->name);
106 if (attr->children && attr->children->type == XML_TEXT_NODE)
107 value = std::string((const char *)attr->children->content);
111 else if (name == "type")
114 throw mp::XMLError("Only attribute id or type allowed"
115 " in filter element. Got " + name);
118 if (!m_factory->exist(type_value))
120 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
121 type_value.c_str(), m_dl_path.c_str());
122 m_factory->add_creator_dl(type_value, m_dl_path);
124 mp::filter::Base* filter_base = m_factory->create(type_value);
126 filter_base->configure(node, test_only);
128 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
129 throw mp::XMLError("Filter " + id_value + " already defined");
131 m_id_filter_map[id_value] =
132 boost::shared_ptr<mp::filter::Base>(filter_base);
134 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
138 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
142 mp::xml::check_element_mp(node, "route");
144 unsigned int route_nr = 0;
145 while(mp::xml::is_element_mp(node, "route"))
149 const struct _xmlAttr *attr;
150 std::string id_value;
151 for (attr = node->properties; attr; attr = attr->next)
153 std::string name = std::string((const char *) attr->name);
156 if (attr->children && attr->children->type == XML_TEXT_NODE)
157 value = std::string((const char *)attr->children->content);
162 throw mp::XMLError("Only attribute 'id' allowed for"
169 // process <filter> nodes in third level
170 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
172 unsigned int filter3_nr = 0;
173 while(node3 && mp::xml::check_element_mp(node3, "filter"))
177 const struct _xmlAttr *attr;
178 std::string refid_value;
179 std::string type_value;
180 for (attr = node3->properties; attr; attr = attr->next)
182 std::string name = std::string((const char *) attr->name);
185 if (attr->children && attr->children->type == XML_TEXT_NODE)
186 value = std::string((const char *)attr->children->content);
190 else if (name == "type")
193 throw mp::XMLError("Only attribute 'refid' or 'type'"
194 " allowed for element 'filter'."
197 if (refid_value.length())
199 std::map<std::string,
200 boost::shared_ptr<const mp::filter::Base > >::iterator it;
201 it = m_id_filter_map.find(refid_value);
202 if (it == m_id_filter_map.end())
203 throw mp::XMLError("Unknown filter refid "
206 route.m_list.push_back(it->second);
208 else if (type_value.length())
210 if (!m_factory->exist(type_value))
212 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
213 type_value.c_str(), m_dl_path.c_str());
214 m_factory->add_creator_dl(type_value, m_dl_path);
216 mp::filter::Base* filter_base = m_factory->create(type_value);
218 filter_base->configure(node3, test_only);
220 route.m_list.push_back(
221 boost::shared_ptr<mp::filter::Base>(filter_base));
223 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
226 std::map<std::string,RouterFleXML::Route>::iterator it;
227 it = m_routes.find(id_value);
228 if (it != m_routes.end())
229 throw mp::XMLError("Route id='" + id_value
230 + "' already exist");
232 m_routes[id_value] = route;
233 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
237 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
241 throw mp::XMLError("Empty XML Document");
243 const xmlNode* root = xmlDocGetRootElement(doc);
245 mp::xml::check_element_mp(root, "metaproxy");
247 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
249 if (mp::xml::is_element_mp(node, "dlpath"))
251 m_dl_path = mp::xml::get_text(node);
252 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
254 // process <start> node which is expected first element node
255 if (mp::xml::check_element_mp(node, "start"))
257 const struct _xmlAttr *attr;
258 std::string id_value;
259 for (attr = node->properties; attr; attr = attr->next)
261 std::string name = std::string((const char *) attr->name);
264 if (attr->children && attr->children->type == XML_TEXT_NODE)
265 value = std::string((const char *)attr->children->content);
268 m_start_route = value;
270 throw mp::XMLError("Only attribute start allowed"
271 " in element 'start'. Got " + name);
273 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
275 // process <filters> node if given
276 if (mp::xml::is_element_mp(node, "filters"))
278 parse_xml_filters(doc, mp::xml::jump_to_children(node,
282 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
284 // process <routes> node which is expected third element node
285 mp::xml::check_element_mp(node, "routes");
287 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
290 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
293 throw mp::XMLError("Unexpected element "
294 + std::string((const char *)node->name));
298 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
302 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
305 m_factory = &factory;
306 parse_xml_config_dom(doc, test_only);
307 m_start_route = "start";
310 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
314 m_p->base(doc, factory, test_only);
317 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
323 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
326 throw mp::XMLError("xmlParseMemory failed");
329 m_p->base(doc, factory, test_only);
334 mp::RouterFleXML::~RouterFleXML()
338 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
342 //std::cout << "move to " << route << "\n";
343 m_route_it = m_p->m_routes.find(route);
344 if (m_route_it == m_p->m_routes.end())
346 std::cout << "no such route " << route << "\n";
347 throw mp::XMLError("bad route " + std::string(route));
349 m_filter_it = m_route_it->second.m_list.begin();
351 if (m_filter_it == m_route_it->second.m_list.end())
353 const mp::filter::Base *f = (*m_filter_it).get();
358 mp::RoutePos *mp::RouterFleXML::createpos() const
360 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
362 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
363 if (p->m_route_it == m_p->m_routes.end())
368 p->m_filter_it = p->m_route_it->second.m_list.begin();
373 mp::RoutePos *mp::RouterFleXML::Pos::clone()
375 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
376 p->m_filter_it = m_filter_it;
377 p->m_route_it = m_route_it;
382 mp::RouterFleXML::Pos::~Pos()
390 * c-file-style: "Stroustrup"
391 * indent-tabs-mode: nil
393 * vim: shiftwidth=4 tabstop=8 expandtab