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

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

Checkstyle:

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