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

« back to all changes in this revision

Viewing changes to GeneratedSaxParser/src/GeneratedSaxParserExpatSaxParser.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 "GeneratedSaxParserExpatSaxParser.h"
 
12
 
 
13
 
 
14
#define XML_STATIC  // to link against static version of expat
 
15
#include "expat.h"
 
16
#undef XML_STATIC  
 
17
#include <stdlib.h>
 
18
#include <stdio.h>
 
19
#include <iostream>
 
20
#include "GeneratedSaxParserParser.h"
 
21
 
 
22
 
 
23
namespace GeneratedSaxParser
 
24
{
 
25
        
 
26
 
 
27
 
 
28
        //--------------------------------------------------------------------
 
29
        ExpatSaxParser::ExpatSaxParser(Parser* parser, size_t bufferSize)
 
30
                : SaxParser(parser)
 
31
                , mParser(0)
 
32
                , mBufferSize(bufferSize)
 
33
        {
 
34
        }
 
35
 
 
36
        //--------------------------------------------------------------------
 
37
        ExpatSaxParser::~ExpatSaxParser()
 
38
        {
 
39
        }
 
40
 
 
41
        //--------------------------------------------------------------------
 
42
        bool ExpatSaxParser::parseBuffer(const char* uri, const char* buffer, int length)
 
43
        {
 
44
                
 
45
                mParser = XML_ParserCreate(0);
 
46
 
 
47
                XML_SetUserData(mParser, this);
 
48
                XML_SetElementHandler(mParser, startElement, endElement);
 
49
                XML_SetCharacterDataHandler(mParser, characters);
 
50
 
 
51
 
 
52
                XML_Status status = XML_STATUS_OK;
 
53
                bool isFinal = true;
 
54
                XML_Parse(mParser, buffer, (int)length, isFinal);
 
55
 
 
56
                XML_ParserFree(mParser);
 
57
 
 
58
                return status != XML_STATUS_ERROR;
 
59
        }
 
60
        bool ExpatSaxParser::parseFile( const char* fileName )
 
61
        {
 
62
                FILE *fd;
 
63
                fd = fopen(fileName, "rb");
 
64
                if (!fd)
 
65
                        return false;
 
66
 
 
67
                //XML_ParserCreate(const XML_Char *encoding);
 
68
                mParser = XML_ParserCreate(0);
 
69
 
 
70
                XML_SetUserData(mParser, this);
 
71
                XML_SetElementHandler(mParser, startElement, endElement);
 
72
                XML_SetCharacterDataHandler(mParser, characters);
 
73
 
 
74
 
 
75
                char * buffer;
 
76
                buffer = (char*) malloc (sizeof(char)*mBufferSize);
 
77
 
 
78
 
 
79
                XML_Status status = XML_STATUS_OK;
 
80
                while (!feof(fd) && (status != XML_STATUS_ERROR) )
 
81
                {
 
82
                        size_t length = fread(buffer, 1,  mBufferSize, fd);
 
83
                        status = XML_Parse(mParser, buffer, (int)length, feof(fd));
 
84
                }
 
85
 
 
86
                fclose (fd);
 
87
                free (buffer);
 
88
                XML_ParserFree(mParser);
 
89
 
 
90
                return status != XML_STATUS_ERROR;
 
91
        }
 
92
 
 
93
        //--------------------------------------------------------------------
 
94
        void ExpatSaxParser::startElement( void* user_data, const XML_Char* name, const XML_Char** attrs )
 
95
        {
 
96
                ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
 
97
                Parser* parser = thisObject->getParser();
 
98
                if ( !parser->elementBegin((const ParserChar*)name, (const ParserChar**)attrs) )
 
99
                        thisObject->abortParsing();
 
100
 
 
101
        }
 
102
 
 
103
        //--------------------------------------------------------------------
 
104
        void ExpatSaxParser::endElement( void* user_data, const XML_Char* name)
 
105
        {
 
106
                ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
 
107
                Parser* parser = thisObject->getParser();
 
108
                if ( !parser->elementEnd((const ParserChar*)name) )
 
109
                        thisObject->abortParsing();
 
110
        }
 
111
 
 
112
 
 
113
        //--------------------------------------------------------------------
 
114
        void ExpatSaxParser::characters( void* user_data, const XML_Char* name, int length )
 
115
        {
 
116
                ExpatSaxParser* thisObject = (ExpatSaxParser*)user_data;
 
117
                Parser* parser = thisObject->getParser();
 
118
                if ( !parser->textData((const ParserChar*)name, (size_t)length) )
 
119
                        thisObject->abortParsing();
 
120
        }
 
121
 
 
122
        //--------------------------------------------------------------------
 
123
        size_t ExpatSaxParser::getLineNumer() const
 
124
        {
 
125
                return (size_t) XML_GetCurrentLineNumber(mParser);
 
126
 
 
127
        }
 
128
 
 
129
        //--------------------------------------------------------------------
 
130
        size_t ExpatSaxParser::getColumnNumer() const
 
131
        {
 
132
                return (size_t) XML_GetCurrentColumnNumber(mParser);
 
133
        }
 
134
 
 
135
        //--------------------------------------------------------------------
 
136
        void ExpatSaxParser::abortParsing()
 
137
        {
 
138
                XML_StopParser(mParser, false);
 
139
        }
 
140
 
 
141
} // namespace GeneratedSaxParser