source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerChangesetReader.java@ 9079

Last change on this file since 9079 was 8540, checked in by Don-vip, 9 years ago

fix remaining checkstyle issues

  • Property svn:eol-style set to native
File size: 8.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;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.io.IOException;
8import java.io.InputStream;
9import java.text.MessageFormat;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.List;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.ChangesetDataSet;
18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.openstreetmap.josm.tools.CheckParameterUtil;
22import org.openstreetmap.josm.tools.XmlParsingException;
23
24/**
25 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
26 *
27 */
28public class OsmServerChangesetReader extends OsmServerReader {
29
30 /**
31 * Constructs a new {@code OsmServerChangesetReader}.
32 */
33 public OsmServerChangesetReader() {
34 setDoAuthenticate(false);
35 }
36
37 /**
38 * don't use - not implemented!
39 */
40 @Override
41 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
42 return null;
43 }
44
45 protected final InputStream getChangesetInputStream(long id, boolean includeDiscussion, ProgressMonitor monitor)
46 throws OsmTransferException {
47 StringBuilder sb = new StringBuilder();
48 sb.append("changeset/").append(id);
49 if (includeDiscussion) {
50 sb.append("?include_discussion=true");
51 }
52 return getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
53 }
54
55 /**
56 * Queries a list
57 * @param query the query specification. Must not be null.
58 * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
59 * @return the list of changesets read from the server
60 * @throws IllegalArgumentException if query is null
61 * @throws OsmTransferException if something goes wrong w
62 */
63 public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
64 CheckParameterUtil.ensureParameterNotNull(query, "query");
65 List<Changeset> result = null;
66 if (monitor == null) {
67 monitor = NullProgressMonitor.INSTANCE;
68 }
69 try {
70 monitor.beginTask(tr("Reading changesets..."));
71 StringBuilder sb = new StringBuilder();
72 sb.append("changesets?").append(query.getQueryString());
73 try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
74 if (in == null)
75 return null;
76 monitor.indeterminateSubTask(tr("Downloading changesets ..."));
77 result = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
78 } catch (IOException e) {
79 Main.warn(e);
80 }
81 } catch (OsmTransferException e) {
82 throw e;
83 } catch (IllegalDataException e) {
84 throw new OsmTransferException(e);
85 } finally {
86 monitor.finishTask();
87 }
88 return result;
89 }
90
91 /**
92 * Reads the changeset with id <code>id</code> from the server.
93 *
94 * @param id the changeset id. id &gt; 0 required.
95 * @param includeDiscussion determines if discussion comments must be downloaded or not
96 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
97 * @return the changeset read
98 * @throws OsmTransferException if something goes wrong
99 * @throws IllegalArgumentException if id &lt;= 0
100 * @since 7704
101 */
102 public Changeset readChangeset(long id, boolean includeDiscussion, ProgressMonitor monitor) throws OsmTransferException {
103 if (id <= 0)
104 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
105 if (monitor == null) {
106 monitor = NullProgressMonitor.INSTANCE;
107 }
108 Changeset result = null;
109 try {
110 monitor.beginTask(tr("Reading changeset {0} ...", id));
111 try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
112 if (in == null)
113 return null;
114 monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
115 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
116 if (changesets == null || changesets.isEmpty())
117 return null;
118 result = changesets.get(0);
119 } catch (IOException e) {
120 Main.warn(e);
121 }
122 } catch (OsmTransferException e) {
123 throw e;
124 } catch (IllegalDataException e) {
125 throw new OsmTransferException(e);
126 } finally {
127 monitor.finishTask();
128 }
129 return result;
130 }
131
132 /**
133 * Reads the changesets with id <code>ids</code> from the server.
134 *
135 * @param ids the list of ids. Ignored if null. Only load changesets for ids &gt; 0.
136 * @param includeDiscussion determines if discussion comments must be downloaded or not
137 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
138 * @return the changeset read
139 * @throws OsmTransferException if something goes wrong
140 * @throws IllegalArgumentException if id &lt;= 0
141 * @since 7704
142 */
143 public List<Changeset> readChangesets(Collection<Integer> ids, boolean includeDiscussion, ProgressMonitor monitor)
144 throws OsmTransferException {
145 if (ids == null)
146 return Collections.emptyList();
147 if (monitor == null) {
148 monitor = NullProgressMonitor.INSTANCE;
149 }
150 try {
151 monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...", ids.size(), ids.size()));
152 monitor.setTicksCount(ids.size());
153 List<Changeset> ret = new ArrayList<>();
154 int i = 0;
155 for (int id : ids) {
156 if (id <= 0) {
157 continue;
158 }
159 i++;
160 try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
161 if (in == null)
162 return null;
163 monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
164 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
165 if (changesets == null || changesets.isEmpty()) {
166 continue;
167 }
168 ret.addAll(changesets);
169 } catch (IOException e) {
170 Main.warn(e);
171 }
172 monitor.worked(1);
173 }
174 return ret;
175 } catch (OsmTransferException e) {
176 throw e;
177 } catch (IllegalDataException e) {
178 throw new OsmTransferException(e);
179 } finally {
180 monitor.finishTask();
181 }
182 }
183
184 /**
185 * Downloads the content of a changeset
186 *
187 * @param id the changeset id. &gt; 0 required.
188 * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
189 * @return the changeset content
190 * @throws IllegalArgumentException if id &lt;= 0
191 * @throws OsmTransferException if something went wrong
192 */
193 public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws OsmTransferException {
194 if (id <= 0)
195 throw new IllegalArgumentException(
196 MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
197 if (monitor == null) {
198 monitor = NullProgressMonitor.INSTANCE;
199 }
200 ChangesetDataSet result = null;
201 try {
202 monitor.beginTask(tr("Downloading changeset content"));
203 StringBuilder sb = new StringBuilder();
204 sb.append("changeset/").append(id).append("/download");
205 try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
206 if (in == null)
207 return null;
208 monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
209 OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
210 result = parser.parse(monitor.createSubTaskMonitor(1, true));
211 } catch (IOException e) {
212 Main.warn(e);
213 }
214 } catch (XmlParsingException e) {
215 throw new OsmTransferException(e);
216 } finally {
217 monitor.finishTask();
218 }
219 return result;
220 }
221}
Note: See TracBrowser for help on using the repository browser.