2
Copyright (c) 2008-2009 NetAllied Systems GmbH
4
This file is part of GeneratedSaxParser.
6
Licensed under the MIT Open Source License,
7
for details please see LICENSE file or the website
8
http://www.opensource.org/licenses/mit-license.php
14
#include "GeneratedSaxParserLibxmlSaxParser.h"
15
#include "GeneratedSaxParserParser.h"
16
#include "GeneratedSaxParserIErrorHandler.h"
18
#include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
21
namespace GeneratedSaxParser
24
xmlSAXHandler LibxmlSaxParser::SAXHANDLER =
26
0, //internalSubsetSAXFunc internalSubset;
27
0, //isStandaloneSAXFunc isStandalone;
28
0, //hasInternalSubsetSAXFunc hasInternalSubset;
29
0, //hasExternalSubsetSAXFunc hasExternalSubset;
30
0, //resolveEntitySAXFunc resolveEntity;
31
0, //getEntitySAXFunc getEntity;
32
0, //entityDeclSAXFunc entityDecl;
33
0, //notationDeclSAXFunc notationDecl;
34
0, //attributeDeclSAXFunc attributeDecl;
35
0, //elementDeclSAXFunc elementDecl;
36
0, //unparsedEntityDeclSAXFunc unparsedEntityDecl;
37
0, //setDocumentLocatorSAXFunc setDocumentLocator;
38
0, //startDocumentSAXFunc startDocument;
39
0, //endDocumentSAXFunc endDocument;
40
&LibxmlSaxParser::startElement, //startElementSAXFunc startElement;
41
&LibxmlSaxParser::endElement, //endElementSAXFunc endElement;
42
0, //referenceSAXFunc reference;
43
&LibxmlSaxParser::characters, //charactersSAXFunc characters;
44
0, //ignorableWhitespaceSAXFunc ignorableWhitespace;
45
0, //processingInstructionSAXFunc processingInstruction;
46
0, //commentSAXFunc comment;
47
0, //warningSAXFunc warning;
48
&LibxmlSaxParser::errorFunction, //errorSAXFunc error;
49
&LibxmlSaxParser::errorFunction //fatalErrorSAXFunc fatalError;
54
//--------------------------------------------------------------------
55
LibxmlSaxParser::LibxmlSaxParser(Parser* parser)
61
//--------------------------------------------------------------------
62
LibxmlSaxParser::~LibxmlSaxParser()
64
//issue 102: removing call of libxml cleanup as stated in issue.
65
//-> libxml documentation says xmlCleanupParser() should only be called before
66
// process terminates and the libxml-library is not to be used anymore.
70
bool LibxmlSaxParser::parseFile( const char* fileName )
72
mParserContext = xmlCreateFileParserCtxt(fileName);
74
if ( !mParserContext )
76
ParserError error(ParserError::SEVERITY_CRITICAL,
77
ParserError::ERROR_COULD_NOT_OPEN_FILE,
83
IErrorHandler* errorHandler = getParser()->getErrorHandler();
86
errorHandler->handleError(error);
91
// We let libxml replace the entities
92
mParserContext->replaceEntities = 1;
94
if (mParserContext->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
96
xmlFree(mParserContext->sax);
99
mParserContext->sax = &SAXHANDLER;
100
mParserContext->userData = (void*)this;
102
initializeParserContext();
103
xmlParseDocument(mParserContext);
105
mParserContext->sax = 0;
107
if ( mParserContext->myDoc )
109
xmlFreeDoc(mParserContext->myDoc);
110
mParserContext->myDoc = 0;
113
xmlFreeParserCtxt(mParserContext);
119
bool LibxmlSaxParser::parseBuffer( const char* uri, const char* buffer, int length )
121
mParserContext = xmlCreateMemoryParserCtxt( buffer, length );
123
if ( !mParserContext )
125
ParserError error(ParserError::SEVERITY_CRITICAL,
126
ParserError::ERROR_COULD_NOT_OPEN_FILE,
132
IErrorHandler* errorHandler = getParser()->getErrorHandler();
135
errorHandler->handleError(error);
140
// We let libxml replace the entities
141
mParserContext->replaceEntities = 1;
143
if (mParserContext->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
145
xmlFree(mParserContext->sax);
148
mParserContext->sax = &SAXHANDLER;
149
mParserContext->userData = (void*)this;
151
initializeParserContext();
152
xmlParseDocument(mParserContext);
154
mParserContext->sax = 0;
156
if ( mParserContext->myDoc )
158
xmlFreeDoc(mParserContext->myDoc);
159
mParserContext->myDoc = 0;
162
xmlFreeParserCtxt(mParserContext);
168
void LibxmlSaxParser::initializeParserContext()
170
mParserContext->linenumbers = true;
171
mParserContext->validate = false;
175
void LibxmlSaxParser::startElement( void* user_data, const ::xmlChar* name, const ::xmlChar** attrs )
177
LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
178
Parser* parser = thisObject->getParser();
179
if ( !parser->elementBegin((const ParserChar*)name, (const ParserChar**)attrs) )
180
thisObject->abortParsing();
183
void LibxmlSaxParser::endElement( void* user_data, const ::xmlChar* name)
185
LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
186
Parser* parser = thisObject->getParser();
187
if ( !parser->elementEnd((const ParserChar*)name) )
188
thisObject->abortParsing();
192
void LibxmlSaxParser::characters( void* user_data, const ::xmlChar* name, int length )
194
LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
195
Parser* parser = thisObject->getParser();
196
if ( !parser->textData((const ParserChar*)name, (size_t)length) )
197
thisObject->abortParsing();
200
void LibxmlSaxParser::abortParsing()
202
xmlStopParser(mParserContext);
205
size_t LibxmlSaxParser::getLineNumer() const
207
return (size_t)xmlSAX2GetLineNumber(mParserContext);
210
size_t LibxmlSaxParser::getColumnNumer() const
212
return (size_t)xmlSAX2GetColumnNumber(mParserContext);
215
void LibxmlSaxParser::errorFunction( void *userData, const char *msg, ... )
217
// if msg is just one string, get it. Otherwise ignore it.
220
if (strcmp(msg, "%s") == 0)
222
va_start(argList, msg);
223
message = va_arg(argList, char*);
226
LibxmlSaxParser* thisObject = (LibxmlSaxParser*)(userData);
227
ParserError error(ParserError::SEVERITY_CRITICAL,
228
ParserError::ERROR_XML_PARSER_ERROR,
233
message != 0 ? message : msg);
240
IErrorHandler* errHandler = thisObject->getParser()->getErrorHandler();
242
errHandler->handleError(error);
245
} // namespace GeneratedSaxParser