root / branches / v2_0_0_prep / libraries / libGPE-XML / src / org / gvsig / gpe / xml / stream / kxml / KxmlXmlStreamReader.java @ 27598
History | View | Annotate | Download (7.15 KB)
1 |
package org.gvsig.gpe.xml.stream.kxml; |
---|---|
2 |
|
3 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
4 |
*
|
5 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
20 |
*
|
21 |
* For more information, contact:
|
22 |
*
|
23 |
* Generalitat Valenciana
|
24 |
* Conselleria d'Infraestructures i Transport
|
25 |
* Av. Blasco Ib??ez, 50
|
26 |
* 46010 VALENCIA
|
27 |
* SPAIN
|
28 |
*
|
29 |
* +34 963862235
|
30 |
* gvsig@gva.es
|
31 |
* www.gvsig.gva.es
|
32 |
*
|
33 |
* or
|
34 |
*
|
35 |
* IVER T.I. S.A
|
36 |
* Salamanca 50
|
37 |
* 46005 Valencia
|
38 |
* Spain
|
39 |
*
|
40 |
* +34 963163400
|
41 |
* dac@iver.es
|
42 |
*/
|
43 |
/* CVS MESSAGES:
|
44 |
*
|
45 |
* $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
|
46 |
* $Log$
|
47 |
*/
|
48 |
|
49 |
import java.io.IOException; |
50 |
import java.io.InputStream; |
51 |
import javax.xml.namespace.QName; |
52 |
import javax.xml.stream.XMLStreamReader; |
53 |
|
54 |
import org.gvsig.gpe.xml.stream.IXmlStreamReader; |
55 |
import org.gvsig.gpe.xml.stream.XmlStreamException; |
56 |
import org.kxml2.io.KXmlParser; |
57 |
import org.xmlpull.v1.XmlPullParser; |
58 |
import org.xmlpull.v1.XmlPullParserException; |
59 |
|
60 |
/**
|
61 |
* An {@link IXmlStreamReader} adapter for xml textual parsing using a
|
62 |
* {@link XmlPullParser pull parser}.
|
63 |
*
|
64 |
* @author Gabriel Roldan (TOPP)
|
65 |
* @version $Id: XmlPullStreamReader.java 19593 2008-03-12 17:23:30Z groldan $
|
66 |
*/
|
67 |
public class KxmlXmlStreamReader implements IXmlStreamReader { |
68 |
|
69 |
private XmlPullParser parser;
|
70 |
/**
|
71 |
* Default constructor for this parser
|
72 |
* @param in
|
73 |
* @throws XmlStreamException
|
74 |
*/
|
75 |
public KxmlXmlStreamReader(InputStream in) throws XmlStreamException { |
76 |
setInput(in); |
77 |
} |
78 |
|
79 |
/**
|
80 |
* Sets the internal xmlpull parser input stream
|
81 |
*
|
82 |
* @param inputStream
|
83 |
* @throws XmlStreamException
|
84 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReader#setInput(java.io.InputStream)
|
85 |
*/
|
86 |
public void setInput(final InputStream inputStream) throws XmlStreamException { |
87 |
try {
|
88 |
parser = new KXmlParser();
|
89 |
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
|
90 |
// let the parser inferr the encoding
|
91 |
parser.setInput(inputStream, null);
|
92 |
} catch (XmlPullParserException e) {
|
93 |
throw new XmlStreamException(e); |
94 |
} |
95 |
} |
96 |
|
97 |
public int getAttributeCount() throws XmlStreamException { |
98 |
return parser.getAttributeCount();
|
99 |
} |
100 |
|
101 |
public QName getAttributeName(int i) throws XmlStreamException { |
102 |
return new QName(parser.getNamespace(), parser.getAttributeName(i)); |
103 |
} |
104 |
|
105 |
public String getAttributeValue(int i) throws XmlStreamException { |
106 |
return parser.getAttributeValue(i);
|
107 |
} |
108 |
|
109 |
public int getEventType() throws XmlStreamException { |
110 |
// TODO: improve this mapping
|
111 |
int xmlPullEventType;
|
112 |
try {
|
113 |
xmlPullEventType = parser.getEventType(); |
114 |
} catch (XmlPullParserException e) {
|
115 |
throw new XmlStreamException(e); |
116 |
} |
117 |
return pullEventToGpeEventType(xmlPullEventType);
|
118 |
} |
119 |
|
120 |
/**
|
121 |
* @return
|
122 |
* @see org.gvsig.gpe.xml.stream.IXmlStreamReader#getName()
|
123 |
*/
|
124 |
public QName getName() throws XmlStreamException { |
125 |
int eventType;
|
126 |
try {
|
127 |
eventType = parser.getEventType(); |
128 |
if (eventType != XMLStreamReader.START_ELEMENT
|
129 |
&& eventType != XMLStreamReader.END_ELEMENT |
130 |
&& eventType != XMLStreamReader.PROCESSING_INSTRUCTION |
131 |
&& eventType != XMLStreamReader.CHARACTERS) { |
132 |
return null; |
133 |
} |
134 |
String name = parser.getName();
|
135 |
if (name != null){ |
136 |
name = name.substring(name.indexOf(":") + 1, name.length()); |
137 |
parser.getNamespace(); |
138 |
return new QName(parser.getNamespace(), name); |
139 |
} |
140 |
} catch (XmlPullParserException e) {
|
141 |
return null; |
142 |
} |
143 |
return null; |
144 |
} |
145 |
|
146 |
public String getText() throws XmlStreamException { |
147 |
return parser.getText();
|
148 |
} |
149 |
|
150 |
public boolean isWhitespace() throws XmlStreamException { |
151 |
try {
|
152 |
return parser.isWhitespace();
|
153 |
} catch (XmlPullParserException e) {
|
154 |
throw new XmlStreamException(e); |
155 |
} |
156 |
} |
157 |
|
158 |
public int next() throws XmlStreamException { |
159 |
int xmlPullEventType;
|
160 |
if (getEventType()==XMLStreamReader.END_DOCUMENT)
|
161 |
throw new XmlStreamException("End Document Exception! \n There aren't more items to get."); |
162 |
try {
|
163 |
xmlPullEventType = parser.next(); |
164 |
} catch (XmlPullParserException e) {
|
165 |
throw new XmlStreamException(e); |
166 |
} catch (IOException e) { |
167 |
throw new XmlStreamException(e); |
168 |
} |
169 |
return pullEventToGpeEventType(xmlPullEventType);
|
170 |
} |
171 |
|
172 |
public int nextTag() throws XmlStreamException { |
173 |
int xmlPullEventType;
|
174 |
if (getEventType()==XMLStreamReader.END_DOCUMENT)
|
175 |
throw new XmlStreamException("End Document Exception! \n There aren't more tags to get."); |
176 |
try {
|
177 |
xmlPullEventType = parser.nextTag(); |
178 |
} catch (XmlPullParserException e) {
|
179 |
throw new XmlStreamException(e); |
180 |
} catch (IOException e) { |
181 |
throw new XmlStreamException(e); |
182 |
} |
183 |
return pullEventToGpeEventType(xmlPullEventType);
|
184 |
} |
185 |
|
186 |
private int pullEventToGpeEventType(int xmlPullEventType) { |
187 |
switch (xmlPullEventType) {
|
188 |
case XmlPullParser.START_DOCUMENT:
|
189 |
return IXmlStreamReader.START_DOCUMENT;
|
190 |
case XmlPullParser.END_DOCUMENT:
|
191 |
return IXmlStreamReader.END_DOCUMENT;
|
192 |
case XmlPullParser.START_TAG:
|
193 |
return IXmlStreamReader.START_ELEMENT;
|
194 |
case XmlPullParser.END_TAG:
|
195 |
return IXmlStreamReader.END_ELEMENT;
|
196 |
case XmlPullParser.TEXT:
|
197 |
return IXmlStreamReader.CHARACTERS;
|
198 |
case XmlPullParser.CDSECT:
|
199 |
return IXmlStreamReader.CDATA;
|
200 |
case XmlPullParser.ENTITY_REF:
|
201 |
return IXmlStreamReader.ENTITY_REFERENCE;
|
202 |
case XmlPullParser.IGNORABLE_WHITESPACE:
|
203 |
return IXmlStreamReader.SPACE;
|
204 |
case XmlPullParser.PROCESSING_INSTRUCTION:
|
205 |
return IXmlStreamReader.PROCESSING_INSTRUCTION;
|
206 |
case XmlPullParser.COMMENT:
|
207 |
return IXmlStreamReader.COMMENT;
|
208 |
case XmlPullParser.DOCDECL:
|
209 |
return IXmlStreamReader.DTD;
|
210 |
default:
|
211 |
throw new IllegalStateException("Unknown tag type, this should't happen!: " |
212 |
+ xmlPullEventType); |
213 |
} |
214 |
} |
215 |
|
216 |
} |