Statistics
| Revision:

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

History | View | Annotate | Download (4.81 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.util.zip.ZipEntry;
10
import java.util.zip.ZipException;
11
import java.util.zip.ZipFile;
12
import java.util.zip.ZipInputStream;
13

    
14
import org.gvsig.exceptions.BaseException;
15
import org.gvsig.gpe.GPEContentHandler;
16
import org.gvsig.gpe.GPEErrorHandler;
17
import org.gvsig.gpe.kml.bindings.header.HeaderBinding;
18
import org.gvsig.gpe.kml.exceptions.KmlException;
19
import org.gvsig.gpe.kml.exceptions.KmlHeaderParseException;
20
import org.gvsig.gpe.kml.versions.AbstractKmlParser;
21
import org.gvsig.gpe.kml.versions.KmlParsersFactory;
22
import org.gvsig.gpe.kml.writer.GPEKmlWriterHandler;
23
import org.gvsig.gpe.writers.GPEWriterHandler;
24
import org.gvsig.gpe.xml.GPEXmlParser;
25

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

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

    
167
        /*
168
         * (non-Javadoc)
169
         * @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
170
         */
171
        protected void initParse() {
172
                try {
173
                        String namespace = HeaderBinding.parse(getParser(),this);
174
                        AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this);
175
                        parser.initParse();
176
                } catch (KmlHeaderParseException e) {
177
                        getErrorHandler().addError(e);
178
                } catch (BaseException e) {
179
                        getErrorHandler().addError(e);
180
                }                
181
        }
182

    
183
}