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,
70 bool test_only, const char *file_include_path);
71 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
72 bool test_only, const char *file_include_path);
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,
95 const char *file_include_path)
97 unsigned int filter_nr = 0;
98 while (node && mp::xml::check_element_mp(node, "filter"))
102 const struct _xmlAttr *attr;
103 std::string id_value;
104 std::string type_value;
105 for (attr = node->properties; attr; attr = attr->next)
107 std::string name = std::string((const char *) attr->name);
110 if (attr->children && attr->children->type == XML_TEXT_NODE)
111 value = std::string((const char *)attr->children->content);
115 else if (name == "type")
118 throw mp::XMLError("Only attribute id or type allowed"
119 " in filter element. Got " + name);
122 if (!m_factory->exist(type_value))
124 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
125 type_value.c_str(), m_dl_path.c_str());
126 m_factory->add_creator_dl(type_value, m_dl_path);
128 mp::filter::Base* filter_base = m_factory->create(type_value);
130 filter_base->configure(node, test_only, file_include_path);
132 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
133 throw mp::XMLError("Filter " + id_value + " already defined");
135 m_id_filter_map[id_value] =
136 boost::shared_ptr<mp::filter::Base>(filter_base);
138 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
142 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
145 const char *file_include_path)
147 mp::xml::check_element_mp(node, "route");
149 unsigned int route_nr = 0;
150 while (mp::xml::is_element_mp(node, "route"))
154 const struct _xmlAttr *attr;
155 std::string id_value;
156 for (attr = node->properties; attr; attr = attr->next)
158 std::string name = std::string((const char *) attr->name);
161 if (attr->children && attr->children->type == XML_TEXT_NODE)
162 value = std::string((const char *)attr->children->content);
167 throw mp::XMLError("Only attribute 'id' allowed for"
174 // process <filter> nodes in third level
175 const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
177 unsigned int filter3_nr = 0;
178 while (node3 && mp::xml::check_element_mp(node3, "filter"))
182 const struct _xmlAttr *attr;
183 std::string refid_value;
184 std::string type_value;
185 for (attr = node3->properties; attr; attr = attr->next)
187 std::string name = std::string((const char *) attr->name);
190 if (attr->children && attr->children->type == XML_TEXT_NODE)
191 value = std::string((const char *)attr->children->content);
195 else if (name == "type")
198 throw mp::XMLError("Only attribute 'refid' or 'type'"
199 " allowed for element 'filter'."
202 if (refid_value.length())
204 std::map<std::string,
205 boost::shared_ptr<const mp::filter::Base > >::iterator it;
206 it = m_id_filter_map.find(refid_value);
207 if (it == m_id_filter_map.end())
208 throw mp::XMLError("Unknown filter refid "
211 route.m_list.push_back(it->second);
213 else if (type_value.length())
215 if (!m_factory->exist(type_value))
217 yaz_log(YLOG_LOG, "Loading %s (dlpath %s)",
218 type_value.c_str(), m_dl_path.c_str());
219 m_factory->add_creator_dl(type_value, m_dl_path);
221 mp::filter::Base* filter_base = m_factory->create(type_value);
223 filter_base->configure(node3, test_only, file_include_path);
225 route.m_list.push_back(
226 boost::shared_ptr<mp::filter::Base>(filter_base));
228 node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
231 std::map<std::string,RouterFleXML::Route>::iterator it;
232 it = m_routes.find(id_value);
233 if (it != m_routes.end())
234 throw mp::XMLError("Route id='" + id_value
235 + "' already exist");
237 m_routes[id_value] = route;
238 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
242 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
244 const char *file_include_path)
247 throw mp::XMLError("Empty XML Document");
249 const xmlNode* root = xmlDocGetRootElement(doc);
251 if (file_include_path)
253 int r = yaz_xml_include_simple((xmlNode *) root, file_include_path);
255 throw mp::XMLError("YAZ XML Include failed");
258 mp::xml::check_element_mp(root, "metaproxy");
260 const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
262 if (mp::xml::is_element_mp(node, "dlpath"))
264 m_dl_path = mp::xml::get_text(node);
265 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
267 // process <start> node which is expected first element node
268 if (mp::xml::check_element_mp(node, "start"))
270 const struct _xmlAttr *attr;
271 for (attr = node->properties; attr; attr = attr->next)
273 std::string name = std::string((const char *) attr->name);
276 if (attr->children && attr->children->type == XML_TEXT_NODE)
277 value = std::string((const char *)attr->children->content);
280 m_start_route = value;
282 throw mp::XMLError("Only attribute route allowed"
283 " in element 'start'. Got " + name);
285 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
287 // process <filters> node if given
288 if (mp::xml::is_element_mp(node, "filters"))
290 parse_xml_filters(doc, mp::xml::jump_to_children(node,
292 test_only, file_include_path);
294 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
296 // process <routes> node which is expected third element node
297 mp::xml::check_element_mp(node, "routes");
299 parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
300 test_only, file_include_path);
302 node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
305 throw mp::XMLError("Unexpected element "
306 + std::string((const char *)node->name));
310 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
314 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
315 bool test_only, const char *file_include_path)
317 m_factory = &factory;
318 parse_xml_config_dom(doc, test_only, file_include_path);
319 m_start_route = "start";
322 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
323 bool test_only, const char *file_include_path)
326 m_p->base(doc, factory, test_only, file_include_path);
329 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
335 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
338 throw mp::XMLError("xmlParseMemory failed");
341 m_p->base(doc, factory, test_only, 0);
346 mp::RouterFleXML::~RouterFleXML()
350 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
354 //std::cout << "move to " << route << "\n";
355 m_route_it = m_p->m_routes.find(route);
356 if (m_route_it == m_p->m_routes.end())
358 std::cout << "no such route " << route << "\n";
359 throw mp::XMLError("bad route " + std::string(route));
361 m_filter_it = m_route_it->second.m_list.begin();
363 if (m_filter_it == m_route_it->second.m_list.end())
365 const mp::filter::Base *f = (*m_filter_it).get();
370 mp::RoutePos *mp::RouterFleXML::createpos() const
372 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
374 p->m_route_it = m_p->m_routes.find(m_p->m_start_route);
375 if (p->m_route_it == m_p->m_routes.end())
380 p->m_filter_it = p->m_route_it->second.m_list.begin();
385 mp::RoutePos *mp::RouterFleXML::Pos::clone()
387 mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
388 p->m_filter_it = m_filter_it;
389 p->m_route_it = m_route_it;
394 mp::RouterFleXML::Pos::~Pos()
399 void mp::RouterFleXML::start()
401 std::map<std::string,RouterFleXML::Route>::iterator route_it;
403 route_it = m_p->m_routes.begin();
404 while (route_it != m_p->m_routes.end())
406 RouterFleXML::Route route = route_it->second;
408 std::list<boost::shared_ptr<const mp::filter::Base> >::iterator it;
410 for (it = route.m_list.begin(); it != route.m_list.end(); it++)
419 * c-file-style: "Stroustrup"
420 * indent-tabs-mode: nil
422 * vim: shiftwidth=4 tabstop=8 expandtab