Ticket #4288: patch-4288.diff

File patch-4288.diff, 12.7 KB (added by bomm, 16 years ago)

patch to process a list of files specified as command line params

  • src/org/openstreetmap/josm/Main.java

    diff --git a/src/org/openstreetmap/josm/Main.java b/src/org/openstreetmap/josm/Main.java
    index e862354..ad0ecb0 100644
    a b import java.awt.Rectangle;  
    99import java.awt.Toolkit;
    1010import java.awt.event.KeyEvent;
    1111import java.io.File;
    12 import java.io.IOException;
    1312import java.net.URI;
    1413import java.net.URISyntaxException;
    1514import java.util.ArrayList;
    import org.openstreetmap.josm.gui.MainMenu;  
    4948import org.openstreetmap.josm.gui.MapFrame;
    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;
    5553import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    import org.openstreetmap.josm.gui.preferences.MapPaintPreference;  
    5856import org.openstreetmap.josm.gui.preferences.ProjectionPreference;
    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;
    6461import org.openstreetmap.josm.tools.OsmUrlToBounds;
    abstract public class Main {  
    377374
    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        }
    389433        if (args.containsKey("selection")) {
    abstract public class Main {  
    416460        return true;
    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         }
    437463
    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;
     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);
    472501        }
     502    }
    473503
     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) {
    476512            Bounds b = new Bounds(
    477513                    new LatLon(Double.parseDouble(st.nextToken()),Double.parseDouble(st.nextToken())),
    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             );
     516            downloadFromParamBounds(rawGps, b);
    509517        }
    510518    }
    511519
     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
     535
    512536    public static void determinePlatformHook() {
    513537        String os = System.getProperty("os.name");
    514538        if (os == null) {
  • src/org/openstreetmap/josm/actions/OpenFileAction.java

    diff --git a/src/org/openstreetmap/josm/actions/OpenFileAction.java b/src/org/openstreetmap/josm/actions/OpenFileAction.java
    index a4f588f..33c9ab4 100644
    a b public class OpenFileAction extends DiskAccessAction {  
    5959        setEnabled(! Main.applet);
    6060    }
    6161
     62
     63    @Deprecated
    6264    static public void openFile(File f) throws IOException, IllegalDataException {
    6365        for (FileImporter importer : ExtensionFileFilter.importers)
    64             if (importer.acceptFile(f)) {
     66            if (!importer.isBatchImporter() && importer.acceptFile(f)) {
    6567                importer.importData(f);
    6668            }
    6769    }
    6870
     71    /**
     72     * Open a list of files. The complete list will be passed to batch importers.
     73     * @param fileList A list of files
     74     */
     75    static public void openFiles(List<File> fileList) {
     76        OpenFileTask task = new OpenFileTask(fileList, null);
     77        Main.worker.submit(task);
     78    }
     79
    6980    static public class OpenFileTask extends PleaseWaitRunnable {
    7081        private List<File> files;
    7182        private FileFilter fileFilter;
  • src/org/openstreetmap/josm/gui/MainApplication.java

    diff --git a/src/org/openstreetmap/josm/gui/MainApplication.java b/src/org/openstreetmap/josm/gui/MainApplication.java
    index e232296..4c303d9 100644
    a b public class MainApplication extends Main {  
    7777                "\t[--download=]<url>                        "+tr("Download the location at the url (with lat=x&lon=y&zoom=z)")+"\n"+
    7878                "\t[--download=]<filename>                   "+tr("Open a file (any file type that can be opened with File/Open)")+"\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"+
    8283                "\t--reset-preferences                       "+tr("Reset the preferences to default")+"\n\n"+