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

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

see #8465 - Use of new Java 7 zip constructors allowing to specify a charset for entries names

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