1 /* This file is part of Metaproxy.
2 Copyright (C) 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>
22 #include "router_flexml.hpp"
23 #include "factory_filter.hpp"
24 #include "factory_static.hpp"
30 #include <yaz/xml_include.h>
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,
53 const char *file_include_path);
55 typedef std::map<std::string,
56 boost::shared_ptr<const mp::filter::Base > >
59 IdFilterMap m_id_filter_map;
61 std::map<std::string,RouterFleXML::Route> m_routes;
63 std::string m_start_route;
65 std::string m_dl_path;
67 void parse_xml_config_dom(xmlDocPtr doc, bool test_only,
68 const char *file_include_path);
70 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
71 bool test_only, const char *file_include_path);
72 void parse_xml_filters1(xmlDocPtr doc, const xmlNode *node,
73 bool test_only, const char *file_include_path,
75 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
76 bool test_only, const char *file_include_path);
77 void check_routes_in_filters(const xmlNode *n);
81 FactoryFilter *m_factory; // TODO shared_ptr
84 class RouterFleXML::Pos : public RoutePos {
86 virtual const filter::Base *move(const char *route);
87 virtual RoutePos *clone();
89 mp::RouterFleXML::Rep *m_p;
92 RouterFleXML::Route>::iterator m_route_it;
93 std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
97 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
100 const char *file_include_path)
102 unsigned int filter_nr = 0;
103 while (node && mp::xml::check_element_mp(node, "filter"))
107 const struct _xmlAttr *attr;
108 std::string id_value;
109 std::string type_value;
110 for (attr = node->properties; attr; attr = attr->next)
112 std::string name = std::string((const char *) attr->name);
115 if (attr->children && attr->children->type == XML_TEXT_NODE)
116 value = std::string((const char *)attr->children->content);
120 else if (name == "type")
123 throw mp::XMLError("Only attribute id or type allowed"
124 " in filter element. Got " + name);
127 if (!m_factory->exist(type_value))
129 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
130 type_value.c_str(), m_dl_path.c_str());
131 m_factory->add_creator_dl(type_value, m_dl_path);
133 boost::shared_ptr<mp::filter::Base> fb(m_factory->create(type_value));
135 fb->configure(node, test_only, file_include_path);
137 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
138 throw mp::XMLError("Filter " + id_value + " already defined");
140 m_id_filter_map[id_value] = fb;
142 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
146 void mp::RouterFleXML::Rep::parse_xml_filters1(xmlDocPtr doc,
149 const char *file_include_path,
154 if (mp::xml::is_element_mp(node, "filters"))
156 const xmlNode* node1 =
157 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
159 parse_xml_filters1(doc, node1, test_only, file_include_path, route);
161 else if (mp::xml::check_element_mp(node, "filter"))
163 const struct _xmlAttr *attr;
164 std::string refid_value;
165 std::string type_value;
166 for (attr = node->properties; attr; attr = attr->next)
168 std::string name = std::string((const char *) attr->name);
171 if (attr->children && attr->children->type == XML_TEXT_NODE)
172 value = std::string((const char *)attr->children->content);
176 else if (name == "type")
179 throw mp::XMLError("Only attribute 'refid' or 'type'"
180 " allowed for element 'filter'."
183 if (refid_value.length())
185 std::map<std::string,
186 boost::shared_ptr<const mp::filter::Base > >::iterator it;
187 it = m_id_filter_map.find(refid_value);
188 if (it == m_id_filter_map.end())
189 throw mp::XMLError("Unknown filter refid "
192 route.m_list.push_back(it->second);
194 else if (type_value.length())
196 if (!m_factory->exist(type_value))
198 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
199 type_value.c_str(), m_dl_path.c_str());
200 m_factory->add_creator_dl(type_value, m_dl_path);
202 mp::filter::Base* filter_base = m_factory->create(type_value);
204 filter_base->configure(node, test_only, file_include_path);
206 route.m_list.push_back(
207 boost::shared_ptr<mp::filter::Base>(filter_base));
211 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
216 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
219 const char *file_include_path)
221 mp::xml::check_element_mp(node, "route");
223 unsigned int route_nr = 0;
224 while (mp::xml::is_element_mp(node, "route"))
228 const struct _xmlAttr *attr;
229 std::string id_value;
230 for (attr = node->properties; attr; attr = attr->next)
232 std::string name = std::string((const char *) attr->name);
235 if (attr->children && attr->children->type == XML_TEXT_NODE)
236 value = std::string((const char *)attr->children->content);
241 throw mp::XMLError("Only attribute 'id' allowed for"
248 // process <filter> / <filters> nodes in third level
249 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
250 parse_xml_filters1(doc, node3, test_only, file_include_path, route);
252 std::map<std::string,RouterFleXML::Route>::iterator it;
253 it = m_routes.find(id_value);
254 if (it != m_routes.end())
255 throw mp::XMLError("Route id='" + id_value
256 + "' already exist");
258 m_routes[id_value] = route;
259 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
263 void mp::RouterFleXML::Rep::check_routes_in_filters(const xmlNode *node)
267 if (mp::xml::is_element_mp(node, "filters"))
270 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
271 check_routes_in_filters(n);
273 else if (mp::xml::is_element_mp(node, "filter"))
276 mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
279 const struct _xmlAttr *attr;
280 // we assume thar that route attribute is only at one level
281 // below filter.. At least that works for multi and virt_db.
282 for (attr = n->properties; attr; attr = attr->next)
284 if (!strcmp((const char *) attr->name, "route"))
288 if (attr->children && attr->children->type == XML_TEXT_NODE)
289 value = std::string((const char *)attr->children->content);
291 std::map<std::string,RouterFleXML::Route>::iterator it;
292 it = m_routes.find(value);
293 if (it == m_routes.end())
295 throw mp::XMLError("Route '" + value + "' does not exist");
299 n = mp::xml::jump_to_next(n, XML_ELEMENT_NODE);
302 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
306 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
308 const char *file_include_path)
311 throw mp::XMLError("Empty XML Document");
313 const xmlNode* root = xmlDocGetRootElement(doc);
315 if (file_include_path)
317 int r = yaz_xml_include_glob((xmlNode *) root, file_include_path,
318 YAZ_FILE_GLOB_FAIL_NOTEXIST);
320 throw mp::XMLError("YAZ XML Include failed");
323 mp::xml::check_element_mp(root, "metaproxy");
325 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
327 if (mp::xml::is_element_mp(node, "dlpath"))
329 m_dl_path = mp::xml::get_text(node);
330 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
332 // process <start> node which is expected first element node
333 if (mp::xml::check_element_mp(node, "start"))
335 const struct _xmlAttr *attr;
336 for (attr = node->properties; attr; attr = attr->next)
338 std::string name = std::string((const char *) attr->name);
341 if (attr->children && attr->children->type == XML_TEXT_NODE)
342 value = std::string((const char *)attr->children->content);
345 m_start_route = value;
347 throw mp::XMLError("Only attribute route allowed"
348 " in element 'start'. Got " + name);
350 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
352 // process <filters> node if given
353 if (mp::xml::is_element_mp(node, "filters"))
355 parse_xml_filters(doc, mp::xml::jump_to_children(node,
357 test_only, file_include_path);
359 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
361 // process <routes> node which is expected third element node
362 mp::xml::check_element_mp(node, "routes");
364 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
365 test_only, file_include_path);
367 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
370 throw mp::XMLError("Unexpected element "
371 + std::string((const char *)node->name));
374 node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
377 if (mp::xml::is_element_mp(node, "filters"))
378 check_routes_in_filters(
379 mp::xml::jump_to_children(node,
381 else if (mp::xml::is_element_mp(node, "routes"))
383 const xmlNode* n = mp::xml::jump_to_children(node,
387 if (mp::xml::is_element_mp(n, "route"))
389 check_routes_in_filters(
390 mp::xml::jump_to_children(n, XML_ELEMENT_NODE));
393 n = mp::xml::jump_to_next(n, XML_ELEMENT_NODE);
396 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
400 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
404 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
405 bool test_only, const char *file_include_path)
407 m_factory = &factory;
408 parse_xml_config_dom(doc, test_only, file_include_path);
409 m_start_route = "start";
412 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
413 bool test_only, const char *file_include_path)
416 m_p->base(doc, factory, test_only, file_include_path);
419 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
425 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
428 throw mp::XMLError("xmlParseMemory failed");
431 m_p->base(doc, factory, test_only, 0);
436 mp::RouterFleXML::~RouterFleXML()
440 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
444 //std::cout << "move to " << route << "\n";
445 m_route_it = m_p->m_routes.find(route);
446 if (m_route_it == m_p->m_routes.end())
448 std::cout << "no such route " << route << "\n";
449 throw mp::XMLError("bad route " + std::string(route));
451 m_filter_it = m_route_it->second.m_list.begin();
453 if (m_filter_it == m_route_it->second.m_list.end())
455 const mp::filter::Base *f = (*m_filter_it).get();
460 mp::RoutePos *mp::RouterFleXML::createpos() const
462 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
464 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
465 if (p->m_route_it == m_p->m_routes.end())
470 p->m_filter_it = p->m_route_it->second.m_list.begin();
475 mp::RoutePos *mp::RouterFleXML::Pos::clone()
477 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
478 p->m_filter_it = m_filter_it;
479 p->m_route_it = m_route_it;
484 mp::RouterFleXML::Pos::~Pos()
489 void mp::RouterFleXML::start()
491 std::map<std::string,RouterFleXML::Route>::iterator route_it;
493 route_it = m_p->m_routes.begin();
494 while (route_it != m_p->m_routes.end())
496 RouterFleXML::Route route = route_it->second;
498 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
500 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
506 void mp::RouterFleXML::stop(int signo)
508 std::map<std::string,RouterFleXML::Route>::iterator route_it;
510 route_it = m_p->m_routes.begin();
511 while (route_it != m_p->m_routes.end())
513 RouterFleXML::Route route = route_it->second;
515 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
517 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
527 * c-file-style: "Stroustrup"
528 * indent-tabs-mode: nil
530 * vim: shiftwidth=4 tabstop=8 expandtab