Ignore:
Timestamp:
2015-09-29T23:30:20+02:00 (9 years ago)
Author:
simon04
Message:

fix #11372 - Main: commandline uses OpenLocationAction for parsing http URLs, refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r8799 r8809  
    5757import org.openstreetmap.josm.actions.JosmAction;
    5858import org.openstreetmap.josm.actions.OpenFileAction;
     59import org.openstreetmap.josm.actions.OpenLocationAction;
    5960import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    6061import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
     
    985986            List<File> fileList = new ArrayList<>();
    986987            for (String s : args.get(Option.DOWNLOAD)) {
    987                 File f = null;
    988                 switch(paramType(s)) {
    989                 case httpUrl:
    990                     downloadFromParamHttp(false, s);
    991                     break;
    992                 case bounds:
    993                     downloadFromParamBounds(false, s);
    994                     break;
    995                 case fileUrl:
    996                     try {
    997                         f = new File(new URI(s));
    998                     } catch (URISyntaxException e) {
    999                         JOptionPane.showMessageDialog(
    1000                                 Main.parent,
    1001                                 tr("Ignoring malformed file URL: \"{0}\"", s),
    1002                                 tr("Warning"),
    1003                                 JOptionPane.WARNING_MESSAGE
    1004                                 );
    1005                     }
    1006                     if (f != null) {
    1007                         fileList.add(f);
    1008                     }
    1009                     break;
    1010                 case fileName:
    1011                     f = new File(s);
    1012                     fileList.add(f);
    1013                     break;
    1014                 }
     988                DownloadParamType.paramType(s).download(s, fileList);
    1015989            }
    1016990            if (!fileList.isEmpty()) {
     
    1020994        if (args.containsKey(Option.DOWNLOADGPS)) {
    1021995            for (String s : args.get(Option.DOWNLOADGPS)) {
    1022                 switch(paramType(s)) {
    1023                 case httpUrl:
    1024                     downloadFromParamHttp(true, s);
    1025                     break;
    1026                 case bounds:
    1027                     downloadFromParamBounds(true, s);
    1028                     break;
    1029                 case fileUrl:
    1030                 case fileName:
    1031                     JOptionPane.showMessageDialog(
    1032                             Main.parent,
    1033                             tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
    1034                             tr("Warning"),
    1035                             JOptionPane.WARNING_MESSAGE
    1036                             );
    1037                 }
     996                DownloadParamType.paramType(s).downloadGps(s);
    1038997            }
    1039998        }
     
    11361095     * @see #paramType
    11371096     */
    1138     enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
    1139 
    1140     /**
    1141      * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
    1142      * @param s A parameter string
    1143      * @return The guessed parameter type
    1144      */
    1145     static DownloadParamType paramType(String s) {
    1146         if (s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl;
    1147         if (s.startsWith("file:")) return DownloadParamType.fileUrl;
    1148         String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
    1149         if (s.matches(coorPattern+"(,"+coorPattern+"){3}")) return DownloadParamType.bounds;
    1150         // everything else must be a file name
    1151         return DownloadParamType.fileName;
    1152     }
    1153 
    1154     /**
    1155      * Download area specified on the command line as OSM URL.
    1156      * @param rawGps Flag to download raw GPS tracks
    1157      * @param s The URL parameter
    1158      */
    1159     private static void downloadFromParamHttp(final boolean rawGps, String s) {
    1160         final Bounds b = OsmUrlToBounds.parse(s);
    1161         if (b == null) {
     1097    enum DownloadParamType {
     1098        httpUrl {
     1099            @Override
     1100            void download(String s, Collection<File> fileList) {
     1101                new OpenLocationAction().openUrl(false, s);
     1102            }
     1103
     1104            @Override
     1105            void downloadGps(String s) {
     1106                final Bounds b = OsmUrlToBounds.parse(s);
     1107                if (b == null) {
     1108                    JOptionPane.showMessageDialog(
     1109                            Main.parent,
     1110                            tr("Ignoring malformed URL: \"{0}\"", s),
     1111                            tr("Warning"),
     1112                            JOptionPane.WARNING_MESSAGE
     1113                    );
     1114                    return;
     1115                }
     1116                downloadFromParamBounds(true, b);
     1117            }
     1118        }, fileUrl {
     1119            @Override
     1120            void download(String s, Collection<File> fileList) {
     1121                File f = null;
     1122                try {
     1123                    f = new File(new URI(s));
     1124                } catch (URISyntaxException e) {
     1125                    JOptionPane.showMessageDialog(
     1126                            Main.parent,
     1127                            tr("Ignoring malformed file URL: \"{0}\"", s),
     1128                            tr("Warning"),
     1129                            JOptionPane.WARNING_MESSAGE
     1130                    );
     1131                }
     1132                if (f != null) {
     1133                    fileList.add(f);
     1134                }
     1135            }
     1136        }, bounds {
     1137
     1138            /**
     1139             * Download area specified on the command line as bounds string.
     1140             * @param rawGps Flag to download raw GPS tracks
     1141             * @param s The bounds parameter
     1142             */
     1143            private void downloadFromParamBounds(final boolean rawGps, String s) {
     1144                final StringTokenizer st = new StringTokenizer(s, ",");
     1145                if (st.countTokens() == 4) {
     1146                    Bounds b = new Bounds(
     1147                            new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
     1148                            new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
     1149                    );
     1150                    Main.downloadFromParamBounds(rawGps, b);
     1151                }
     1152            }
     1153
     1154            @Override
     1155            void download(String param, Collection<File> fileList) {
     1156                downloadFromParamBounds(false, param);
     1157            }
     1158
     1159            @Override
     1160            void downloadGps(String param) {
     1161                downloadFromParamBounds(true, param);
     1162            }
     1163        }, fileName {
     1164            @Override
     1165            void download(String s, Collection<File> fileList) {
     1166                fileList.add(new File(s));
     1167            }
     1168        };
     1169
     1170        /**
     1171         * Performs the download
     1172         * @param param represents the object to be downloaded
     1173         * @param fileList files which shall be opened, should be added to this collection
     1174         */
     1175        abstract void download(String param, Collection<File> fileList);
     1176
     1177        /**
     1178         * Performs the GPS download
     1179         * @param param represents the object to be downloaded
     1180         */
     1181        void downloadGps(String param) {
    11621182            JOptionPane.showMessageDialog(
    11631183                    Main.parent,
    1164                     tr("Ignoring malformed URL: \"{0}\"", s),
     1184                    tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
    11651185                    tr("Warning"),
    11661186                    JOptionPane.WARNING_MESSAGE
    1167                     );
    1168         } else {
    1169             downloadFromParamBounds(rawGps, b);
    1170         }
    1171     }
    1172 
    1173     /**
    1174      * Download area specified on the command line as bounds string.
    1175      * @param rawGps Flag to download raw GPS tracks
    1176      * @param s The bounds parameter
    1177      */
    1178     private static void downloadFromParamBounds(final boolean rawGps, String s) {
    1179         final StringTokenizer st = new StringTokenizer(s, ",");
    1180         if (st.countTokens() == 4) {
    1181             Bounds b = new Bounds(
    1182                     new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken())),
    1183                     new LatLon(Double.parseDouble(st.nextToken()), Double.parseDouble(st.nextToken()))
    1184                     );
    1185             downloadFromParamBounds(rawGps, b);
     1187            );
     1188        }
     1189
     1190        /**
     1191         * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
     1192         *
     1193         * @param s A parameter string
     1194         * @return The guessed parameter type
     1195         */
     1196        static DownloadParamType paramType(String s) {
     1197            if (s.startsWith("http:") || s.startsWith("https:")) return DownloadParamType.httpUrl;
     1198            if (s.startsWith("file:")) return DownloadParamType.fileUrl;
     1199            String coorPattern = "\\s*[+-]?[0-9]+(\\.[0-9]+)?\\s*";
     1200            if (s.matches(coorPattern + "(," + coorPattern + "){3}")) return DownloadParamType.bounds;
     1201            // everything else must be a file name
     1202            return DownloadParamType.fileName;
    11861203        }
    11871204    }
     
    11911208     * @param rawGps Flag to download raw GPS tracks
    11921209     * @param b The bounds value
    1193      * @see #downloadFromParamBounds(boolean, String)
    1194      * @see #downloadFromParamHttp
    11951210     */
    11961211    private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
Note: See TracChangeset for help on using the changeset viewer.