Statistics
| Revision:

root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / GPEKmlParser.java @ 12439

History | View | Annotate | Download (5.17 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.reader.AbstractKmlParser;
19
import org.gvsig.gpe.kml.reader.KmlParsersFactory;
20
import org.gvsig.gpe.kml.reader.bindings.header.HeaderBinding;
21
import org.gvsig.gpe.xml.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: GPEKmlParser.java 12439 2007-07-02 10:00:46Z jorpiell $
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 class GPEKmlParser extends GPEXmlParser {
108
        
109
        public GPEKmlParser(String name, String description) {
110
                super(name, description);
111
        }
112

    
113
        /*
114
         * (non-Javadoc)
115
         * @see org.gvsig.gpe.GPEParser#accept(java.io.File)
116
         */
117
        public boolean accept(URI uri) {
118
                if ((uri.getPath().toUpperCase().endsWith("KML")) ||
119
                                (uri.getPath().toUpperCase().endsWith("KMZ"))){
120
                        return true;
121
                }
122
                return false;
123
        }
124
        
125
        /*
126
         * (non-Javadoc)
127
         * @see org.gvsig.gpe.GPEParser#getFormats()
128
         */
129
        public String[] getFormats() {
130
                String[] formats = new String[2];
131
                formats[0] = "KML";
132
                formats[1] = "KMZ";
133
                return formats;
134
        }        
135
        
136
        /*
137
         * (non-Javadoc)
138
         * @see org.gvsig.gpe.GPEParser#getVersions()
139
         */
140
        public String[] getVersions() {
141
                String[] versions = new String[0];
142
                versions[0] = "2.1";
143
                return versions;
144
                
145
        }
146
        
147
        /**
148
         * It creates an InputStream. The Kml file can have the
149
         * KML or the KMZ extension
150
         * @return
151
         * @throws KmlException
152
         */
153
        protected InputStream createInputStream(File file){
154
                try {
155
                        if (file.getName().toUpperCase().endsWith("KML")){
156
                                return new FileInputStream(file);
157
                        }else if(file.getName().toUpperCase().endsWith("KMZ")){
158
                                FileInputStream fis = new FileInputStream(file);
159
                                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
160
                                ZipEntry entry = null;
161
                                while((entry = zis.getNextEntry()) != null){
162
                                        if (!entry.isDirectory()){
163
                                                ZipFile fz = new ZipFile(file);
164
                                                return fz.getInputStream(entry);
165
                                        }
166
                                }
167
                        }
168
                } catch (FileNotFoundException e) {
169
                        // TODO Auto-generated catch block
170
                        e.printStackTrace();
171
                } catch (ZipException e) {
172
                        // TODO Auto-generated catch block
173
                        e.printStackTrace();
174
                } catch (IOException e) {
175
                        // TODO Auto-generated catch block
176
                        e.printStackTrace();
177
                }        
178
                return null;
179
        }
180

    
181
        /*
182
         * (non-Javadoc)
183
         * @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
184
         */
185
        protected void initParse() {
186
                try {
187
                        String namespace = HeaderBinding.parse(getParser(),this);
188
                        AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this);
189
                        parser.initParse();
190
                } catch (KmlHeaderParseException e) {
191
                        getErrorHandler().addError(e);
192
                } catch (BaseException e) {
193
                        getErrorHandler().addError(e);
194
                }                
195
        }
196

    
197
}