From 6ed3503878603e0bae8ab0ee62bf900b3a60f2ca Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 8 Dec 2005 15:34:08 +0000 Subject: [PATCH] Make exception class for XML router --- src/router_flexml.cpp | 32 ++++++++++++---------- src/router_flexml.hpp | 7 ++++- src/test_router_flexml.cpp | 65 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/router_flexml.cpp b/src/router_flexml.cpp index a421930..e66b56c 100644 --- a/src/router_flexml.cpp +++ b/src/router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: router_flexml.cpp,v 1.4 2005-12-08 15:10:34 adam Exp $ +/* $Id: router_flexml.cpp,v 1.5 2005-12-08 15:34:08 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -27,9 +27,7 @@ namespace yp2 { typedef std::list FilterIdList; typedef std::map IdRouteMap ; - std::string m_xmlconf; bool m_xinclude; - xmlDoc * m_xmlconf_doc; IdFilterMap m_id_filter_map; FilterIdList m_filter_id_list; IdRouteMap m_id_route_map; @@ -39,7 +37,7 @@ namespace yp2 { const xmlDoc * xmldoc, std::string id = ""); - void parse_xml_config_dom(); + void parse_xml_config_dom(xmlDocPtr doc); const xmlNode* jump_to(const xmlNode* node, int xml_node_type); @@ -80,13 +78,15 @@ const xmlNode* yp2::RouterFleXML::Rep::jump_to(const xmlNode* node, int xml_node return node; } -void yp2::RouterFleXML::Rep::parse_xml_config_dom() +void yp2::RouterFleXML::Rep::parse_xml_config_dom(xmlDocPtr doc) { - if (!m_xmlconf_doc){ + if (!doc) + { std::cerr << "XML configuration DOM pointer empty" << std::endl; + return; } - const xmlNode* root = xmlDocGetRootElement(m_xmlconf_doc); + const xmlNode* root = xmlDocGetRootElement(doc); if ((std::string((const char *) root->name) != "yp2") || (std::string((const char *)(root->ns->href)) @@ -184,9 +184,8 @@ void yp2::RouterFleXML::Rep::xml_dom_error (const xmlNode* node, std::string msg << std::endl; } - yp2::RouterFleXML::Rep::Rep() : - m_xmlconf(""), m_xinclude(false), m_xmlconf_doc(0) + m_xinclude(false) { } @@ -195,16 +194,19 @@ yp2::RouterFleXML::RouterFleXML(std::string xmlconf) { LIBXML_TEST_VERSION; - m_p->m_xmlconf = xmlconf; - - m_p->m_xmlconf_doc = xmlParseMemory(m_p->m_xmlconf.c_str(), - m_p->m_xmlconf.size()); - m_p->parse_xml_config_dom(); + xmlDocPtr doc = xmlParseMemory(xmlconf.c_str(), + xmlconf.size()); + if (!doc) + throw XMLError("xmlParseMemory failed"); + else + { + m_p->parse_xml_config_dom(doc); + xmlFreeDoc(doc); + } } yp2::RouterFleXML::~RouterFleXML() { - xmlFreeDoc(m_p->m_xmlconf_doc); } const yp2::filter::Base * diff --git a/src/router_flexml.hpp b/src/router_flexml.hpp index 1eae3e8..14e368c 100644 --- a/src/router_flexml.hpp +++ b/src/router_flexml.hpp @@ -1,4 +1,4 @@ -/* $Id: router_flexml.hpp,v 1.6 2005-11-10 23:10:42 adam Exp $ +/* $Id: router_flexml.hpp,v 1.7 2005-12-08 15:34:08 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -22,6 +22,11 @@ namespace yp2 virtual const filter::Base *move(const filter::Base *filter, const Package *package) const; + class XMLError : public std::runtime_error { + public: + XMLError(const std::string msg) : + std::runtime_error("XMLError : " + msg) {} ; + }; private: boost::scoped_ptr m_p; }; diff --git a/src/test_router_flexml.cpp b/src/test_router_flexml.cpp index c458286..039eca5 100644 --- a/src/test_router_flexml.cpp +++ b/src/test_router_flexml.cpp @@ -1,4 +1,4 @@ -/* $Id: test_router_flexml.cpp,v 1.5 2005-12-02 12:21:07 adam Exp $ +/* $Id: test_router_flexml.cpp,v 1.6 2005-12-08 15:34:08 adam Exp $ Copyright (c) 2005, Index Data. %LICENSE% @@ -24,8 +24,8 @@ public: BOOST_AUTO_UNIT_TEST( test_router_flexml_1 ) { - try{ - + try + { std::string xmlconf = "\n" "\n" "\n" @@ -46,18 +46,65 @@ BOOST_AUTO_UNIT_TEST( test_router_flexml_1 ) "\n"; yp2::RouterFleXML rflexml(xmlconf); - - - BOOST_CHECK (true); - - //BOOST_CHECK_EQUAL(filter.name(), std::string("filter1")); - } catch ( ... ) { BOOST_CHECK (false); } } +BOOST_AUTO_UNIT_TEST( test_router_flexml_2 ) +{ + bool got_xml_error = false; + try + { + std::string xmlconf_invalid = "\n" + "\n" + "\n" + "\n"; + + yp2::RouterFleXML rflexml(xmlconf_invalid); + } + catch ( yp2::RouterFleXML::XMLError &e) { + got_xml_error = true; + } + catch ( ... ) { + ; + } + BOOST_CHECK(got_xml_error); +} + +BOOST_AUTO_UNIT_TEST( test_router_flexml_3 ) +{ + try + { + std::string xmlconf = "\n" + "\n" + " \n" + " \n" + " \n" + " 210\n" + " \n" + " \n" + " mylog.log\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "\n"; + + yp2::RouterFleXML rflexml(xmlconf); + } + catch ( ... ) { + BOOST_CHECK (false); + } +} + + + /* * Local variables: * c-basic-offset: 4 -- 1.7.10.4