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 |
|
---|
9 | import org.apache.tools.bzip2.CBZip2InputStream;
|
---|
10 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
13 | import org.xml.sax.SAXException;
|
---|
14 |
|
---|
15 | public class OsmServerLocationReader extends OsmServerReader {
|
---|
16 |
|
---|
17 | protected final String url;
|
---|
18 |
|
---|
19 | public OsmServerLocationReader(String url) {
|
---|
20 | this.url = url;
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected abstract class Parser<T> {
|
---|
24 | public InputStream in = null;
|
---|
25 | public abstract T parse() throws OsmTransferException, IllegalDataException, IOException, SAXException;
|
---|
26 | }
|
---|
27 |
|
---|
28 | protected final <T> T doParse(Parser<T> parser, final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
29 | progressMonitor.beginTask(tr("Contacting Server...", 10));
|
---|
30 | try {
|
---|
31 | return parser.parse();
|
---|
32 | } catch(OsmTransferException e) {
|
---|
33 | throw e;
|
---|
34 | } catch (Exception e) {
|
---|
35 | if (cancel)
|
---|
36 | return null;
|
---|
37 | throw new OsmTransferException(e);
|
---|
38 | } finally {
|
---|
39 | progressMonitor.finishTask();
|
---|
40 | try {
|
---|
41 | activeConnection = null;
|
---|
42 | if (parser.in != null) {
|
---|
43 | parser.in.close();
|
---|
44 | parser.in = null;
|
---|
45 | }
|
---|
46 | } catch(Exception e) {/* ignore it */}
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Method to download OSM files from somewhere
|
---|
52 | */
|
---|
53 | @Override
|
---|
54 | public DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
55 | return doParse(new Parser<DataSet>() {
|
---|
56 | @Override
|
---|
57 | public DataSet parse() throws OsmTransferException, IllegalDataException {
|
---|
58 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
|
---|
59 | if (in == null)
|
---|
60 | return null;
|
---|
61 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
62 | return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
63 | }
|
---|
64 | }, progressMonitor);
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Method to download BZip2-compressed OSM files from somewhere
|
---|
69 | */
|
---|
70 | @Override
|
---|
71 | public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
72 | return doParse(new Parser<DataSet>() {
|
---|
73 | @Override
|
---|
74 | public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
|
---|
75 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
|
---|
76 | if (in == null)
|
---|
77 | return null;
|
---|
78 | CBZip2InputStream bzin = OsmBzip2Importer.getBZip2InputStream(in);
|
---|
79 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
80 | return OsmReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
|
---|
81 | }
|
---|
82 | }, progressMonitor);
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* (non-Javadoc)
|
---|
86 | * @see org.openstreetmap.josm.io.OsmServerReader#parseOsmChange(org.openstreetmap.josm.gui.progress.ProgressMonitor)
|
---|
87 | */
|
---|
88 | @Override
|
---|
89 | public DataSet parseOsmChange(final ProgressMonitor progressMonitor)
|
---|
90 | throws OsmTransferException {
|
---|
91 | return doParse(new Parser<DataSet>() {
|
---|
92 | @Override
|
---|
93 | public DataSet parse() throws OsmTransferException, IllegalDataException {
|
---|
94 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
|
---|
95 | if (in == null)
|
---|
96 | return null;
|
---|
97 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
98 | return OsmChangeReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
99 | }
|
---|
100 | }, progressMonitor);
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
105 | return doParse(new Parser<GpxData>() {
|
---|
106 | @Override
|
---|
107 | public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
|
---|
108 | in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true));
|
---|
109 | if (in == null)
|
---|
110 | return null;
|
---|
111 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
112 | GpxReader reader = new GpxReader(in);
|
---|
113 | reader.parse(false);
|
---|
114 | GpxData result = reader.data;
|
---|
115 | result.fromServer = true;
|
---|
116 | return result;
|
---|
117 | }
|
---|
118 | }, progressMonitor);
|
---|
119 | }
|
---|
120 | }
|
---|