1 /* $Id: router_flexml.cpp,v 1.10 2006-01-05 16:39:37 adam Exp $
2 Copyright (c) 2005, Index Data.
8 #include "router_flexml.hpp"
9 #include "factory_filter.hpp"
10 #include "factory_static.hpp"
16 #include <boost/shared_ptr.hpp>
18 #include <libxml/xmlversion.h>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
24 class RouterFleXML::Route {
25 friend class RouterFleXML::Rep;
26 std::list<boost::shared_ptr<const yp2::filter::Base> > m_list;
28 class RouterFleXML::Rep {
29 friend class RouterFleXML;
32 typedef std::map<std::string,
33 boost::shared_ptr<const yp2::filter::Base > >
36 IdFilterMap m_id_filter_map;
38 std::map<std::string,RouterFleXML::Route> m_routes;
40 void parse_xml_config_dom(xmlDocPtr doc);
42 bool is_element(const xmlNode *ptr,
43 const std::string &ns,
44 const std::string &name);
46 bool is_element_yp2(const xmlNode *ptr,
47 const std::string &name);
49 bool check_element_yp2(const xmlNode *ptr,
50 const std::string &name);
52 const xmlNode* jump_to(const xmlNode* node, int xml_node_type);
54 const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
56 const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
57 void parse_xml_filters(xmlDocPtr doc, const xmlNode *node);
58 void parse_xml_routes(xmlDocPtr doc, const xmlNode *node);
62 FactoryFilter *m_factory; // TODO shared_ptr
66 const xmlNode* yp2::RouterFleXML::Rep::jump_to_children(const xmlNode* node,
69 node = node->children;
70 for (; node && node->type != xml_node_type; node = node->next)
75 const xmlNode* yp2::RouterFleXML::Rep::jump_to_next(const xmlNode* node,
79 for (; node && node->type != xml_node_type; node = node->next)
84 const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node,
87 for (; node && node->type != xml_node_type; node = node->next)
92 bool yp2::RouterFleXML::Rep::is_element(const xmlNode *ptr,
93 const std::string &ns,
94 const std::string &name)
96 if (ptr && ptr->type == XML_ELEMENT_NODE && ptr->ns && ptr->ns->href
97 && !xmlStrcmp(BAD_CAST ns.c_str(), ptr->ns->href)
98 && !xmlStrcmp(BAD_CAST name.c_str(), ptr->name))
103 bool yp2::RouterFleXML::Rep::is_element_yp2(const xmlNode *ptr,
104 const std::string &name)
106 return is_element(ptr, "http://indexdata.dk/yp2/config/1", name);
109 bool yp2::RouterFleXML::Rep::check_element_yp2(const xmlNode *ptr,
110 const std::string &name)
112 if (!is_element_yp2(ptr, name))
113 throw XMLError("Expected element name " + name);
118 void yp2::RouterFleXML::Rep::parse_xml_filters(xmlDocPtr doc,
121 unsigned int filter_nr = 0;
122 while(node && check_element_yp2(node, "filter"))
125 std::cout << "processing /yp2/filters/filter["
126 << filter_nr << "]" << std::endl;
128 const struct _xmlAttr *attr;
129 std::string id_value;
130 std::string type_value;
131 for (attr = node->properties; attr; attr = attr->next)
133 std::string name = std::string((const char *) attr->name);
136 if (attr->children && attr->children->type == XML_TEXT_NODE)
137 value = std::string((const char *)attr->children->content);
141 else if (name == "type")
144 throw XMLError("Only attribute id or type allowed"
145 " in filter element. Got " + name);
147 std::cout << "attr " << name << "=" << value << "\n";
150 yp2::filter::Base* filter_base = m_factory->create(type_value);
152 filter_base->configure(node);
154 if (m_id_filter_map.find(id_value) != m_id_filter_map.end())
155 throw XMLError("Filter " + id_value + " already defined");
157 m_id_filter_map[id_value] =
158 boost::shared_ptr<yp2::filter::Base>(filter_base);
160 node = jump_to_next(node, XML_ELEMENT_NODE);
164 void yp2::RouterFleXML::Rep::parse_xml_routes(xmlDocPtr doc,
167 check_element_yp2(node, "route");
169 unsigned int route_nr = 0;
170 while(is_element_yp2(node, "route"))
174 const struct _xmlAttr *attr;
175 std::string id_value;
176 for (attr = node->properties; attr; attr = attr->next)
178 std::string name = std::string((const char *) attr->name);
181 if (attr->children && attr->children->type == XML_TEXT_NODE)
182 value = std::string((const char *)attr->children->content);
187 throw XMLError("Only attribute refid allowed route element. Got " + name);
189 std::cout << "attr " << name << "=" << value << "\n";
194 std::cout << "processing /yp2/routes/route["
195 << route_nr << "]" << std::endl;
197 // process <filter> nodes in third level
199 const xmlNode* node3 = jump_to_children(node, XML_ELEMENT_NODE);
201 unsigned int filter3_nr = 0;
202 while(node3 && check_element_yp2(node3, "filter"))
206 const struct _xmlAttr *attr;
207 std::string refid_value;
208 std::string type_value;
209 for (attr = node3->properties; attr; attr = attr->next)
211 std::string name = std::string((const char *) attr->name);
214 if (attr->children && attr->children->type == XML_TEXT_NODE)
215 value = std::string((const char *)attr->children->content);
219 else if (name == "type")
222 throw XMLError("Only attribute refid or type"
223 " allowed in filter element. Got " + name);
225 std::cout << "attr " << name << "=" << value << "\n";
227 if (refid_value.length())
229 std::map<std::string,
230 boost::shared_ptr<const yp2::filter::Base > >::iterator it;
231 it = m_id_filter_map.find(refid_value);
232 if (it == m_id_filter_map.end())
233 throw XMLError("Unknown filter refid " + refid_value);
235 route.m_list.push_back(it->second);
237 else if (type_value.length())
239 yp2::filter::Base* filter_base = m_factory->create(type_value);
241 filter_base->configure(node3);
243 route.m_list.push_back(
244 boost::shared_ptr<yp2::filter::Base>(filter_base));
246 std::cout << "processing /yp2/routes/route["
247 << route_nr << "]/filter["
248 << filter3_nr << "]" << std::endl;
250 node3 = jump_to_next(node3, XML_ELEMENT_NODE);
253 std::map<std::string,RouterFleXML::Route>::iterator it;
254 it = m_routes.find(id_value);
255 if (it != m_routes.end())
256 throw XMLError("Route id='" + id_value + "' already exist");
258 m_routes[id_value] = route;
259 node = jump_to_next(node, XML_ELEMENT_NODE);
263 void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc)
266 throw XMLError("Empty XML Document");
268 const xmlNode* root = xmlDocGetRootElement(doc);
270 check_element_yp2(root, "yp2");
272 std::cout << "processing /yp2" << std::endl;
274 // process <start> node which is expected first element node
275 const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
276 //for (; node && node->type != XML_ELEMENT_NODE; node = node->next)
279 check_element_yp2(node, "start");
281 std::cout << "processing /yp2/start" << std::endl;
283 // process <filters> node which is expected second element node
284 node = jump_to_next(node, XML_ELEMENT_NODE);
285 check_element_yp2(node, "filters");
286 std::cout << "processing /yp2/filters" << std::endl;
288 parse_xml_filters(doc, jump_to_children(node, XML_ELEMENT_NODE));
290 // process <routes> node which is expected third element node
291 node = jump_to_next(node, XML_ELEMENT_NODE);
292 check_element_yp2(node, "routes");
293 std::cout << "processing /yp2/routes" << std::endl;
295 parse_xml_routes(doc, jump_to_children(node, XML_ELEMENT_NODE));
298 yp2::RouterFleXML::Rep::Rep() : m_xinclude(false)
302 yp2::RouterFleXML::RouterFleXML(std::string xmlconf, yp2::FactoryFilter &factory)
306 m_p->m_factory = &factory;
310 xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(),
313 throw XMLError("xmlParseMemory failed");
316 m_p->parse_xml_config_dom(doc);
321 yp2::RouterFleXML::~RouterFleXML()
325 const yp2::filter::Base *
326 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
327 const yp2::Package *package) const
336 * indent-tabs-mode: nil
337 * c-file-style: "stroustrup"
339 * vim: shiftwidth=4 tabstop=8 expandtab