1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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"
29 #include <yaz/xml_include.h>
31 #include <boost/shared_ptr.hpp>
33 #include <libxml/xmlversion.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
37 namespace mp = metaproxy_1;
39 namespace metaproxy_1 {
40 class RouterFleXML::Route {
41 friend class RouterFleXML::Rep;
42 friend class RouterFleXML::Pos;
43 friend class RouterFleXML;
44 std::list<boost::shared_ptr<const mp::filter::Base> > m_list;
46 class RouterFleXML::Rep {
47 friend class RouterFleXML;
48 friend class RouterFleXML::Pos;
51 void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only,
52 const char *file_include_path);
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,
67 const char *file_include_path);
69 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
71 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
76 FactoryFilter *m_factory; // TODO shared_ptr
79 class RouterFleXML::Pos : public RoutePos {
81 virtual const filter::Base *move(const char *route);
82 virtual RoutePos *clone();
84 mp::RouterFleXML::Rep *m_p;
87 RouterFleXML::Route>::iterator m_route_it;
88 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
92 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
96 unsigned int filter_nr = 0;
97 while(node && mp::xml::check_element_mp(node, "filter"))
101 const struct _xmlAttr *attr;
102 std::string id_value;
103 std::string type_value;
104 for (attr = node->properties; attr; attr = attr->next)
106 std::string name = std::string((const char *) attr->name);
109 if (attr->children && attr->children->type == XML_TEXT_NODE)
110 value = std::string((const char *)attr->children->content);
114 else if (name == "type")
117 throw mp::XMLError("Only attribute id or type allowed"
118 " in filter element. Got " + name);
121 if (!m_factory->exist(type_value))
123 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
124 type_value.c_str(), m_dl_path.c_str());
125 m_factory->add_creator_dl(type_value, m_dl_path);
127 mp::filter::Base* filter_base = m_factory->create(type_value);
129 filter_base->configure(node, test_only);
131 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
132 throw mp::XMLError("Filter " + id_value + " already defined");
134 m_id_filter_map[id_value] =
135 boost::shared_ptr<mp::filter::Base>(filter_base);
137 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
141 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
145 mp::xml::check_element_mp(node, "route");
147 unsigned int route_nr = 0;
148 while(mp::xml::is_element_mp(node, "route"))
152 const struct _xmlAttr *attr;
153 std::string id_value;
154 for (attr = node->properties; attr; attr = attr->next)
156 std::string name = std::string((const char *) attr->name);
159 if (attr->children && attr->children->type == XML_TEXT_NODE)
160 value = std::string((const char *)attr->children->content);
165 throw mp::XMLError("Only attribute 'id' allowed for"
172 // process <filter> nodes in third level
173 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
175 unsigned int filter3_nr = 0;
176 while(node3 && mp::xml::check_element_mp(node3, "filter"))
180 const struct _xmlAttr *attr;
181 std::string refid_value;
182 std::string type_value;
183 for (attr = node3->properties; attr; attr = attr->next)
185 std::string name = std::string((const char *) attr->name);
188 if (attr->children && attr->children->type == XML_TEXT_NODE)
189 value = std::string((const char *)attr->children->content);
193 else if (name == "type")
196 throw mp::XMLError("Only attribute 'refid' or 'type'"
197 " allowed for element 'filter'."
200 if (refid_value.length())
202 std::map<std::string,
203 boost::shared_ptr<const mp::filter::Base > >::iterator it;
204 it = m_id_filter_map.find(refid_value);
205 if (it == m_id_filter_map.end())
206 throw mp::XMLError("Unknown filter refid "
209 route.m_list.push_back(it->second);
211 else if (type_value.length())
213 if (!m_factory->exist(type_value))
215 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
216 type_value.c_str(), m_dl_path.c_str());
217 m_factory->add_creator_dl(type_value, m_dl_path);
219 mp::filter::Base* filter_base = m_factory->create(type_value);
221 filter_base->configure(node3, test_only);
223 route.m_list.push_back(
224 boost::shared_ptr<mp::filter::Base>(filter_base));
226 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
229 std::map<std::string,RouterFleXML::Route>::iterator it;
230 it = m_routes.find(id_value);
231 if (it != m_routes.end())
232 throw mp::XMLError("Route id='" + id_value
233 + "' already exist");
235 m_routes[id_value] = route;
236 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
240 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
242 const char *file_include_path)
245 throw mp::XMLError("Empty XML Document");
247 const xmlNode* root = xmlDocGetRootElement(doc);
249 if (file_include_path)
251 int r = yaz_xml_include_simple((xmlNode *) root, file_include_path);
253 throw mp::XMLError("YAZ XML Include failed");
256 mp::xml::check_element_mp(root, "metaproxy");
258 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
260 if (mp::xml::is_element_mp(node, "dlpath"))
262 m_dl_path = mp::xml::get_text(node);
263 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
265 // process <start> node which is expected first element node
266 if (mp::xml::check_element_mp(node, "start"))
268 const struct _xmlAttr *attr;
269 std::string id_value;
270 for (attr = node->properties; attr; attr = attr->next)
272 std::string name = std::string((const char *) attr->name);
275 if (attr->children && attr->children->type == XML_TEXT_NODE)
276 value = std::string((const char *)attr->children->content);
279 m_start_route = value;
281 throw mp::XMLError("Only attribute start allowed"
282 " in element 'start'. Got " + name);
284 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
286 // process <filters> node if given
287 if (mp::xml::is_element_mp(node, "filters"))
289 parse_xml_filters(doc, mp::xml::jump_to_children(node,
293 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
295 // process <routes> node which is expected third element node
296 mp::xml::check_element_mp(node, "routes");
298 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
301 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
304 throw mp::XMLError("Unexpected element "
305 + std::string((const char *)node->name));
309 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
313 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
314 bool test_only, const char *file_include_path)
316 m_factory = &factory;
317 parse_xml_config_dom(doc, test_only, file_include_path);
318 m_start_route = "start";
321 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
322 bool test_only, const char *file_include_path)
325 m_p->base(doc, factory, test_only, file_include_path);
328 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
334 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
337 throw mp::XMLError("xmlParseMemory failed");
340 m_p->base(doc, factory, test_only, 0);
345 mp::RouterFleXML::~RouterFleXML()
349 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
353 //std::cout << "move to " << route << "\n";
354 m_route_it = m_p->m_routes.find(route);
355 if (m_route_it == m_p->m_routes.end())
357 std::cout << "no such route " << route << "\n";
358 throw mp::XMLError("bad route " + std::string(route));
360 m_filter_it = m_route_it->second.m_list.begin();
362 if (m_filter_it == m_route_it->second.m_list.end())
364 const mp::filter::Base *f = (*m_filter_it).get();
369 mp::RoutePos *mp::RouterFleXML::createpos() const
371 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
373 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
374 if (p->m_route_it == m_p->m_routes.end())
379 p->m_filter_it = p->m_route_it->second.m_list.begin();
384 mp::RoutePos *mp::RouterFleXML::Pos::clone()
386 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
387 p->m_filter_it = m_filter_it;
388 p->m_route_it = m_route_it;
393 mp::RouterFleXML::Pos::~Pos()
398 void mp::RouterFleXML::start()
400 std::map<std::string,RouterFleXML::Route>::iterator route_it;
402 route_it = m_p->m_routes.begin();
403 while (route_it != m_p->m_routes.end())
405 RouterFleXML::Route route = route_it->second;
407 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
409 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
418 * c-file-style: "Stroustrup"
419 * indent-tabs-mode: nil
421 * vim: shiftwidth=4 tabstop=8 expandtab