1 /* $Id: router_flexml.cpp,v 1.4 2005-12-08 15:10:34 adam Exp $
2 Copyright (c) 2005, Index Data.
8 #include "router_flexml.hpp"
14 #include <boost/shared_ptr.hpp>
16 #include <libxml/xmlversion.h>
17 #include <libxml/parser.h>
18 #include <libxml/tree.h>
21 class RouterFleXML::Rep {
22 friend class RouterFleXML;
25 typedef std::map<std::string, boost::shared_ptr<const yp2::filter::Base> >
27 typedef std::list<std::string> FilterIdList;
28 typedef std::map<std::string, FilterIdList > IdRouteMap ;
30 std::string m_xmlconf;
32 xmlDoc * m_xmlconf_doc;
33 IdFilterMap m_id_filter_map;
34 FilterIdList m_filter_id_list;
35 IdRouteMap m_id_route_map;
36 void xml_dom_error (const xmlNode* node, std::string msg);
38 void create_filter(std::string type,
39 const xmlDoc * xmldoc,
42 void parse_xml_config_dom();
44 const xmlNode* jump_to(const xmlNode* node, int xml_node_type);
46 const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type);
48 const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type);
49 void check_node_name(const xmlNode* node, std::string name);
53 void yp2::RouterFleXML::Rep::check_node_name(const xmlNode* node, std::string name)
55 if (std::string((const char *)node->name)
57 xml_dom_error(node, "expected <" + name + ">, got ");
60 const xmlNode* yp2::RouterFleXML::Rep::jump_to_children(const xmlNode* node, int xml_node_type)
62 node = node->children;
63 for (; node && node->type != xml_node_type; node = node->next)
68 const xmlNode* yp2::RouterFleXML::Rep::jump_to_next(const xmlNode* node, int xml_node_type)
71 for (; node && node->type != xml_node_type; node = node->next)
76 const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node, int xml_node_type)
78 for (; node && node->type != xml_node_type; node = node->next)
83 void yp2::RouterFleXML::Rep::parse_xml_config_dom()
86 std::cerr << "XML configuration DOM pointer empty" << std::endl;
89 const xmlNode* root = xmlDocGetRootElement(m_xmlconf_doc);
91 if ((std::string((const char *) root->name) != "yp2")
92 || (std::string((const char *)(root->ns->href))
93 != "http://indexdata.dk/yp2/config/1")
96 "expected <yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">, got ");
99 for (const struct _xmlAttr *attr = root->properties; attr; attr = attr->next)
101 if (std::string((const char *)attr->name) == "xmlns")
103 const xmlNode *val = attr->children;
104 if (std::string((const char *)val->content)
105 != "http://indexdata.dk/yp2/config/1")
107 "expected xmlns=\"http://indexdata.dk/yp2/config/1\", got ");
110 std::cout << "processing /yp2" << std::endl;
112 // process <start> node which is expected first element node
113 const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
114 //for (; node && node->type != XML_ELEMENT_NODE; node = node->next)
117 check_node_name(node, "start");
118 std::cout << "processing /yp2/start" << std::endl;
120 // process <filters> node which is expected second element node
121 node = jump_to_next(node, XML_ELEMENT_NODE);
122 check_node_name(node, "filters");
123 std::cout << "processing /yp2/filters" << std::endl;
125 // process <filter> nodes in next level
126 const xmlNode* node2 = jump_to_children(node, XML_ELEMENT_NODE);
127 check_node_name(node2, "filter");
129 unsigned int filter_nr = 0;
130 while(node2 && std::string((const char *)node2->name) == "filter"){
132 std::cout << "processing /yp2/filters/filter["
133 << filter_nr << "]" << std::endl;
134 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
137 // process <routes> node which is expected third element node
138 node = jump_to_next(node, XML_ELEMENT_NODE);
139 check_node_name(node, "routes");
140 std::cout << "processing /yp2/routes" << std::endl;
142 // process <route> nodes in next level
143 node2 = jump_to_children(node, XML_ELEMENT_NODE);
144 check_node_name(node2, "route");
146 unsigned int route_nr = 0;
147 while(node2 && std::string((const char *)node2->name) == "route"){
149 std::cout << "processing /yp2/routes/route["
150 << route_nr << "]" << std::endl;
152 // process <filter> nodes in third level
154 = jump_to_children(node2, XML_ELEMENT_NODE);
155 check_node_name(node3, "filter");
157 unsigned int filter3_nr = 0;
158 while(node3 && std::string((const char *)node3->name) == "filter"){
161 std::cout << "processing /yp2/routes/route["
162 << route_nr << "]/filter["
163 << filter3_nr << "]" << std::endl;
165 node3 = jump_to_next(node3, XML_ELEMENT_NODE);
168 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
172 void yp2::RouterFleXML::Rep::create_filter(std::string type,
173 const xmlDoc * xmldoc,
176 std::cout << "Created Filter type='" << type
177 << "' id='" << id << "'" << std::endl;
180 void yp2::RouterFleXML::Rep::xml_dom_error (const xmlNode* node, std::string msg)
182 std::cerr << "ERROR: " << msg << " <"
188 yp2::RouterFleXML::Rep::Rep() :
189 m_xmlconf(""), m_xinclude(false), m_xmlconf_doc(0)
193 yp2::RouterFleXML::RouterFleXML(std::string xmlconf)
198 m_p->m_xmlconf = xmlconf;
200 m_p->m_xmlconf_doc = xmlParseMemory(m_p->m_xmlconf.c_str(),
201 m_p->m_xmlconf.size());
202 m_p->parse_xml_config_dom();
205 yp2::RouterFleXML::~RouterFleXML()
207 xmlFreeDoc(m_p->m_xmlconf_doc);
210 const yp2::filter::Base *
211 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
212 const yp2::Package *package) const
221 * indent-tabs-mode: nil
222 * c-file-style: "stroustrup"
224 * vim: shiftwidth=4 tabstop=8 expandtab