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

Last change on this file since 6104 was 6104, checked in by Don-vip, 11 years ago

see #8902 - Small performance enhancements / coding style (patch by shinigami):

  • while -> foreach
  • for -> for each

plus:

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