1 /* $Id: router_flexml.cpp,v 1.3 2005-11-14 23:35:22 adam Exp $
2 Copyright (c) 2005, Index Data.
9 #include "router_flexml.hpp"
15 #include <boost/shared_ptr.hpp>
17 #include <libxml/xmlversion.h>
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
22 class RouterFleXML::Rep {
23 friend class RouterFleXML;
27 typedef std::map<std::string, boost::shared_ptr<const yp2::filter::Base> >
29 typedef std::list<std::string> FilterIdList;
30 typedef std::map<std::string, FilterIdList > IdRouteMap ;
33 std::string m_xmlconf;
35 xmlDoc * m_xmlconf_doc;
36 IdFilterMap m_id_filter_map;
37 FilterIdList m_filter_id_list;
38 IdRouteMap m_id_route_map;
39 void xml_dom_error (const xmlNode* node, std::string msg)
41 std::cerr << "ERROR: " << msg << " <"
46 void create_filter(std::string type,
47 const xmlDoc * xmldoc,
50 std::cout << "Created Filter type='" << type
51 << "' id='" << id << "'" << std::endl;
54 void parse_xml_config_dom() {
57 std::cerr << "XML configuration DOM pointer empty" << std::endl;
60 const xmlNode* root = xmlDocGetRootElement(m_xmlconf_doc);
62 if ((std::string((const char *) root->name) != "yp2")
63 || (std::string((const char *)(root->ns->href))
64 != "http://indexdata.dk/yp2/config/1")
67 "expected <yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">, got ");
70 for (const struct _xmlAttr *attr = root->properties; attr; attr = attr->next)
72 if (std::string((const char *)attr->name) == "xmlns")
74 const xmlNode *val = attr->children;
75 if (std::string((const char *)val->content)
76 != "http://indexdata.dk/yp2/config/1")
78 "expected xmlns=\"http://indexdata.dk/yp2/config/1\", got ");
81 std::cout << "processing /yp2" << std::endl;
83 // process <start> node which is expected first element node
84 const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
85 //for (; node && node->type != XML_ELEMENT_NODE; node = node->next)
88 check_node_name(node, "start");
89 std::cout << "processing /yp2/start" << std::endl;
91 // process <filters> node which is expected second element node
92 node = jump_to_next(node, XML_ELEMENT_NODE);
93 check_node_name(node, "filters");
94 std::cout << "processing /yp2/filters" << std::endl;
96 // process <filter> nodes in next level
97 const xmlNode* node2 = jump_to_children(node, XML_ELEMENT_NODE);
98 check_node_name(node2, "filter");
100 unsigned int filter_nr = 0;
101 while(node2 && std::string((const char *)node2->name) == "filter"){
103 std::cout << "processing /yp2/filters/filter["
104 << filter_nr << "]" << std::endl;
105 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
108 // process <routes> node which is expected third element node
109 node = jump_to_next(node, XML_ELEMENT_NODE);
110 check_node_name(node, "routes");
111 std::cout << "processing /yp2/routes" << std::endl;
113 // process <route> nodes in next level
114 node2 = jump_to_children(node, XML_ELEMENT_NODE);
115 check_node_name(node2, "route");
117 unsigned int route_nr = 0;
118 while(node2 && std::string((const char *)node2->name) == "route"){
120 std::cout << "processing /yp2/routes/route["
121 << route_nr << "]" << std::endl;
123 // process <filter> nodes in third level
125 = jump_to_children(node2, XML_ELEMENT_NODE);
126 check_node_name(node3, "filter");
128 unsigned int filter3_nr = 0;
129 while(node3 && std::string((const char *)node3->name) == "filter"){
132 std::cout << "processing /yp2/routes/route["
133 << route_nr << "]/filter["
134 << filter3_nr << "]" << std::endl;
136 node3 = jump_to_next(node3, XML_ELEMENT_NODE);
139 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
146 const xmlNode* jump_to(const xmlNode* node, int xml_node_type){
147 for (; node && node->type != xml_node_type; node = node->next)
152 const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type){
154 for (; node && node->type != xml_node_type; node = node->next)
159 const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type){
160 node = node->children;
161 for (; node && node->type != xml_node_type; node = node->next)
166 void check_node_name(const xmlNode* node, std::string name){
167 if (std::string((const char *)node->name)
169 xml_dom_error(node, "expected <" + name + ">, got ");
175 yp2::RouterFleXML::Rep::Rep() : m_xmlconf(""), m_xinclude(false), m_xmlconf_doc(0)
179 yp2::RouterFleXML::RouterFleXML(std::string xmlconf)
184 m_p->m_xmlconf = xmlconf;
186 m_p->m_xmlconf_doc = xmlParseMemory(m_p->m_xmlconf.c_str(), m_p->m_xmlconf.size());
188 m_p->parse_xml_config_dom();
191 yp2::RouterFleXML::~RouterFleXML()
193 xmlFreeDoc(m_p->m_xmlconf_doc);
196 const yp2::filter::Base *
197 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
198 const yp2::Package *package) const
208 * indent-tabs-mode: nil
209 * c-file-style: "stroustrup"
211 * vim: shiftwidth=4 tabstop=8 expandtab