Changeset 5361 in josm for trunk


Ignore:
Timestamp:
2012-07-25T02:31:52+02:00 (12 years ago)
Author:
Don-vip
Message:

fix #7879 - Allow to open local and remote gzipped/bzipped osmChange files + remote osm.gz files + make some public constants of File filters to share between same importers/exporters

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
18 edited
1 moved

Legend:

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

    r5317 r5361  
    2323import org.openstreetmap.josm.Main;
    2424import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    25 import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmBzip2Task;
     25import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeCompressedTask;
     26import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmCompressedTask;
    2627import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmChangeTask;
    2728import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
     
    5657        addDownloadTaskClass(DownloadOsmChangeTask.class);
    5758        addDownloadTaskClass(DownloadOsmUrlTask.class);
    58         addDownloadTaskClass(DownloadOsmBzip2Task.class);
     59        addDownloadTaskClass(DownloadOsmCompressedTask.class);
     60        addDownloadTaskClass(DownloadOsmChangeCompressedTask.class);
    5961    }
    6062
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmCompressedTask.java

    r5357 r5361  
    88import org.openstreetmap.josm.data.osm.DataSet;
    99import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    10 import org.openstreetmap.josm.io.OsmBzip2Importer;
    1110import org.openstreetmap.josm.io.OsmServerLocationReader;
    1211import org.openstreetmap.josm.io.OsmTransferException;
    1312
    14 public class DownloadOsmBzip2Task extends DownloadOsmTask {
    15    
    16     OsmBzip2Importer importer;
     13public class DownloadOsmCompressedTask extends DownloadOsmTask {
    1714   
    1815    /* (non-Javadoc)
     
    2118    @Override
    2219    public boolean acceptsUrl(String url) {
    23         return url != null && url.matches("http://.*/.*\\.osm.bz2?"); // Remote .osm.bz / .osm.bz2 files
     20        return url != null && url.matches("http://.*/.*\\.osm.(gz|bz2?)"); // Remote .osm.gz / .osm.bz / .osm.bz2 files
    2421    }
    2522   
     
    3835     * @param The URL as String
    3936     */
    40     public Future<?> loadUrl(boolean new_layer, String url, ProgressMonitor progressMonitor) {
     37    public Future<?> loadUrl(boolean new_layer, final String url, ProgressMonitor progressMonitor) {
    4138        downloadTask = new DownloadTask(new_layer, new OsmServerLocationReader(url), progressMonitor) {
    4239            @Override
    4340            protected DataSet parseDataSet() throws OsmTransferException {
    44                 return reader.parseOsmBzip2(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
     41                ProgressMonitor subTaskMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
     42                if (url.matches("http://.*/.*\\.osm.bz2?")) {
     43                    return reader.parseOsmBzip2(subTaskMonitor);
     44                } else {
     45                    return reader.parseOsmGzip(subTaskMonitor);
     46                }
    4547            }
    4648        };
    4749        currentBounds = null;
    48         // Extract .osm.bz/bz2 filename from URL to set the new layer name
    49         extractOsmFilename("http://.*/(.*\\.osm.bz2?)", url);
     50        // Extract .osm.gz/bz/bz2 filename from URL to set the new layer name
     51        extractOsmFilename("http://.*/(.*\\.osm.(gz|bz2?))", url);
    5052        return Main.worker.submit(downloadTask);
    5153    }
  • trunk/src/org/openstreetmap/josm/io/FileImporter.java

    r3679 r5361  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.io.BufferedInputStream;
    67import java.io.File;
    78import java.io.IOException;
     9import java.io.InputStream;
    810import java.util.List;
     11import java.util.zip.GZIPInputStream;
    912
    1013import javax.swing.JOptionPane;
    1114
     15import org.apache.tools.bzip2.CBZip2InputStream;
    1216import org.openstreetmap.josm.Main;
    1317import org.openstreetmap.josm.actions.ExtensionFileFilter;
     
    97101        return (new Double(this.getPriority())).compareTo(other.getPriority());
    98102    }
     103   
     104    public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
     105        if (in == null) {
     106            return null;
     107        }
     108        BufferedInputStream bis = new BufferedInputStream(in);
     109        int b = bis.read();
     110        if (b != 'B')
     111            throw new IOException(tr("Invalid bz2 file."));
     112        b = bis.read();
     113        if (b != 'Z')
     114            throw new IOException(tr("Invalid bz2 file."));
     115        return new CBZip2InputStream(bis);
     116    }
    99117
     118    public static GZIPInputStream getGZipInputStream(InputStream in) throws IOException {
     119        if (in == null) {
     120            return null;
     121        }
     122        return new GZIPInputStream(in);
     123    }
    100124}
  • trunk/src/org/openstreetmap/josm/io/GeoJSONExporter.java

    r4898 r5361  
    1212public class GeoJSONExporter extends FileExporter {
    1313
     14    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     15            "json,geojson", "json", tr("GeoJSON Files") + " (*.json *.geojson)");
     16   
    1417    public GeoJSONExporter() {
    15         super(new ExtensionFileFilter("json,geojson", "json", tr("GeoJSON Files") + " (*.json *.geojson)"));
     18        super(FILE_FILTER);
    1619    }
    1720
  • trunk/src/org/openstreetmap/josm/io/GpxExporter.java

    r4483 r5361  
    2727
    2828import org.openstreetmap.josm.Main;
    29 import org.openstreetmap.josm.actions.ExtensionFileFilter;
    3029import org.openstreetmap.josm.data.gpx.GpxData;
    3130import org.openstreetmap.josm.data.osm.DataSet;
     
    4342
    4443    public GpxExporter() {
    45         super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
     44        super(GpxImporter.FILE_FILTER);
    4645    }
    4746
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r4874 r5361  
    2222public class GpxImporter extends FileImporter {
    2323
     24    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     25            "gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)");
     26   
    2427    protected static class GpxImporterData {
    2528        public GpxLayer gpxLayer;
     
    2932
    3033    public GpxImporter() {
    31         super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
     34        super(FILE_FILTER);
    3235    }
    3336
  • trunk/src/org/openstreetmap/josm/io/JpgImporter.java

    r4310 r5361  
    1919public class JpgImporter extends FileImporter {
    2020    private GpxLayer gpx;
     21   
     22    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     23            "jpg", "jpg", tr("Image Files") + " (*.jpg, "+ tr("folder")+")");
    2124
    2225    public JpgImporter() {
    23         super(new ExtensionFileFilter("jpg", "jpg", tr("Image Files") + " (*.jpg, "+ tr("folder")+")"));
     26        super(FILE_FILTER);
    2427    }
    2528
  • trunk/src/org/openstreetmap/josm/io/NMEAImporter.java

    r4244 r5361  
    2020public class NMEAImporter extends FileImporter {
    2121
     22    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     23            "nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files") + " (*.nmea *.nme *.nma *.log *.txt)");
     24   
    2225    public NMEAImporter() {
    23         super(
    24                 new ExtensionFileFilter("nmea,nme,nma,log,txt", "nmea", tr("NMEA-0183 Files")
    25                         + " (*.nmea *.nme *.nma *.log *.txt)"));
     26        super(FILE_FILTER);
    2627    }
    2728
  • trunk/src/org/openstreetmap/josm/io/OsmBzip2Exporter.java

    r3083 r5361  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.io.File;
     
    119
    1210import org.apache.tools.bzip2.CBZip2OutputStream;
    13 import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1411public class OsmBzip2Exporter extends OsmExporter {
    1512
    1613    public OsmBzip2Exporter() {
    17         super(new ExtensionFileFilter("osm.bz2, osm.bz", "osm.bz2", tr("OSM Server Files bzip2 compressed")
    18                 + " (*.osm.bz2 *.osm.bz)"));
     14        super(OsmBzip2Importer.FILE_FILTER);
    1915    }
    2016
  • trunk/src/org/openstreetmap/josm/io/OsmBzip2Importer.java

    r5317 r5361  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.BufferedInputStream;
    76import java.io.File;
    87import java.io.FileInputStream;
    98import java.io.IOException;
    10 import java.io.InputStream;
    119
    12 import org.apache.tools.bzip2.CBZip2InputStream;
    1310import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1411import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    1613public class OsmBzip2Importer extends OsmImporter {
    1714
     15    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     16            "osm.bz2,osm.bz", "osm.bz2", tr("OSM Server Files bzip2 compressed") + " (*.osm.bz2 *.osm.bz)");
     17   
    1818    public OsmBzip2Importer() {
    19         super(new ExtensionFileFilter("osm.bz2,osm.bz", "osm.bz2", tr("OSM Server Files bzip2 compressed")
    20                 + " (*.osm.bz2 *.osm.bz)"));
     19        super(FILE_FILTER);
    2120    }
    2221
     
    2524        importData(getBZip2InputStream(new FileInputStream(file)), file);
    2625    }
    27    
    28     public static CBZip2InputStream getBZip2InputStream(InputStream in) throws IOException {
    29         if (in == null) {
    30             return null;
    31         }
    32         BufferedInputStream bis = new BufferedInputStream(in);
    33         int b = bis.read();
    34         if (b != 'B')
    35             throw new IOException(tr("Invalid bz2 file."));
    36         b = bis.read();
    37         if (b != 'Z')
    38             throw new IOException(tr("Invalid bz2 file."));
    39         return new CBZip2InputStream(bis);
    40     }
    4126}
  • trunk/src/org/openstreetmap/josm/io/OsmChangeImporter.java

    r4533 r5361  
    2222public class OsmChangeImporter extends FileImporter {
    2323
     24    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     25            "osc,osc.bz2,osc.bz,osc.gz", "osc", tr("OsmChange File") + " (*.osc *.osc.bz2 *.osc.bz *.osc.gz)");
     26   
    2427    public OsmChangeImporter() {
    25         super(new ExtensionFileFilter("osc", "osc", tr("OsmChange File") + " (*.osc)"));
     28        super(FILE_FILTER);
    2629    }
    2730
     
    3336        try {
    3437            FileInputStream in = new FileInputStream(file);
    35             importData(in, file);
     38           
     39            if (file.getName().endsWith(".osc")) {
     40                importData(in, file);
     41            } else if (file.getName().endsWith(".gz")) {
     42                importData(getGZipInputStream(in), file);
     43            } else {
     44                importData(getBZip2InputStream(in), file);
     45            }
     46           
    3647        } catch (FileNotFoundException e) {
    3748            e.printStackTrace();
  • trunk/src/org/openstreetmap/josm/io/OsmExporter.java

    r5025 r5361  
    2525
    2626    public OsmExporter() {
    27         super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
     27        super(OsmImporter.FILE_FILTER);
    2828    }
    2929
  • trunk/src/org/openstreetmap/josm/io/OsmGzipExporter.java

    r3083 r5361  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.io.File;
     
    108import java.io.OutputStream;
    119import java.util.zip.GZIPOutputStream;
    12 
    13 import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1410public class OsmGzipExporter extends OsmExporter {
    1511
    1612    public OsmGzipExporter() {
    17         super(new ExtensionFileFilter("osm.gz", "osm.gz", tr("OSM Server Files gzip compressed") + " (*.osm.gz)"));
     13        super(OsmGzipImporter.FILE_FILTER);
    1814    }
    1915
  • trunk/src/org/openstreetmap/josm/io/OsmGzipImporter.java

    r3083 r5361  
    77import java.io.FileInputStream;
    88import java.io.IOException;
    9 import java.util.zip.GZIPInputStream;
    109
    1110import org.openstreetmap.josm.actions.ExtensionFileFilter;
     
    1413public class OsmGzipImporter extends OsmImporter {
    1514
     15    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     16            "osm.gz", "osm.gz", tr("OSM Server Files gzip compressed") + " (*.osm.gz)");
     17   
    1618    public OsmGzipImporter() {
    17         super(new ExtensionFileFilter("osm.gz", "osm.gz", tr("OSM Server Files gzip compressed") + " (*.osm.gz)"));
     19        super(FILE_FILTER);
    1820    }
    1921
    2022    @Override
    2123    public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
    22         GZIPInputStream in = new GZIPInputStream(new FileInputStream(file));
    23         importData(in, file);
     24        importData(getGZipInputStream(new FileInputStream(file)), file);
    2425    }
    2526}
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r5205 r5361  
    2222public class OsmImporter extends FileImporter {
    2323
     24    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     25            "osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)");
     26   
    2427    public static class OsmImporterData {
    2528
     
    4245
    4346    public OsmImporter() {
    44         super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
     47        super(FILE_FILTER);
    4548    }
    4649
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r5317 r5361  
    66import java.io.IOException;
    77import java.io.InputStream;
     8import java.util.zip.GZIPInputStream;
    89
    910import org.apache.tools.bzip2.CBZip2InputStream;
     
    7677                if (in == null)
    7778                    return null;
    78                 CBZip2InputStream bzin = OsmBzip2Importer.getBZip2InputStream(in);
     79                CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
    7980                progressMonitor.subTask(tr("Downloading OSM data..."));
    8081                return OsmReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
     82            }
     83        }, progressMonitor);
     84    }
     85   
     86    /**
     87     * Method to download GZip-compressed OSM files from somewhere
     88     */
     89    @Override
     90    public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
     91        return doParse(new Parser<DataSet>() {
     92            @Override
     93            public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
     94                in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
     95                if (in == null)
     96                    return null;
     97                GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
     98                progressMonitor.subTask(tr("Downloading OSM data..."));
     99                return OsmReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
    81100            }
    82101        }, progressMonitor);
     
    97116                progressMonitor.subTask(tr("Downloading OSM data..."));
    98117                return OsmChangeReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
     118            }
     119        }, progressMonitor);
     120    }
     121   
     122    /**
     123     * Method to download BZip2-compressed OSM Change files from somewhere
     124     */
     125    @Override
     126    public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
     127        return doParse(new Parser<DataSet>() {
     128            @Override
     129            public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
     130                in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
     131                if (in == null)
     132                    return null;
     133                CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
     134                progressMonitor.subTask(tr("Downloading OSM data..."));
     135                return OsmChangeReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
     136            }
     137        }, progressMonitor);
     138    }
     139   
     140    /**
     141     * Method to download GZip-compressed OSM Change files from somewhere
     142     */
     143    @Override
     144    public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
     145        return doParse(new Parser<DataSet>() {
     146            @Override
     147            public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
     148                in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
     149                if (in == null)
     150                    return null;
     151                GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
     152                progressMonitor.subTask(tr("Downloading OSM data..."));
     153                return OsmChangeReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
    99154            }
    100155        }, progressMonitor);
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r5324 r5361  
    151151        return null;
    152152    }
    153    
     153
     154    public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
     155        return null;
     156    }
     157
     158    public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
     159        return null;
     160    }
     161
    154162    public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
    155163        return null;
     
    159167        return null;
    160168    }
    161    
     169
     170    public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
     171        return null;
     172    }
     173
    162174    /**
    163175     * Returns true if this reader is adding authentication credentials to the read
  • trunk/src/org/openstreetmap/josm/io/WMSLayerExporter.java

    r3720 r5361  
    22package org.openstreetmap.josm.io;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import org.openstreetmap.josm.actions.ExtensionFileFilter;
    7 
    8 public class WMSLayerExporter extends FileExporter{
     4public class WMSLayerExporter extends FileExporter {
    95
    106    public WMSLayerExporter() {
    11         super(new ExtensionFileFilter("wms", "wms", tr("WMS Files (*.wms)")));
     7        super(WMSLayerImporter.FILE_FILTER);
    128    }
    139}
  • trunk/src/org/openstreetmap/josm/io/WMSLayerImporter.java

    r3720 r5361  
    66import org.openstreetmap.josm.actions.ExtensionFileFilter;
    77
    8 public class WMSLayerImporter extends FileImporter{
     8public class WMSLayerImporter extends FileImporter {
    99
     10    public static final ExtensionFileFilter FILE_FILTER = new ExtensionFileFilter(
     11            "wms", "wms", tr("WMS Files (*.wms)"));
     12   
    1013    public WMSLayerImporter() {
    11         super(new ExtensionFileFilter("wms", "wms", tr("WMS Files (*.wms)")));
     14        super(FILE_FILTER);
    1215    }
    13 
    1416}
Note: See TracChangeset for help on using the changeset viewer.