00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00081 if (!ret) {
00082 throw NXmlError::getLastError("XML document parsing failed");
00083 }
00084
00085
00086
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
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
00118
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
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
00154
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
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 }