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

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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.