001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import org.openstreetmap.josm.data.gpx.GpxData;
005
006/**
007 * Collection of {@link UrlPattern}s.
008 * @since 15784
009 */
010public final class UrlPatterns {
011
012    private static final String HTTPS = "https?://";
013    private static final String COMPRESSED = "(gz|xz|bz2?|zip)";
014
015    private UrlPatterns() {
016        // Hide public constructor
017    }
018
019    // CHECKSTYLE.OFF: MethodParamPad
020    // CHECKSTYLE.OFF: SingleSpaceSeparator
021
022    /**
023     * Patterns for Geojson download URLs.
024     */
025    public enum GeoJsonUrlPattern implements UrlPattern {
026        /** URL of remote geojson files, optionally compressed */
027        COMPRESSED_FILE(".*/(.*\\.(json|geojson)(\\."+COMPRESSED+")?)"),
028        /** URL of generic service providing geojson as output format */
029        FORMAT_GEOJSON (".*format=geojson.*");
030
031        private final String urlPattern;
032
033        GeoJsonUrlPattern(String urlPattern) {
034            this.urlPattern = HTTPS + urlPattern;
035        }
036
037        @Override
038        public String pattern() {
039            return urlPattern;
040        }
041    }
042
043    /**
044     * Patterns for GPX download URLs.
045     */
046    public enum GpxUrlPattern implements UrlPattern {
047        /** URL of identified GPX trace on OpenStreetMap website */
048        TRACE_ID     (".*(osm|openstreetmap).org/trace/\\p{Digit}+/data"),
049        /** URL of identified GPX trace belonging to any user on OpenStreetMap website */
050        USER_TRACE_ID(".*(osm|openstreetmap).org/user/[^/]+/traces/(\\p{Digit}+)"),
051        /** URL of the edit link from the OpenStreetMap trace page */
052        EDIT_TRACE_ID(".*(osm|openstreetmap).org/edit/?\\?gpx=(\\p{Digit}+)(#.*)?"),
053
054        /** URL of OSM API trackpoints endpoint */
055        TRACKPOINTS_BBOX(".*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*"),
056        /** URL of HOT Tasking Manager (TM) */
057        TASKING_MANAGER(".*/api/v\\p{Digit}+/projects?/\\p{Digit}+/(tasks_as_gpx?.*|tasks/queries/gpx/\\?tasks=.*)"),
058
059        /** External GPX script */
060        EXTERNAL_GPX_SCRIPT(".*exportgpx.*"),
061        /** External GPX file */
062        EXTERNAL_GPX_FILE  (".*/(.*\\.gpx)");
063
064        private final String urlPattern;
065
066        GpxUrlPattern(String urlPattern) {
067            this.urlPattern = HTTPS + urlPattern;
068        }
069
070        @Override
071        public String pattern() {
072            return urlPattern;
073        }
074
075        /**
076         * Determines if the given URL denotes an OSM gpx-related API call.
077         * @param url The url to check
078         * @return true if the url matches "Trace ID" API call or "Trackpoints bbox" API call, false otherwise
079         * @see GpxData#fromServer
080         */
081        public static boolean isGpxFromServer(String url) {
082            return TRACE_ID.matches(url) || TRACKPOINTS_BBOX.matches(url);
083        }
084    }
085
086    /**
087     * Patterns for Note download URLs.
088     */
089    public enum NoteUrlPattern implements UrlPattern {
090        /** URL of OSM API Notes endpoint */
091        API_URL  (".*/api/0.6/notes.*"),
092        /** URL of OSM API Notes compressed dump file */
093        DUMP_FILE(".*/(.*\\.osn(\\."+COMPRESSED+")?)");
094
095        private final String urlPattern;
096
097        NoteUrlPattern(String urlPattern) {
098            this.urlPattern = HTTPS + urlPattern;
099        }
100
101        @Override
102        public String pattern() {
103            return urlPattern;
104        }
105    }
106
107    /**
108     * Patterns for OsmChange data download URLs.
109     */
110    public enum OsmChangeUrlPattern implements UrlPattern {
111        /** URL of OSM changeset on OpenStreetMap website */
112        OSM_WEBSITE             ("www\\.(osm|openstreetmap)\\.org/changeset/(\\p{Digit}+).*"),
113        /** URL of OSM API 0.6 changeset */
114        OSM_API                 (".*/api/0.6/changeset/\\p{Digit}+/download"),
115        /** URL of remote .osc file */
116        EXTERNAL_OSC_FILE       (".*/(.*\\.osc)"),
117        /** URL of remote compressed osc file */
118        EXTERNAL_COMPRESSED_FILE(".*/(.*\\.osc."+COMPRESSED+")");
119
120        private final String urlPattern;
121
122        OsmChangeUrlPattern(String urlPattern) {
123            this.urlPattern = HTTPS + urlPattern;
124        }
125
126        @Override
127        public String pattern() {
128            return urlPattern;
129        }
130    }
131
132    /**
133     * Patterns for OSM data download URLs.
134     */
135    public enum OsmUrlPattern implements UrlPattern {
136        /** URL of OSM API */
137        OSM_API_URL             (".*/api/0.6/(map|nodes?|ways?|relations?|\\*).*"),
138        /** URL of Overpass API */
139        OVERPASS_API_URL        (".*/interpreter\\?data=.*"),
140        /** URL of Overpass API (XAPI compatibility) */
141        OVERPASS_API_XAPI_URL   (".*/xapi(?:\\?.*\\[@meta\\]|_meta\\?)(.*)"),
142        /** URL of remote .osm file */
143        EXTERNAL_OSM_FILE       (".*/(.*\\.osm)"),
144        /** URL of remote compressed osm file */
145        EXTERNAL_COMPRESSED_FILE(".*/(.*\\.osm\\."+COMPRESSED+")");
146
147        private final String urlPattern;
148
149        OsmUrlPattern(String urlPattern) {
150            this.urlPattern = HTTPS + urlPattern;
151        }
152
153        @Override
154        public String pattern() {
155            return urlPattern;
156        }
157    }
158
159    // CHECKSTYLE.ON: SingleSpaceSeparator
160    // CHECKSTYLE.ON: MethodParamPad
161}