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

Last change on this file since 7521 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

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