nxmlreader.cpp

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 "nxmlreader.h"
00023 
00028 NXmlReader::NXmlReader(void)
00029       : m_reader(NULL),
00030       m_doc(NULL),
00031       m_xml_doc(NULL),
00032       m_schema(NXmlAbstractParser::XMLNOPARSING),
00033       m_parser(NULL),
00034       m_url(),
00035       m_encoding()
00036 {
00037 
00038 }
00039 
00040 
00041 NXmlReader::~NXmlReader(void) {
00042       if (m_parser) { 
00043             delete m_parser;
00044       }
00045 }
00046 
00047 const char *NXmlReader::evalEncoding(const NString &encoding) { 
00048       if (encoding == "") { 
00049             return NULL;
00050       }
00051       
00052       return encoding.toChar();
00053 }
00054 
00055 
00056 void NXmlReader::setupRootElement(void) { 
00057       NXmlNode node(xmlDocGetRootElement(m_doc));
00058       
00059       m_xml_doc->setRootNode(node);
00060 }
00061 
00062 
00063 void NXmlReader::setSchema(NXmlAbstractParser::XmlSchema schema) {
00064       m_schema = schema;
00065 }
00066 
00067 
00068 NXmlAbstractParser::XmlSchema NXmlReader::getSchema(void) const {
00069       return m_schema;
00070 }
00071 
00072 
00073 void NXmlReader::setEncoding(const NString &encoding) { 
00074       m_encoding = encoding;
00075 }
00076 
00077 
00078 NString NXmlReader::getEncoding(void) const {
00079       return m_encoding;
00080 }
00081 
00082 void NXmlReader::setDefinitionUrl(const NString &url) {
00083       m_url = url;
00084 }
00085 
00086 
00087 NString NXmlReader::getDefinitionUrl(void) const {
00088       return m_url;
00089 }
00090 
00091 
00092 NXmlDocument *NXmlReader::getDocument(void) { 
00093       return m_xml_doc;
00094 }
00095 
00096 
00097 void NXmlReader::initDocument(void) { 
00098       m_xml_doc = new NXmlDocument;
00099       m_xml_doc->setXmlDoc(m_doc);
00100       
00101       setupRootElement();
00102 }
00103 
00104 
00105 void NXmlReader::load(const NString &path) {
00106       switch (m_schema) { 
00107             case NXmlAbstractParser::XMLDTD: { 
00108                   m_parser = new NXmlParser;
00109                   m_doc = m_parser->parse(path);
00110                   
00111                   break;
00112                   
00113             }
00114             case NXmlAbstractParser::XMLSCHEMA: {
00115                   m_parser = new NXmlSchemaParser;
00116                   
00117                   m_parser->setDefinitionUrl(m_url);
00118                   m_doc = m_parser->parse(path);
00119                   break;
00120             }
00121             
00122             case NXmlAbstractParser::XMLNOPARSING:
00123             default: { 
00124                   loadNoParse(path);
00125                   break;
00126             }
00127       }
00128             
00129       initDocument();
00130 }
00131 
00132 
00133 void NXmlReader::loadNoParse(const NString &path) { 
00134       m_reader = xmlReaderForFile(path.toChar(), 
00135                             evalEncoding(m_encoding), 0);
00136             
00137       if (!m_reader) { 
00138             throw NXmlError::getLastError(NSTR("Unable to read from file ")
00139                         + path, NXmlException::XML_INIT_ERROR);
00140       }
00141       
00142       xmlTextReaderRead(m_reader);
00143       m_doc = xmlTextReaderCurrentDoc(m_reader);
00144       
00145       if (!m_doc) { 
00146             throw NXmlError::getLastError("Unable to create a xml doc object",
00147                   NXmlException::XML_INIT_ERROR);
00148       }
00149 }
00150 
00151 
00152 void NXmlReader::load(const NString &buffer, nint32 size) {
00153       switch (m_schema) { 
00154             case NXmlAbstractParser::XMLDTD: { 
00155                   m_parser = new NXmlParser;
00156                   m_doc = m_parser->parse(buffer, m_url);
00157                   
00158                   break;
00159             }
00160             case NXmlAbstractParser::XMLSCHEMA: {
00161                   m_parser = new NXmlSchemaParser;
00162                   
00163                   m_parser->setDefinitionUrl(m_url);
00164                   m_doc = m_parser->parse(buffer, m_url);
00165                   
00166                   break;
00167             }
00168             
00169             case NXmlAbstractParser::XMLNOPARSING:
00170             default: { 
00171                   loadNoParse(buffer, size);
00172                   break;
00173             }
00174       }
00175       
00176       initDocument();
00177 }
00178 
00179 
00180 void NXmlReader::loadNoParse(const NString &buffer, nint32 size) {
00181       if (buffer == "") { 
00182             NDebug::print() << "The buffer to parse is empty";
00183       }
00184       
00185       m_reader = xmlReaderForMemory(buffer.toChar(), size, m_url.toChar(),
00186                               evalEncoding(m_encoding), 0);
00187       
00188       xmlTextReaderRead(m_reader);
00189       
00190       m_doc = xmlTextReaderCurrentDoc(m_reader);
00191       
00192       if (!m_doc) { 
00193             throw NXmlError::getLastError("Unable to create a xml doc object",
00194                         NXmlException::XML_INIT_ERROR);
00195       }
00196 }
00197 
00198 
00199 void NXmlReader::load(const NFile *file) {
00200       switch (m_schema) { 
00201             case NXmlAbstractParser::XMLDTD: { 
00202                   m_parser = new NXmlParser;
00203                   m_doc = m_parser->parse(file, m_url);
00204                   break;
00205             }
00206             case NXmlAbstractParser::XMLSCHEMA: {
00207                   m_parser = new NXmlSchemaParser;
00208                   
00209                   m_parser->setDefinitionUrl(m_url);
00210                   m_doc = m_parser->parse(file, m_url);
00211                   break;
00212             }
00213             
00214             case NXmlAbstractParser::XMLNOPARSING:
00215             default: { 
00216                   loadNoParse(file);
00217                   break;
00218             }
00219       }
00220       
00221       initDocument();
00222 } 
00223 
00224 
00225 
00226 void NXmlReader::loadNoParse(const NFile *file) { 
00227       if (!file) { 
00228             throw NXmlException("Invalid 'file' object: null object", 
00229                         NException::XML, NXmlException::XML_INIT_ERROR);
00230       }
00231       
00232       m_reader = xmlReaderForFd(file->handle(), m_url.toChar(), 
00233                           evalEncoding(m_encoding), 0);
00234       
00235       if (!m_reader) { 
00236             throw NXmlError::getLastError("Unable to read from file handle",
00237                   NXmlException::XML_INIT_ERROR);
00238       }
00239       
00240       xmlTextReaderRead(m_reader);
00241       
00242       m_doc = xmlTextReaderCurrentDoc(m_reader);
00243       
00244       if (!m_doc) { 
00245             throw NXmlError::getLastError("Unable to create a xml doc object", 
00246                   NXmlException::XML_INIT_ERROR);
00247       }
00248 }

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