1 /* $Id: router_flexml.cpp,v 1.2 2005-11-10 23:10:42 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;
26 typedef std::map<std::string, boost::shared_ptr<const yp2::filter::Base> >
28 typedef std::list<std::string> FilterIdList;
29 typedef std::map<std::string, FilterIdList > IdRouteMap ;
32 std::string m_xmlconf;
34 xmlDoc * m_xmlconf_doc;
35 IdFilterMap m_id_filter_map;
36 FilterIdList m_filter_id_list;
37 IdRouteMap m_id_route_map;
38 void xml_dom_error (const xmlNode* node, std::string msg)
40 std::cerr << "ERROR: " << msg << " <"
45 void create_filter(std::string type,
46 const xmlDoc * xmldoc,
49 std::cout << "Created Filter type='" << type
50 << "' id='" << id << "'" << std::endl;
53 void parse_xml_config_dom() {
56 std::cerr << "XML configuration DOM pointer empty" << std::endl;
59 const xmlNode* root = xmlDocGetRootElement(m_xmlconf_doc);
61 if ((std::string((const char *) root->name) != "yp2")
62 || (std::string((const char *)(root->ns->href))
63 != "http://indexdata.dk/yp2/config/1")
66 "expected <yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">, got ");
69 for (const struct _xmlAttr *attr = root->properties; attr; attr = attr->next)
71 if (std::string((const char *)attr->name) == "xmlns")
73 const xmlNode *val = attr->children;
74 if (std::string((const char *)val->content)
75 != "http://indexdata.dk/yp2/config/1")
77 "expected xmlns=\"http://indexdata.dk/yp2/config/1\", got ");
80 std::cout << "processing /yp2" << std::endl;
82 // process <start> node which is expected first element node
83 const xmlNode* node = jump_to_children(root, XML_ELEMENT_NODE);
84 //for (; node && node->type != XML_ELEMENT_NODE; node = node->next)
87 check_node_name(node, "start");
88 std::cout << "processing /yp2/start" << std::endl;
90 // process <filters> node which is expected second element node
91 node = jump_to_next(node, XML_ELEMENT_NODE);
92 check_node_name(node, "filters");
93 std::cout << "processing /yp2/filters" << std::endl;
95 // process <filter> nodes in next level
96 const xmlNode* node2 = jump_to_children(node, XML_ELEMENT_NODE);
97 check_node_name(node2, "filter");
99 unsigned int filter_nr = 0;
100 while(node2 && std::string((const char *)node2->name) == "filter"){
102 std::cout << "processing /yp2/filters/filter["
103 << filter_nr << "]" << std::endl;
104 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
107 // process <routes> node which is expected third element node
108 node = jump_to_next(node, XML_ELEMENT_NODE);
109 check_node_name(node, "routes");
110 std::cout << "processing /yp2/routes" << std::endl;
112 // process <route> nodes in next level
113 node2 = jump_to_children(node, XML_ELEMENT_NODE);
114 check_node_name(node2, "route");
116 unsigned int route_nr = 0;
117 while(node2 && std::string((const char *)node2->name) == "route"){
119 std::cout << "processing /yp2/routes/route["
120 << route_nr << "]" << std::endl;
122 // process <filter> nodes in third level
124 = jump_to_children(node2, XML_ELEMENT_NODE);
125 check_node_name(node3, "filter");
127 unsigned int filter3_nr = 0;
128 while(node3 && std::string((const char *)node3->name) == "filter"){
131 std::cout << "processing /yp2/routes/route["
132 << route_nr << "]/filter["
133 << filter3_nr << "]" << std::endl;
135 node3 = jump_to_next(node3, XML_ELEMENT_NODE);
138 node2 = jump_to_next(node2, XML_ELEMENT_NODE);
145 const xmlNode* jump_to(const xmlNode* node, int xml_node_type){
146 for (; node && node->type != xml_node_type; node = node->next)
151 const xmlNode* jump_to_next(const xmlNode* node, int xml_node_type){
153 for (; node && node->type != xml_node_type; node = node->next)
158 const xmlNode* jump_to_children(const xmlNode* node, int xml_node_type){
159 node = node->children;
160 for (; node && node->type != xml_node_type; node = node->next)
165 void check_node_name(const xmlNode* node, std::string name){
166 if (std::string((const char *)node->name)
168 xml_dom_error(node, "expected <" + name + ">, got ");
174 yp2::RouterFleXML::Rep::Rep() : m_xmlconf(""), m_xinclude(false), m_xmlconf_doc(0)
178 yp2::RouterFleXML::RouterFleXML(std::string xmlconf)
183 m_p->m_xmlconf = xmlconf;
185 m_p->m_xmlconf_doc = xmlParseMemory(m_p->m_xmlconf.c_str(), m_p->m_xmlconf.size());
187 m_p->parse_xml_config_dom();
190 yp2::RouterFleXML::~RouterFleXML()
192 xmlFreeDoc(m_p->m_xmlconf_doc);
195 const yp2::filter::Base *
196 yp2::RouterFleXML::move(const yp2::filter::Base *filter,
197 const yp2::Package *package) const
207 * indent-tabs-mode: nil
208 * c-file-style: "stroustrup"
210 * vim: shiftwidth=4 tabstop=8 expandtab