nxmlschemaparser.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 "nxmlschemaparser.h"
00023 
00028 NXmlSchemaParser::NXmlSchemaParser(void)
00029       : NXmlAbstractParser(),
00030       m_parser_ctxt(NULL),
00031       m_schema_ptr(NULL),
00032       m_valid_ctxt(NULL)
00033 {
00034       NDebug::print() << "Creating ... ";
00035       xmlInitParser();
00036 }
00037 
00038 
00039 NXmlSchemaParser::~NXmlSchemaParser(void) { 
00040       if (m_parser_ctxt) { 
00041             xmlSchemaFreeParserCtxt(m_parser_ctxt);
00042       }
00043       
00044       if (m_valid_ctxt) { 
00045             xmlSchemaFreeValidCtxt(m_valid_ctxt);
00046       }
00047       
00048       if (m_schema_ptr) { 
00049             xmlSchemaFree(m_schema_ptr);
00050       }
00051 }
00052 
00053 void NXmlSchemaParser::parseSchema(void) { 
00054       m_schema_ptr = xmlSchemaParse(m_parser_ctxt);
00055       
00056       xmlSchemaFreeParserCtxt(m_parser_ctxt);
00057       m_parser_ctxt = NULL;
00058       
00059       if (!m_schema_ptr) { 
00060             throw NXmlError::getLastError("XML schema parsing failed", 
00061                         NXmlException::XML_PARSER_ERROR);
00062       }
00063       
00064       m_valid_ctxt = xmlSchemaNewValidCtxt(m_schema_ptr);
00065       if (!m_valid_ctxt) { 
00066             throw NXmlError::getLastError(
00067                         "Unable to create a valid schema context", 
00068                         NXmlException::XML_INIT_ERROR);
00069       }
00070       
00071 }
00072 
00073 
00074 xmlDocPtr NXmlSchemaParser::parse(const NString &filename) { 
00075       xmlDocPtr ret; 
00076       
00077       ret = xmlReadFile(filename.toChar(), NULL, 
00078                         XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
00079       
00080       //ret = xmlParseFile(filename.toChar());
00081       if (!ret) { 
00082             throw NXmlError::getLastError("XML document parsing failed");
00083       }
00084       
00085       // Create the parser context and do the commom parsing of 
00086       // xml schema structutures
00087       m_parser_ctxt = xmlSchemaNewParserCtxt(m_url.toChar());
00088       if (!m_parser_ctxt) { 
00089             throw NXmlError::getLastError(
00090                         "Unable to create parser context", 
00091                         NXmlException::XML_INIT_ERROR);
00092       }
00093       parseSchema();
00094       
00095       // Validate the doc against the schema
00096       if (xmlSchemaValidateDoc(m_valid_ctxt, ret)) { 
00097             throw NXmlError::getLastError("XML validation failed",
00098                         NXmlException::XML_PARSER_ERROR);
00099       }
00100             
00101       return ret;
00102 }
00103 
00104 
00105 xmlDocPtr NXmlSchemaParser::parse(const NString &buffer, const NString &url) { 
00106       xmlDocPtr ret; 
00107       
00108       ret = xmlReadMemory(buffer.toChar(), buffer.length(), url.toChar(),
00109                       NULL, XML_PARSE_DTDVALID 
00110                         | XML_PARSE_NOERROR 
00111                           | XML_PARSE_NOWARNING);
00112       if (!ret) { 
00113             throw NXmlError::getLastError("XML document parsing failed", 
00114                         NXmlException::XML_PARSER_ERROR);
00115       }
00116       
00117       // Create the parser context and do the commom parsing of 
00118       // xml schema structutures
00119       m_parser_ctxt = xmlSchemaNewDocParserCtxt(ret);
00120       if (!m_parser_ctxt) { 
00121             throw NXmlError::getLastError("Unable to create parser context",
00122                         NXmlException::XML_INIT_ERROR);
00123       }
00124       parseSchema();
00125       
00126       // Validate the doc against the schema
00127       if (xmlSchemaValidateDoc(m_valid_ctxt, ret)) { 
00128             throw NXmlError::getLastError("XML validation failed",
00129                         NXmlException::XML_PARSER_ERROR);
00130       }
00131       
00132       return ret;
00133 }
00134 
00135 
00136 xmlDocPtr NXmlSchemaParser::parse(const NFile *file, const NString &url) { 
00137       xmlDocPtr ret; 
00138       
00139       if (!file) { 
00140             throw NXmlException("Invalid 'file' object: null object", 
00141                         NException::XML, NXmlException::XML_INIT_ERROR);
00142       }
00143       
00144       ret = xmlReadFd(file->handle(), url.toChar(), NULL, 
00145                         XML_PARSE_DTDVALID 
00146                         | XML_PARSE_NOERROR 
00147                           | XML_PARSE_NOWARNING);
00148       if (!ret) { 
00149             throw NXmlError::getLastError("XML document parsing failed",
00150                         NXmlException::XML_PARSER_ERROR);
00151       }
00152       
00153       // Create the parser context and do the commom parsing of 
00154       // xml schema structutures
00155       m_parser_ctxt = xmlSchemaNewDocParserCtxt(ret);
00156       if (!m_parser_ctxt) { 
00157             throw NXmlError::getLastError("Unable to create parser context",
00158                         NXmlException::XML_INIT_ERROR);
00159       }
00160       parseSchema();
00161       
00162       // Validate the doc against the schema
00163       if (xmlSchemaValidateDoc(m_valid_ctxt, ret)) { 
00164             throw NXmlError::getLastError("XML validation failed", 
00165                         NXmlException::XML_PARSER_ERROR);
00166       }
00167       
00168       return ret;
00169 }

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