Ticket #20310: 20310.1.patch
File 20310.1.patch, 15.3 KB (added by , 3 years ago) |
---|
-
src/org/openstreetmap/josm/actions/OpenFileAction.java
16 16 import java.util.Arrays; 17 17 import java.util.Collection; 18 18 import java.util.Collections; 19 import java.util.EnumSet; 19 20 import java.util.HashSet; 20 21 import java.util.LinkedHashSet; 21 22 import java.util.LinkedList; 22 23 import java.util.List; 24 import java.util.Objects; 23 25 import java.util.Set; 24 26 import java.util.concurrent.Future; 25 27 import java.util.regex.Matcher; 26 28 import java.util.regex.Pattern; 29 import java.util.stream.Stream; 27 30 28 31 import javax.swing.JOptionPane; 29 32 import javax.swing.SwingUtilities; … … 37 40 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 38 41 import org.openstreetmap.josm.gui.io.importexport.AllFormatsImporter; 39 42 import org.openstreetmap.josm.gui.io.importexport.FileImporter; 43 import org.openstreetmap.josm.gui.io.importexport.Options; 40 44 import org.openstreetmap.josm.gui.util.GuiHelper; 41 45 import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 42 46 import org.openstreetmap.josm.io.OsmTransferException; … … 95 99 * @since 11986 (return task) 96 100 */ 97 101 public static Future<?> openFiles(List<File> fileList) { 98 return openFiles(fileList, false);102 return openFiles(fileList, (Options[]) null); 99 103 } 100 104 101 105 /** … … 104 108 * @param recordHistory {@code true} to save filename in history (default: false) 105 109 * @return the future task 106 110 * @since 11986 (return task) 111 * @deprecated Since xxx, use {@link OpenFileAction#openFiles(List, Options...)} with {@link Options#RECORD_HISTORY} instead. 107 112 */ 113 @Deprecated 108 114 public static Future<?> openFiles(List<File> fileList, boolean recordHistory) { 115 Options[] options = recordHistory ? new Options[] {Options.RECORD_HISTORY} : null; 116 return openFiles(fileList, options); 117 } 118 119 /** 120 * Open a list of files. The complete list will be passed to batch importers. 121 * @param fileList A list of files 122 * @param options The options to use 123 * @return the future task 124 * @since xxx ({@link Options}) 125 */ 126 public static Future<?> openFiles(List<File> fileList, Options... options) { 109 127 OpenFileTask task = new OpenFileTask(fileList, null); 110 task.set RecordHistory(recordHistory);128 task.setOptions(options); 111 129 return MainApplication.worker.submit(task); 112 130 } 113 131 … … 121 139 private final Set<String> failedAll = new HashSet<>(); 122 140 private final FileFilter fileFilter; 123 141 private boolean canceled; 124 private boolean recordHistory;142 private final EnumSet<Options> options = EnumSet.noneOf(Options.class); 125 143 126 144 /** 127 145 * Constructs a new {@code OpenFileTask}. … … 167 185 /** 168 186 * Sets whether to save filename in history (for list of recently opened files). 169 187 * @param recordHistory {@code true} to save filename in history (default: false) 188 * @deprecated since xxx (use {@link #setOptions} instead). 170 189 */ 190 @Deprecated 171 191 public void setRecordHistory(boolean recordHistory) { 172 this.recordHistory = recordHistory; 192 if (recordHistory) { 193 this.options.add(Options.RECORD_HISTORY); 194 } else { 195 this.options.remove(Options.RECORD_HISTORY); 196 } 173 197 } 174 198 175 199 /** 200 * Set the options for the task. 201 * @param options The options to set 202 * @see Options 203 */ 204 public void setOptions(Options[] options) { 205 this.options.clear(); 206 if (options != null) { 207 Stream.of(options).filter(Objects::nonNull).forEach(this.options::add); 208 } 209 } 210 211 /** 176 212 * Determines if filename must be saved in history (for list of recently opened files). 177 213 * @return {@code true} if filename must be saved in history 178 214 */ 179 215 public boolean isRecordHistory() { 180 return recordHistory;216 return this.options.contains(Options.RECORD_HISTORY); 181 217 } 182 218 219 /** 220 * Get the options for this task 221 * @return A set of options 222 */ 223 public Set<Options> getOptions() { 224 return Collections.unmodifiableSet(this.options); 225 } 226 183 227 @Override 184 228 protected void cancel() { 185 229 this.canceled = true; … … 339 383 } 340 384 } 341 385 342 if ( recordHistory) {386 if (this.options.contains(Options.RECORD_HISTORY)) { 343 387 Collection<String> oldFileHistory = Config.getPref().getList("file-open.history"); 344 388 fileHistory.addAll(oldFileHistory); 345 389 // remove the files which failed to load from the list … … 355 399 * @param files data files to import 356 400 */ 357 401 public void importData(FileImporter importer, List<File> files) { 402 importer.setOptions(this.options.toArray(new Options[0])); 358 403 if (importer.isBatchImporter()) { 359 404 if (canceled) return; 360 405 String msg = trn("Opening {0} file...", "Opening {0} files...", files.size(), files.size()); … … 372 417 } 373 418 } 374 419 } 375 if ( recordHistory&& !importer.isBatchImporter()) {420 if (this.options.contains(Options.RECORD_HISTORY) && !importer.isBatchImporter()) { 376 421 for (File f : files) { 377 422 try { 378 423 if (successfullyOpenedFiles.contains(f)) { -
src/org/openstreetmap/josm/gui/io/importexport/FileImporter.java
5 5 6 6 import java.io.File; 7 7 import java.io.IOException; 8 import java.util.EnumSet; 8 9 import java.util.List; 10 import java.util.Objects; 11 import java.util.stream.Stream; 9 12 10 13 import javax.swing.JOptionPane; 11 14 … … 36 39 37 40 private boolean enabled; 38 41 42 protected final EnumSet<Options> options = EnumSet.noneOf(Options.class); 43 39 44 /** 40 45 * Constructs a new {@code FileImporter} with the given extension file filter. 41 46 * @param filter The extension file filter … … 190 195 public final void setEnabled(boolean enabled) { 191 196 this.enabled = enabled; 192 197 } 198 199 /** 200 * Set the options for the {@code FileImporter}. 201 * @param options The options to set 202 * @since xxx 203 */ 204 public final void setOptions(Options[] options) { 205 this.options.clear(); 206 if (options != null) { 207 Stream.of(options).filter(Objects::nonNull).forEach(this.options::add); 208 } 209 } 193 210 } -
src/org/openstreetmap/josm/gui/io/importexport/JpgImporter.java
7 7 import java.io.IOException; 8 8 import java.util.ArrayList; 9 9 import java.util.Arrays; 10 import java.util.EnumSet; 10 11 import java.util.HashSet; 11 12 import java.util.List; 12 13 import java.util.Set; 14 import java.util.regex.Matcher; 15 import java.util.regex.Pattern; 13 16 14 17 import org.openstreetmap.josm.actions.ExtensionFileFilter; 15 18 import org.openstreetmap.josm.gui.layer.GpxLayer; 16 19 import org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer; 17 20 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 21 import org.openstreetmap.josm.io.CachedFile; 18 22 import org.openstreetmap.josm.io.IllegalDataException; 19 23 20 24 /** … … 22 26 * 23 27 */ 24 28 public class JpgImporter extends FileImporter { 29 /** Check if the filename starts with a borked path ({@link java.io.File#File} drops consecutive {@code /} characters). */ 30 private static final Pattern URL_START_BAD = Pattern.compile("^(https?:/)([^/].*)$"); 31 /** Check for the beginning of a "good" url */ 32 private static final Pattern URL_START_GOOD = Pattern.compile("^https?://.*$"); 33 25 34 private GpxLayer gpx; 26 35 27 36 /** … … 73 82 try { 74 83 List<File> files = new ArrayList<>(); 75 84 Set<String> visitedDirs = new HashSet<>(); 76 addRecursiveFiles( files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true));85 addRecursiveFiles(this.options, files, visitedDirs, sel, progressMonitor.createSubTaskMonitor(1, true)); 77 86 78 87 if (progressMonitor.isCanceled()) 79 88 return; … … 89 98 90 99 static void addRecursiveFiles(List<File> files, Set<String> visitedDirs, List<File> sel, ProgressMonitor progressMonitor) 91 100 throws IOException { 101 addRecursiveFiles(EnumSet.noneOf(Options.class), files, visitedDirs, sel, progressMonitor); 102 } 92 103 104 static void addRecursiveFiles(Set<Options> options, List<File> files, Set<String> visitedDirs, List<File> sel, 105 ProgressMonitor progressMonitor) throws IOException { 106 93 107 if (progressMonitor.isCanceled()) 94 108 return; 95 109 … … 100 114 if (visitedDirs.add(f.getCanonicalPath())) { // Do not loop over symlinks 101 115 File[] dirFiles = f.listFiles(); // Can be null for some strange directories (like lost+found) 102 116 if (dirFiles != null) { 103 addRecursiveFiles(files, visitedDirs, Arrays.asList(dirFiles), progressMonitor.createSubTaskMonitor(1, true)); 117 addRecursiveFiles(options, files, visitedDirs, Arrays.asList(dirFiles), 118 progressMonitor.createSubTaskMonitor(1, true)); 104 119 } 105 120 } else { 106 121 progressMonitor.worked(1); 107 122 } 108 123 } else { 109 if (FILE_FILTER.accept(f)) { 124 /* Check if the path is a web path, and if so, ensure that it is "correct" */ 125 final String path = f.getPath(); 126 Matcher matcherBad = URL_START_BAD.matcher(path); 127 final String realPath; 128 if (matcherBad.matches()) { 129 realPath = matcherBad.replaceFirst(matcherBad.group(1) + "/" + matcherBad.group(2)); 130 } else { 131 realPath = path; 132 } 133 if (URL_START_GOOD.matcher(realPath).matches() && FILE_FILTER.accept(f) 134 && options.contains(Options.ALLOW_WEB_RESOURCES)) { 135 try (CachedFile cachedFile = new CachedFile(realPath)) { 136 files.add(cachedFile.getFile()); 137 } 138 } else if (FILE_FILTER.accept(f)) { 110 139 files.add(f); 111 140 } 112 141 progressMonitor.worked(1); -
src/org/openstreetmap/josm/gui/io/importexport/Options.java
1 // License: GPL. For details, see LICENSE file. 2 package org.openstreetmap.josm.gui.io.importexport; 3 4 /** 5 * Options for ImportExport classes. Not all ImportExport classes support all options. 6 * @author Taylor Smock 7 * 8 */ 9 public enum Options { 10 /** Allow import/export of web resources */ 11 ALLOW_WEB_RESOURCES, 12 /** Record history. Primarily used in OpenFileAction */ 13 RECORD_HISTORY 14 } -
src/org/openstreetmap/josm/io/remotecontrol/PermissionPrefWithDefault.java
29 29 /** Open local files */ 30 30 public static final PermissionPrefWithDefault OPEN_FILES = 31 31 new PermissionPrefWithDefault("remotecontrol.permission.open-files", false, tr("Open local files")); 32 /** Open web files */ 33 public static final PermissionPrefWithDefault ALLOW_WEB_RESOURCES = 34 new PermissionPrefWithDefault("remotecontrol.permission.open-remote-files", false, tr("Open remote files")); 32 35 /** Load imagery layers */ 33 36 public static final PermissionPrefWithDefault LOAD_IMAGERY = 34 37 new PermissionPrefWithDefault("remotecontrol.permission.imagery", true, tr("Load imagery layers")); … … 44 47 /** Read protocol version */ 45 48 public static final PermissionPrefWithDefault READ_PROTOCOL_VERSION = 46 49 new PermissionPrefWithDefault("remotecontrol.permission.read-protocolversion", true, tr("Read protocol version")); 50 47 51 /** 48 52 * name of the preference setting to permit the remote operation 49 53 */ -
src/org/openstreetmap/josm/io/remotecontrol/RequestProcessor.java
162 162 addRequestHandlerClass(LoadDataHandler.command, LoadDataHandler.class, true); 163 163 addRequestHandlerClass(ImportHandler.command, ImportHandler.class, true); 164 164 addRequestHandlerClass(OpenFileHandler.command, OpenFileHandler.class, true); 165 PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.ALLOW_WEB_RESOURCES); 165 166 addRequestHandlerClass(ImageryHandler.command, ImageryHandler.class, true); 166 167 PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_SELECTION); 167 168 PermissionPrefWithDefault.addPermissionPref(PermissionPrefWithDefault.CHANGE_VIEWPORT); -
src/org/openstreetmap/josm/io/remotecontrol/handler/OpenFileHandler.java
5 5 6 6 import java.io.File; 7 7 import java.util.Arrays; 8 import java.util.EnumSet; 8 9 9 10 import org.openstreetmap.josm.actions.OpenFileAction; 11 import org.openstreetmap.josm.gui.io.importexport.Options; 10 12 import org.openstreetmap.josm.gui.util.GuiHelper; 11 13 import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault; 12 14 … … 42 44 43 45 @Override 44 46 protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException { 45 GuiHelper.runInEDTAndWait(() -> OpenFileAction.openFiles(Arrays.asList(new File(args.get("filename"))))); 47 EnumSet<Options> options = EnumSet.noneOf(Options.class); 48 if (PermissionPrefWithDefault.ALLOW_WEB_RESOURCES.isAllowed()) { 49 options.add(Options.ALLOW_WEB_RESOURCES); 50 } 51 GuiHelper.runInEDTAndWait(() -> 52 OpenFileAction.openFiles(Arrays.asList(new File(args.get("filename"))), options.toArray(new Options[0]))); 46 53 } 47 54 48 55 @Override