CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

xmlserverconfig.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include <serverconfig/xmlserverconfig.h>
00021 #include <sptk4/CBuffer.h>
00022 #include <sptk4/cxml>
00023 #include <unistd.h>
00024 #include <errno.h>
00025 #include <fcntl.h>
00026 
00027 using namespace sptk;
00028 namespace container {
00029 namespace serverconfig {
00030 
00031 XMLServerConfig::XMLServerConfig()
00032     : ServerConfig()
00033     , m_path("/etc/engine.xml")
00034 {
00035 }
00036 
00037 
00038 XMLServerConfig::~XMLServerConfig()
00039 {
00040     if(m_root)
00041         delete m_root;
00042 }
00043 
00044 
00045 ConfigNode::childlist_t XMLServerConfig::getNodes(const ConfigNode* parent)
00046 {
00047     XMLConfigNode* pparent=(XMLConfigNode*)parent;
00048     ConfigNode::childlist_t ret;
00049     for(ConfigNode::childlist_t::iterator i=pparent->getChildren().begin();i!=pparent->getChildren().end();i++){
00050         ret.push_back(*i);
00051     }
00052     return ret;
00053 }
00054 ConfigNode::childlist_t XMLServerConfig::getNodes(const ConfigNode* parent, const std::string& type)
00055 {
00056     XMLConfigNode* pparent=(XMLConfigNode*)parent;
00057     ConfigNode::childlist_t ret;
00058     for(ConfigNode::childlist_t::iterator i=pparent->getChildren().begin();i!=pparent->getChildren().end();i++){
00059         if((*i)->getType()==type)
00060             ret.push_back(*i);
00061     }
00062     return ret;
00063 }
00064 
00072 void XMLServerConfig::addKids(CXmlNode* n, XMLServerConfig::XMLConfigNode* parent)
00073 {
00074     for(CXmlNode::iterator node=n->begin();node!=n->end();node++){
00075         switch((*node)->type()){
00076         case CXmlNode::DOM_ELEMENT:
00077             {
00078                 XMLServerConfig::XMLConfigNode* cnode =
00079                      new XMLServerConfig::XMLConfigNode(std::string((*node)->name()),parent);
00080                 if ((*node)->hasAttributes()) {
00081 #ifdef XML_ATTRIBUTE_IS_NODE
00082                     const CXmlAttributes &attributes = (*node)->attributes();
00083                     for(CXmlAttributes::const_iterator it=attributes.begin();
00084                         it!=attributes.end(); it++) {
00085                         const CXmlNode* attr = *it;
00086                         cnode->addAttr(attr->name(),attr->value());
00087                     }
00088 #else
00089                     const CXmlAttributes &attr_map = (*node)->attributes;
00090                     for(CXmlAttributes::const_iterator it=attr_map.begin();
00091                         it!=attr_map.end(); it++) {
00092                         cnode->addAttr(it->first,it->second);
00093                     }
00094 #endif
00095                 }
00096                 if(!(*node)->empty())
00097                     addKids(*node,cnode);
00098                 parent->addNode(cnode);//Presumes parent is always set
00099             }
00100             break;
00101         default:
00102             break;
00103         }
00104     }
00105 }
00106 
00110 void XMLServerConfig::load()
00111 {
00112     XMLServerConfig::XMLConfigNode *r=new XMLServerConfig::XMLConfigNode("root","root",0);
00113     m_root=r;
00114     try {
00115         sptk::CBuffer buf;
00116         buf.loadFromFile(m_path.c_str());
00117         CXmlDoc doc;
00118         doc.load(buf);
00119         addKids(&doc,r);
00120     } catch (CXmlException& exp) {
00121         std::cerr<<"XML error: "<<exp.what()<<std::endl;
00122     } catch (CException& e) {
00123         std::cerr<<"error: "<<e.message()<<std::endl;
00124     } catch (...) {
00125         std::cerr<<"Error reading config\n";
00126     }
00127 }
00128 
00129 
00130 void XMLServerConfig::XMLConfigNode::addAttr(const std::string& name, const std::string& val)
00131 {
00132     if(name=="name")
00133         this->m_name=val;
00134     m_attrs[name]=val;
00135 }
00136 
00137 void XMLServerConfig::XMLConfigNode::addNode(XMLConfigNode* node)
00138 {
00139     m_children.push_back(node);
00140 }
00141 
00142 }
00143 }

SourceForge.net Logo