Changeset 29680 in osm for applications/editors/josm/plugins/opendata
- Timestamp:
- 2013-06-21T00:58:00+02:00 (12 years ago)
- Location:
- applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata
- Files:
-
- 3 added
- 4 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/OdPlugin.java
r29657 r29680 51 51 import org.openstreetmap.josm.plugins.opendata.core.io.AbstractImporter; 52 52 import org.openstreetmap.josm.plugins.opendata.core.io.XmlImporter; 53 import org.openstreetmap.josm.plugins.opendata.core.io.archive.SevenZipImporter; 53 54 import org.openstreetmap.josm.plugins.opendata.core.io.archive.ZipImporter; 54 55 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.GmlImporter; … … 86 87 new CsvImporter(), new OdsImporter(), new XlsImporter(), // Tabular file formats 87 88 new KmlKmzImporter(), new ShpImporter(), new MifTabImporter(), new GmlImporter(), // Geographic file formats 88 new ZipImporter(), // Archive containing any of the others 89 new ZipImporter(), // Zip archive containing any of the others 90 new SevenZipImporter(), // 7Zip archive containing any of the others 89 91 xmlImporter // Generic importer for XML files (currently used for Neptune files) 90 92 })) { -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/OdConstants.java
r29653 r29680 104 104 public static final String MAPCSS_EXT = "mapcss"; 105 105 public static final String ZIP_EXT = "zip"; 106 public static final String SEVENZIP_EXT = "7z"; 106 107 public static final String JAR_EXT = "jar"; 107 108 public static final String XML_EXT = "xml"; … … 124 125 public static final ExtensionFileFilter GML_FILE_FILTER = new ExtensionFileFilter(GML_EXT, GML_EXT, tr("GML files") + " (*."+GML_EXT+")"); 125 126 public static final ExtensionFileFilter ZIP_FILE_FILTER = new ExtensionFileFilter(ZIP_EXT, ZIP_EXT, tr("Zip Files") + " (*."+ZIP_EXT+")"); 127 public static final ExtensionFileFilter SEVENZIP_FILE_FILTER = new ExtensionFileFilter(SEVENZIP_EXT, SEVENZIP_EXT, tr("7Zip Files") + " (*."+SEVENZIP_EXT+")"); 126 128 public static final ExtensionFileFilter XML_FILE_FILTER = new ExtensionFileFilter(XML_EXT, XML_EXT, tr("OpenData XML files") + " (*."+XML_EXT+")"); 127 129 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/datasets/AbstractDataSetHandler.java
r29657 r29680 35 35 import org.openstreetmap.josm.io.AbstractReader; 36 36 import org.openstreetmap.josm.plugins.opendata.core.OdConstants; 37 import org.openstreetmap.josm.plugins.opendata.core.io.archive.Default ZipHandler;38 import org.openstreetmap.josm.plugins.opendata.core.io.archive. ZipHandler;37 import org.openstreetmap.josm.plugins.opendata.core.io.archive.DefaultArchiveHandler; 38 import org.openstreetmap.josm.plugins.opendata.core.io.archive.ArchiveHandler; 39 39 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.DefaultGmlHandler; 40 40 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.DefaultShpHandler; … … 92 92 public AbstractDataSetHandler() { 93 93 setShpHandler(new DefaultShpHandler()); 94 set ZipHandler(new DefaultZipHandler());94 setArchiveHandler(new DefaultArchiveHandler()); 95 95 setCsvHandler(new DefaultCsvHandler()); 96 96 setGmlHandler(new DefaultGmlHandler()); … … 460 460 } 461 461 462 // ------------ Ziphandling ------------463 464 private ZipHandlerzipHandler;465 466 public final void set ZipHandler(ZipHandler handler) {467 zipHandler = handler;468 } 469 470 public ZipHandler getZipHandler() {471 return zipHandler;462 // ------------ Archive handling ------------ 463 464 private ArchiveHandler archiveHandler; 465 466 public final void setArchiveHandler(ArchiveHandler handler) { 467 archiveHandler = handler; 468 } 469 470 public ArchiveHandler getArchiveHandler() { 471 return archiveHandler; 472 472 } 473 473 -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ArchiveHandler.java
r29666 r29680 1 1 // JOSM opendata plugin. 2 // Copyright (C) 2011-201 2Don-vip2 // Copyright (C) 2011-2013 Don-vip 3 3 // 4 4 // This program is free software: you can redistribute it and/or modify … … 18 18 import java.io.File; 19 19 20 public interface ZipHandler {20 public interface ArchiveHandler { 21 21 22 22 public void notifyTempFileWritten(File file); -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/DefaultArchiveHandler.java
r29666 r29680 1 1 // JOSM opendata plugin. 2 // Copyright (C) 2011-201 2Don-vip2 // Copyright (C) 2011-2013 Don-vip 3 3 // 4 4 // This program is free software: you can redistribute it and/or modify … … 18 18 import java.io.File; 19 19 20 public class Default ZipHandler implementsZipHandler {20 public class DefaultArchiveHandler implements ArchiveHandler { 21 21 22 22 private boolean skipXsdValidation = false; -
applications/editors/josm/plugins/opendata/src/org/openstreetmap/josm/plugins/opendata/core/io/archive/ZipReader.java
r29107 r29680 19 19 20 20 import java.io.File; 21 import java.io.File InputStream;21 import java.io.FileNotFoundException; 22 22 import java.io.FileOutputStream; 23 23 import java.io.IOException; 24 24 import java.io.InputStream; 25 import java.util.ArrayList;26 25 import java.util.List; 27 26 import java.util.zip.ZipEntry; … … 34 33 import org.openstreetmap.josm.data.osm.DataSet; 35 34 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 36 import org.openstreetmap.josm.io.AbstractReader;37 import org.openstreetmap.josm.plugins.opendata.OdPlugin;38 import org.openstreetmap.josm.plugins.opendata.core.OdConstants;39 35 import org.openstreetmap.josm.plugins.opendata.core.datasets.AbstractDataSetHandler; 40 import org.openstreetmap.josm.plugins.opendata.core.gui.DialogPrompter;41 import org.openstreetmap.josm.plugins.opendata.core.io.NeptuneReader;42 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.GmlReader;43 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.KmlReader;44 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.KmzReader;45 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.MifReader;46 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.ShpReader;47 import org.openstreetmap.josm.plugins.opendata.core.io.geographic.TabReader;48 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.CsvReader;49 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.OdsReader;50 import org.openstreetmap.josm.plugins.opendata.core.io.tabular.XlsReader;51 import org.openstreetmap.josm.plugins.opendata.core.util.OdUtils;52 36 53 public class ZipReader extends A bstractReader implements OdConstants{37 public class ZipReader extends ArchiveReader { 54 38 55 39 private final ZipInputStream zis; 56 private final AbstractDataSetHandler handler;57 private final ZipHandler zipHandler;58 private final boolean promptUser;59 40 60 private File file;61 41 private ZipEntry entry; 42 62 43 public ZipReader(InputStream in, AbstractDataSetHandler handler, boolean promptUser) { 44 super(handler, handler != null ? handler.getArchiveHandler() : null, promptUser); 63 45 this.zis = in instanceof ZipInputStream ? (ZipInputStream) in : new ZipInputStream(in); 64 this.handler = handler;65 this.zipHandler = handler != null ? handler.getZipHandler() : null;66 this.promptUser = promptUser;67 46 } 68 47 … … 71 50 return new ZipReader(in, handler, promptUser).parseDoc(instance); 72 51 } 73 74 public final File getReadFile() { 75 return file; 76 } 77 78 public DataSet parseDoc(final ProgressMonitor progressMonitor) throws IOException, XMLStreamException, FactoryConfigurationError, JAXBException { 79 80 final File temp = OdUtils.createTempDir(); 81 final List<File> candidates = new ArrayList<File>(); 82 83 try { 84 if (progressMonitor != null) { 85 progressMonitor.beginTask(tr("Reading Zip file...")); 86 } 87 ZipEntry entry; 88 while ((entry = zis.getNextEntry()) != null) { 89 File file = new File(temp + File.separator + entry.getName()); 90 File parent = file.getParentFile(); 91 if (parent != null && !parent.exists()) { 92 parent.mkdirs(); 93 } 94 if (file.exists() && !file.delete()) { 95 throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath()); 96 } 97 if (!entry.isDirectory()) { 98 if (!file.createNewFile()) { 99 throw new IOException("Could not create temp file: " + file.getAbsolutePath()); 100 } 101 // Write temp file 102 FileOutputStream fos = new FileOutputStream(file); 103 byte[] buffer = new byte[8192]; 104 int count = 0; 105 while ((count = zis.read(buffer, 0, buffer.length)) > 0) { 106 fos.write(buffer, 0, count); 107 } 108 fos.close(); 109 // Allow handler to perform specific treatments (for example, fix invalid .prj files) 110 if (zipHandler != null) { 111 zipHandler.notifyTempFileWritten(file); 112 } 113 // Set last modification date 114 long time = entry.getTime(); 115 if (time > -1) { 116 file.setLastModified(time); 117 } 118 // Test file name to see if it may contain useful data 119 for (String ext : new String[] { 120 CSV_EXT, KML_EXT, KMZ_EXT, XLS_EXT, ODS_EXT, SHP_EXT, MIF_EXT, TAB_EXT, GML_EXT 121 }) { 122 if (entry.getName().toLowerCase().endsWith("."+ext)) { 123 candidates.add(file); 124 System.out.println(entry.getName()); 125 break; 126 } 127 } 128 // Special treatment for XML files (check supported XSD), unless handler explicitely skip it 129 if (XML_FILE_FILTER.accept(file) && ((zipHandler != null && zipHandler.skipXsdValidation()) 130 || OdPlugin.getInstance().xmlImporter.acceptFile(file))) { 131 candidates.add(file); 132 System.out.println(entry.getName()); 133 } 134 } else if (!file.mkdir()) { 135 throw new IOException("Could not create temp dir: " + file.getAbsolutePath()); 136 } 137 } 138 139 file = null; 140 141 if (promptUser && candidates.size() > 1) { 142 DialogPrompter<CandidateChooser> prompt = new DialogPrompter() { 143 @Override 144 protected CandidateChooser buildDialog() { 145 return new CandidateChooser(progressMonitor.getWindowParent(), candidates); 146 } 147 }; 148 if (prompt.promptInEdt().getValue() == 1) { 149 file = prompt.getDialog().getSelectedFile(); 150 } 151 } else if (!candidates.isEmpty()) { 152 file = candidates.get(0); 153 } 154 155 if (file == null) { 156 return null; 157 } else { 158 DataSet from = null; 159 FileInputStream in = new FileInputStream(file); 160 ProgressMonitor instance = null; 161 if (progressMonitor != null) { 162 instance = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false); 163 } 164 if (file.getName().toLowerCase().endsWith(CSV_EXT)) { 165 from = CsvReader.parseDataSet(in, handler, instance); 166 } else if (file.getName().toLowerCase().endsWith(KML_EXT)) { 167 from = KmlReader.parseDataSet(in, instance); 168 } else if (file.getName().toLowerCase().endsWith(KMZ_EXT)) { 169 from = KmzReader.parseDataSet(in, instance); 170 } else if (file.getName().toLowerCase().endsWith(XLS_EXT)) { 171 from = XlsReader.parseDataSet(in, handler, instance); 172 } else if (file.getName().toLowerCase().endsWith(ODS_EXT)) { 173 from = OdsReader.parseDataSet(in, handler, instance); 174 } else if (file.getName().toLowerCase().endsWith(SHP_EXT)) { 175 from = ShpReader.parseDataSet(in, file, handler, instance); 176 } else if (file.getName().toLowerCase().endsWith(MIF_EXT)) { 177 from = MifReader.parseDataSet(in, file, handler, instance); 178 } else if (file.getName().toLowerCase().endsWith(TAB_EXT)) { 179 from = TabReader.parseDataSet(in, file, handler, instance); 180 } else if (file.getName().toLowerCase().endsWith(GML_EXT)) { 181 from = GmlReader.parseDataSet(in, handler, instance); 182 } else if (file.getName().toLowerCase().endsWith(XML_EXT)) { 183 if (OdPlugin.getInstance().xmlImporter.acceptFile(file)) { 184 from = NeptuneReader.parseDataSet(in, handler, instance); 185 } else { 186 System.err.println("Unsupported XML file: "+file.getName()); 187 } 188 189 } else { 190 System.err.println("Unsupported file extension: "+file.getName()); 191 } 192 if (from != null) { 193 ds = from; 194 } 195 } 196 } catch (IllegalArgumentException e) { 197 System.err.println(e.getMessage()); 198 } finally { 199 OdUtils.deleteDir(temp); 200 if (progressMonitor != null) { 201 progressMonitor.finishTask(); 202 } 203 } 204 205 return ds; 206 } 52 53 protected void extractArchive(final File temp, final List<File> candidates) throws IOException, FileNotFoundException { 54 while ((entry = zis.getNextEntry()) != null) { 55 File file = new File(temp + File.separator + entry.getName()); 56 File parent = file.getParentFile(); 57 if (parent != null && !parent.exists()) { 58 parent.mkdirs(); 59 } 60 if (file.exists() && !file.delete()) { 61 throw new IOException("Could not delete temp file/dir: " + file.getAbsolutePath()); 62 } 63 if (!entry.isDirectory()) { 64 if (!file.createNewFile()) { 65 throw new IOException("Could not create temp file: " + file.getAbsolutePath()); 66 } 67 // Write temp file 68 FileOutputStream fos = new FileOutputStream(file); 69 byte[] buffer = new byte[8192]; 70 int count = 0; 71 while ((count = zis.read(buffer, 0, buffer.length)) > 0) { 72 fos.write(buffer, 0, count); 73 } 74 fos.close(); 75 // Allow handler to perform specific treatments (for example, fix invalid .prj files) 76 if (archiveHandler != null) { 77 archiveHandler.notifyTempFileWritten(file); 78 } 79 // Set last modification date 80 long time = entry.getTime(); 81 if (time > -1) { 82 file.setLastModified(time); 83 } 84 lookForCandidate(entry.getName(), candidates, file); 85 } else if (!file.mkdir()) { 86 throw new IOException("Could not create temp dir: " + file.getAbsolutePath()); 87 } 88 } 89 } 90 91 @Override protected String getTaskMessage() { 92 return tr("Reading Zip file..."); 93 } 207 94 }
Note:
See TracChangeset
for help on using the changeset viewer.