source: josm/trunk/src/org/openstreetmap/josm/io/FileImporter.java@ 7050

Last change on this file since 7050 was 6882, checked in by Don-vip, 10 years ago

enable loading of .osm.zip files

  • Property svn:eol-style set to native
File size: 6.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedInputStream;
7import java.io.File;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.List;
11import java.util.zip.GZIPInputStream;
12import java.util.zip.ZipEntry;
13import java.util.zip.ZipInputStream;
14
15import javax.swing.JOptionPane;
16
17import org.apache.tools.bzip2.CBZip2InputStream;
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.ExtensionFileFilter;
20import org.openstreetmap.josm.gui.HelpAwareOptionPane;
21import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.gui.layer.Layer;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26
27public abstract class FileImporter implements Comparable<FileImporter>, LayerChangeListener {
28
29 public final ExtensionFileFilter filter;
30
31 private boolean enabled;
32
33 public FileImporter(ExtensionFileFilter filter) {
34 this.filter = filter;
35 this.enabled = true;
36 }
37
38 public boolean acceptFile(File pathname) {
39 return filter.acceptName(pathname.getName());
40 }
41
42 /**
43 * A batch importer is a file importer that prefers to read multiple files at the same time.
44 */
45 public boolean isBatchImporter() {
46 return false;
47 }
48
49 /**
50 * Needs to be implemented if isBatchImporter() returns false.
51 */
52 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
53 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
54 }
55
56 /**
57 * Needs to be implemented if isBatchImporter() returns true.
58 */
59 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
60 throw new IOException(tr("Could not import files."));
61 }
62
63 /**
64 * Wrapper to give meaningful output if things go wrong.
65 * @return true if data import was successful
66 */
67 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
68 try {
69 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
70 importData(f, progressMonitor);
71 return true;
72 } catch (IllegalDataException e) {
73 Throwable cause = e.getCause();
74 if (cause instanceof ImportCancelException) {
75 displayCancel(cause);
76 } else {
77 displayError(f, e);
78 }
79 return false;
80 } catch (Exception e) {
81 displayError(f, e);
82 return false;
83 }
84 }
85
86 private static void displayError(File f, Exception e) {
87 Main.error(e);
88 HelpAwareOptionPane.showMessageDialogInEDT(
89 Main.parent,
90 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>", f.getName(), e.getMessage()),
91 tr("Error"),
92 JOptionPane.ERROR_MESSAGE, null
93 );
94 }
95
96 private static void displayCancel(final Throwable t) {
97 GuiHelper.runInEDTAndWait(new Runnable() {
98 @Override
99 public void run() {
100 Notification note = new Notification(t.getMessage());
101 note.setIcon(JOptionPane.INFORMATION_MESSAGE);
102 note.setDuration(Notification.TIME_SHORT);
103 note.show();
104 }
105 });
106 }
107
108 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
109 try {
110 Main.info("Open "+files.size()+" files");
111 importData(files, progressMonitor);
112 return true;
113 } catch (Exception e) {
114 Main.error(e);
115 HelpAwareOptionPane.showMessageDialogInEDT(
116 Main.parent,
117 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", e.getMessage()),
118 tr("Error"),
119 JOptionPane.ERROR_MESSAGE, null
120 );
121 return false;
122 }
123 }
124
125 /**
126 * If multiple files (with multiple file formats) are selected,
127 * they are opened in the order of their priorities.
128 * Highest priority comes first.
129 */
130 public double getPriority() {
131 return 0;
132 }
133
134 @Override
135 public int compareTo(FileImporter other) {
136 return Double.compare(this.getPriority(), other.getPriority());
137 }
138
139 public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
140 if (in == null) {
141 return null;
142 }
143 BufferedInputStream bis = new BufferedInputStream(in);
144 int b = bis.read();
145 if (b != 'B')
146 throw new IOException(tr("Invalid bz2 file."));
147 b = bis.read();
148 if (b != 'Z')
149 throw new IOException(tr("Invalid bz2 file."));
150 return new CBZip2InputStream(bis, /* see #9537 */ true);
151 }
152
153 public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
154 if (in == null) {
155 return null;
156 }
157 return new GZIPInputStream(in);
158 }
159
160 public static ZipInputStream getZipInputStream(InputStream in) throws IOException {
161 if (in == null) {
162 return null;
163 }
164 ZipInputStream zis = new ZipInputStream(in);
165 // Positions the stream at the beginning of first entry
166 ZipEntry ze = zis.getNextEntry();
167 if (ze != null && Main.isDebugEnabled()) {
168 Main.debug("Zip entry: "+ze.getName());
169 }
170 return zis;
171 }
172
173 /**
174 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
175 * @return true if this {@code FileImporter} is enabled
176 * @since 5459
177 */
178 public final boolean isEnabled() {
179 return enabled;
180 }
181
182 /**
183 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
184 * @param enabled true to enable this {@code FileImporter}, false to disable it
185 * @since 5459
186 */
187 public final void setEnabled(boolean enabled) {
188 this.enabled = enabled;
189 }
190
191 @Override
192 public void activeLayerChange(Layer oldLayer, Layer newLayer) {
193 // To be overriden by subclasses if their enabled state depends of the active layer nature
194 }
195
196 @Override
197 public void layerAdded(Layer newLayer) {
198 // To be overriden by subclasses if needed
199 }
200
201 @Override
202 public void layerRemoved(Layer oldLayer) {
203 // To be overriden by subclasses if needed
204 }
205}
Note: See TracBrowser for help on using the repository browser.