Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / stream / XmlPullStreamReader.java @ 19593

History | View | Annotate | Download (4.37 KB)

1
package org.gvsig.gpe.xml.stream;
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
*
4
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
*
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU General Public License
8
* as published by the Free Software Foundation; either version 2
9
* of the License, or (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
*
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
*
20
* For more information, contact:
21
*
22
*  Generalitat Valenciana
23
*   Conselleria d'Infraestructures i Transport
24
*   Av. Blasco Ib??ez, 50
25
*   46010 VALENCIA
26
*   SPAIN
27
*
28
*      +34 963862235
29
*   gvsig@gva.es
30
*      www.gvsig.gva.es
31
*
32
*    or
33
*
34
*   IVER T.I. S.A
35
*   Salamanca 50
36
*   46005 Valencia
37
*   Spain
38
*
39
*   +34 963163400
40
*   dac@iver.es
41
*/
42
/* CVS MESSAGES:
43
*
44
* $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
45
* $Log$
46
*/
47

    
48
import java.io.IOException;
49
import java.io.InputStream;
50

    
51
import org.kxml2.io.KXmlParser;
52
import org.xmlpull.v1.XmlPullParser;
53
import org.xmlpull.v1.XmlPullParserException;
54

    
55
/**
56
 * An {@link IXmlStreamReader} adapter for xml textual parsing using a
57
 * {@link XmlPullParser pull parser}.
58
 * 
59
 * @author Gabriel Roldan (TOPP)
60
 * @version $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
61
 */
62
public class XmlPullStreamReader implements IXmlStreamReader {
63

    
64
    private XmlPullParser parser;
65

    
66
    public XmlPullStreamReader() {
67
        parser = new KXmlParser();
68
    }
69

    
70
    public void setInput(InputStream inputStream, String encoding) throws XmlStreamException {
71
        try {
72
            parser.setInput(inputStream, encoding);
73
        } catch (XmlPullParserException e) {
74
            throw new XmlStreamException(e);
75
        }
76
    }
77

    
78
    public int getAttributeCount() {
79
        return parser.getAttributeCount();
80
    }
81

    
82
    public String getAttributeName(int i) {
83
        return parser.getAttributeName(i);
84
    }
85

    
86
    public String getAttributeValue(int i) {
87
        return parser.getAttributeValue(i);
88
    }
89

    
90
    public int getEventType() throws XmlStreamException {
91
        // TODO: improve this mapping
92
        int xmlPullEventType;
93
        try {
94
            xmlPullEventType = parser.getEventType();
95
        } catch (XmlPullParserException e) {
96
            throw new XmlStreamException(e);
97
        }
98
        return pullEventToGpeEventType(xmlPullEventType);
99
    }
100

    
101
    public String getName() {
102
        return parser.getName();
103
    }
104

    
105
    public String getText() {
106
        return parser.getText();
107
    }
108

    
109
    public boolean isWhitespace() throws XmlStreamException {
110
        try {
111
            return parser.isWhitespace();
112
        } catch (XmlPullParserException e) {
113
            throw new XmlStreamException(e);
114
        }
115
    }
116

    
117
    public int next() throws XmlStreamException {
118
        int xmlPullEventType;
119
        try {
120
            xmlPullEventType = parser.next();
121
        } catch (XmlPullParserException e) {
122
            throw new XmlStreamException(e);
123
        } catch (IOException e) {
124
            throw new XmlStreamException(e);
125
        }
126
        return pullEventToGpeEventType(xmlPullEventType);
127
    }
128

    
129
    private int pullEventToGpeEventType(int xmlPullEventType) {
130
        switch (xmlPullEventType) {
131
        case XmlPullParser.START_DOCUMENT:
132
            return START_DOCUMENT;
133
        case XmlPullParser.END_DOCUMENT:
134
            return END_DOCUMENT;
135
        case XmlPullParser.START_TAG:
136
            return START_TAG;
137
        case XmlPullParser.END_TAG:
138
            return END_TAG;
139
        case XmlPullParser.TEXT:
140
            return TEXT;
141
        case XmlPullParser.CDSECT:
142
            return CDSECT;
143
        case XmlPullParser.ENTITY_REF:
144
            return ENTITY_REF;
145
        case XmlPullParser.IGNORABLE_WHITESPACE:
146
            return IGNORABLE_WHITESPACE;
147
        case XmlPullParser.PROCESSING_INSTRUCTION:
148
            return PROCESSING_INSTRUCTION;
149
        case XmlPullParser.COMMENT:
150
            return COMMENT;
151
        case XmlPullParser.DOCDECL:
152
            return DOCDECL;
153
        default:
154
            throw new IllegalStateException("Unknown tag type, this should't happen!: "
155
                    + xmlPullEventType);
156
        }
157
    }
158

    
159
}