nxmlparser.cpp

Go to the documentation of this file.
00001 // LICENSE: (Please see the file COPYING for details)
00002 //
00003 // NUS - Nemesis Utilities System: A C++ application development framework 
00004 // Copyright (C) 2006, 2008 Otavio Rodolfo Piske
00005 //
00006 //  This file is part of NUS
00007 //
00008 //  This library is free software; you can redistribute it and/or
00009 //  modify it under the terms of the GNU Lesser General Public
00010 //  License as published by the Free Software Foundation version 2.1
00011 //  of the License.
00012 //
00013 //  This library is distributed in the hope that it will be useful,
00014 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 //  Lesser General Public License for more details.
00017 //
00018 //  You should have received a copy of the GNU Lesser General Public
00019 //  License along with this library; if not, write to the Free Software
00020 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021 //
00022 #include "nxmlparser.h"
00023 
00028 NXmlParser::NXmlParser(void)
00029       : NXmlAbstractParser(),
00030       m_parser_ctxt(NULL)
00031 {
00032       NDebug::print() << "Creating ... ";
00033       xmlInitParser();
00034       m_parser_ctxt = xmlNewParserCtxt();
00035       if (!m_parser_ctxt) { 
00036             throw NXmlError::getLastError("Unable to create parser context",
00037                   NXmlException::XML_PARSER_ERROR);
00038       }
00039       NDebug::print() << "Done creating ... ";
00040 }
00041 
00042 NXmlParser::~NXmlParser(void) { 
00043       xmlFreeParserCtxt(m_parser_ctxt);
00044 }
00045 
00046 xmlDocPtr NXmlParser::parse(const NString &filename) { 
00047       xmlDocPtr ret = NULL;
00048       const char *enc = NULL;
00049       
00050       if (!m_encoding.isNull()) {
00051             enc = m_encoding.toChar();
00052       }
00053       
00054       NDebug::print() << "Parsing ... ";
00055       
00056       // XML_PARSE_NOERROR makes the libxml parser be silent about error
00057       // reporting ... after all, it's our job to report them upstream.
00058       ret = xmlCtxtReadFile(m_parser_ctxt, filename.toChar(), enc,
00059                         XML_PARSE_DTDVALID 
00060                         | XML_PARSE_NOERROR 
00061                         | XML_PARSE_NOWARNING);
00062       if (!ret || m_parser_ctxt->valid == 0) { 
00063             throw NXmlError::getLastError("XML validation failed", 
00064                         NXmlException::XML_PARSER_ERROR);
00065       }
00066       
00067       return ret;
00068 }
00069 
00070 
00071 xmlDocPtr NXmlParser::parse(const NString &buffer, const NString &url) {
00072       xmlDocPtr ret = NULL;
00073       const char *enc = NULL;
00074       
00075       if (!m_encoding.isNull()) {
00076             enc = m_encoding.toChar();
00077       }
00078       
00079       ret = xmlCtxtReadMemory(m_parser_ctxt, buffer.toChar(), buffer.length(),
00080                         url.toChar(), enc, 
00081                         XML_PARSE_DTDVALID 
00082                         | XML_PARSE_NOERROR 
00083                           | XML_PARSE_NOWARNING);
00084       return ret;
00085 }
00086 
00087 
00088 xmlDocPtr NXmlParser::parse(const NFile *file, const NString &url) { 
00089       xmlDocPtr ret = NULL;
00090       const char *enc = NULL;
00091       
00092       if (!file) { 
00093             throw NXmlException("Invalid 'file' object: null object", 
00094                   NException::XML, NXmlException::XML_INIT_ERROR);
00095       }
00096       
00097       if (!m_encoding.isNull()) {
00098             enc = m_encoding.toChar();
00099       }
00100       
00101       ret = xmlCtxtReadFd(m_parser_ctxt, file->handle(), url.toChar(),
00102                         enc, XML_PARSE_DTDVALID);
00103       return ret;
00104 }

Generated on Wed Mar 5 23:10:36 2008 for NemesisUtilitiesSystem by  doxygen 1.5.4