Changeset 2798 in josm for trunk


Ignore:
Timestamp:
2010-01-09T23:26:18+01:00 (14 years ago)
Author:
stoecker
Message:

fix commandline file loading - closes #4288 - patch by bomm

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

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

    r2715 r2798  
    1010import java.awt.event.KeyEvent;
    1111import java.io.File;
    12 import java.io.IOException;
    1312import java.net.URI;
    1413import java.net.URISyntaxException;
     
    5049import org.openstreetmap.josm.gui.SplashScreen;
    5150import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
    52 import org.openstreetmap.josm.gui.help.HelpBrowser;
    5351import org.openstreetmap.josm.gui.io.SaveLayersDialog;
    5452import org.openstreetmap.josm.gui.layer.Layer;
     
    5957import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
    6058import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
    61 import org.openstreetmap.josm.io.IllegalDataException;
    6259import org.openstreetmap.josm.plugins.PluginHandler;
    6360import org.openstreetmap.josm.tools.ImageProvider;
     
    378375    public void postConstructorProcessCmdLine(Map<String, Collection<String>> args) {
    379376        if (args.containsKey("download")) {
     377            List<File> fileList = new ArrayList<File>();
    380378            for (String s : args.get("download")) {
    381                 downloadFromParamString(false, s);
     379                File f = null;
     380                switch(paramType(s)) {
     381                case httpUrl:
     382                    downloadFromParamHttp(false, s);
     383                    break;
     384                case bounds:
     385                    downloadFromParamBounds(false, s);
     386                    break;
     387                case fileUrl:
     388                    try {
     389                        f = new File(new URI(s));
     390                    } catch (URISyntaxException e) {
     391                        JOptionPane.showMessageDialog(
     392                                Main.parent,
     393                                tr("Ignoring malformed file URL: \"{0}\"", s),
     394                                tr("Warning"),
     395                                JOptionPane.WARNING_MESSAGE
     396                        );
     397                    }
     398                    if (f!=null) {
     399                        fileList.add(f);
     400                    }
     401                    break;
     402                case fileName:
     403                    f = new File(s);
     404                    fileList.add(f);
     405                    break;
     406                }
     407            }
     408            if(!fileList.isEmpty())
     409            {
     410                OpenFileAction.openFiles(fileList);
    382411            }
    383412        }
    384413        if (args.containsKey("downloadgps")) {
    385414            for (String s : args.get("downloadgps")) {
    386                 downloadFromParamString(true, s);
     415                switch(paramType(s)) {
     416                case httpUrl:
     417                    downloadFromParamHttp(true, s);
     418                    break;
     419                case bounds:
     420                    downloadFromParamBounds(true, s);
     421                    break;
     422                case fileUrl:
     423                case fileName:
     424                    JOptionPane.showMessageDialog(
     425                            Main.parent,
     426                            tr("Parameter \"downloadgps\" does not accept file names or file URLs"),
     427                            tr("Warning"),
     428                            JOptionPane.WARNING_MESSAGE
     429                    );
     430                }
    387431            }
    388432        }
     
    417461    }
    418462
    419     private static void downloadFromParamString(final boolean rawGps, String s) {
    420         if (s.startsWith("http:")) {
    421             final Bounds b = OsmUrlToBounds.parse(s);
    422             if (b == null) {
    423                 JOptionPane.showMessageDialog(
    424                         Main.parent,
    425                         tr("Ignoring malformed URL: \"{0}\"", s),
    426                         tr("Warning"),
    427                         JOptionPane.WARNING_MESSAGE
    428                 );
    429             } else {
    430                 //DownloadTask osmTask = main.menu.download.downloadTasks.get(0);
    431                 DownloadTask osmTask = new DownloadOsmTask();
    432                 Future<?> future = osmTask.download(true, b, null);
    433                 Main.worker.submit(new PostDownloadHandler(osmTask, future));
    434             }
    435             return;
    436         }
    437 
    438         if (s.startsWith("file:")) {
    439             File f = null;
    440             try {
    441                 f = new File(new URI(s));
    442             } catch (URISyntaxException e) {
    443                 JOptionPane.showMessageDialog(
    444                         Main.parent,
    445                         tr("Ignoring malformed file URL: \"{0}\"", s),
    446                         tr("Warning"),
    447                         JOptionPane.WARNING_MESSAGE
    448                 );
    449             }
    450             try {
    451                 if (f!=null) {
    452                     OpenFileAction.openFile(f);
    453                 }
    454             } catch(IllegalDataException e) {
    455                 e.printStackTrace();
    456                 JOptionPane.showMessageDialog(
    457                         Main.parent,
    458                         tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
    459                         tr("Error"),
    460                         JOptionPane.ERROR_MESSAGE
    461                 );
    462             }catch(IOException e) {
    463                 e.printStackTrace();
    464                 JOptionPane.showMessageDialog(
    465                         Main.parent,
    466                         tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
    467                         tr("Error"),
    468                         JOptionPane.ERROR_MESSAGE
    469                 );
    470             }
    471             return;
    472         }
    473 
     463
     464    /**
     465     * The type of a command line parameter, to be used in switch statements.
     466     * @see paramType
     467     */
     468    private enum DownloadParamType { httpUrl, fileUrl, bounds, fileName }
     469
     470    /**
     471     * Guess the type of a parameter string specified on the command line with --download= or --downloadgps.
     472     * @param s A parameter string
     473     * @return The guessed parameter type
     474     */
     475    private DownloadParamType paramType(String s) {
     476        if(s.startsWith("http:")) return DownloadParamType.httpUrl;
     477        if(s.startsWith("file:")) return DownloadParamType.fileUrl;
     478        final StringTokenizer st = new StringTokenizer(s, ",");
     479        // we assume a string with exactly 3 commas is a bounds parameter
     480        if (st.countTokens() == 4) return DownloadParamType.bounds;
     481        // everything else must be a file name
     482        return DownloadParamType.fileName;
     483    }
     484
     485    /**
     486     * Download area specified on the command line as OSM URL.
     487     * @param rawGps Flag to download raw GPS tracks
     488     * @param s The URL parameter
     489     */
     490    private static void downloadFromParamHttp(final boolean rawGps, String s) {
     491        final Bounds b = OsmUrlToBounds.parse(s);
     492        if (b == null) {
     493            JOptionPane.showMessageDialog(
     494                    Main.parent,
     495                    tr("Ignoring malformed URL: \"{0}\"", s),
     496                    tr("Warning"),
     497                    JOptionPane.WARNING_MESSAGE
     498            );
     499        } else {
     500            downloadFromParamBounds(rawGps, b);
     501        }
     502    }
     503
     504    /**
     505     * Download area specified on the command line as bounds string.
     506     * @param rawGps Flag to download raw GPS tracks
     507     * @param s The bounds parameter
     508     */
     509    private static void downloadFromParamBounds(final boolean rawGps, String s) {
    474510        final StringTokenizer st = new StringTokenizer(s, ",");
    475511        if (st.countTokens() == 4) {
     
    478514                    new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken()))
    479515            );
    480             try {
    481                 DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
    482                 // asynchronously launch the download task ...
    483                 Future<?> future = task.download(true, b, null);
    484                 // ... and the continuation when the download is finished (this will wait for the download to finish)
    485                 Main.worker.execute(new PostDownloadHandler(task, future));
    486                 return;
    487             } catch (final NumberFormatException e) {
    488             }
    489         }
    490         File f = new File(s);
    491         try {
    492             OpenFileAction.openFile(f);
    493         }catch(IllegalDataException e) {
    494             e.printStackTrace();
    495             JOptionPane.showMessageDialog(
    496                     Main.parent,
    497                     tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
    498                     tr("Error"),
    499                     JOptionPane.ERROR_MESSAGE
    500             );
    501         }catch(IOException e) {
    502             e.printStackTrace();
    503             JOptionPane.showMessageDialog(
    504                     Main.parent,
    505                     tr("<html>Could not read file ''{0}\''.<br> Error is: <br>{1}</html>", f.getName(), e.getMessage()),
    506                     tr("Error"),
    507                     JOptionPane.ERROR_MESSAGE
    508             );
    509         }
    510     }
     516            downloadFromParamBounds(rawGps, b);
     517        }
     518    }
     519
     520    /**
     521     * Download area specified as Bounds value.
     522     * @param rawGps Flag to download raw GPS tracks
     523     * @param b The bounds value
     524     * @see downloadFromParamBounds(final boolean rawGps, String s)
     525     * @see downloadFromParamHttp
     526     */
     527    private static void downloadFromParamBounds(final boolean rawGps, Bounds b) {
     528        DownloadTask task = rawGps ? new DownloadGpsTask() : new DownloadOsmTask();
     529        // asynchronously launch the download task ...
     530        Future<?> future = task.download(true, b, null);
     531        // ... and the continuation when the download is finished (this will wait for the download to finish)
     532        Main.worker.execute(new PostDownloadHandler(task, future));
     533    }
     534
    511535
    512536    public static void determinePlatformHook() {
  • trunk/src/org/openstreetmap/josm/actions/OpenFileAction.java

    r2703 r2798  
    6060    }
    6161
    62     static public void openFile(File f) throws IOException, IllegalDataException {
    63         for (FileImporter importer : ExtensionFileFilter.importers)
    64             if (importer.acceptFile(f)) {
    65                 importer.importData(f);
    66             }
     62    /**
     63     * Open a list of files. The complete list will be passed to batch importers.
     64     * @param fileList A list of files
     65     */
     66    static public void openFiles(List<File> fileList) {
     67        OpenFileTask task = new OpenFileTask(fileList, null);
     68        Main.worker.submit(task);
    6769    }
    6870
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r2748 r2798  
    7878                "\t[--download=]<filename>                   "+tr("Open file (as raw gps, if .gpx)")+"\n"+
    7979                "\t--downloadgps=minlat,minlon,maxlat,maxlon "+tr("Download the bounding box as raw gps")+"\n"+
     80                "\t--downloadgps=<url>                       "+tr("Download the location at the url (with lat=x&lon=y&zoom=z) as raw gps")+"\n"+
    8081                "\t--selection=<searchstring>                "+tr("Select with the given search")+"\n"+
    8182                "\t--[no-]maximize                           "+tr("Launch in maximized mode")+"\n"+
Note: See TracChangeset for help on using the changeset viewer.