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