~ubuntu-branches/ubuntu/wily/opencollada/wily-proposed

« back to all changes in this revision

Viewing changes to GeneratedSaxParser/src/GeneratedSaxParserLibxmlSaxParser.cpp

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2015-05-14 17:23:27 UTC
  • Revision ID: package-import@ubuntu.com-20150514172327-f862u8envms01fra
Tags: upstream-0.1.0~20140703.ddf8f47+dfsg1
ImportĀ upstreamĀ versionĀ 0.1.0~20140703.ddf8f47+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2008-2009 NetAllied Systems GmbH
 
3
 
 
4
    This file is part of GeneratedSaxParser.
 
5
 
 
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
 
9
*/
 
10
 
 
11
#include <cstdarg>
 
12
#include <cstring>
 
13
 
 
14
#include "GeneratedSaxParserLibxmlSaxParser.h"
 
15
#include "GeneratedSaxParserParser.h"
 
16
#include "GeneratedSaxParserIErrorHandler.h"
 
17
 
 
18
#include <libxml/parserInternals.h> // for xmlCreateFileParserCtxt
 
19
 
 
20
 
 
21
namespace GeneratedSaxParser
 
22
{
 
23
 
 
24
    xmlSAXHandler LibxmlSaxParser::SAXHANDLER =
 
25
        {
 
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;
 
50
 
 
51
        };
 
52
 
 
53
 
 
54
        //--------------------------------------------------------------------
 
55
        LibxmlSaxParser::LibxmlSaxParser(Parser* parser)
 
56
                : SaxParser(parser),
 
57
                mParserContext(0)
 
58
        {
 
59
        }
 
60
 
 
61
        //--------------------------------------------------------------------
 
62
        LibxmlSaxParser::~LibxmlSaxParser()
 
63
        {
 
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.
 
67
                //xmlCleanupParser();
 
68
        }
 
69
 
 
70
        bool LibxmlSaxParser::parseFile( const char* fileName )
 
71
        {
 
72
                        mParserContext = xmlCreateFileParserCtxt(fileName);
 
73
                        
 
74
                        if ( !mParserContext )
 
75
                        {
 
76
                                ParserError error(ParserError::SEVERITY_CRITICAL,
 
77
                                                                        ParserError::ERROR_COULD_NOT_OPEN_FILE,
 
78
                                                                        0,
 
79
                                                                        0,
 
80
                                                                        0,
 
81
                                                                        0,
 
82
                                                                        fileName);
 
83
                                IErrorHandler* errorHandler = getParser()->getErrorHandler();
 
84
                                if ( errorHandler )
 
85
                                {
 
86
                                        errorHandler->handleError(error);
 
87
                                }
 
88
                                return false;
 
89
                        }
 
90
 
 
91
                        // We let libxml replace the entities
 
92
                        mParserContext->replaceEntities = 1;
 
93
 
 
94
                        if (mParserContext->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
 
95
                        {
 
96
                                xmlFree(mParserContext->sax);
 
97
                        }
 
98
 
 
99
                        mParserContext->sax = &SAXHANDLER;
 
100
                        mParserContext->userData = (void*)this;
 
101
 
 
102
                        initializeParserContext();
 
103
                        xmlParseDocument(mParserContext);
 
104
 
 
105
                        mParserContext->sax = 0;
 
106
 
 
107
                        if ( mParserContext->myDoc )
 
108
                        {
 
109
                                xmlFreeDoc(mParserContext->myDoc);
 
110
                                mParserContext->myDoc = 0;
 
111
                        }
 
112
 
 
113
                        xmlFreeParserCtxt(mParserContext);
 
114
                        mParserContext = 0;
 
115
 
 
116
                        return true;
 
117
        }
 
118
 
 
119
        bool LibxmlSaxParser::parseBuffer( const char* uri, const char* buffer, int length )
 
120
        {
 
121
        mParserContext = xmlCreateMemoryParserCtxt( buffer, length );
 
122
        
 
123
        if ( !mParserContext )
 
124
        {
 
125
            ParserError error(ParserError::SEVERITY_CRITICAL,
 
126
                              ParserError::ERROR_COULD_NOT_OPEN_FILE,
 
127
                              0,
 
128
                              0,
 
129
                              0,
 
130
                              0,
 
131
                              uri);
 
132
            IErrorHandler* errorHandler = getParser()->getErrorHandler();
 
133
            if ( errorHandler )
 
134
            {
 
135
                errorHandler->handleError(error);
 
136
            }
 
137
            return false;
 
138
        }
 
139
        
 
140
        // We let libxml replace the entities
 
141
        mParserContext->replaceEntities = 1;
 
142
 
 
143
        if (mParserContext->sax != (xmlSAXHandlerPtr) &xmlDefaultSAXHandler)
 
144
        {
 
145
            xmlFree(mParserContext->sax);
 
146
        }
 
147
        
 
148
        mParserContext->sax = &SAXHANDLER;
 
149
        mParserContext->userData = (void*)this;
 
150
        
 
151
        initializeParserContext();
 
152
        xmlParseDocument(mParserContext);
 
153
        
 
154
        mParserContext->sax = 0;
 
155
        
 
156
        if ( mParserContext->myDoc )
 
157
        {
 
158
            xmlFreeDoc(mParserContext->myDoc);
 
159
            mParserContext->myDoc = 0;
 
160
        }
 
161
        
 
162
        xmlFreeParserCtxt(mParserContext);
 
163
        mParserContext = 0;
 
164
        
 
165
        return true;
 
166
        }
 
167
 
 
168
        void LibxmlSaxParser::initializeParserContext()
 
169
        {
 
170
                mParserContext->linenumbers = true;
 
171
                mParserContext->validate = false;
 
172
        }
 
173
 
 
174
 
 
175
        void LibxmlSaxParser::startElement( void* user_data, const ::xmlChar* name, const ::xmlChar** attrs )
 
176
        {
 
177
                LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
 
178
                Parser* parser = thisObject->getParser();
 
179
                if ( !parser->elementBegin((const ParserChar*)name, (const ParserChar**)attrs) )
 
180
                        thisObject->abortParsing();
 
181
        }
 
182
 
 
183
        void LibxmlSaxParser::endElement( void* user_data, const ::xmlChar* name)
 
184
        {
 
185
                LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
 
186
                Parser* parser = thisObject->getParser();
 
187
                if ( !parser->elementEnd((const ParserChar*)name) )
 
188
                        thisObject->abortParsing();
 
189
        }
 
190
 
 
191
 
 
192
        void LibxmlSaxParser::characters( void* user_data, const ::xmlChar* name, int length )
 
193
        {
 
194
                LibxmlSaxParser* thisObject = (LibxmlSaxParser*)user_data;
 
195
                Parser* parser = thisObject->getParser();
 
196
                if ( !parser->textData((const ParserChar*)name, (size_t)length) )
 
197
                        thisObject->abortParsing();
 
198
        }
 
199
 
 
200
        void LibxmlSaxParser::abortParsing()
 
201
        {
 
202
                xmlStopParser(mParserContext);
 
203
        }
 
204
 
 
205
        size_t LibxmlSaxParser::getLineNumer() const
 
206
        {
 
207
                return (size_t)xmlSAX2GetLineNumber(mParserContext);
 
208
        }
 
209
 
 
210
        size_t LibxmlSaxParser::getColumnNumer() const
 
211
        {
 
212
                return (size_t)xmlSAX2GetColumnNumber(mParserContext);
 
213
        }
 
214
 
 
215
        void LibxmlSaxParser::errorFunction( void *userData, const char *msg, ... )
 
216
        {
 
217
        // if msg is just one string, get it. Otherwise ignore it.
 
218
        char* message = 0;
 
219
        va_list argList;
 
220
        if (strcmp(msg, "%s") == 0)
 
221
        {
 
222
            va_start(argList, msg); 
 
223
            message = va_arg(argList, char*);
 
224
        }
 
225
 
 
226
                LibxmlSaxParser* thisObject = (LibxmlSaxParser*)(userData);
 
227
                ParserError error(ParserError::SEVERITY_CRITICAL,
 
228
                                        ParserError::ERROR_XML_PARSER_ERROR,
 
229
                                        0,
 
230
                                        0,
 
231
                                        0,
 
232
                                        0,
 
233
                    message != 0 ? message : msg);
 
234
 
 
235
        if (message != 0)
 
236
        {
 
237
            va_end(argList);
 
238
        }
 
239
 
 
240
        IErrorHandler* errHandler = thisObject->getParser()->getErrorHandler();
 
241
        if ( errHandler ) 
 
242
            errHandler->handleError(error);
 
243
        }
 
244
 
 
245
} // namespace GeneratedSaxParser