source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerHistoryReader.java@ 17335

Last change on this file since 17335 was 14763, checked in by GerdP, 5 years ago

fix #17279: improve performance when retrieving history

  • Property svn:eol-style set to native
File size: 2.9 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.text.MessageFormat;
9
10import org.openstreetmap.josm.data.osm.DataSet;
11import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
12import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
13import org.openstreetmap.josm.gui.progress.ProgressMonitor;
14import org.openstreetmap.josm.tools.CheckParameterUtil;
15import org.xml.sax.SAXException;
16
17/**
18 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
19 *
20 */
21public class OsmServerHistoryReader extends OsmServerReader {
22
23 private final OsmPrimitiveType primitiveType;
24 private final long id;
25
26 /**
27 * constructor
28 *
29 * @param type the type of the primitive whose history is to be fetched from the server.
30 * Must not be null.
31 * @param id the id of the primitive
32 *
33 * @throws IllegalArgumentException if type is null
34 */
35 public OsmServerHistoryReader(OsmPrimitiveType type, long id) {
36 CheckParameterUtil.ensureParameterNotNull(type, "type");
37 if (id < 0)
38 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected. Got ''{1}''.", "id", id));
39 this.primitiveType = type;
40 this.id = id;
41 }
42
43 /**
44 * don't use - not implemented!
45 *
46 */
47 @Override
48 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
49 return null;
50 }
51
52 /**
53 * Fetches the history from the OSM API and parses it
54 * @param progressMonitor progress monitor
55 *
56 * @return the data set with the parsed history data
57 * @throws OsmTransferException if an exception occurs
58 */
59 public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException {
60 progressMonitor.beginTask("");
61 try {
62 final String urlStr = primitiveType.getAPIName() + '/' + id + "/history";
63 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server for {0}", urlStr));
64
65 try (InputStream in = getInputStream(urlStr, progressMonitor.createSubTaskMonitor(1, true))) {
66 if (in == null)
67 return null;
68 progressMonitor.indeterminateSubTask(tr("Downloading history..."));
69 OsmHistoryReader reader = new OsmHistoryReader(in);
70 return reader.parse(progressMonitor.createSubTaskMonitor(1, true));
71 }
72 } catch (OsmTransferException e) {
73 throw e;
74 } catch (IOException | SAXException e) {
75 if (cancel)
76 return null;
77 throw new OsmTransferException(e);
78 } finally {
79 progressMonitor.finishTask();
80 activeConnection = null;
81 }
82 }
83}
Note: See TracBrowser for help on using the repository browser.