source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java@ 5745

Last change on this file since 5745 was 5745, checked in by Don-vip, 11 years ago

see #8453 - Do not consider remote .gpx files as downloaded from OSM API server

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1//License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.zip.GZIPInputStream;
9
10import org.apache.tools.bzip2.CBZip2InputStream;
11import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
12import org.openstreetmap.josm.data.gpx.GpxData;
13import org.openstreetmap.josm.data.osm.DataSet;
14import org.openstreetmap.josm.gui.progress.ProgressMonitor;
15import org.xml.sax.SAXException;
16
17public class OsmServerLocationReader extends OsmServerReader {
18
19 protected final String url;
20
21 public OsmServerLocationReader(String url) {
22 this.url = url;
23 }
24
25 protected abstract class Parser<T> {
26 public InputStream in = null;
27 public abstract T parse() throws OsmTransferException, IllegalDataException, IOException, SAXException;
28 }
29
30 protected final <T> T doParse(Parser<T> parser, final ProgressMonitor progressMonitor) throws OsmTransferException {
31 progressMonitor.beginTask(tr("Contacting Server...", 10));
32 try {
33 return parser.parse();
34 } catch(OsmTransferException e) {
35 throw e;
36 } catch (Exception e) {
37 if (cancel)
38 return null;
39 throw new OsmTransferException(e);
40 } finally {
41 progressMonitor.finishTask();
42 try {
43 activeConnection = null;
44 if (parser.in != null) {
45 parser.in.close();
46 parser.in = null;
47 }
48 } catch(Exception e) {/* ignore it */}
49 }
50 }
51
52 /**
53 * Method to download OSM files from somewhere
54 */
55 @Override
56 public DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException {
57 return doParse(new Parser<DataSet>() {
58 @Override
59 public DataSet parse() throws OsmTransferException, IllegalDataException {
60 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
61 if (in == null)
62 return null;
63 progressMonitor.subTask(tr("Downloading OSM data..."));
64 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
65 }
66 }, progressMonitor);
67 }
68
69 /**
70 * Method to download BZip2-compressed OSM files from somewhere
71 */
72 @Override
73 public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
74 return doParse(new Parser<DataSet>() {
75 @Override
76 public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
77 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
78 if (in == null)
79 return null;
80 CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
81 progressMonitor.subTask(tr("Downloading OSM data..."));
82 return OsmReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
83 }
84 }, progressMonitor);
85 }
86
87 /**
88 * Method to download GZip-compressed OSM files from somewhere
89 */
90 @Override
91 public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
92 return doParse(new Parser<DataSet>() {
93 @Override
94 public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
95 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
96 if (in == null)
97 return null;
98 GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
99 progressMonitor.subTask(tr("Downloading OSM data..."));
100 return OsmReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
101 }
102 }, progressMonitor);
103 }
104
105 /* (non-Javadoc)
106 * @see org.openstreetmap.josm.io.OsmServerReader#parseOsmChange(org.openstreetmap.josm.gui.progress.ProgressMonitor)
107 */
108 @Override
109 public DataSet parseOsmChange(final ProgressMonitor progressMonitor)
110 throws OsmTransferException {
111 return doParse(new Parser<DataSet>() {
112 @Override
113 public DataSet parse() throws OsmTransferException, IllegalDataException {
114 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
115 if (in == null)
116 return null;
117 progressMonitor.subTask(tr("Downloading OSM data..."));
118 return OsmChangeReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
119 }
120 }, progressMonitor);
121 }
122
123 /**
124 * Method to download BZip2-compressed OSM Change files from somewhere
125 */
126 @Override
127 public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
128 return doParse(new Parser<DataSet>() {
129 @Override
130 public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
131 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
132 if (in == null)
133 return null;
134 CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
135 progressMonitor.subTask(tr("Downloading OSM data..."));
136 return OsmChangeReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
137 }
138 }, progressMonitor);
139 }
140
141 /**
142 * Method to download GZip-compressed OSM Change files from somewhere
143 */
144 @Override
145 public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
146 return doParse(new Parser<DataSet>() {
147 @Override
148 public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
149 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
150 if (in == null)
151 return null;
152 GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
153 progressMonitor.subTask(tr("Downloading OSM data..."));
154 return OsmChangeReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
155 }
156 }, progressMonitor);
157 }
158
159 @Override
160 public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
161 return doParse(new Parser<GpxData>() {
162 @Override
163 public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
164 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true));
165 if (in == null)
166 return null;
167 progressMonitor.subTask(tr("Downloading OSM data..."));
168 GpxReader reader = new GpxReader(in);
169 gpxParsedProperly = reader.parse(false);
170 GpxData result = reader.getGpxData();
171 result.fromServer = DownloadGpsTask.isFromServer(url);
172 return result;
173 }
174 }, progressMonitor);
175 }
176}
Note: See TracBrowser for help on using the repository browser.