1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
12 | import org.openstreetmap.josm.data.notes.Note;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 | import org.xml.sax.SAXException;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Read content from OSM server for a given URL
|
---|
20 | * @since 1146
|
---|
21 | */
|
---|
22 | public 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
|
---|
106 |
|
---|
107 | protected final String url;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Constructs a new {@code OsmServerLocationReader}.
|
---|
111 | * @param url The URL to fetch
|
---|
112 | */
|
---|
113 | public OsmServerLocationReader(String url) {
|
---|
114 | this.url = url;
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected abstract static class Parser<T> {
|
---|
118 | protected final ProgressMonitor progressMonitor;
|
---|
119 | protected final Compression compression;
|
---|
120 | protected InputStream in;
|
---|
121 |
|
---|
122 | public Parser(ProgressMonitor progressMonitor, Compression compression) {
|
---|
123 | this.progressMonitor = progressMonitor;
|
---|
124 | this.compression = compression;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public abstract T parse() throws OsmTransferException, IllegalDataException, IOException, SAXException;
|
---|
128 | }
|
---|
129 |
|
---|
130 | protected final <T> T doParse(Parser<T> parser, final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
131 | progressMonitor.beginTask(tr("Contacting Server...", 10));
|
---|
132 | try {
|
---|
133 | return parser.parse();
|
---|
134 | } catch (OsmTransferException e) {
|
---|
135 | throw e;
|
---|
136 | } catch (IOException | SAXException | IllegalDataException e) {
|
---|
137 | if (cancel)
|
---|
138 | return null;
|
---|
139 | throw new OsmTransferException(e);
|
---|
140 | } finally {
|
---|
141 | progressMonitor.finishTask();
|
---|
142 | activeConnection = null;
|
---|
143 | Utils.close(parser.in);
|
---|
144 | parser.in = null;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
150 | return doParse(new OsmParser(progressMonitor, Compression.NONE), progressMonitor);
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
155 | return doParse(new OsmParser(progressMonitor, Compression.BZIP2), progressMonitor);
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
160 | return doParse(new OsmParser(progressMonitor, Compression.GZIP), progressMonitor);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public DataSet parseOsmZip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
165 | return doParse(new OsmParser(progressMonitor, Compression.ZIP), progressMonitor);
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
170 | return doParse(new OsmChangeParser(progressMonitor, Compression.NONE), progressMonitor);
|
---|
171 | }
|
---|
172 |
|
---|
173 | @Override
|
---|
174 | public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
175 | return doParse(new OsmChangeParser(progressMonitor, Compression.BZIP2), progressMonitor);
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Override
|
---|
179 | public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
180 | return doParse(new OsmChangeParser(progressMonitor, Compression.GZIP), progressMonitor);
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
185 | return doParse(new GpxParser(progressMonitor, Compression.NONE), progressMonitor);
|
---|
186 | }
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
190 | return doParse(new GpxParser(progressMonitor, Compression.BZIP2), progressMonitor);
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
195 | return doParse(new NoteParser(progressMonitor, Compression.NONE), progressMonitor);
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public List<Note> parseRawNotesBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
200 | return doParse(new NoteParser(progressMonitor, Compression.BZIP2), progressMonitor);
|
---|
201 | }
|
---|
202 |
|
---|
203 | protected class OsmParser extends Parser<DataSet> {
|
---|
204 | protected OsmParser(ProgressMonitor progressMonitor, Compression compression) {
|
---|
205 | super(progressMonitor, compression);
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Override
|
---|
209 | public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
|
---|
210 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
|
---|
211 | if (in == null)
|
---|
212 | return null;
|
---|
213 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
214 | return OsmReader.parseDataSet(compression.getUncompressedInputStream(in), progressMonitor.createSubTaskMonitor(1, false));
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | protected class OsmChangeParser extends Parser<DataSet> {
|
---|
219 | protected OsmChangeParser(ProgressMonitor progressMonitor, Compression compression) {
|
---|
220 | super(progressMonitor, compression);
|
---|
221 | }
|
---|
222 |
|
---|
223 | @Override
|
---|
224 | public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
|
---|
225 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
|
---|
226 | if (in == null)
|
---|
227 | return null;
|
---|
228 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
229 | return OsmChangeReader.parseDataSet(compression.getUncompressedInputStream(in), progressMonitor.createSubTaskMonitor(1, false));
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | protected class GpxParser extends Parser<GpxData> {
|
---|
234 | protected GpxParser(ProgressMonitor progressMonitor, Compression compression) {
|
---|
235 | super(progressMonitor, compression);
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override
|
---|
239 | public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
|
---|
240 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true), null, true);
|
---|
241 | if (in == null)
|
---|
242 | return null;
|
---|
243 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
244 | GpxReader reader = new GpxReader(compression.getUncompressedInputStream(in));
|
---|
245 | gpxParsedProperly = reader.parse(false);
|
---|
246 | GpxData result = reader.getGpxData();
|
---|
247 | result.fromServer = isGpxFromServer(url);
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | protected class NoteParser extends Parser<List<Note>> {
|
---|
253 |
|
---|
254 | public NoteParser(ProgressMonitor progressMonitor, Compression compression) {
|
---|
255 | super(progressMonitor, compression);
|
---|
256 | }
|
---|
257 |
|
---|
258 | @Override
|
---|
259 | public List<Note> parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
|
---|
260 | in = getInputStream(url, progressMonitor.createSubTaskMonitor(1, true));
|
---|
261 | if (in == null) {
|
---|
262 | return Collections.emptyList();
|
---|
263 | }
|
---|
264 | progressMonitor.subTask(tr("Downloading OSM notes..."));
|
---|
265 | NoteReader reader = new NoteReader(compression.getUncompressedInputStream(in));
|
---|
266 | return reader.parse();
|
---|
267 | }
|
---|
268 | }
|
---|
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 | }
|
---|
280 | }
|
---|