[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[18] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[916] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[9732] | 6 | import java.io.IOException;
|
---|
[103] | 7 | import java.io.InputStream;
|
---|
[12992] | 8 | import java.net.Authenticator.RequestorType;
|
---|
[18] | 9 | import java.net.HttpURLConnection;
|
---|
[1670] | 10 | import java.net.MalformedURLException;
|
---|
[18] | 11 | import java.net.URL;
|
---|
[6803] | 12 | import java.util.List;
|
---|
[18] | 13 |
|
---|
[12510] | 14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 15 |
|
---|
[34] | 16 | import org.openstreetmap.josm.Main;
|
---|
[4521] | 17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
[7531] | 18 | import org.openstreetmap.josm.data.notes.Note;
|
---|
[1146] | 19 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1811] | 20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[11194] | 21 | import org.openstreetmap.josm.io.auth.CredentialsAgentException;
|
---|
| 22 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
[9172] | 23 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
[12620] | 24 | import org.openstreetmap.josm.tools.Logging;
|
---|
[12510] | 25 | import org.openstreetmap.josm.tools.Utils;
|
---|
| 26 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
| 27 | import org.w3c.dom.Document;
|
---|
| 28 | import org.w3c.dom.Node;
|
---|
| 29 | import org.xml.sax.SAXException;
|
---|
[18] | 30 |
|
---|
| 31 | /**
|
---|
[228] | 32 | * This DataReader reads directly from the REST API of the osm server.
|
---|
[1169] | 33 | *
|
---|
[228] | 34 | * It supports plain text transfer as well as gzip or deflate encoded transfers;
|
---|
| 35 | * if compressed transfers are unwanted, set property osm-server.use-compression
|
---|
| 36 | * to false.
|
---|
[153] | 37 | *
|
---|
[18] | 38 | * @author imi
|
---|
| 39 | */
|
---|
[1146] | 40 | public abstract class OsmServerReader extends OsmConnection {
|
---|
[9078] | 41 | private final OsmApi api = OsmApi.getOsmApi();
|
---|
[11194] | 42 | private boolean doAuthenticate;
|
---|
[5494] | 43 | protected boolean gpxParsedProperly;
|
---|
[1664] | 44 |
|
---|
[1169] | 45 | /**
|
---|
[11194] | 46 | * Constructs a new {@code OsmServerReader}.
|
---|
| 47 | */
|
---|
| 48 | public OsmServerReader() {
|
---|
| 49 | try {
|
---|
| 50 | doAuthenticate = OsmApi.isUsingOAuth() && CredentialsManager.getInstance().lookupOAuthAccessToken() != null;
|
---|
| 51 | } catch (CredentialsAgentException e) {
|
---|
[12620] | 52 | Logging.warn(e);
|
---|
[11194] | 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | /**
|
---|
[1169] | 57 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 58 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
[5863] | 59 | * Relative URL's are directed to API base URL.
|
---|
| 60 | * @param urlStr The url to connect to.
|
---|
| 61 | * @param progressMonitor progress monitoring and abort handler
|
---|
[6695] | 62 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 63 | * @throws OsmTransferException if data transfer errors occur
|
---|
[1169] | 64 | */
|
---|
[10378] | 65 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[6695] | 66 | return getInputStream(urlStr, progressMonitor, null);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 71 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 72 | * Relative URL's are directed to API base URL.
|
---|
| 73 | * @param urlStr The url to connect to.
|
---|
| 74 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 75 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 76 | * @return A reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 77 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6695] | 78 | */
|
---|
[10378] | 79 | protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
[1875] | 80 | try {
|
---|
[2035] | 81 | api.initialize(progressMonitor);
|
---|
[6695] | 82 | String url = urlStr.startsWith("http") ? urlStr : (getBaseUrl() + urlStr);
|
---|
| 83 | return getInputStreamRaw(url, progressMonitor, reason);
|
---|
[1875] | 84 | } finally {
|
---|
| 85 | progressMonitor.invalidate();
|
---|
| 86 | }
|
---|
[1670] | 87 | }
|
---|
[1664] | 88 |
|
---|
[5863] | 89 | /**
|
---|
[6695] | 90 | * Return the base URL for relative URL requests
|
---|
[5863] | 91 | * @return base url of API
|
---|
| 92 | */
|
---|
[5097] | 93 | protected String getBaseUrl() {
|
---|
| 94 | return api.getBaseUrl();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[5863] | 97 | /**
|
---|
| 98 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 99 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 100 | * @param urlStr The exact url to connect to.
|
---|
| 101 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 102 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 103 | * @throws OsmTransferException if data transfer errors occur
|
---|
[5863] | 104 | */
|
---|
[1811] | 105 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[6695] | 106 | return getInputStreamRaw(urlStr, progressMonitor, null);
|
---|
| 107 | }
|
---|
[6787] | 108 |
|
---|
[6695] | 109 | /**
|
---|
| 110 | * Open a connection to the given url and return a reader on the input stream
|
---|
| 111 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 112 | * @param urlStr The exact url to connect to.
|
---|
| 113 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 114 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 115 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 116 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6695] | 117 | */
|
---|
| 118 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason) throws OsmTransferException {
|
---|
[6803] | 119 | return getInputStreamRaw(urlStr, progressMonitor, reason, false);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /**
|
---|
[12596] | 123 | * Open a connection to the given url (if HTTP, trough a GET request) and return a reader on the input stream
|
---|
[6803] | 124 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 125 | * @param urlStr The exact url to connect to.
|
---|
| 126 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 127 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 128 | * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
|
---|
| 129 | * for {@code filename} and uncompress a gzip/bzip2 stream.
|
---|
| 130 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
[8291] | 131 | * @throws OsmTransferException if data transfer errors occur
|
---|
[6803] | 132 | */
|
---|
[12596] | 133 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
|
---|
| 134 | boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
|
---|
| 135 | return getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition, "GET", null);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Open a connection to the given url (if HTTP, with the specified method) and return a reader on the input stream
|
---|
| 140 | * from that connection. In case of user cancel, return <code>null</code>.
|
---|
| 141 | * @param urlStr The exact url to connect to.
|
---|
| 142 | * @param progressMonitor progress monitoring and abort handler
|
---|
| 143 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 144 | * @param uncompressAccordingToContentDisposition Whether to inspect the HTTP header {@code Content-Disposition}
|
---|
| 145 | * for {@code filename} and uncompress a gzip/bzip2 stream.
|
---|
| 146 | * @param httpMethod HTTP method ("GET", "POST" or "PUT")
|
---|
| 147 | * @param requestBody HTTP request body (for "POST" and "PUT" methods only). Must be null for "GET" method.
|
---|
| 148 | * @return An reader reading the input stream (servers answer) or <code>null</code>.
|
---|
| 149 | * @throws OsmTransferException if data transfer errors occur
|
---|
| 150 | * @since 12596
|
---|
| 151 | */
|
---|
[7033] | 152 | @SuppressWarnings("resource")
|
---|
[8509] | 153 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
|
---|
[12596] | 154 | boolean uncompressAccordingToContentDisposition, String httpMethod, byte[] requestBody) throws OsmTransferException {
|
---|
[1608] | 155 | try {
|
---|
[7434] | 156 | OnlineResource.JOSM_WEBSITE.checkOfflineAccess(urlStr, Main.getJOSMWebsite());
|
---|
[9353] | 157 | OnlineResource.OSM_API.checkOfflineAccess(urlStr, OsmApi.getOsmApi().getServerUrl());
|
---|
[7434] | 158 |
|
---|
[1875] | 159 | URL url = null;
|
---|
| 160 | try {
|
---|
[3021] | 161 | url = new URL(urlStr.replace(" ", "%20"));
|
---|
[8510] | 162 | } catch (MalformedURLException e) {
|
---|
[1875] | 163 | throw new OsmTransferException(e);
|
---|
| 164 | }
|
---|
[228] | 165 |
|
---|
[9732] | 166 | if ("file".equals(url.getProtocol())) {
|
---|
| 167 | try {
|
---|
| 168 | return url.openStream();
|
---|
| 169 | } catch (IOException e) {
|
---|
| 170 | throw new OsmTransferException(e);
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[12596] | 174 | final HttpClient client = HttpClient.create(url, httpMethod)
|
---|
| 175 | .setFinishOnCloseOutput(false)
|
---|
| 176 | .setReasonForRequest(reason)
|
---|
[12711] | 177 | .setOutputMessage(tr("Downloading data..."))
|
---|
[12596] | 178 | .setRequestBody(requestBody);
|
---|
[9309] | 179 | activeConnection = client;
|
---|
[9308] | 180 | adaptRequest(client);
|
---|
[2641] | 181 | if (doAuthenticate) {
|
---|
[9172] | 182 | addAuth(client);
|
---|
[2124] | 183 | }
|
---|
[2641] | 184 | if (cancel)
|
---|
[8415] | 185 | throw new OsmTransferCanceledException("Operation canceled");
|
---|
[1169] | 186 |
|
---|
[9309] | 187 | final HttpClient.Response response;
|
---|
[1875] | 188 | try {
|
---|
[9309] | 189 | response = client.connect(progressMonitor);
|
---|
[10212] | 190 | } catch (IOException e) {
|
---|
[12620] | 191 | Logging.error(e);
|
---|
[8509] | 192 | OsmTransferException ote = new OsmTransferException(
|
---|
| 193 | tr("Could not connect to the OSM server. Please check your internet connection."), e);
|
---|
[5575] | 194 | ote.setUrl(url.toString());
|
---|
| 195 | throw ote;
|
---|
[1875] | 196 | }
|
---|
| 197 | try {
|
---|
[12992] | 198 | if (response.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
|
---|
| 199 | CredentialsManager.getInstance().purgeCredentialsCache(RequestorType.SERVER);
|
---|
[8510] | 200 | throw new OsmApiException(HttpURLConnection.HTTP_UNAUTHORIZED, null, null);
|
---|
[12992] | 201 | }
|
---|
[1353] | 202 |
|
---|
[9309] | 203 | if (response.getResponseCode() == HttpURLConnection.HTTP_PROXY_AUTH)
|
---|
[8415] | 204 | throw new OsmTransferCanceledException("Proxy Authentication Required");
|
---|
[2641] | 205 |
|
---|
[9309] | 206 | if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
|
---|
| 207 | String errorHeader = response.getHeaderField("Error");
|
---|
[10420] | 208 | String errorBody = fetchResponseText(response);
|
---|
[9309] | 209 | throw new OsmApiException(response.getResponseCode(), errorHeader, errorBody, url.toString());
|
---|
[1670] | 210 | }
|
---|
[1169] | 211 |
|
---|
[9309] | 212 | response.uncompressAccordingToContentDisposition(uncompressAccordingToContentDisposition);
|
---|
| 213 | return response.getContent();
|
---|
[6246] | 214 | } catch (OsmTransferException e) {
|
---|
| 215 | throw e;
|
---|
[10212] | 216 | } catch (IOException e) {
|
---|
[6246] | 217 | throw new OsmTransferException(e);
|
---|
[1670] | 218 | }
|
---|
[1875] | 219 | } finally {
|
---|
| 220 | progressMonitor.invalidate();
|
---|
[1169] | 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
[10420] | 224 | private static String fetchResponseText(final HttpClient.Response response) {
|
---|
| 225 | try {
|
---|
| 226 | return response.fetchContent();
|
---|
| 227 | } catch (IOException e) {
|
---|
[12620] | 228 | Logging.error(e);
|
---|
[10420] | 229 | return tr("Reading error text failed.");
|
---|
| 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[6244] | 233 | /**
|
---|
[9308] | 234 | * Allows subclasses to modify the request.
|
---|
| 235 | * @param request the prepared request
|
---|
| 236 | * @since 9308
|
---|
| 237 | */
|
---|
| 238 | protected void adaptRequest(HttpClient request) {
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
[6244] | 242 | * Download OSM files from somewhere
|
---|
| 243 | * @param progressMonitor The progress monitor
|
---|
| 244 | * @return The corresponding dataset
|
---|
[6643] | 245 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 246 | */
|
---|
[11023] | 247 | public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
|
---|
[1169] | 248 |
|
---|
[6244] | 249 | /**
|
---|
| 250 | * Download OSM Change files from somewhere
|
---|
| 251 | * @param progressMonitor The progress monitor
|
---|
| 252 | * @return The corresponding dataset
|
---|
[6643] | 253 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 254 | */
|
---|
[5317] | 255 | public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[4530] | 256 | return null;
|
---|
| 257 | }
|
---|
[5361] | 258 |
|
---|
[6244] | 259 | /**
|
---|
| 260 | * Download BZip2-compressed OSM Change files from somewhere
|
---|
| 261 | * @param progressMonitor The progress monitor
|
---|
| 262 | * @return The corresponding dataset
|
---|
[6643] | 263 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 264 | */
|
---|
[5361] | 265 | public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 266 | return null;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[6244] | 269 | /**
|
---|
| 270 | * Download GZip-compressed OSM Change files from somewhere
|
---|
| 271 | * @param progressMonitor The progress monitor
|
---|
| 272 | * @return The corresponding dataset
|
---|
[6643] | 273 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 274 | */
|
---|
[5361] | 275 | public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 276 | return null;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[6244] | 279 | /**
|
---|
| 280 | * Retrieve raw gps waypoints from the server API.
|
---|
| 281 | * @param progressMonitor The progress monitor
|
---|
| 282 | * @return The corresponding GPX tracks
|
---|
| 283 | * @throws OsmTransferException if any error occurs
|
---|
| 284 | */
|
---|
[5317] | 285 | public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[4521] | 286 | return null;
|
---|
| 287 | }
|
---|
[5317] | 288 |
|
---|
[6244] | 289 | /**
|
---|
| 290 | * Retrieve BZip2-compressed GPX files from somewhere.
|
---|
| 291 | * @param progressMonitor The progress monitor
|
---|
| 292 | * @return The corresponding GPX tracks
|
---|
| 293 | * @throws OsmTransferException if any error occurs
|
---|
| 294 | * @since 6244
|
---|
| 295 | */
|
---|
| 296 | public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 297 | return null;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | /**
|
---|
| 301 | * Download BZip2-compressed OSM files from somewhere
|
---|
| 302 | * @param progressMonitor The progress monitor
|
---|
| 303 | * @return The corresponding dataset
|
---|
[6643] | 304 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 305 | */
|
---|
[5317] | 306 | public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 307 | return null;
|
---|
| 308 | }
|
---|
[5361] | 309 |
|
---|
[6244] | 310 | /**
|
---|
| 311 | * Download GZip-compressed OSM files from somewhere
|
---|
| 312 | * @param progressMonitor The progress monitor
|
---|
| 313 | * @return The corresponding dataset
|
---|
[6643] | 314 | * @throws OsmTransferException if any error occurs
|
---|
[6244] | 315 | */
|
---|
[5361] | 316 | public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 317 | return null;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[2124] | 320 | /**
|
---|
[6882] | 321 | * Download Zip-compressed OSM files from somewhere
|
---|
| 322 | * @param progressMonitor The progress monitor
|
---|
| 323 | * @return The corresponding dataset
|
---|
| 324 | * @throws OsmTransferException if any error occurs
|
---|
| 325 | * @since 6882
|
---|
| 326 | */
|
---|
| 327 | public DataSet parseOsmZip(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 328 | return null;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | /**
|
---|
[2124] | 332 | * Returns true if this reader is adding authentication credentials to the read
|
---|
| 333 | * request sent to the server.
|
---|
[2512] | 334 | *
|
---|
[2124] | 335 | * @return true if this reader is adding authentication credentials to the read
|
---|
| 336 | * request sent to the server
|
---|
| 337 | */
|
---|
| 338 | public boolean isDoAuthenticate() {
|
---|
| 339 | return doAuthenticate;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | /**
|
---|
| 343 | * Sets whether this reader adds authentication credentials to the read
|
---|
| 344 | * request sent to the server.
|
---|
[2512] | 345 | *
|
---|
[2124] | 346 | * @param doAuthenticate true if this reader adds authentication credentials to the read
|
---|
| 347 | * request sent to the server
|
---|
| 348 | */
|
---|
| 349 | public void setDoAuthenticate(boolean doAuthenticate) {
|
---|
| 350 | this.doAuthenticate = doAuthenticate;
|
---|
| 351 | }
|
---|
[6070] | 352 |
|
---|
[5494] | 353 | /**
|
---|
| 354 | * Determines if the GPX data has been parsed properly.
|
---|
| 355 | * @return true if the GPX data has been parsed properly, false otherwise
|
---|
| 356 | * @see GpxReader#parse
|
---|
| 357 | */
|
---|
| 358 | public final boolean isGpxParsedProperly() {
|
---|
| 359 | return gpxParsedProperly;
|
---|
| 360 | }
|
---|
[7531] | 361 |
|
---|
| 362 | /**
|
---|
| 363 | * Downloads notes from the API, given API limit parameters
|
---|
| 364 | *
|
---|
[8218] | 365 | * @param noteLimit How many notes to download.
|
---|
[7531] | 366 | * @param daysClosed Return notes closed this many days in the past. -1 means all notes, ever. 0 means only unresolved notes.
|
---|
| 367 | * @param progressMonitor Progress monitor for user feedback
|
---|
| 368 | * @return List of notes returned by the API
|
---|
| 369 | * @throws OsmTransferException if any errors happen
|
---|
| 370 | */
|
---|
[8218] | 371 | public List<Note> parseNotes(int noteLimit, int daysClosed, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[7531] | 372 | return null;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | /**
|
---|
| 376 | * Downloads notes from a given raw URL. The URL is assumed to be complete and no API limits are added
|
---|
| 377 | *
|
---|
[8470] | 378 | * @param progressMonitor progress monitor
|
---|
[7531] | 379 | * @return A list of notes parsed from the URL
|
---|
[8470] | 380 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[7531] | 381 | */
|
---|
| 382 | public List<Note> parseRawNotes(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 383 | return null;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | /**
|
---|
| 387 | * Download notes from a URL that contains a bzip2 compressed notes dump file
|
---|
[8470] | 388 | * @param progressMonitor progress monitor
|
---|
[7531] | 389 | * @return A list of notes parsed from the URL
|
---|
[8470] | 390 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[7531] | 391 | */
|
---|
| 392 | public List<Note> parseRawNotesBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 393 | return null;
|
---|
| 394 | }
|
---|
[12510] | 395 |
|
---|
| 396 | /**
|
---|
| 397 | * Returns an attribute from the given DOM node.
|
---|
| 398 | * @param node DOM node
|
---|
| 399 | * @param name attribute name
|
---|
| 400 | * @return attribute value for the given attribute
|
---|
| 401 | * @since 12510
|
---|
| 402 | */
|
---|
| 403 | protected static String getAttribute(Node node, String name) {
|
---|
| 404 | return node.getAttributes().getNamedItem(name).getNodeValue();
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /**
|
---|
| 408 | * DOM document parser.
|
---|
| 409 | * @param <R> resulting type
|
---|
| 410 | * @since 12510
|
---|
| 411 | */
|
---|
| 412 | @FunctionalInterface
|
---|
| 413 | protected interface DomParser<R> {
|
---|
| 414 | /**
|
---|
| 415 | * Parses a given DOM document.
|
---|
| 416 | * @param doc DOM document
|
---|
| 417 | * @return parsed data
|
---|
| 418 | * @throws XmlParsingException if an XML parsing error occurs
|
---|
| 419 | */
|
---|
| 420 | R parse(Document doc) throws XmlParsingException;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /**
|
---|
| 424 | * Fetches generic data from the DOM document resulting an API call.
|
---|
| 425 | * @param api the OSM API call
|
---|
| 426 | * @param subtask the subtask translated message
|
---|
| 427 | * @param parser the parser converting the DOM document (OSM API result)
|
---|
| 428 | * @param <T> data type
|
---|
| 429 | * @param monitor The progress monitor
|
---|
| 430 | * @param reason The reason to show on console. Can be {@code null} if no reason is given
|
---|
| 431 | * @return The converted data
|
---|
| 432 | * @throws OsmTransferException if something goes wrong
|
---|
| 433 | * @since 12510
|
---|
| 434 | */
|
---|
| 435 | public <T> T fetchData(String api, String subtask, DomParser<T> parser, ProgressMonitor monitor, String reason)
|
---|
| 436 | throws OsmTransferException {
|
---|
| 437 | try {
|
---|
| 438 | monitor.beginTask("");
|
---|
| 439 | monitor.indeterminateSubTask(subtask);
|
---|
| 440 | try (InputStream in = getInputStream(api, monitor.createSubTaskMonitor(1, true), reason)) {
|
---|
| 441 | return parser.parse(Utils.parseSafeDOM(in));
|
---|
| 442 | }
|
---|
| 443 | } catch (OsmTransferException e) {
|
---|
| 444 | throw e;
|
---|
| 445 | } catch (IOException | ParserConfigurationException | SAXException e) {
|
---|
| 446 | throw new OsmTransferException(e);
|
---|
| 447 | } finally {
|
---|
| 448 | monitor.finishTask();
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
[18] | 451 | }
|
---|