001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GraphicsEnvironment; 007import java.io.File; 008import java.net.URI; 009import java.net.URISyntaxException; 010import java.util.Collection; 011import java.util.Collections; 012import java.util.List; 013import java.util.StringTokenizer; 014import java.util.concurrent.Future; 015 016import javax.swing.JOptionPane; 017 018import org.openstreetmap.josm.actions.OpenLocationAction; 019import org.openstreetmap.josm.data.Bounds; 020import org.openstreetmap.josm.data.coor.LatLon; 021import org.openstreetmap.josm.tools.Logging; 022import org.openstreetmap.josm.tools.OsmUrlToBounds; 023 024/** 025 * The type of a command line parameter, to be used in switch statements. 026 * @since 12633 (extracted from {@code Main}) 027 */ 028public enum DownloadParamType { 029 /** http(s):// URL */ 030 httpUrl { 031 @Override 032 public List<Future<?>> download(String s, Collection<File> fileList) { 033 return new OpenLocationAction().openUrl(false, s); 034 } 035 036 @Override 037 public List<Future<?>> downloadGps(String s) { 038 final Bounds b = OsmUrlToBounds.parse(s); 039 if (b == null) { 040 JOptionPane.showMessageDialog( 041 MainApplication.getMainFrame(), 042 tr("Ignoring malformed URL: \"{0}\"", s), 043 tr("Warning"), 044 JOptionPane.WARNING_MESSAGE 045 ); 046 return Collections.emptyList(); 047 } 048 return MainApplication.downloadFromParamBounds(true, b); 049 } 050 }, 051 /** file:// URL */ 052 fileUrl { 053 @Override 054 public List<Future<?>> download(String s, Collection<File> fileList) { 055 File f = null; 056 try { 057 f = new File(new URI(s)); 058 } catch (URISyntaxException e) { 059 Logging.warn(e); 060 JOptionPane.showMessageDialog( 061 MainApplication.getMainFrame(), 062 tr("Ignoring malformed file URL: \"{0}\"", s), 063 tr("Warning"), 064 JOptionPane.WARNING_MESSAGE 065 ); 066 } 067 if (f != null) { 068 fileList.add(f); 069 } 070 return Collections.emptyList(); 071 } 072 }, 073 /** geographic area */ 074 bounds { 075 076 /** 077 * Download area specified on the command line as bounds string. 078 * @param rawGps Flag to download raw GPS tracks 079 * @param s The bounds parameter. Coordinates must use dot decimal separator as comma is used to delimit values 080 * @return the complete download task (including post-download handler), or {@code null} 081 */ 082 private List<Future<?>> downloadFromParamBounds(final boolean rawGps, String s) { 083 final StringTokenizer st = new StringTokenizer(s, ","); 084 if (st.countTokens() == 4) { 085 return MainApplication.downloadFromParamBounds(rawGps, new Bounds( 086 new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())), 087 new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())) 088 )); 089 } 090 return Collections.emptyList(); 091 } 092 093 @Override 094 public List<Future<?>> download(String param, Collection<File> fileList) { 095 return downloadFromParamBounds(false, param); 096 } 097 098 @Override 099 public List<Future<?>> downloadGps(String param) { 100 return downloadFromParamBounds(true, param); 101 } 102 }, 103 /** local file name */ 104 fileName { 105 @Override 106 public List<Future<?>> download(String s, Collection<File> fileList) { 107 fileList.add(new File(s)); 108 return Collections.emptyList(); 109 } 110 }; 111 112 /** 113 * Performs the download 114 * @param param represents the object to be downloaded 115 * @param fileList files which shall be opened, should be added to this collection 116 * @return the download task, or {@code null} 117 */ 118 public abstract List<Future<?>> download(String param, Collection<File> fileList); 119 120 /** 121 * Performs the GPS download 122 * @param param represents the object to be downloaded 123 * @return the download task, or {@code null} 124 */ 125 public List<Future<?>> downloadGps(String param) { 126 if (!GraphicsEnvironment.isHeadless()) { 127 JOptionPane.showMessageDialog( 128 MainApplication.getMainFrame(), 129 tr("Parameter \"downloadgps\" does not accept file names or file URLs"), 130 tr("Warning"), 131 JOptionPane.WARNING_MESSAGE 132 ); 133 } 134 return Collections.emptyList(); 135 } 136 137 /** 138 * Guess the type of a parameter string specified on the command line with --download= or --downloadgps. 139 * 140 * @param s A parameter string 141 * @return The guessed parameter type 142 */ 143 public static DownloadParamType paramType(String s) { 144 if (s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl; 145 if (s.startsWith("file:")) return DownloadParamType.fileUrl; 146 String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*"; 147 if (s.matches(coorPattern + "(," + coorPattern + "){3}")) return DownloadParamType.bounds; 148 // everything else must be a file name 149 return DownloadParamType.fileName; 150 } 151}