001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import java.io.File; 005import java.util.ArrayList; 006import java.util.Arrays; 007import java.util.Collection; 008import java.util.Collections; 009import java.util.Comparator; 010import java.util.LinkedHashSet; 011import java.util.List; 012import java.util.Objects; 013import java.util.ServiceConfigurationError; 014import java.util.function.Predicate; 015import java.util.stream.Collectors; 016 017import javax.swing.filechooser.FileFilter; 018 019import org.openstreetmap.josm.gui.MainApplication; 020import org.openstreetmap.josm.gui.io.importexport.AllFormatsImporter; 021import org.openstreetmap.josm.gui.io.importexport.FileExporter; 022import org.openstreetmap.josm.gui.io.importexport.FileImporter; 023import org.openstreetmap.josm.gui.io.importexport.GeoJSONImporter; 024import org.openstreetmap.josm.gui.io.importexport.GpxImporter; 025import org.openstreetmap.josm.gui.io.importexport.ImageImporter; 026import org.openstreetmap.josm.gui.io.importexport.NMEAImporter; 027import org.openstreetmap.josm.gui.io.importexport.NoteImporter; 028import org.openstreetmap.josm.gui.io.importexport.OsmChangeImporter; 029import org.openstreetmap.josm.gui.io.importexport.OsmImporter; 030import org.openstreetmap.josm.gui.io.importexport.OsmPbfImporter; 031import org.openstreetmap.josm.gui.io.importexport.OziWptImporter; 032import org.openstreetmap.josm.gui.io.importexport.RtkLibImporter; 033import org.openstreetmap.josm.gui.io.importexport.WMSLayerImporter; 034import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 035import org.openstreetmap.josm.io.session.SessionImporter; 036import org.openstreetmap.josm.tools.Logging; 037import org.openstreetmap.josm.tools.Utils; 038 039/** 040 * A file filter that filters after the extension. Also includes a list of file 041 * filters used in JOSM. 042 * @since 32 043 */ 044public class ExtensionFileFilter extends FileFilter implements java.io.FileFilter { 045 046 /** 047 * List of supported formats for import. 048 * @since 4869 049 */ 050 private static final ArrayList<FileImporter> importers; 051 052 /** 053 * List of supported formats for export. 054 * @since 4869 055 */ 056 private static final ArrayList<FileExporter> exporters; 057 058 // add some file types only if the relevant classes are there. 059 // this gives us the option to painlessly drop them from the .jar 060 // and build JOSM versions without support for these formats 061 062 static { 063 064 importers = new ArrayList<>(); 065 066 final List<Class<? extends FileImporter>> importerNames = Arrays.asList( 067 OsmImporter.class, 068 OsmChangeImporter.class, 069 OsmPbfImporter.class, 070 GeoJSONImporter.class, 071 GpxImporter.class, 072 NMEAImporter.class, 073 OziWptImporter.class, 074 RtkLibImporter.class, 075 NoteImporter.class, 076 ImageImporter.class, 077 WMSLayerImporter.class, 078 AllFormatsImporter.class, 079 SessionImporter.class 080 ); 081 082 for (final Class<? extends FileImporter> importerClass : importerNames) { 083 try { 084 FileImporter importer = importerClass.getConstructor().newInstance(); 085 importers.add(importer); 086 } catch (ReflectiveOperationException e) { 087 Logging.debug(e); 088 } catch (ServiceConfigurationError e) { 089 // error seen while initializing WMSLayerImporter in plugin unit tests: 090 // - 091 // ServiceConfigurationError: javax.imageio.spi.ImageWriterSpi: 092 // Provider com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi could not be instantiated 093 // Caused by: java.lang.IllegalArgumentException: vendorName == null! 094 // at javax.imageio.spi.IIOServiceProvider.<init>(IIOServiceProvider.java:76) 095 // at javax.imageio.spi.ImageReaderWriterSpi.<init>(ImageReaderWriterSpi.java:231) 096 // at javax.imageio.spi.ImageWriterSpi.<init>(ImageWriterSpi.java:213) 097 // at com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageWriterSpi.<init>(CLibJPEGImageWriterSpi.java:84) 098 // - 099 // This is a very strange behaviour of JAI: 100 // http://thierrywasyl.wordpress.com/2009/07/24/jai-how-to-solve-vendorname-null-exception/ 101 // - 102 // that can lead to various problems, see #8583 comments 103 Logging.error(e); 104 } 105 } 106 107 exporters = new ArrayList<>(); 108 109 final List<Class<? extends FileExporter>> exporterClasses = Arrays.asList( 110 org.openstreetmap.josm.gui.io.importexport.GpxExporter.class, 111 org.openstreetmap.josm.gui.io.importexport.OsmExporter.class, 112 org.openstreetmap.josm.gui.io.importexport.OsmGzipExporter.class, 113 org.openstreetmap.josm.gui.io.importexport.OsmBzip2Exporter.class, 114 org.openstreetmap.josm.gui.io.importexport.OsmXzExporter.class, 115 org.openstreetmap.josm.gui.io.importexport.GeoJSONExporter.class, 116 org.openstreetmap.josm.gui.io.importexport.WMSLayerExporter.class, 117 org.openstreetmap.josm.gui.io.importexport.NoteExporter.class, 118 org.openstreetmap.josm.gui.io.importexport.ValidatorErrorExporter.class 119 ); 120 121 for (final Class<? extends FileExporter> exporterClass : exporterClasses) { 122 try { 123 FileExporter exporter = exporterClass.getConstructor().newInstance(); 124 exporters.add(exporter); 125 MainApplication.getLayerManager().addAndFireActiveLayerChangeListener(exporter); 126 } catch (ReflectiveOperationException e) { 127 Logging.debug(e); 128 } catch (ServiceConfigurationError e) { 129 // see above in importers initialization 130 Logging.error(e); 131 } 132 } 133 } 134 135 private final String extensions; 136 private final String description; 137 private final String defaultExtension; 138 139 protected static Comparator<ExtensionFileFilter> comparator() { 140 AllFormatsImporter all = new AllFormatsImporter(); 141 return (o1, o2) -> { 142 if (o1.getDescription().equals(all.filter.getDescription())) return 1; 143 if (o2.getDescription().equals(all.filter.getDescription())) return -1; 144 return o1.getDescription().compareTo(o2.getDescription()); 145 }; 146 } 147 148 /** 149 * Strategy to determine if extensions must be added to the description. 150 */ 151 public enum AddArchiveExtension { 152 /** No extension is added */ 153 NONE, 154 /** Only base extension is added */ 155 BASE, 156 /** All extensions are added (base + archives) */ 157 ALL 158 } 159 160 /** 161 * Adds a new file importer at the end of the global list. This importer will be evaluated after core ones. 162 * @param importer new file importer 163 * @since 10407 164 */ 165 public static void addImporter(FileImporter importer) { 166 if (importer != null) { 167 importers.add(importer); 168 } 169 } 170 171 /** 172 * Adds a new file importer at the beginning of the global list. This importer will be evaluated before core ones. 173 * @param importer new file importer 174 * @since 10407 175 */ 176 public static void addImporterFirst(FileImporter importer) { 177 if (importer != null) { 178 importers.add(0, importer); 179 } 180 } 181 182 /** 183 * Adds a new file exporter at the end of the global list. This exporter will be evaluated after core ones. 184 * @param exporter new file exporter 185 * @since 10407 186 */ 187 public static void addExporter(FileExporter exporter) { 188 if (exporter != null) { 189 exporters.add(exporter); 190 } 191 } 192 193 /** 194 * Adds a new file exporter at the beginning of the global list. This exporter will be evaluated before core ones. 195 * @param exporter new file exporter 196 * @since 10407 197 */ 198 public static void addExporterFirst(FileExporter exporter) { 199 if (exporter != null) { 200 exporters.add(0, exporter); 201 } 202 } 203 204 /** 205 * Returns the list of file importers. 206 * @return unmodifiable list of file importers 207 * @since 10407 208 */ 209 public static List<FileImporter> getImporters() { 210 return Collections.unmodifiableList(importers); 211 } 212 213 /** 214 * Returns the list of file exporters. 215 * @return unmodifiable list of file exporters 216 * @since 10407 217 */ 218 public static List<FileExporter> getExporters() { 219 return Collections.unmodifiableList(exporters); 220 } 221 222 /** 223 * Updates the {@link AllFormatsImporter} that is contained in the importers list. If 224 * you do not use the importers variable directly, you don't need to call this. 225 * <p> 226 * Updating the AllFormatsImporter is required when plugins add new importers that 227 * support new file extensions. The old AllFormatsImporter doesn't include the new 228 * extensions and thus will not display these files. 229 * 230 * @since 5131 231 */ 232 public static void updateAllFormatsImporter() { 233 for (int i = 0; i < importers.size(); i++) { 234 if (importers.get(i) instanceof AllFormatsImporter) { 235 importers.set(i, new AllFormatsImporter()); 236 } 237 } 238 } 239 240 /** 241 * Replies an ordered list of {@link ExtensionFileFilter}s for importing. 242 * The list is ordered according to their description, an {@link AllFormatsImporter} 243 * is append at the end. 244 * 245 * @return an ordered list of {@link ExtensionFileFilter}s for importing. 246 * @since 2029 247 */ 248 public static List<ExtensionFileFilter> getImportExtensionFileFilters() { 249 updateAllFormatsImporter(); 250 return importers.stream() 251 .map(importer -> importer.filter) 252 .sorted(comparator()) 253 .collect(Collectors.toList()); 254 } 255 256 /** 257 * Replies an ordered list of enabled {@link ExtensionFileFilter}s for exporting. 258 * The list is ordered according to their description, an {@link AllFormatsImporter} 259 * is append at the end. 260 * 261 * @return an ordered list of enabled {@link ExtensionFileFilter}s for exporting. 262 * @since 2029 263 */ 264 public static List<ExtensionFileFilter> getExportExtensionFileFilters() { 265 return exporters.stream() 266 .filter(FileExporter::isEnabled) 267 .map(exporter -> exporter.filter) 268 .distinct() 269 .sorted(comparator()) 270 .collect(Collectors.toList()); 271 } 272 273 /** 274 * Replies the default {@link ExtensionFileFilter} for a given extension 275 * 276 * @param extension the extension 277 * @return the default {@link ExtensionFileFilter} for a given extension 278 * @since 2029 279 */ 280 public static ExtensionFileFilter getDefaultImportExtensionFileFilter(String extension) { 281 if (extension == null) return new AllFormatsImporter().filter; 282 return importers.stream() 283 .filter(importer -> extension.equals(importer.filter.getDefaultExtension())) 284 .findFirst().map(importer -> importer.filter) 285 .orElseGet(() -> new AllFormatsImporter().filter); 286 } 287 288 /** 289 * Replies the default {@link ExtensionFileFilter} for a given extension 290 * 291 * @param extension the extension 292 * @return the default {@link ExtensionFileFilter} for a given extension 293 * @since 2029 294 */ 295 public static ExtensionFileFilter getDefaultExportExtensionFileFilter(String extension) { 296 if (extension == null) return new AllFormatsImporter().filter; 297 for (FileExporter exporter : exporters) { 298 if (extension.equals(exporter.filter.getDefaultExtension())) 299 return exporter.filter; 300 } 301 // if extension did not match defaultExtension of any exporter, 302 // scan all supported extensions 303 File file = new File("file." + extension); 304 for (FileExporter exporter : exporters) { 305 if (exporter.filter.accept(file)) 306 return exporter.filter; 307 } 308 return new AllFormatsImporter().filter; 309 } 310 311 /** 312 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the 313 * file chooser for selecting a file for reading. 314 * 315 * @param fileChooser the file chooser 316 * @param extension the default extension 317 * @param additionalTypes matching types will additionally be added to the "file type" combobox. 318 * @since 14668 (signature) 319 */ 320 public static void applyChoosableImportFileFilters( 321 AbstractFileChooser fileChooser, String extension, Predicate<ExtensionFileFilter> additionalTypes) { 322 getImportExtensionFileFilters().stream() 323 .filter(filter -> additionalTypes.test(filter) || filter.acceptName("file."+extension)) 324 .forEach(fileChooser::addChoosableFileFilter); 325 fileChooser.setFileFilter(getDefaultImportExtensionFileFilter(extension)); 326 } 327 328 /** 329 * Applies the choosable {@link FileFilter} to a {@link AbstractFileChooser} before using the 330 * file chooser for selecting a file for writing. 331 * 332 * @param fileChooser the file chooser 333 * @param extension the default extension 334 * @param additionalTypes matching types will additionally be added to the "file type" combobox. 335 * @since 14668 (signature) 336 */ 337 public static void applyChoosableExportFileFilters( 338 AbstractFileChooser fileChooser, String extension, Predicate<ExtensionFileFilter> additionalTypes) { 339 for (ExtensionFileFilter filter: getExportExtensionFileFilters()) { 340 if (additionalTypes.test(filter) || filter.acceptName("file."+extension)) { 341 fileChooser.addChoosableFileFilter(filter); 342 } 343 } 344 fileChooser.setFileFilter(getDefaultExportExtensionFileFilter(extension)); 345 } 346 347 /** 348 * Construct an extension file filter by giving the extension to check after. 349 * @param extension The comma-separated list of file extensions 350 * @param defaultExtension The default extension 351 * @param description A short textual description of the file type 352 * @since 1169 353 */ 354 public ExtensionFileFilter(String extension, String defaultExtension, String description) { 355 this.extensions = extension; 356 this.defaultExtension = defaultExtension; 357 this.description = description; 358 } 359 360 /** 361 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression} 362 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description 363 * in the form {@code old-description (*.ext1, *.ext2)}. 364 * @param extensions The comma-separated list of file extensions 365 * @param defaultExtension The default extension 366 * @param description A short textual description of the file type without supported extensions in parentheses 367 * @param addArchiveExtension Whether to also add the archive extensions to the description 368 * @param archiveExtensions List of extensions to be added 369 * @return The constructed filter 370 */ 371 public static ExtensionFileFilter newFilterWithArchiveExtensions(String extensions, String defaultExtension, 372 String description, AddArchiveExtension addArchiveExtension, List<String> archiveExtensions) { 373 final Collection<String> extensionsPlusArchive = new LinkedHashSet<>(); 374 final Collection<String> extensionsForDescription = new LinkedHashSet<>(); 375 for (String e : extensions.split(",", -1)) { 376 extensionsPlusArchive.add(e); 377 if (addArchiveExtension != AddArchiveExtension.NONE) { 378 extensionsForDescription.add("*." + e); 379 } 380 for (String extension : archiveExtensions) { 381 extensionsPlusArchive.add(e + '.' + extension); 382 if (addArchiveExtension == AddArchiveExtension.ALL) { 383 extensionsForDescription.add("*." + e + '.' + extension); 384 } 385 } 386 } 387 return new ExtensionFileFilter( 388 String.join(",", extensionsPlusArchive), 389 defaultExtension, 390 description + (!extensionsForDescription.isEmpty() 391 ? (" (" + String.join(", ", extensionsForDescription) + ')') 392 : "") 393 ); 394 } 395 396 /** 397 * Construct an extension file filter with the extensions supported by {@link org.openstreetmap.josm.io.Compression} 398 * automatically added to the {@code extensions}. The specified {@code extensions} will be added to the description 399 * in the form {@code old-description (*.ext1, *.ext2)}. 400 * @param extensions The comma-separated list of file extensions 401 * @param defaultExtension The default extension 402 * @param description A short textual description of the file type without supported extensions in parentheses 403 * @param addArchiveExtensionsToDescription Whether to also add the archive extensions to the description 404 * @return The constructed filter 405 */ 406 public static ExtensionFileFilter newFilterWithArchiveExtensions( 407 String extensions, String defaultExtension, String description, boolean addArchiveExtensionsToDescription) { 408 409 List<String> archiveExtensions = Arrays.asList("gz", "bz", "bz2", "xz", "zip"); 410 return newFilterWithArchiveExtensions( 411 extensions, 412 defaultExtension, 413 description, 414 addArchiveExtensionsToDescription ? AddArchiveExtension.ALL : AddArchiveExtension.BASE, 415 archiveExtensions 416 ); 417 } 418 419 /** 420 * Returns true if this file filter accepts the given filename. 421 * @param filename The filename to check after 422 * @return true if this file filter accepts the given filename (i.e if this filename ends with one of the extensions) 423 * @since 1169 424 */ 425 public boolean acceptName(String filename) { 426 return Utils.hasExtension(filename, extensions.split(",", -1)); 427 } 428 429 @Override 430 public boolean accept(File pathname) { 431 if (pathname.isDirectory()) 432 return true; 433 return acceptName(pathname.getName()); 434 } 435 436 @Override 437 public String getDescription() { 438 return description; 439 } 440 441 /** 442 * Replies the comma-separated list of file extensions of this file filter. 443 * @return the comma-separated list of file extensions of this file filter, as a String 444 * @since 5131 445 */ 446 public String getExtensions() { 447 return extensions; 448 } 449 450 /** 451 * Replies the default file extension of this file filter. 452 * @return the default file extension of this file filter 453 * @since 2029 454 */ 455 public String getDefaultExtension() { 456 return defaultExtension; 457 } 458 459 @Override 460 public int hashCode() { 461 return Objects.hash(extensions, description, defaultExtension); 462 } 463 464 @Override 465 public boolean equals(Object obj) { 466 if (this == obj) return true; 467 if (obj == null || getClass() != obj.getClass()) return false; 468 ExtensionFileFilter that = (ExtensionFileFilter) obj; 469 return Objects.equals(extensions, that.extensions) && 470 Objects.equals(description, that.description) && 471 Objects.equals(defaultExtension, that.defaultExtension); 472 } 473}