Statistics
| Revision:

root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / parser / GPEDeafultKmlParser.java @ 19686

History | View | Annotate | Download (5.28 KB)

1
package org.gvsig.gpe.kml.parser;
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.profiles.IBindingProfile;
19
import org.gvsig.gpe.xml.parser.GPEXmlParser;
20

    
21
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
22
 *
23
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
24
 *
25
 * This program is free software; you can redistribute it and/or
26
 * modify it under the terms of the GNU General Public License
27
 * as published by the Free Software Foundation; either version 2
28
 * of the License, or (at your option) any later version.
29
 *
30
 * This program is distributed in the hope that it will be useful,
31
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
 * GNU General Public License for more details.
34
 *
35
 * You should have received a copy of the GNU General Public License
36
 * along with this program; if not, write to the Free Software
37
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
38
 *
39
 * For more information, contact:
40
 *
41
 *  Generalitat Valenciana
42
 *   Conselleria d'Infraestructures i Transport
43
 *   Av. Blasco Ib??ez, 50
44
 *   46010 VALENCIA
45
 *   SPAIN
46
 *
47
 *      +34 963862235
48
 *   gvsig@gva.es
49
 *      www.gvsig.gva.es
50
 *
51
 *    or
52
 *
53
 *   IVER T.I. S.A
54
 *   Salamanca 50
55
 *   46005 Valencia
56
 *   Spain
57
 *
58
 *   +34 963163400
59
 *   dac@iver.es
60
 */
61
/* CVS MESSAGES:
62
 *
63
 * $Id: GPEDeafultKmlParser.java 366 2008-01-10 09:09:38Z jpiera $
64
 * $Log$
65
 * Revision 1.11  2007/07/02 09:59:44  jorpiell
66
 * The generated xsd schemas have to be valid
67
 *
68
 * Revision 1.10  2007/06/07 14:53:59  jorpiell
69
 * Add the schema support
70
 *
71
 * Revision 1.9  2007/05/11 07:06:29  jorpiell
72
 * Refactoring of some package names
73
 *
74
 * Revision 1.8  2007/05/09 06:54:24  jorpiell
75
 * Change the File by URI
76
 *
77
 * Revision 1.7  2007/05/08 09:28:17  jorpiell
78
 * Add comments to create javadocs
79
 *
80
 * Revision 1.6  2007/05/07 07:07:18  jorpiell
81
 * Add a constructor with the name and the description fields
82
 *
83
 * Revision 1.5  2007/04/20 08:38:59  jorpiell
84
 * Tests updating
85
 *
86
 * Revision 1.4  2007/04/14 16:08:07  jorpiell
87
 * Kml writing support added
88
 *
89
 * Revision 1.3  2007/04/13 13:16:21  jorpiell
90
 * Add KML reading support
91
 *
92
 * Revision 1.2  2007/04/12 17:06:43  jorpiell
93
 * First GML writing tests
94
 *
95
 * Revision 1.1  2007/04/12 10:21:52  jorpiell
96
 * Add the writers
97
 *
98
 *
99
 */
100
/**
101
 * This is a KML parser. This class must be registered in 
102
 * the GPE register to read and write KML/KMZ files.
103
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
104
 */
105
public abstract class GPEDeafultKmlParser extends GPEXmlParser {
106
        private IBindingProfile profile = null;
107
        
108
        /**
109
         * @return the profile
110
         */
111
        public IBindingProfile getProfile() {
112
                return profile;
113
        }
114

    
115
        /**
116
         * @param profile the profile to set
117
         */
118
        public void setProfile(IBindingProfile profile) {
119
                this.profile = profile;
120
        }
121

    
122
        public GPEDeafultKmlParser() {
123
                super();
124
        }
125

    
126
        /*
127
         * (non-Javadoc)
128
         * @see org.gvsig.gpe.GPEParser#accept(java.io.File)
129
         */
130
        public boolean accept(URI uri) {
131
                if ((uri.getPath().toUpperCase().endsWith("KML")) ||
132
                                (uri.getPath().toUpperCase().endsWith("KMZ"))){
133
                        return true;
134
                }
135
                return false;
136
        }
137

    
138
        /*
139
         * (non-Javadoc)
140
         * @see org.gvsig.gpe.GPEParser#getFormats()
141
         */
142
        public String[] getFormats() {
143
                String[] formats = new String[2];
144
                formats[0] = "KML";
145
                formats[1] = "KMZ";
146
                return formats;
147
        }        
148

    
149
        /*
150
         * (non-Javadoc)
151
         * @see org.gvsig.gpe.GPEParser#getVersions()
152
         */
153
        public String[] getVersions() {
154
                String[] versions = new String[0];
155
                versions[0] = "2.1";
156
                return versions;
157

    
158
        }
159

    
160
        /**
161
         * It creates an InputStream. The Kml file can have the
162
         * KML or the KMZ extension
163
         * @return
164
         * @throws KmlException
165
         */
166
        protected InputStream createInputStream(File file){
167
                //KMZ
168
                try {
169
                        FileInputStream fis = new FileInputStream(file);
170
                        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
171
                        ZipEntry entry = null;
172
                        while((entry = zis.getNextEntry()) != null){
173
                                if (!entry.isDirectory()){
174
                                        ZipFile fz = new ZipFile(file);
175
                                        return fz.getInputStream(entry);
176
                                }
177
                        }
178
                } catch (FileNotFoundException e) {
179
                        getErrorHandler().addError(e);
180
                } catch (ZipException e) {
181
                        getErrorHandler().addError(e);
182
                } catch (IOException e) {
183
                        getErrorHandler().addError(e);
184
                }        
185
                //KML
186
                try{
187
                        return new FileInputStream(file);
188
                } catch (FileNotFoundException e) {
189
                        getErrorHandler().addError(e);
190
                }
191
                return null;
192
        }
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
197
         */
198
        protected void initParse() {
199
                try {
200
                        String namespace = getProfile().getHeaderBinding().parse(getParser(),this);
201
                        AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this);
202
                        parser.initParse();
203
                } catch (KmlHeaderParseException e) {
204
                        getErrorHandler().addError(e);
205
                } catch (BaseException e) {
206
                        getErrorHandler().addError(e);
207
                }                
208
        }
209

    
210
}