Revision 21966 trunk/libraries/libGPE-KML/src/org/gvsig/gpe/kml/parser/GPEDeafultKmlParser.java
GPEDeafultKmlParser.java | ||
---|---|---|
1 | 1 |
package org.gvsig.gpe.kml.parser; |
2 | 2 |
|
3 |
import java.io.BufferedInputStream; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileInputStream; |
|
6 |
import java.io.FileNotFoundException; |
|
7 | 3 |
import java.io.IOException; |
8 |
import java.io.InputStream; |
|
9 |
import java.net.URI; |
|
10 |
import java.util.zip.ZipEntry; |
|
11 |
import java.util.zip.ZipException; |
|
12 |
import java.util.zip.ZipFile; |
|
13 |
import java.util.zip.ZipInputStream; |
|
14 | 4 |
|
15 | 5 |
import javax.xml.namespace.QName; |
16 | 6 |
|
17 | 7 |
import org.gvsig.exceptions.BaseException; |
18 |
import org.gvsig.gpe.kml.exceptions.KmlException; |
|
19 | 8 |
import org.gvsig.gpe.kml.exceptions.KmlHeaderParseException; |
20 | 9 |
import org.gvsig.gpe.kml.parser.profiles.IBindingProfile; |
21 |
import org.gvsig.gpe.kml.utils.KmlTags; |
|
10 |
import org.gvsig.gpe.kml.utils.Kml2_1_Tags;
|
|
22 | 11 |
import org.gvsig.gpe.xml.parser.GPEXmlParser; |
23 | 12 |
import org.gvsig.gpe.xml.stream.IXmlStreamReader; |
24 | 13 |
import org.gvsig.gpe.xml.stream.XmlStreamException; |
... | ... | |
129 | 118 |
public GPEDeafultKmlParser() { |
130 | 119 |
super(); |
131 | 120 |
} |
132 |
|
|
121 |
|
|
133 | 122 |
/* |
134 | 123 |
* (non-Javadoc) |
135 |
* @see org.gvsig.gpe.GPEParser#accept(java.io.File) |
|
136 |
*/ |
|
137 |
public boolean accept(URI uri) { |
|
138 |
if ((uri.getPath().toUpperCase().endsWith("KML")) || |
|
139 |
(uri.getPath().toUpperCase().endsWith("KMZ"))){ |
|
140 |
return true; |
|
141 |
} |
|
142 |
return false; |
|
143 |
} |
|
144 |
|
|
145 |
/* |
|
146 |
* (non-Javadoc) |
|
147 |
* @see org.gvsig.gpe.GPEParser#getFormats() |
|
148 |
*/ |
|
149 |
public String[] getFormats() { |
|
150 |
String[] formats = new String[2]; |
|
151 |
formats[0] = "KML"; |
|
152 |
formats[1] = "KMZ"; |
|
153 |
return formats; |
|
154 |
} |
|
155 |
|
|
156 |
/* |
|
157 |
* (non-Javadoc) |
|
158 |
* @see org.gvsig.gpe.GPEParser#getVersions() |
|
159 |
*/ |
|
160 |
public String[] getVersions() { |
|
161 |
String[] versions = new String[0]; |
|
162 |
versions[0] = "2.1"; |
|
163 |
return versions; |
|
164 |
|
|
165 |
} |
|
166 |
|
|
167 |
/** |
|
168 |
* It creates an InputStream. The Kml file can have the |
|
169 |
* KML or the KMZ extension |
|
170 |
* @return |
|
171 |
* @throws KmlException |
|
172 |
*/ |
|
173 |
protected InputStream createInputStream(File file){ |
|
174 |
//KMZ |
|
175 |
try { |
|
176 |
FileInputStream fis = new FileInputStream(file); |
|
177 |
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); |
|
178 |
ZipEntry entry = null; |
|
179 |
while((entry = zis.getNextEntry()) != null){ |
|
180 |
if (!entry.isDirectory()){ |
|
181 |
ZipFile fz = new ZipFile(file); |
|
182 |
return fz.getInputStream(entry); |
|
183 |
} |
|
184 |
} |
|
185 |
} catch (FileNotFoundException e) { |
|
186 |
getErrorHandler().addError(e); |
|
187 |
} catch (ZipException e) { |
|
188 |
getErrorHandler().addError(e); |
|
189 |
} catch (IOException e) { |
|
190 |
getErrorHandler().addError(e); |
|
191 |
} |
|
192 |
//KML |
|
193 |
try{ |
|
194 |
return new FileInputStream(file); |
|
195 |
} catch (FileNotFoundException e) { |
|
196 |
getErrorHandler().addError(e); |
|
197 |
} |
|
198 |
return null; |
|
199 |
} |
|
200 |
|
|
201 |
/* |
|
202 |
* (non-Javadoc) |
|
203 | 124 |
* @see org.gvsig.gpe.xml.GPEXmlParser#initParse() |
204 | 125 |
*/ |
205 | 126 |
protected void initParse() { |
127 |
layer = null; |
|
206 | 128 |
try { |
207 | 129 |
String namespace = getProfile().getHeaderBinding().parse(getParser(),this); |
208 |
//AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this); |
|
209 |
//parser.initParse(); |
|
210 | 130 |
boolean endFeature = false; |
211 | 131 |
int currentTag; |
212 | 132 |
|
... | ... | |
216 | 136 |
while (!endFeature){ |
217 | 137 |
switch(currentTag){ |
218 | 138 |
case IXmlStreamReader.START_ELEMENT: |
219 |
if (CompareUtils.compareWithNamespace(tag,KmlTags.DOCUMENT)){ |
|
139 |
if (CompareUtils.compareWithNamespace(tag,Kml2_1_Tags.DOCUMENT)){
|
|
220 | 140 |
getProfile().getDocumentBinding().parse(getParser(), this); |
221 |
}else if (CompareUtils.compareWithNamespace(tag,KmlTags.FOLDER)){ |
|
141 |
}else if (CompareUtils.compareWithNamespace(tag,Kml2_1_Tags.FOLDER)){
|
|
222 | 142 |
getProfile().getFolderBinding().parse(getParser(), this, null); |
223 |
}else if (CompareUtils.compareWithNamespace(tag,KmlTags.PLACEMARK)){ |
|
143 |
}else if (CompareUtils.compareWithNamespace(tag,Kml2_1_Tags.PLACEMARK)){
|
|
224 | 144 |
if (layer == null){ |
225 |
layer = getContentHandler().startLayer(null, KmlTags.NAMESPACE_21, null, null, KmlTags.DEFAULT_SRS, null, null);
|
|
145 |
layer = getContentHandler().startLayer(null, Kml2_1_Tags.NAMESPACE_21, null, null, Kml2_1_Tags.DEFAULT_SRS, null, null);
|
|
226 | 146 |
} |
227 | 147 |
Object feature = getProfile().getPlaceMarketBinding().parse(getParser(), this); |
228 | 148 |
getContentHandler().addFeatureToLayer(feature, layer); |
... | ... | |
230 | 150 |
break; |
231 | 151 |
case IXmlStreamReader.END_DOCUMENT: |
232 | 152 |
endFeature = true; |
233 |
|
|
234 | 153 |
break; |
235 | 154 |
} |
236 | 155 |
if (!endFeature){ |
Also available in: Unified diff