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

Last change on this file since 2832 was 2781, checked in by Gubaer, 14 years ago

fixed #4170: Downloaded open changesets are not listed

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.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Iterator;
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;
20
21/**
22 * Reads the history of an {@see OsmPrimitive} from the OSM API server.
23 *
24 */
25public class OsmServerChangesetReader extends OsmServerReader {
26
27 /**
28 * constructor
29 *
30 */
31 public OsmServerChangesetReader(){
32 setDoAuthenticate(false);
33 }
34
35 /**
36 * don't use - not implemented!
37 *
38 */
39 @Override
40 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
41 return null;
42 }
43
44 /**
45 * Queries a list
46 * @param query the query specification. Must not be null.
47 * @param monitor a progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
48 * @return the list of changesets read from the server
49 * @throws IllegalArgumentException thrown if query is null
50 * @throws OsmTransferException thrown if something goes wrong w
51 */
52 public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
53 if (query == null)
54 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null.", "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 {@see 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(tr("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 {@see 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 (Iterator<Integer> it = ids.iterator(); it.hasNext(); ) {
134 int id = it.next();
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 changset id. >0 required.
166 * @param monitor the progress monitor. {@see NullProgressMonitor#INSTANCE} assumed if null.
167 * @return the changset 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(tr("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.