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

Last change on this file since 11995 was 11848, checked in by Don-vip, 7 years ago

fix #14613 - Special HTML characters not escaped in GUI error messages

  • Property svn:eol-style set to native
File size: 6.2 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.File;
7import java.io.IOException;
8import java.util.List;
9
10import javax.swing.JOptionPane;
11
12import org.openstreetmap.josm.Main;
13import org.openstreetmap.josm.actions.ExtensionFileFilter;
14import org.openstreetmap.josm.gui.HelpAwareOptionPane;
15import org.openstreetmap.josm.gui.Notification;
16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
17import org.openstreetmap.josm.gui.util.GuiHelper;
18import org.openstreetmap.josm.tools.Utils;
19
20/**
21 * Abstract file importer.
22 * @since 1637
23 * @since 10386 (signature)
24 */
25public abstract class FileImporter implements Comparable<FileImporter> {
26
27 /**
28 * The extension file filter used to accept files.
29 */
30 public final ExtensionFileFilter filter;
31
32 private boolean enabled;
33
34 /**
35 * Constructs a new {@code FileImporter} with the given extension file filter.
36 * @param filter The extension file filter
37 */
38 public FileImporter(ExtensionFileFilter filter) {
39 this.filter = filter;
40 this.enabled = true;
41 }
42
43 /**
44 * Determines if this file importer accepts the given file.
45 * @param pathname The file to test
46 * @return {@code true} if this file importer accepts the given file, {@code false} otherwise
47 */
48 public boolean acceptFile(File pathname) {
49 return filter.acceptName(pathname.getName());
50 }
51
52 /**
53 * A batch importer is a file importer that prefers to read multiple files at the same time.
54 * @return {@code true} if this importer is a batch importer
55 */
56 public boolean isBatchImporter() {
57 return false;
58 }
59
60 /**
61 * Needs to be implemented if isBatchImporter() returns false.
62 * @param file file to import
63 * @param progressMonitor progress monitor
64 * @throws IOException if any I/O error occurs
65 * @throws IllegalDataException if invalid data is read
66 */
67 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
68 throw new IOException(tr("Could not import ''{0}''.", file.getName()));
69 }
70
71 /**
72 * Needs to be implemented if isBatchImporter() returns true.
73 * @param files files to import
74 * @param progressMonitor progress monitor
75 * @throws IOException if any I/O error occurs
76 * @throws IllegalDataException if invalid data is read
77 */
78 public void importData(List<File> files, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
79 throw new IOException(tr("Could not import files."));
80 }
81
82 /**
83 * Wrapper to {@link #importData(File, ProgressMonitor)} to give meaningful output if things go wrong.
84 * @param f data file to import
85 * @param progressMonitor progress monitor
86 * @return true if data import was successful
87 */
88 public boolean importDataHandleExceptions(File f, ProgressMonitor progressMonitor) {
89 try {
90 Main.info("Open file: " + f.getAbsolutePath() + " (" + f.length() + " bytes)");
91 importData(f, progressMonitor);
92 return true;
93 } catch (IllegalDataException e) {
94 Throwable cause = e.getCause();
95 if (cause instanceof ImportCancelException) {
96 displayCancel(cause);
97 } else {
98 displayError(f, e);
99 }
100 return false;
101 } catch (IOException e) {
102 displayError(f, e);
103 return false;
104 }
105 }
106
107 private static void displayError(File f, Exception e) {
108 Main.error(e);
109 HelpAwareOptionPane.showMessageDialogInEDT(
110 Main.parent,
111 tr("<html>Could not read file ''{0}''.<br>Error is:<br>{1}</html>",
112 f.getName(), Utils.escapeReservedCharactersHTML(e.getMessage())),
113 tr("Error"),
114 JOptionPane.ERROR_MESSAGE, null
115 );
116 }
117
118 private static void displayCancel(final Throwable t) {
119 GuiHelper.runInEDTAndWait(() -> {
120 Notification note = new Notification(t.getMessage());
121 note.setIcon(JOptionPane.INFORMATION_MESSAGE);
122 note.setDuration(Notification.TIME_SHORT);
123 note.show();
124 });
125 }
126
127 /**
128 * Wrapper to {@link #importData(List, ProgressMonitor)} to give meaningful output if things go wrong.
129 * @param files data files to import
130 * @param progressMonitor progress monitor
131 * @return true if data import was successful
132 */
133 public boolean importDataHandleExceptions(List<File> files, ProgressMonitor progressMonitor) {
134 try {
135 Main.info("Open "+files.size()+" files");
136 importData(files, progressMonitor);
137 return true;
138 } catch (IOException | IllegalDataException e) {
139 Main.error(e);
140 HelpAwareOptionPane.showMessageDialogInEDT(
141 Main.parent,
142 tr("<html>Could not read files.<br>Error is:<br>{0}</html>", Utils.escapeReservedCharactersHTML(e.getMessage())),
143 tr("Error"),
144 JOptionPane.ERROR_MESSAGE, null
145 );
146 return false;
147 }
148 }
149
150 /**
151 * If multiple files (with multiple file formats) are selected,
152 * they are opened in the order of their priorities.
153 * Highest priority comes first.
154 * @return priority
155 */
156 public double getPriority() {
157 return 0;
158 }
159
160 @Override
161 public int compareTo(FileImporter other) {
162 return Double.compare(this.getPriority(), other.getPriority());
163 }
164
165 /**
166 * Returns the enabled state of this {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
167 * @return true if this {@code FileImporter} is enabled
168 * @since 5459
169 */
170 public final boolean isEnabled() {
171 return enabled;
172 }
173
174 /**
175 * Sets the enabled state of the {@code FileImporter}. When enabled, it is listed and usable in "File-&gt;Open" dialog.
176 * @param enabled true to enable this {@code FileImporter}, false to disable it
177 * @since 5459
178 */
179 public final void setEnabled(boolean enabled) {
180 this.enabled = enabled;
181 }
182}
Note: See TracBrowser for help on using the repository browser.