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
11
#include "GeneratedSaxParserExpatSaxParser.h"
14
#define XML_STATIC // to link against static version of expat
20
#include "GeneratedSaxParserParser.h"
23
namespace GeneratedSaxParser
28
//--------------------------------------------------------------------
29
ExpatSaxParser::ExpatSaxParser(Parser* parser, size_t bufferSize)
32
, mBufferSize(bufferSize)
36
//--------------------------------------------------------------------
37
ExpatSaxParser::~ExpatSaxParser()
41
//--------------------------------------------------------------------
42
bool ExpatSaxParser::parseBuffer(const char* uri, const char* buffer, int length)
45
mParser = XML_ParserCreate(0);
47
XML_SetUserData(mParser, this);
48
XML_SetElementHandler(mParser, startElement, endElement);
49
XML_SetCharacterDataHandler(mParser, characters);
52
XML_Status status = XML_STATUS_OK;
54
XML_Parse(mParser, buffer, (int)length, isFinal);
56
XML_ParserFree(mParser);
58
return status != XML_STATUS_ERROR;
60
bool ExpatSaxParser::parseFile( const char* fileName )
63
fd = fopen(fileName, "rb");
67
//XML_ParserCreate(const XML_Char *encoding);
68
mParser = XML_ParserCreate(0);
70
XML_SetUserData(mParser, this);
71
XML_SetElementHandler(mParser, startElement, endElement);
72
XML_SetCharacterDataHandler(mParser, characters);
76
buffer = (char*) malloc (sizeof(char)*mBufferSize);
79
XML_Status status = XML_STATUS_OK;
80
while (!feof(fd) && (status != XML_STATUS_ERROR) )
82
size_t length = fread(buffer, 1, mBufferSize, fd);
83
status = XML_Parse(mParser, buffer, (int)length, feof(fd));
88
XML_ParserFree(mParser);
90
return status != XML_STATUS_ERROR;
93
//--------------------------------------------------------------------
94
void ExpatSaxParser::startElement( void* user_data, const XML_Char* name, const XML_Char** attrs )
96
ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
97
Parser* parser = thisObject->getParser();
98
if ( !parser->elementBegin((const ParserChar*)name, (const ParserChar**)attrs) )
99
thisObject->abortParsing();
103
//--------------------------------------------------------------------
104
void ExpatSaxParser::endElement( void* user_data, const XML_Char* name)
106
ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
107
Parser* parser = thisObject->getParser();
108
if ( !parser->elementEnd((const ParserChar*)name) )
109
thisObject->abortParsing();
113
//--------------------------------------------------------------------
114
void ExpatSaxParser::characters( void* user_data, const XML_Char* name, int length )
116
ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
117
Parser* parser = thisObject->getParser();
118
if ( !parser->textData((const ParserChar*)name, (size_t)length) )
119
thisObject->abortParsing();
122
//--------------------------------------------------------------------
123
size_t ExpatSaxParser::getLineNumer() const
125
return (size_t) XML_GetCurrentLineNumber(mParser);
129
//--------------------------------------------------------------------
130
size_t ExpatSaxParser::getColumnNumer() const
132
return (size_t) XML_GetCurrentColumnNumber(mParser);
135
//--------------------------------------------------------------------
136
void ExpatSaxParser::abortParsing()
138
XML_StopParser(mParser, false);
141
} // namespace GeneratedSaxParser