Statistics
| Revision:

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

History | View | Annotate | Download (4.7 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.prefs.AbstractPreferences;
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.GPEContentHandler;
17
import org.gvsig.gpe.GPEErrorHandler;
18
import org.gvsig.gpe.kml.bindings.header.HeaderBinding;
19
import org.gvsig.gpe.kml.exceptions.KmlException;
20
import org.gvsig.gpe.kml.exceptions.KmlHeaderParseException;
21
import org.gvsig.gpe.kml.versions.AbstractKmlParser;
22
import org.gvsig.gpe.kml.versions.KmlParsersFactory;
23
import org.gvsig.gpe.kml.writer.GPEKmlWriterHandler;
24
import org.gvsig.gpe.writers.GPEWriterHandler;
25
import org.gvsig.gpe.xml.GPEXmlParser;
26

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

    
91
        /*
92
         * (non-Javadoc)
93
         * @see org.gvsig.gpe.GPEParser#accept(java.io.File)
94
         */
95
        public boolean accept(File file) {
96
                if ((file.getName().toUpperCase().endsWith("KML")) ||
97
                                (file.getName().toUpperCase().endsWith("KMZ"))){
98
                        return true;
99
                }
100
                return false;
101
        }
102
        
103
        /*
104
         * (non-Javadoc)
105
         * @see org.gvsig.gpe.GPEParser#getFormats()
106
         */
107
        public String[] getFormats() {
108
                String[] formats = new String[2];
109
                formats[0] = "KML";
110
                formats[1] = "KMZ";
111
                return formats;
112
        }
113

    
114
        /*
115
         * (non-Javadoc)
116
         * @see org.gvsig.gpe.GPEParser#getWriter(java.lang.String, java.lang.String)
117
         */
118
        public GPEWriterHandler getWriter(String format, File file) throws IOException {
119
                return new GPEKmlWriterHandler(format,file,getErrorHandler());
120
        }
121
        
122
        
123
        /**
124
         * It creates an InputStream. The Kml file can have the
125
         * KML or the KMZ extension
126
         * @return
127
         * @throws KmlException
128
         */
129
        protected InputStream createInputStream(File file){
130
                try {
131
                        if (file.getName().toUpperCase().endsWith("KML")){
132
                                return new FileInputStream(file);
133
                        }else if(file.getName().toUpperCase().endsWith("KMZ")){
134
                                FileInputStream fis = new FileInputStream(file);
135
                                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
136
                                ZipEntry entry = null;
137
                                while((entry = zis.getNextEntry()) != null){
138
                                        if (!entry.isDirectory()){
139
                                                ZipFile fz = new ZipFile(file);
140
                                                return fz.getInputStream(entry);
141
                                        }
142
                                }
143
                        }
144
                } catch (FileNotFoundException e) {
145
                        // TODO Auto-generated catch block
146
                        e.printStackTrace();
147
                } catch (ZipException e) {
148
                        // TODO Auto-generated catch block
149
                        e.printStackTrace();
150
                } catch (IOException e) {
151
                        // TODO Auto-generated catch block
152
                        e.printStackTrace();
153
                }        
154
                return null;
155
        }
156

    
157
        /*
158
         * (non-Javadoc)
159
         * @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
160
         */
161
        protected void initParse() {
162
                try {
163
                        String namespace = HeaderBinding.parse(getParser(),this);
164
                        AbstractKmlParser parser = new KmlParsersFactory().createParser(namespace,getParser(), this);
165
                        parser.initParse();
166
                } catch (KmlHeaderParseException e) {
167
                        getErrorHandler().addError(e);
168
                } catch (BaseException e) {
169
                        getErrorHandler().addError(e);
170
                }                
171
        }
172
}