1 | // JOSM opendata plugin.
|
---|
2 | // Copyright (C) 2011-2012 Don-vip
|
---|
3 | //
|
---|
4 | // This program is free software: you can redistribute it and/or modify
|
---|
5 | // it under the terms of the GNU General Public License as published by
|
---|
6 | // the Free Software Foundation, either version 3 of the License, or
|
---|
7 | // (at your option) any later version.
|
---|
8 | //
|
---|
9 | // This program is distributed in the hope that it will be useful,
|
---|
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | // GNU General Public License for more details.
|
---|
13 | //
|
---|
14 | // You should have received a copy of the GNU General Public License
|
---|
15 | // along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
16 | package org.openstreetmap.josm.plugins.opendata.core.io.archive;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.io.FileInputStream;
|
---|
20 | import java.io.FileOutputStream;
|
---|
21 | import java.io.IOException;
|
---|
22 | import java.io.InputStream;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.zip.ZipEntry;
|
---|
26 | import java.util.zip.ZipInputStream;
|
---|
27 |
|
---|
28 | import javax.xml.bind.JAXBException;
|
---|
29 | import javax.xml.stream.FactoryConfigurationError;
|
---|
30 | import javax.xml.stream.XMLStreamException;
|
---|
31 |
|
---|
32 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
33 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
34 | import org.openstreetmap.josm.io.AbstractReader;
|
---|
35 | import org.openstreetmap.josm.plugins.opendata.OdPlugin;
|
---|
36 | import org.openstreetmap.josm.plugins.opendata.core.OdConstants;
|
---|
37 | import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler;
|
---|
38 | import org.openstreetmap.josm.plugins.opendata.core.io.NeptuneReader;
|
---|
39 | import org.openstreetmap.josm.plugins.opendata.core.io.geographic.KmlReader;
|
---|
40 | import org.openstreetmap.josm.plugins.opendata.core.io.geographic.KmzReader;
|
---|
41 | import org.openstreetmap.josm.plugins.opendata.core.io.geographic.MifReader;
|
---|
42 | import org.openstreetmap.josm.plugins.opendata.core.io.geographic.ShpReader;
|
---|
43 | import org.openstreetmap.josm.plugins.opendata.core.io.geographic.TabReader;
|
---|
44 | import org.openstreetmap.josm.plugins.opendata.core.io.tabular.CsvReader;
|
---|
45 | import org.openstreetmap.josm.plugins.opendata.core.io.tabular.OdsReader;
|
---|
46 | import org.openstreetmap.josm.plugins.opendata.core.io.tabular.XlsReader;
|
---|
47 |
|
---|
48 | public class ZipReader extends AbstractReader implements OdConstants {
|
---|
49 |
|
---|
50 | private final ZipInputStream zis;
|
---|
51 | private final AbstractDataSetHandler handler;
|
---|
52 |
|
---|
53 | private File file;
|
---|
54 |
|
---|
55 | public ZipReader(InputStream in, AbstractDataSetHandler handler) {
|
---|
56 | this.zis = in instanceof ZipInputStream ? (ZipInputStream) in : new ZipInputStream(in);
|
---|
57 | this.handler = handler;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static DataSet parseDataSet(InputStream in, AbstractDataSetHandler handler, ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
|
---|
61 | return new ZipReader(in, handler).parseDoc(instance);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public final File getReadFile() {
|
---|
65 | return file;
|
---|
66 | }
|
---|
67 |
|
---|
68 | private static final File createTempDir() throws IOException {
|
---|
69 | final File temp = File.createTempFile("josm_opendata_temp_", Long.toString(System.nanoTime()));
|
---|
70 |
|
---|
71 | if (!temp.delete()) {
|
---|
72 | throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (!temp.mkdir()) {
|
---|
76 | throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
|
---|
77 | }
|
---|
78 |
|
---|
79 | return temp;
|
---|
80 | }
|
---|
81 |
|
---|
82 | private static final void deleteDir(File dir) {
|
---|
83 | for (File file : dir.listFiles()) {
|
---|
84 | file.delete();
|
---|
85 | }
|
---|
86 | dir.delete();
|
---|
87 | }
|
---|
88 |
|
---|
89 | public DataSet parseDoc(ProgressMonitor instance) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException {
|
---|
90 |
|
---|
91 | final File temp = createTempDir();
|
---|
92 | final List<File> candidates = new ArrayList<File>();
|
---|
93 |
|
---|
94 | try {
|
---|
95 | ZipEntry entry;
|
---|
96 | while ((entry = zis.getNextEntry()) != null) {
|
---|
97 | File file = new File(temp + File.separator + entry.getName());
|
---|
98 | File parent = file.getParentFile();
|
---|
99 | if (parent != null && !parent.exists()) {
|
---|
100 | parent.mkdirs();
|
---|
101 | }
|
---|
102 | if (file.exists() && !file.delete()) {
|
---|
103 | throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath());
|
---|
104 | }
|
---|
105 | if (!entry.isDirectory()) {
|
---|
106 | if (!file.createNewFile()) {
|
---|
107 | throw new IOException("Could not create temp file: " + file.getAbsolutePath());
|
---|
108 | }
|
---|
109 | FileOutputStream fos = new FileOutputStream(file);
|
---|
110 | byte[] buffer = new byte[8192];
|
---|
111 | int count = 0;
|
---|
112 | while ((count = zis.read(buffer, 0, buffer.length)) > 0) {
|
---|
113 | fos.write(buffer, 0, count);
|
---|
114 | }
|
---|
115 | fos.close();
|
---|
116 | long time = entry.getTime();
|
---|
117 | if (time > -1) {
|
---|
118 | file.setLastModified(time);
|
---|
119 | }
|
---|
120 | for (String ext : new String[] {
|
---|
121 | CSV_EXT, KML_EXT, KMZ_EXT, XLS_EXT, ODS_EXT, SHP_EXT, MIF_EXT, TAB_EXT
|
---|
122 | }) {
|
---|
123 | if (entry.getName().toLowerCase().endsWith("."+ext)) {
|
---|
124 | candidates.add(file);
|
---|
125 | System.out.println(entry.getName());
|
---|
126 | break;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | // Special treatment for XML files (check supported XSD), unless handler explicitely skip it
|
---|
130 | if (XML_FILE_FILTER.accept(file) && ((handler != null && handler.skipXsdValidationInZipReading())
|
---|
131 | || OdPlugin.getInstance().xmlImporter.acceptFile(file))) {
|
---|
132 | candidates.add(file);
|
---|
133 | System.out.println(entry.getName());
|
---|
134 | }
|
---|
135 | } else if (!file.mkdir()) {
|
---|
136 | throw new IOException("Could not create temp dir: " + file.getAbsolutePath());
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 | file = null;
|
---|
141 |
|
---|
142 | if (candidates.size() > 1) {
|
---|
143 | CandidateChooser dialog = (CandidateChooser) new CandidateChooser(instance.getWindowParent(), candidates).showDialog();
|
---|
144 | if (dialog.getValue() != 1) {
|
---|
145 | return null; // User clicked Cancel
|
---|
146 | }
|
---|
147 | file = dialog.getSelectedFile();
|
---|
148 | } else if (candidates.size() == 1) {
|
---|
149 | file = candidates.get(0);
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (file != null) {
|
---|
153 | DataSet from = null;
|
---|
154 | FileInputStream in = new FileInputStream(file);
|
---|
155 | if (file.getName().toLowerCase().endsWith(CSV_EXT)) {
|
---|
156 | from = CsvReader.parseDataSet(in, handler, instance);
|
---|
157 | } else if (file.getName().toLowerCase().endsWith(KML_EXT)) {
|
---|
158 | from = KmlReader.parseDataSet(in, instance);
|
---|
159 | } else if (file.getName().toLowerCase().endsWith(KMZ_EXT)) {
|
---|
160 | from = KmzReader.parseDataSet(in, instance);
|
---|
161 | } else if (file.getName().toLowerCase().endsWith(XLS_EXT)) {
|
---|
162 | from = XlsReader.parseDataSet(in, handler, instance);
|
---|
163 | } else if (file.getName().toLowerCase().endsWith(ODS_EXT)) {
|
---|
164 | from = OdsReader.parseDataSet(in, handler, instance);
|
---|
165 | } else if (file.getName().toLowerCase().endsWith(SHP_EXT)) {
|
---|
166 | from = ShpReader.parseDataSet(in, file, handler, instance);
|
---|
167 | } else if (file.getName().toLowerCase().endsWith(MIF_EXT)) {
|
---|
168 | from = MifReader.parseDataSet(in, file, handler, instance);
|
---|
169 | } else if (file.getName().toLowerCase().endsWith(TAB_EXT)) {
|
---|
170 | from = TabReader.parseDataSet(in, file, handler, instance);
|
---|
171 | } else if (file.getName().toLowerCase().endsWith(XML_EXT)) {
|
---|
172 | if (OdPlugin.getInstance().xmlImporter.acceptFile(file)) {
|
---|
173 | from = NeptuneReader.parseDataSet(in, handler, instance);
|
---|
174 | } else {
|
---|
175 | System.err.println("Unsupported XML file: "+file.getName());
|
---|
176 | }
|
---|
177 |
|
---|
178 | } else {
|
---|
179 | System.err.println("Unsupported file extension: "+file.getName());
|
---|
180 | }
|
---|
181 | if (from != null) {
|
---|
182 | ds.mergeFrom(from);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | } catch (IllegalArgumentException e) {
|
---|
186 | System.err.println(e.getMessage());
|
---|
187 | } finally {
|
---|
188 | deleteDir(temp);
|
---|
189 | }
|
---|
190 |
|
---|
191 | return ds;
|
---|
192 | }
|
---|
193 | }
|
---|