root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / GPEDeafultKmlParser.java @ 19367
History | View | Annotate | Download (5.42 KB)
1 |
package org.gvsig.gpe.kml; |
---|---|
2 |
|
3 |
import java.io.BufferedInputStream; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
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 |
|
15 |
import org.gvsig.exceptions.BaseException; |
16 |
import org.gvsig.gpe.kml.exceptions.KmlException; |
17 |
import org.gvsig.gpe.kml.exceptions.KmlHeaderParseException; |
18 |
import org.gvsig.gpe.kml.parser.AbstractKmlParser; |
19 |
import org.gvsig.gpe.kml.parser.KmlParsersFactory; |
20 |
import org.gvsig.gpe.kml.parser.profiles.IBindingProfile; |
21 |
import org.gvsig.gpe.xml.parser.GPEXmlParser; |
22 |
|
23 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
24 |
*
|
25 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
26 |
*
|
27 |
* This program is free software; you can redistribute it and/or
|
28 |
* modify it under the terms of the GNU General Public License
|
29 |
* as published by the Free Software Foundation; either version 2
|
30 |
* of the License, or (at your option) any later version.
|
31 |
*
|
32 |
* This program is distributed in the hope that it will be useful,
|
33 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
34 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
35 |
* GNU General Public License for more details.
|
36 |
*
|
37 |
* You should have received a copy of the GNU General Public License
|
38 |
* along with this program; if not, write to the Free Software
|
39 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
40 |
*
|
41 |
* For more information, contact:
|
42 |
*
|
43 |
* Generalitat Valenciana
|
44 |
* Conselleria d'Infraestructures i Transport
|
45 |
* Av. Blasco Ib??ez, 50
|
46 |
* 46010 VALENCIA
|
47 |
* SPAIN
|
48 |
*
|
49 |
* +34 963862235
|
50 |
* gvsig@gva.es
|
51 |
* www.gvsig.gva.es
|
52 |
*
|
53 |
* or
|
54 |
*
|
55 |
* IVER T.I. S.A
|
56 |
* Salamanca 50
|
57 |
* 46005 Valencia
|
58 |
* Spain
|
59 |
*
|
60 |
* +34 963163400
|
61 |
* dac@iver.es
|
62 |
*/
|
63 |
/* CVS MESSAGES:
|
64 |
*
|
65 |
* $Id: GPEDeafultKmlParser.java 366 2008-01-10 09:09:38Z jpiera $
|
66 |
* $Log$
|
67 |
* Revision 1.11 2007/07/02 09:59:44 jorpiell
|
68 |
* The generated xsd schemas have to be valid
|
69 |
*
|
70 |
* Revision 1.10 2007/06/07 14:53:59 jorpiell
|
71 |
* Add the schema support
|
72 |
*
|
73 |
* Revision 1.9 2007/05/11 07:06:29 jorpiell
|
74 |
* Refactoring of some package names
|
75 |
*
|
76 |
* Revision 1.8 2007/05/09 06:54:24 jorpiell
|
77 |
* Change the File by URI
|
78 |
*
|
79 |
* Revision 1.7 2007/05/08 09:28:17 jorpiell
|
80 |
* Add comments to create javadocs
|
81 |
*
|
82 |
* Revision 1.6 2007/05/07 07:07:18 jorpiell
|
83 |
* Add a constructor with the name and the description fields
|
84 |
*
|
85 |
* Revision 1.5 2007/04/20 08:38:59 jorpiell
|
86 |
* Tests updating
|
87 |
*
|
88 |
* Revision 1.4 2007/04/14 16:08:07 jorpiell
|
89 |
* Kml writing support added
|
90 |
*
|
91 |
* Revision 1.3 2007/04/13 13:16:21 jorpiell
|
92 |
* Add KML reading support
|
93 |
*
|
94 |
* Revision 1.2 2007/04/12 17:06:43 jorpiell
|
95 |
* First GML writing tests
|
96 |
*
|
97 |
* Revision 1.1 2007/04/12 10:21:52 jorpiell
|
98 |
* Add the writers
|
99 |
*
|
100 |
*
|
101 |
*/
|
102 |
/**
|
103 |
* This is a KML parser. This class must be registered in
|
104 |
* the GPE register to read and write KML/KMZ files.
|
105 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
106 |
*/
|
107 |
public abstract class GPEDeafultKmlParser extends GPEXmlParser { |
108 |
private IBindingProfile profile = null; |
109 |
|
110 |
/**
|
111 |
* @return the profile
|
112 |
*/
|
113 |
public IBindingProfile getProfile() {
|
114 |
return profile;
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* @param profile the profile to set
|
119 |
*/
|
120 |
public void setProfile(IBindingProfile profile) { |
121 |
this.profile = profile;
|
122 |
} |
123 |
|
124 |
public GPEDeafultKmlParser(String name, String description) { |
125 |
super(name, description);
|
126 |
} |
127 |
|
128 |
/*
|
129 |
* (non-Javadoc)
|
130 |
* @see org.gvsig.gpe.GPEParser#accept(java.io.File)
|
131 |
*/
|
132 |
public boolean accept(URI uri) { |
133 |
if ((uri.getPath().toUpperCase().endsWith("KML")) || |
134 |
(uri.getPath().toUpperCase().endsWith("KMZ"))){
|
135 |
return true; |
136 |
} |
137 |
return false; |
138 |
} |
139 |
|
140 |
/*
|
141 |
* (non-Javadoc)
|
142 |
* @see org.gvsig.gpe.GPEParser#getFormats()
|
143 |
*/
|
144 |
public String[] getFormats() { |
145 |
String[] formats = new String[2]; |
146 |
formats[0] = "KML"; |
147 |
formats[1] = "KMZ"; |
148 |
return formats;
|
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see org.gvsig.gpe.GPEParser#getVersions()
|
154 |
*/
|
155 |
public String[] getVersions() { |
156 |
String[] versions = new String[0]; |
157 |
versions[0] = "2.1"; |
158 |
return versions;
|
159 |
|
160 |
} |
161 |
|
162 |
/**
|
163 |
* It creates an InputStream. The Kml file can have the
|
164 |
* KML or the KMZ extension
|
165 |
* @return
|
166 |
* @throws KmlException
|
167 |
*/
|
168 |
protected InputStream createInputStream(File file){ |
169 |
//KMZ
|
170 |
try {
|
171 |
FileInputStream fis = new FileInputStream(file); |
172 |
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); |
173 |
ZipEntry entry = null; |
174 |
while((entry = zis.getNextEntry()) != null){ |
175 |
if (!entry.isDirectory()){
|
176 |
ZipFile fz = new ZipFile(file); |
177 |
return fz.getInputStream(entry);
|
178 |
} |
179 |
} |
180 |
} catch (FileNotFoundException e) { |
181 |
getErrorHandler().addError(e); |
182 |
} catch (ZipException e) { |
183 |
getErrorHandler().addError(e); |
184 |
} catch (IOException e) { |
185 |
getErrorHandler().addError(e); |
186 |
} |
187 |
//KML
|
188 |
try{
|
189 |
return new FileInputStream(file); |
190 |
} catch (FileNotFoundException e) { |
191 |
getErrorHandler().addError(e); |
192 |
} |
193 |
return null; |
194 |
} |
195 |
|
196 |
/*
|
197 |
* (non-Javadoc)
|
198 |
* @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
|
199 |
*/
|
200 |
protected void initParse() { |
201 |
try {
|
202 |
String namespace = getProfile().getHeaderBinding().parse(getParser(),this); |
203 |
AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this); |
204 |
parser.initParse(); |
205 |
} catch (KmlHeaderParseException e) {
|
206 |
getErrorHandler().addError(e); |
207 |
} catch (BaseException e) {
|
208 |
getErrorHandler().addError(e); |
209 |
} |
210 |
} |
211 |
|
212 |
} |