XmlDocument.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2008  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 "XmlDocument.h"
00024 #include "XmlException.h"
00025 
00026 namespace Xmlw {
00027 
00028 XmlDocument::XmlDocument(const std::string& filename,const std::string& verify_ns,const std::string& verify_root)
00029 : m_doc(NULL)
00030 , m_ok(false)
00031 {
00032         xmlNodePtr cur;
00033         xmlNsPtr ns;
00034 
00035         if (!(m_doc = xmlParseFile(filename.c_str() )))
00036         {
00037                 throw XmlException("Parse of file failed: " + filename);
00038         }
00039         if (!(cur = xmlDocGetRootElement(m_doc)))
00040         {
00041                 xmlFreeDoc(m_doc);
00042                 m_doc = NULL;
00043                 throw XmlException("Document is empty: " + filename);
00044         }
00045         if (verify_ns.size())
00046         {
00047                 if (!(ns = xmlSearchNsByHref(m_doc, cur, (const xmlChar *) verify_ns.c_str() )))
00048                 {
00049                         xmlFreeDoc(m_doc);
00050                         m_doc = NULL;
00051                         throw XmlException("Document namespace != " + verify_ns);
00052                 }
00053         }
00054         if (verify_root.size())
00055         {
00056                 if (xmlStrcmp(cur -> name, (const xmlChar *) verify_root.c_str() ))
00057                 {
00058                         xmlFreeDoc(m_doc);
00059                         m_doc = NULL;
00060                         throw XmlException("Document root != " + verify_root);
00061                 }
00062         }
00063         m_ok = true;
00064 }
00065 
00066 
00067 XmlDocument::~XmlDocument()
00068 {
00069         if (m_doc)
00070         {
00071                 xmlFreeDoc(m_doc);
00072         }
00073 }
00074 
00075 
00076 XmlDocument::operator xmlDocPtr()
00077 {
00078         return m_doc;
00079 }
00080 
00081 
00082 }

Generated on Sun Feb 10 17:01:02 2008 for xmlw - libxml2 C++ wrapper by  doxygen 1.5.2