Statistics
| Revision:

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

History | View | Annotate | Download (3.94 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.gpe.GPEContentHandler;
15
import org.gvsig.gpe.GPEErrorHandler;
16
import org.gvsig.gpe.GPEParser;
17
import org.gvsig.gpe.kml.exceptions.KmlException;
18
import org.gvsig.gpe.kml.writer.GPEKmlWriterHandler;
19
import org.gvsig.gpe.writer.IGPEWriterHandler;
20
import org.gvsig.gpe.xml.GPEXmlParser;
21

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

    
76
        public GPEKmlParser(GPEContentHandler contentHandler,GPEErrorHandler errorHandler) {
77
                super(contentHandler,errorHandler);                
78
        }
79

    
80
        /*
81
         * (non-Javadoc)
82
         * @see org.gvsig.gpe.GPEParser#accept(java.io.File)
83
         */
84
        public boolean accept(File file) {
85
                if ((file.getName().toUpperCase().endsWith("KML")) ||
86
                                (file.getName().toUpperCase().endsWith("KMZ"))){
87
                        return true;
88
                }
89
                return false;
90
        }
91
        
92
        /*
93
         * (non-Javadoc)
94
         * @see org.gvsig.gpe.GPEParser#getFormats()
95
         */
96
        public String[] getFormats() {
97
                String[] formats = new String[2];
98
                formats[0] = "KML";
99
                formats[1] = "KMZ";
100
                return formats;
101
        }
102

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

    
146
        /*
147
         * (non-Javadoc)
148
         * @see org.gvsig.gpe.xml.GPEXmlParser#initParse()
149
         */
150
        protected void initParse() {
151
                                
152
        }
153
}