source: josm/trunk/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java@ 14668

Last change on this file since 14668 was 14668, checked in by simon04, 5 years ago

fix #17185 - Do not offer unsupported file types for saving OsmDataLayer

  • Property svn:eol-style set to native
File size: 19.0 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[236]2package org.openstreetmap.josm.actions;
3
4import java.io.File;
[1637]5import java.util.ArrayList;
[8813]6import java.util.Arrays;
[8894]7import java.util.Collection;
[2029]8import java.util.Collections;
9import java.util.Comparator;
[8894]10import java.util.LinkedHashSet;
[2029]11import java.util.LinkedList;
12import java.util.List;
[9371]13import java.util.Objects;
[7369]14import java.util.ServiceConfigurationError;
[14668]15import java.util.function.Predicate;
[236]16
17import javax.swing.filechooser.FileFilter;
18
[12636]19import org.openstreetmap.josm.gui.MainApplication;
[12671]20import org.openstreetmap.josm.gui.io.importexport.AllFormatsImporter;
21import org.openstreetmap.josm.gui.io.importexport.FileExporter;
22import org.openstreetmap.josm.gui.io.importexport.FileImporter;
23import org.openstreetmap.josm.gui.io.importexport.GpxImporter;
24import org.openstreetmap.josm.gui.io.importexport.JpgImporter;
25import org.openstreetmap.josm.gui.io.importexport.NMEAImporter;
26import org.openstreetmap.josm.gui.io.importexport.NoteImporter;
27import org.openstreetmap.josm.gui.io.importexport.OsmChangeImporter;
28import org.openstreetmap.josm.gui.io.importexport.OsmImporter;
29import org.openstreetmap.josm.gui.io.importexport.WMSLayerImporter;
[7578]30import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
[10755]31import org.openstreetmap.josm.io.session.SessionImporter;
[12620]32import org.openstreetmap.josm.tools.Logging;
[8404]33import org.openstreetmap.josm.tools.Utils;
[1637]34
[236]35/**
[1169]36 * A file filter that filters after the extension. Also includes a list of file
[236]37 * filters used in JOSM.
[5438]38 * @since 32
[236]39 */
[5566]40public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter {
[236]41
[2001]42 /**
[5438]43 * List of supported formats for import.
44 * @since 4869
[2001]45 */
[12542]46 private static final ArrayList<FileImporter> importers;
[2512]47
[5438]48 /**
49 * List of supported formats for export.
50 * @since 4869
51 */
[12542]52 private static final ArrayList<FileExporter> exporters;
[2512]53
[6296]54 // add some file types only if the relevant classes are there.
[2367]55 // this gives us the option to painlessly drop them from the .jar
56 // and build JOSM versions without support for these formats
[236]57
[2367]58 static {
[2029]59
[12542]60 importers = new ArrayList<>();
[2512]61
[8813]62 final List<Class<? extends FileImporter>> importerNames = Arrays.asList(
[10755]63 OsmImporter.class,
64 OsmChangeImporter.class,
65 GpxImporter.class,
66 NMEAImporter.class,
67 NoteImporter.class,
68 JpgImporter.class,
69 WMSLayerImporter.class,
70 AllFormatsImporter.class,
71 SessionImporter.class
[8813]72 );
[2029]73
[8813]74 for (final Class<? extends FileImporter> importerClass : importerNames) {
[2367]75 try {
[10208]76 FileImporter importer = importerClass.getConstructor().newInstance();
[12542]77 importers.add(importer);
[10208]78 } catch (ReflectiveOperationException e) {
[12620]79 Logging.debug(e);
[7369]80 } catch (ServiceConfigurationError e) {
81 // error seen while initializing WMSLayerImporter in plugin unit tests:
82 // -
83 // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi:
84 // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated
85 // Caused by: java.lang.IllegalArgumentException: vendorName == null!
86 // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76)
87 // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231)
88 // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213)
89 // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84)
90 // -
91 // This is a very strange behaviour of JAI:
92 // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/
93 // -
94 // that can lead to various problems, see #8583 comments
[12620]95 Logging.error(e);
[6310]96 }
[2367]97 }
98
[12542]99 exporters = new ArrayList<>();
[2367]100
[8813]101 final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList(
[12671]102 org.openstreetmap.josm.gui.io.importexport.GpxExporter.class,
103 org.openstreetmap.josm.gui.io.importexport.OsmExporter.class,
104 org.openstreetmap.josm.gui.io.importexport.OsmGzipExporter.class,
105 org.openstreetmap.josm.gui.io.importexport.OsmBzip2Exporter.class,
[13352]106 org.openstreetmap.josm.gui.io.importexport.OsmXzExporter.class,
[12671]107 org.openstreetmap.josm.gui.io.importexport.GeoJSONExporter.class,
108 org.openstreetmap.josm.gui.io.importexport.WMSLayerExporter.class,
109 org.openstreetmap.josm.gui.io.importexport.NoteExporter.class,
110 org.openstreetmap.josm.gui.io.importexport.ValidatorErrorExporter.class
[8813]111 );
[2367]112
[8813]113 for (final Class<? extends FileExporter> exporterClass : exporterClasses) {
[2367]114 try {
[10208]115 FileExporter exporter = exporterClass.getConstructor().newInstance();
[12542]116 exporters.add(exporter);
[12636]117 MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(exporter);
[10208]118 } catch (ReflectiveOperationException e) {
[12620]119 Logging.debug(e);
[7369]120 } catch (ServiceConfigurationError e) {
121 // see above in importers initialization
[12620]122 Logging.error(e);
[6310]123 }
[2367]124 }
125 }
126
[2029]127 private final String extensions;
128 private final String description;
[2288]129 private final String defaultExtension;
[2029]130
[6889]131 protected static void sort(List<ExtensionFileFilter> filters) {
[10619]132 filters.sort(new Comparator<ExtensionFileFilter>() {
133 private AllFormatsImporter all = new AllFormatsImporter();
134 @Override
135 public int compare(ExtensionFileFilter o1, ExtensionFileFilter o2) {
136 if (o1.getDescription().equals(all.filter.getDescription())) return 1;
137 if (o2.getDescription().equals(all.filter.getDescription())) return -1;
138 return o1.getDescription().compareTo(o2.getDescription());
[2029]139 }
[10619]140 }
[5131]141 );
[2029]142 }
143
[13329]144 /**
145 * Strategy to determine if extensions must be added to the description.
146 */
147 public enum AddArchiveExtension {
148 /** No extension is added */
149 NONE,
150 /** Only base extension is added */
151 BASE,
152 /** All extensions are added (base + archives) */
153 ALL
154 }
[9537]155
[2029]156 /**
[10407]157 * Adds a new file importer at the end of the global list. This importer will be evaluated after core ones.
158 * @param importer new file importer
159 * @since 10407
160 */
161 public static void addImporter(FileImporter importer) {
162 if (importer != null) {
[12542]163 importers.add(importer);
[10407]164 }
165 }
166
167 /**
168 * Adds a new file importer at the beginning of the global list. This importer will be evaluated before core ones.
169 * @param importer new file importer
170 * @since 10407
171 */
172 public static void addImporterFirst(FileImporter importer) {
173 if (importer != null) {
[12542]174 importers.add(0, importer);
[10407]175 }
176 }
177
178 /**
179 * Adds a new file exporter at the end of the global list. This exporter will be evaluated after core ones.
180 * @param exporter new file exporter
181 * @since 10407
182 */
183 public static void addExporter(FileExporter exporter) {
184 if (exporter != null) {
[12542]185 exporters.add(exporter);
[10407]186 }
187 }
188
189 /**
190 * Adds a new file exporter at the beginning of the global list. This exporter will be evaluated before core ones.
191 * @param exporter new file exporter
192 * @since 10407
193 */
194 public static void addExporterFirst(FileExporter exporter) {
195 if (exporter != null) {
[12542]196 exporters.add(0, exporter);
[10407]197 }
198 }
199
200 /**
201 * Returns the list of file importers.
202 * @return unmodifiable list of file importers
203 * @since 10407
204 */
205 public static List<FileImporter> getImporters() {
[12542]206 return Collections.unmodifiableList(importers);
[10407]207 }
208
209 /**
210 * Returns the list of file exporters.
211 * @return unmodifiable list of file exporters
212 * @since 10407
213 */
214 public static List<FileExporter> getExporters() {
[12542]215 return Collections.unmodifiableList(exporters);
[10407]216 }
217
218 /**
[5266]219 * Updates the {@link AllFormatsImporter} that is contained in the importers list. If
[10873]220 * you do not use the importers variable directly, you don't need to call this.
[5438]221 * <p>
222 * Updating the AllFormatsImporter is required when plugins add new importers that
[10873]223 * support new file extensions. The old AllFormatsImporter doesn't include the new
[5438]224 * extensions and thus will not display these files.
[6069]225 *
[5438]226 * @since 5131
[5131]227 */
228 public static void updateAllFormatsImporter() {
[12542]229 for (int i = 0; i < importers.size(); i++) {
230 if (importers.get(i) instanceof AllFormatsImporter) {
231 importers.set(i, new AllFormatsImporter());
[5131]232 }
233 }
234 }
235
236 /**
[5266]237 * Replies an ordered list of {@link ExtensionFileFilter}s for importing.
238 * The list is ordered according to their description, an {@link AllFormatsImporter}
[2029]239 * is append at the end.
[2512]240 *
[5266]241 * @return an ordered list of {@link ExtensionFileFilter}s for importing.
[5438]242 * @since 2029
[2029]243 */
244 public static List<ExtensionFileFilter> getImportExtensionFileFilters() {
[5131]245 updateAllFormatsImporter();
[8338]246 List<ExtensionFileFilter> filters = new LinkedList<>();
[12542]247 for (FileImporter importer : importers) {
[2029]248 filters.add(importer.filter);
249 }
250 sort(filters);
251 return filters;
252 }
253
254 /**
[5459]255 * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
[5266]256 * The list is ordered according to their description, an {@link AllFormatsImporter}
[2029]257 * is append at the end.
[2512]258 *
[5459]259 * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting.
[5438]260 * @since 2029
[2029]261 */
262 public static List<ExtensionFileFilter> getExportExtensionFileFilters() {
[8338]263 List<ExtensionFileFilter> filters = new LinkedList<>();
[12542]264 for (FileExporter exporter : exporters) {
[5459]265 if (filters.contains(exporter.filter) || !exporter.isEnabled()) {
[2029]266 continue;
267 }
268 filters.add(exporter.filter);
269 }
270 sort(filters);
271 return filters;
272 }
273
274 /**
[5266]275 * Replies the default {@link ExtensionFileFilter} for a given extension
[2512]276 *
[2029]277 * @param extension the extension
[5266]278 * @return the default {@link ExtensionFileFilter} for a given extension
[5438]279 * @since 2029
[2029]280 */
281 public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) {
282 if (extension == null) return new AllFormatsImporter().filter;
[12542]283 for (FileImporter importer : importers) {
[2029]284 if (extension.equals(importer.filter.getDefaultExtension()))
285 return importer.filter;
286 }
287 return new AllFormatsImporter().filter;
288 }
289
290 /**
[5266]291 * Replies the default {@link ExtensionFileFilter} for a given extension
[2512]292 *
[2029]293 * @param extension the extension
[5266]294 * @return the default {@link ExtensionFileFilter} for a given extension
[5438]295 * @since 2029
[2029]296 */
297 public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) {
298 if (extension == null) return new AllFormatsImporter().filter;
[12542]299 for (FileExporter exporter : exporters) {
[2029]300 if (extension.equals(exporter.filter.getDefaultExtension()))
301 return exporter.filter;
302 }
[9466]303 // if extension did not match defaultExtension of any exporter,
304 // scan all supported extensions
305 File file = new File("file." + extension);
[12542]306 for (FileExporter exporter : exporters) {
[9466]307 if (exporter.filter.accept(file))
308 return exporter.filter;
309 }
[2029]310 return new AllFormatsImporter().filter;
311 }
312
313 /**
[7578]314 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
[2029]315 * file chooser for selecting a file for reading.
[2512]316 *
[2029]317 * @param fileChooser the file chooser
318 * @param extension the default extension
[14668]319 * @param additionalTypes matching types will additionally be added to the "file type" combobox.
320 * @since 14668 (signature)
[2029]321 */
[14668]322 public static void applyChoosableImportFileFilters(
323 AbstractFileChooser fileChooser, String extension, Predicate<ExtensionFileFilter> additionalTypes) {
[2029]324 for (ExtensionFileFilter filter: getImportExtensionFileFilters()) {
[9466]325
[14668]326 if (additionalTypes.test(filter) || filter.acceptName("file."+extension)) {
[5438]327 fileChooser.addChoosableFileFilter(filter);
328 }
[2029]329 }
330 fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension));
331 }
332
333 /**
[7578]334 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the
[2029]335 * file chooser for selecting a file for writing.
[2512]336 *
[2029]337 * @param fileChooser the file chooser
338 * @param extension the default extension
[14668]339 * @param additionalTypes matching types will additionally be added to the "file type" combobox.
340 * @since 14668 (signature)
[2029]341 */
[14668]342 public static void applyChoosableExportFileFilters(
343 AbstractFileChooser fileChooser, String extension, Predicate<ExtensionFileFilter> additionalTypes) {
[2029]344 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) {
[14668]345 if (additionalTypes.test(filter) || filter.acceptName("file."+extension)) {
[5438]346 fileChooser.addChoosableFileFilter(filter);
347 }
[2029]348 }
349 fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension));
350 }
351
352 /**
[1169]353 * Construct an extension file filter by giving the extension to check after.
[5438]354 * @param extension The comma-separated list of file extensions
355 * @param defaultExtension The default extension
356 * @param description A short textual description of the file type
357 * @since 1169
[1169]358 */
[2029]359 public ExtensionFileFilter(String extension, String defaultExtension, String description) {
360 this.extensions = extension;
361 this.defaultExtension = defaultExtension;
[1169]362 this.description = description;
363 }
[236]364
[5438]365 /**
[8894]366 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
367 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
368 * in the form {@code old-description (*.ext1, *.ext2)}.
369 * @param extensions The comma-separated list of file extensions
370 * @param defaultExtension The default extension
371 * @param description A short textual description of the file type without supported extensions in parentheses
[9537]372 * @param addArchiveExtension Whether to also add the archive extensions to the description
373 * @param archiveExtensions List of extensions to be added
[8894]374 * @return The constructed filter
375 */
[9547]376 public static ExtensionFileFilter newFilterWithArchiveExtensions(String extensions, String defaultExtension,
377 String description, AddArchiveExtension addArchiveExtension, List<String> archiveExtensions) {
[8894]378 final Collection<String> extensionsPlusArchive = new LinkedHashSet<>();
379 final Collection<String> extensionsForDescription = new LinkedHashSet<>();
380 for (String e : extensions.split(",")) {
381 extensionsPlusArchive.add(e);
[9537]382 if (addArchiveExtension != AddArchiveExtension.NONE) {
383 extensionsForDescription.add("*." + e);
[8895]384 }
[9537]385 for (String extension : archiveExtensions) {
386 extensionsPlusArchive.add(e + '.' + extension);
387 if (addArchiveExtension == AddArchiveExtension.ALL) {
388 extensionsForDescription.add("*." + e + '.' + extension);
389 }
390 }
[8894]391 }
[9537]392 return new ExtensionFileFilter(
393 Utils.join(",", extensionsPlusArchive),
394 defaultExtension,
395 description + (!extensionsForDescription.isEmpty()
[10299]396 ? (" (" + Utils.join(", ", extensionsForDescription) + ')')
[9537]397 : "")
398 );
[8894]399 }
400
401 /**
[9537]402 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression}
403 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description
404 * in the form {@code old-description (*.ext1, *.ext2)}.
405 * @param extensions The comma-separated list of file extensions
406 * @param defaultExtension The default extension
407 * @param description A short textual description of the file type without supported extensions in parentheses
408 * @param addArchiveExtensionsToDescription Whether to also add the archive extensions to the description
409 * @return The constructed filter
410 */
411 public static ExtensionFileFilter newFilterWithArchiveExtensions(
412 String extensions, String defaultExtension, String description, boolean addArchiveExtensionsToDescription) {
413
[13350]414 List<String> archiveExtensions = Arrays.asList("gz", "bz", "bz2", "xz", "zip");
[9537]415 return newFilterWithArchiveExtensions(
416 extensions,
417 defaultExtension,
418 description,
419 addArchiveExtensionsToDescription ? AddArchiveExtension.ALL : AddArchiveExtension.BASE,
420 archiveExtensions
421 );
422 }
423
424 /**
[5438]425 * Returns true if this file filter accepts the given filename.
426 * @param filename The filename to check after
427 * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions)
428 * @since 1169
429 */
[1169]430 public boolean acceptName(String filename) {
[8404]431 return Utils.hasExtension(filename, extensions.split(","));
[1169]432 }
[236]433
[5438]434 @Override
435 public boolean accept(File pathname) {
[1169]436 if (pathname.isDirectory())
437 return true;
438 return acceptName(pathname.getName());
439 }
440
[5438]441 @Override
442 public String getDescription() {
[1169]443 return description;
444 }
[2029]445
[5438]446 /**
[6069]447 * Replies the comma-separated list of file extensions of this file filter.
[5438]448 * @return the comma-separated list of file extensions of this file filter, as a String
449 * @since 5131
450 */
[5131]451 public String getExtensions() {
452 return extensions;
453 }
454
[5438]455 /**
456 * Replies the default file extension of this file filter.
457 * @return the default file extension of this file filter
458 * @since 2029
459 */
[2029]460 public String getDefaultExtension() {
461 return defaultExtension;
462 }
463
464 @Override
465 public int hashCode() {
[9371]466 return Objects.hash(extensions, description, defaultExtension);
[2029]467 }
468
469 @Override
470 public boolean equals(Object obj) {
[9371]471 if (this == obj) return true;
472 if (obj == null || getClass() != obj.getClass()) return false;
473 ExtensionFileFilter that = (ExtensionFileFilter) obj;
474 return Objects.equals(extensions, that.extensions) &&
475 Objects.equals(description, that.description) &&
476 Objects.equals(defaultExtension, that.defaultExtension);
[2029]477 }
[236]478}
Note: See TracBrowser for help on using the repository browser.