Changeset 12679 in josm


Ignore:
Timestamp:
2017-08-27T23:42:54+02:00 (4 weeks ago)
Author:
Don-vip
Message:

see #15182 - make actions.downloadtasks.Download*Task depend on io.OsmServerLocationReader, not the opposite

Location:
trunk
Files:
5 edited

Legend:

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

    r12671 r12679  
    66import java.io.IOException;
    77import java.net.URL;
     8import java.util.Arrays;
    89import java.util.Optional;
    910import java.util.concurrent.Future;
     
    3132import org.openstreetmap.josm.io.BoundingBoxDownloader;
    3233import org.openstreetmap.josm.io.OsmServerLocationReader;
     34import org.openstreetmap.josm.io.OsmServerLocationReader.GpxUrlPattern;
    3335import org.openstreetmap.josm.io.OsmServerReader;
    3436import org.openstreetmap.josm.io.OsmTransferException;
     
    4446    private GpxLayer gpxLayer;
    4547
    46     private static final String PATTERN_TRACE_ID = "https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data";
    47     private static final String PATTERN_USER_TRACE_ID = "https?://.*(osm|openstreetmap).org/user/[^/]+/traces/(\\p{Digit}+)";
    48     private static final String PATTERN_EDIT_TRACE_ID = "https?://.*(osm|openstreetmap).org/edit/?\\?gpx=(\\p{Digit}+)(#.*)?";
    49 
    50     private static final String PATTERN_TRACKPOINTS_BBOX = "https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
    51 
    52     private static final String PATTERN_EXTERNAL_GPX_SCRIPT = "https?://.*exportgpx.*";
    53     private static final String PATTERN_EXTERNAL_GPX_FILE = "https?://.*/(.*\\.gpx)";
    54 
    5548    protected String newLayerName;
    5649
    5750    @Override
    5851    public String[] getPatterns() {
    59         return new String[] {
    60                 PATTERN_EXTERNAL_GPX_FILE, PATTERN_EXTERNAL_GPX_SCRIPT,
    61                 PATTERN_TRACE_ID, PATTERN_USER_TRACE_ID, PATTERN_EDIT_TRACE_ID,
    62                 PATTERN_TRACKPOINTS_BBOX,
    63         };
     52        return Arrays.stream(GpxUrlPattern.values()).map(GpxUrlPattern::pattern).toArray(String[]::new);
    6453    }
    6554
     
    8170    public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
    8271        CheckParameterUtil.ensureParameterNotNull(url, "url");
    83         final Optional<String> mappedUrl = Stream.of(PATTERN_USER_TRACE_ID, PATTERN_EDIT_TRACE_ID)
    84                 .map(p -> Pattern.compile(p).matcher(url))
     72        final Optional<String> mappedUrl = Stream.of(GpxUrlPattern.USER_TRACE_ID, GpxUrlPattern.EDIT_TRACE_ID)
     73                .map(p -> Pattern.compile(p.pattern()).matcher(url))
    8574                .filter(Matcher::matches)
    8675                .map(m -> "https://www.openstreetmap.org/trace/" + m.group(2) + "/data")
     
    8978            return loadUrl(newLayer, mappedUrl.get(), progressMonitor);
    9079        }
    91         if (url.matches(PATTERN_TRACE_ID)
    92          || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT)
    93          || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
     80        if (Stream.of(GpxUrlPattern.TRACE_ID, GpxUrlPattern.EXTERNAL_GPX_SCRIPT, GpxUrlPattern.EXTERNAL_GPX_FILE)
     81                .anyMatch(p -> url.matches(p.pattern()))) {
    9482            downloadTask = new DownloadTask(newLayer,
    9583                    new OsmServerLocationReader(url), progressMonitor);
    9684            // Extract .gpx filename from URL to set the new layer name
    97             Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
     85            Matcher matcher = Pattern.compile(GpxUrlPattern.EXTERNAL_GPX_FILE.pattern()).matcher(url);
    9886            newLayerName = matcher.matches() ? matcher.group(1) : null;
    9987            // We need submit instead of execute so we can wait for it to finish and get the error
     
    10189            return MainApplication.worker.submit(downloadTask);
    10290
    103         } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) {
     91        } else if (url.matches(GpxUrlPattern.TRACKPOINTS_BBOX.pattern())) {
    10492            String[] table = url.split("\\?|=|&");
    10593            for (int i = 0; i < table.length; i++) {
     
    224212        return true;
    225213    }
    226 
    227     /**
    228      * Determines if the given URL denotes an OSM gpx-related API call.
    229      * @param url The url to check
    230      * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
    231      * @see GpxData#fromServer
    232      * @since 5745
    233      */
    234     public static final boolean isFromServer(String url) {
    235         return url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_TRACKPOINTS_BBOX));
    236     }
    237214}
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadNotesTask.java

    r12636 r12679  
    77import java.io.IOException;
    88import java.net.URL;
     9import java.util.Arrays;
    910import java.util.List;
    1011import java.util.concurrent.Future;
     
    2829import org.openstreetmap.josm.io.OsmApi;
    2930import org.openstreetmap.josm.io.OsmServerLocationReader;
     31import org.openstreetmap.josm.io.OsmServerLocationReader.NoteUrlPattern;
    3032import org.openstreetmap.josm.io.OsmServerReader;
    3133import org.openstreetmap.josm.io.OsmTransferException;
     
    4143public class DownloadNotesTask extends AbstractDownloadTask<NoteData> {
    4244
    43     private static final String PATTERN_API_URL = "https?://.*/api/0.6/notes.*";
    44     private static final String PATTERN_DUMP_FILE = "https?://.*/(.*\\.osn(.bz2)?)";
    4545    /** Property defining the number of notes to be downloaded */
    4646    public static final IntegerProperty DOWNLOAD_LIMIT = new IntegerProperty("osm.notes.downloadLimit", 1000);
     
    9898    @Override
    9999    public String[] getPatterns() {
    100         return new String[] {PATTERN_API_URL, PATTERN_DUMP_FILE};
     100        return Arrays.stream(NoteUrlPattern.values()).map(NoteUrlPattern::pattern).toArray(String[]::new);
    101101    }
    102102
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r12636 r12679  
    77import java.net.URL;
    88import java.util.ArrayList;
     9import java.util.Arrays;
    910import java.util.Collection;
    1011import java.util.Collections;
     
    3536import org.openstreetmap.josm.io.BoundingBoxDownloader;
    3637import org.openstreetmap.josm.io.OsmServerLocationReader;
     38import org.openstreetmap.josm.io.OsmServerLocationReader.OsmUrlPattern;
    3739import org.openstreetmap.josm.io.OsmServerReader;
    3840import org.openstreetmap.josm.io.OsmTransferCanceledException;
     
    4850public class DownloadOsmTask extends AbstractDownloadTask<DataSet> {
    4951
    50     // CHECKSTYLE.OFF: SingleSpaceSeparator
    51     protected static final String PATTERN_OSM_API_URL           = "https?://.*/api/0.6/(map|nodes?|ways?|relations?|\\*).*";
    52     protected static final String PATTERN_OVERPASS_API_URL      = "https?://.*/interpreter\\?data=.*";
    53     protected static final String PATTERN_OVERPASS_API_XAPI_URL = "https?://.*/xapi(\\?.*\\[@meta\\]|_meta\\?).*";
    54     protected static final String PATTERN_EXTERNAL_OSM_FILE     = "https?://.*/.*\\.osm";
    55     // CHECKSTYLE.ON: SingleSpaceSeparator
    56 
    5752    protected Bounds currentBounds;
    5853    protected DownloadTask downloadTask;
     
    6661    public String[] getPatterns() {
    6762        if (this.getClass() == DownloadOsmTask.class) {
    68             return new String[]{PATTERN_OSM_API_URL, PATTERN_OVERPASS_API_URL,
    69                 PATTERN_OVERPASS_API_XAPI_URL, PATTERN_EXTERNAL_OSM_FILE};
     63            return Arrays.stream(OsmUrlPattern.values()).map(OsmUrlPattern::pattern).toArray(String[]::new);
    7064        } else {
    7165            return super.getPatterns();
     
    408402        if (url != null) {
    409403            String urlString = url.toExternalForm();
    410             if (urlString.matches(PATTERN_OSM_API_URL)) {
     404            if (urlString.matches(OsmUrlPattern.OSM_API_URL.pattern())) {
    411405                // TODO: proper i18n after stabilization
    412406                Collection<String> items = new ArrayList<>();
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r10315 r12679  
    99import java.util.List;
    1010
    11 import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    1211import org.openstreetmap.josm.data.gpx.GpxData;
    1312import org.openstreetmap.josm.data.notes.Note;
     
    2221 */
    2322public class OsmServerLocationReader extends OsmServerReader {
     23
     24    // CHECKSTYLE.OFF: MethodParamPad
     25    // CHECKSTYLE.OFF: SingleSpaceSeparator
     26
     27    /**
     28     * Patterns for OSM data download URLs.
     29     * @since 12679
     30     */
     31    public enum OsmUrlPattern {
     32        OSM_API_URL           ("https?://.*/api/0.6/(map|nodes?|ways?|relations?|\\*).*"),
     33        OVERPASS_API_URL      ("https?://.*/interpreter\\?data=.*"),
     34        OVERPASS_API_XAPI_URL ("https?://.*/xapi(\\?.*\\[@meta\\]|_meta\\?).*"),
     35        EXTERNAL_OSM_FILE     ("https?://.*/.*\\.osm");
     36
     37        private final String urlPattern;
     38
     39        OsmUrlPattern(String urlPattern) {
     40            this.urlPattern = urlPattern;
     41        }
     42
     43        /**
     44         * Returns the URL pattern.
     45         * @return the URL pattern
     46         */
     47        public String pattern() {
     48            return urlPattern;
     49        }
     50    }
     51
     52    /**
     53     * Patterns for GPX download URLs.
     54     * @since 12679
     55     */
     56    public enum GpxUrlPattern {
     57        TRACE_ID     ("https?://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data"),
     58        USER_TRACE_ID("https?://.*(osm|openstreetmap).org/user/[^/]+/traces/(\\p{Digit}+)"),
     59        EDIT_TRACE_ID("https?://.*(osm|openstreetmap).org/edit/?\\?gpx=(\\p{Digit}+)(#.*)?"),
     60
     61        TRACKPOINTS_BBOX("https?://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*"),
     62
     63        EXTERNAL_GPX_SCRIPT("https?://.*exportgpx.*"),
     64        EXTERNAL_GPX_FILE  ("https?://.*/(.*\\.gpx)");
     65
     66        private final String urlPattern;
     67
     68        GpxUrlPattern(String urlPattern) {
     69            this.urlPattern = urlPattern;
     70        }
     71
     72        /**
     73         * Returns the URL pattern.
     74         * @return the URL pattern
     75         */
     76        public String pattern() {
     77            return urlPattern;
     78        }
     79    }
     80
     81    /**
     82     * Patterns for Note download URLs.
     83     * @since 12679
     84     */
     85    public enum NoteUrlPattern {
     86        API_URL  ("https?://.*/api/0.6/notes.*"),
     87        DUMP_FILE("https?://.*/(.*\\.osn(.bz2)?)");
     88
     89        private final String urlPattern;
     90
     91        NoteUrlPattern(String urlPattern) {
     92            this.urlPattern = urlPattern;
     93        }
     94
     95        /**
     96         * Returns the URL pattern.
     97         * @return the URL pattern
     98         */
     99        public String pattern() {
     100            return urlPattern;
     101        }
     102    }
     103
     104    // CHECKSTYLE.ON: SingleSpaceSeparator
     105    // CHECKSTYLE.ON: MethodParamPad
    24106
    25107    protected final String url;
     
    163245            gpxParsedProperly = reader.parse(false);
    164246            GpxData result = reader.getGpxData();
    165             result.fromServer = DownloadGpsTask.isFromServer(url);
     247            result.fromServer = isGpxFromServer(url);
    166248            return result;
    167249        }
     
    185267        }
    186268    }
     269
     270    /**
     271     * Determines if the given URL denotes an OSM gpx-related API call.
     272     * @param url The url to check
     273     * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
     274     * @see GpxData#fromServer
     275     * @since 12679
     276     */
     277    public static final boolean isGpxFromServer(String url) {
     278        return url != null && (url.matches(GpxUrlPattern.TRACE_ID.pattern()) || url.matches(GpxUrlPattern.TRACKPOINTS_BBOX.pattern()));
     279    }
    187280}
  • trunk/test/unit/org/openstreetmap/josm/gui/util/WindowGeometryTest.java

    r12678 r12679  
    1717import org.junit.Test;
    1818import org.openstreetmap.josm.Main;
    19 import org.openstreetmap.josm.gui.util.WindowGeometry;
    2019import org.openstreetmap.josm.gui.util.WindowGeometry.WindowGeometryException;
    2120import org.openstreetmap.josm.testutils.JOSMTestRules;
Note: See TracChangeset for help on using the changeset viewer.