Logo
~Sockets~
~Examples~
~Contact~


BaseXMLFile.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 //#include <stdio.h>
00024 
00025 #include "BaseXMLFile.h"
00026 
00027 
00028 
00029 
00030 BaseXMLFile::BaseXMLFile(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00031 : m_doc(NULL)
00032 ,m_current(NULL)
00033 {
00034         xmlNodePtr cur;
00035         xmlNsPtr ns;
00036 
00037         if (!(m_doc = xmlParseFile(filename.c_str() )))
00038         {
00039                 fprintf(stderr,"xmlParseFile('%s') failed\n",filename.c_str());
00040                 return;
00041         }
00042 
00043         if (!(cur = xmlDocGetRootElement(m_doc)))
00044         {
00045                 fprintf(stderr, "Empty document\n");
00046                 xmlFreeDoc(m_doc);
00047                 m_doc = NULL;
00048                 return;
00049         }
00050 
00051         if (!verify_ns.size())
00052         {
00053                 return; // we're ok
00054         }
00055         if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00056         {
00057                 fprintf(stderr, "Document namespace != '%s'\n", verify_ns.c_str());
00058                 xmlFreeDoc(m_doc);
00059                 m_doc = NULL;
00060                 return;
00061         }
00062 
00063         if (!verify_root.size())
00064         {
00065                 return;
00066         }
00067         if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00068         {
00069                 fprintf(stderr, "Document root != '%s'\n",verify_root.c_str());
00070                 xmlFreeDoc(m_doc);
00071                 m_doc = NULL;
00072                 return;
00073         }
00074 
00075 }
00076 
00077 
00078 BaseXMLFile::~BaseXMLFile()
00079 {
00080         if (m_doc)
00081         {
00082                 xmlFreeDoc(m_doc);
00083         }
00084 }
00085 
00086 
00087 xmlNodePtr BaseXMLFile::GetRootElement()
00088 {
00089         m_current = m_doc ? xmlDocGetRootElement(m_doc) : NULL;
00090         return m_current;
00091 }
00092 
00093 
00094 std::string BaseXMLFile::GetProperty(const std::string& name)
00095 {
00096         xmlChar *p = m_current ? xmlGetProp(m_current, (const xmlChar *) name.c_str() ) : NULL;
00097         if (!p)
00098         {
00099                 return "";
00100         }
00101         std::string str = (char *)p;
00102         xmlFree(p);
00103         return str;
00104 }
00105 
00106 
00107 xmlNodePtr BaseXMLFile::GetChildrenNode()
00108 {
00109         m_current = m_current ? m_current -> xmlChildrenNode : NULL;
00110         return m_current;
00111 }
00112 
00113 
00114 xmlNodePtr BaseXMLFile::GetNextNode()
00115 {
00116         do
00117         {
00118                 m_current = m_current ? m_current -> next : NULL;
00119         } while (m_current && xmlIsBlankNode( m_current ));
00120         return m_current;
00121 }
00122 
00123 
00124 const std::string& BaseXMLFile::GetNodeName()
00125 {
00126         if (m_current)
00127         {
00128                 m_current_name = (char *)m_current -> name;
00129         }
00130         else
00131         {
00132                 m_current_name = "";
00133         }
00134         return m_current_name;
00135 }
00136 
00137 
00138 xmlNsPtr BaseXMLFile::GetNodeNs()
00139 {
00140         if (m_current)
00141                 return m_current -> ns;
00142         return NULL;
00143 }
00144 
00145 
00146 const std::string& BaseXMLFile::GetNodeNsPrefix()
00147 {
00148         if (m_current && m_current -> ns && m_current -> ns -> prefix)
00149         {
00150                 m_ns_prefix = (char *)m_current -> ns -> prefix;
00151         }
00152         else
00153         {
00154                 m_ns_prefix = "";
00155         }
00156         return m_ns_prefix;
00157 }
00158 
00159 
00160 const std::string& BaseXMLFile::GetNodeNsHref()
00161 {
00162         if (m_current && m_current -> ns && m_current -> ns -> href)
00163         {
00164                 m_ns_href = (char *)m_current -> ns -> href;
00165         }
00166         else
00167         {
00168                 m_ns_href = "";
00169         }
00170         return m_ns_href;
00171 }
00172 
00173 
00174 xmlNodePtr BaseXMLFile::GetFirstElement(const std::string& name)
00175 {
00176         GetRootElement();
00177         xmlNodePtr p = GetChildrenNode();
00178         while (p)
00179         {
00180                 if (name == GetNodeName())
00181                 {
00182                         return p;
00183                 }
00184                 p = GetNextNode();
00185         }
00186         return NULL;
00187 }
00188 
00189 
00190 xmlNodePtr BaseXMLFile::GetFirstElement(xmlNodePtr base,const std::string& name)
00191 {
00192         SetCurrent(base);
00193         xmlNodePtr p = GetChildrenNode();
00194         while (p)
00195         {
00196                 if (name == GetNodeName())
00197                 {
00198                         return p;
00199                 }
00200                 p = GetNextNode();
00201         }
00202         return NULL;
00203 }
00204 
00205 
00206 xmlNodePtr BaseXMLFile::GetNextElement(xmlNodePtr p,const std::string& name)
00207 {
00208         SetCurrent(p);
00209         p = GetNextNode();
00210         while (p)
00211         {
00212                 if (name == GetNodeName())
00213                 {
00214                         return p;
00215                 }
00216                 p = GetNextNode();
00217         }
00218         return NULL;
00219 }
00220 
00221 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4