759ef06e4380c47e8d089c0137264cce226a01c3
[metaproxy-moved-to-github.git] / src / router_flexml.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <metaproxy/xmlutil.hpp>
21 #include "router_flexml.hpp"
22 #include "factory_filter.hpp"
23 #include "factory_static.hpp"
24
25 #include <iostream>
26 #include <map>
27 #include <list>
28 #include <yaz/log.h>
29 #include <yaz/xml_include.h>
30
31 #include <boost/shared_ptr.hpp>
32
33 #include <libxml/xmlversion.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
36
37 namespace mp = metaproxy_1;
38
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;
45     };
46     class RouterFleXML::Rep {
47         friend class RouterFleXML;
48         friend class RouterFleXML::Pos;
49         Rep();
50
51         void base(xmlDocPtr doc, mp::FactoryFilter &factory, bool test_only,
52                   const char *file_include_path);
53
54         typedef std::map<std::string,
55                          boost::shared_ptr<const mp::filter::Base > >
56                          IdFilterMap ;
57
58         IdFilterMap m_id_filter_map;
59
60         std::map<std::string,RouterFleXML::Route> m_routes;
61
62         std::string m_start_route;
63
64         std::string m_dl_path;
65
66         void parse_xml_config_dom(xmlDocPtr doc, bool test_only,
67                                   const char *file_include_path);
68
69         void parse_xml_filters(xmlDocPtr doc, const xmlNode *node,
70             bool test_only);
71         void parse_xml_routes(xmlDocPtr doc, const xmlNode *node,
72             bool test_only);
73
74         bool m_xinclude;
75     private:
76         FactoryFilter *m_factory; // TODO shared_ptr
77     };
78
79     class RouterFleXML::Pos : public RoutePos {
80     public:
81         virtual const filter::Base *move(const char *route);
82         virtual RoutePos *clone();
83         virtual ~Pos();
84         mp::RouterFleXML::Rep *m_p;
85
86         std::map<std::string, 
87                  RouterFleXML::Route>::iterator m_route_it;
88         std::list<boost::shared_ptr <const mp::filter::Base> >::iterator m_filter_it;
89     };
90 }
91
92 void mp::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
93                                               const xmlNode *node,
94                                               bool test_only)
95 {
96     unsigned int filter_nr = 0;
97     while(node && mp::xml::check_element_mp(node, "filter"))
98     {
99         filter_nr++;
100
101         const struct _xmlAttr *attr;
102         std::string id_value;
103         std::string type_value;
104         for (attr = node->properties; attr; attr = attr->next)
105         {
106             std::string name = std::string((const char *) attr->name);
107             std::string value;
108
109             if (attr->children && attr->children->type == XML_TEXT_NODE)
110                 value = std::string((const char *)attr->children->content);
111
112             if (name == "id")
113                 id_value = value;
114             else if (name == "type")
115                 type_value = value;
116             else
117                 throw mp::XMLError("Only attribute id or type allowed"
118                                     " in filter element. Got " + name);
119         }
120
121         if (!m_factory->exist(type_value))
122         {
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);
126         }
127         mp::filter::Base* filter_base = m_factory->create(type_value);
128
129         filter_base->configure(node, test_only);
130
131         if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
132             throw mp::XMLError("Filter " + id_value + " already defined");
133
134         m_id_filter_map[id_value] =
135             boost::shared_ptr<mp::filter::Base>(filter_base);
136
137         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
138     }
139 }
140
141 void mp::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
142                                              const xmlNode *node,
143                                              bool test_only)
144 {
145     mp::xml::check_element_mp(node, "route");
146
147     unsigned int route_nr = 0;
148     while(mp::xml::is_element_mp(node, "route"))
149     {
150         route_nr++;
151
152         const struct _xmlAttr *attr;
153         std::string id_value;
154         for (attr = node->properties; attr; attr = attr->next)
155         {
156             std::string name = std::string((const char *) attr->name);
157             std::string value;
158             
159             if (attr->children && attr->children->type == XML_TEXT_NODE)
160                 value = std::string((const char *)attr->children->content);
161             
162             if (name == "id")
163                 id_value = value;
164             else
165                 throw mp::XMLError("Only attribute 'id' allowed for"
166                                     " element 'route'."
167                                     " Got " + name);
168         }
169
170         Route route;
171
172         // process <filter> nodes in third level
173         const xmlNode* node3 = mp::xml::jump_to_children(node, XML_ELEMENT_NODE);
174
175         unsigned int filter3_nr = 0;
176         while(node3 && mp::xml::check_element_mp(node3, "filter"))
177         {
178             filter3_nr++;
179             
180             const struct _xmlAttr *attr;
181             std::string refid_value;
182             std::string type_value;
183             for (attr = node3->properties; attr; attr = attr->next)
184             {
185                 std::string name = std::string((const char *) attr->name);
186                 std::string value;
187                 
188                 if (attr->children && attr->children->type == XML_TEXT_NODE)
189                     value = std::string((const char *)attr->children->content);
190                 
191                 if (name == "refid")
192                     refid_value = value;
193                 else if (name == "type")
194                     type_value = value;
195                 else
196                     throw mp::XMLError("Only attribute 'refid' or 'type'"
197                                         " allowed for element 'filter'."
198                                         " Got " + name);
199             }
200             if (refid_value.length())
201             {
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 "
207                                         + refid_value);
208                 else
209                     route.m_list.push_back(it->second);
210             }
211             else if (type_value.length())
212             {
213                 if (!m_factory->exist(type_value))
214                 {
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);
218                 }
219                 mp::filter::Base* filter_base = m_factory->create(type_value);
220
221                 filter_base->configure(node3, test_only);
222                 
223                 route.m_list.push_back(
224                     boost::shared_ptr<mp::filter::Base>(filter_base));
225             }
226             node3 = mp::xml::jump_to_next(node3, XML_ELEMENT_NODE);
227             
228         }
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");
234         else
235             m_routes[id_value] = route;
236         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
237     }
238 }
239
240 void mp::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc,
241                                                  bool test_only,
242                                                  const char *file_include_path)
243 {
244     if (!doc)
245         throw mp::XMLError("Empty XML Document");
246     
247     const xmlNode* root = xmlDocGetRootElement(doc);
248
249     if (file_include_path)
250     {
251         int r = yaz_xml_include_simple((xmlNode *) root, file_include_path);
252         if (r)
253             throw mp::XMLError("YAZ XML Include failed");
254     }
255     
256     mp::xml::check_element_mp(root,  "metaproxy");
257
258     const xmlNode* node = mp::xml::jump_to_children(root, XML_ELEMENT_NODE);
259
260     if (mp::xml::is_element_mp(node, "dlpath"))
261     {
262         m_dl_path = mp::xml::get_text(node);
263         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
264     }
265     // process <start> node which is expected first element node
266     if (mp::xml::check_element_mp(node, "start"))
267     {
268         const struct _xmlAttr *attr;
269         std::string id_value;
270         for (attr = node->properties; attr; attr = attr->next)
271         {
272             std::string name = std::string((const char *) attr->name);
273             std::string value;
274
275             if (attr->children && attr->children->type == XML_TEXT_NODE)
276                 value = std::string((const char *)attr->children->content);
277
278             if (name == "route")
279                 m_start_route = value;
280             else
281                 throw mp::XMLError("Only attribute start allowed"
282                                     " in element 'start'. Got " + name);
283         }
284         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
285     }
286     // process <filters> node if given
287     if (mp::xml::is_element_mp(node, "filters"))
288     {
289         parse_xml_filters(doc, mp::xml::jump_to_children(node,
290                                                          XML_ELEMENT_NODE),
291             test_only);
292                       
293         node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
294     }
295     // process <routes> node which is expected third element node
296     mp::xml::check_element_mp(node, "routes");
297     
298     parse_xml_routes(doc, mp::xml::jump_to_children(node, XML_ELEMENT_NODE),
299         test_only);
300
301     node = mp::xml::jump_to_next(node, XML_ELEMENT_NODE);
302     if (node)
303     {
304         throw mp::XMLError("Unexpected element " 
305                             + std::string((const char *)node->name));
306     }
307 }        
308
309 mp::RouterFleXML::Rep::Rep() : m_xinclude(false)
310 {
311 }
312
313 void mp::RouterFleXML::Rep::base(xmlDocPtr doc, mp::FactoryFilter &factory,
314                                  bool test_only, const char *file_include_path)
315 {
316     m_factory = &factory;
317     parse_xml_config_dom(doc, test_only, file_include_path);
318     m_start_route = "start";
319 }
320
321 mp::RouterFleXML::RouterFleXML(xmlDocPtr doc, mp::FactoryFilter &factory,
322                                bool test_only, const char *file_include_path)
323     : m_p(new Rep)
324 {
325     m_p->base(doc, factory, test_only, file_include_path);
326 }
327
328 mp::RouterFleXML::RouterFleXML(std::string xmlconf, mp::FactoryFilter &factory,
329     bool test_only) 
330     : m_p(new Rep)
331 {            
332     LIBXML_TEST_VERSION;
333     
334     xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
335                                    xmlconf.size());
336     if (!doc)
337         throw mp::XMLError("xmlParseMemory failed");
338     else
339     {
340         m_p->base(doc, factory, test_only, 0);
341         xmlFreeDoc(doc);
342     }
343 }
344
345 mp::RouterFleXML::~RouterFleXML()
346 {
347 }
348
349 const mp::filter::Base *mp::RouterFleXML::Pos::move(const char *route)
350 {
351     if (route && *route)
352     {
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())
356         {
357             std::cout << "no such route " << route << "\n";
358             throw mp::XMLError("bad route " + std::string(route));
359         }
360         m_filter_it = m_route_it->second.m_list.begin();
361     }
362     if (m_filter_it == m_route_it->second.m_list.end())
363         return 0;
364     const mp::filter::Base *f = (*m_filter_it).get();
365     m_filter_it++;
366     return f;
367 }
368
369 mp::RoutePos *mp::RouterFleXML::createpos() const
370 {
371     mp::RouterFleXML::Pos *p = new mp::RouterFleXML::Pos;
372
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())
375     {
376         delete p;
377         return 0;
378     }
379     p->m_filter_it = p->m_route_it->second.m_list.begin();
380     p->m_p = m_p.get();
381     return p;
382 }
383
384 mp::RoutePos *mp::RouterFleXML::Pos::clone()
385 {
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;
389     p->m_p = m_p;
390     return p;
391 }
392
393 mp::RouterFleXML::Pos::~Pos()
394 {
395 }
396
397
398 /*
399  * Local variables:
400  * c-basic-offset: 4
401  * c-file-style: "Stroustrup"
402  * indent-tabs-mode: nil
403  * End:
404  * vim: shiftwidth=4 tabstop=8 expandtab
405  */
406