00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00057
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 }