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

Last change on this file since 6718 was 6552, checked in by simon04, 10 years ago

Refactoring: introduce Utils.UTF_8 charset to avoid handling of UnsupportedEncodingException

According to the Javadoc of Charset, every implementation of the Java
platform is required to support UTF-8.

  • Property svn:eol-style set to native
File size: 7.7 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.text.MessageFormat;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.List;
13
14import org.openstreetmap.josm.data.osm.Changeset;
15import org.openstreetmap.josm.data.osm.ChangesetDataSet;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * Reads the history of an {@link org.openstreetmap.josm.data.osm.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 {@link 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 CheckParameterUtil.ensureParameterNotNull(query, "query");
54 if (monitor == null) {
55 monitor = NullProgressMonitor.INSTANCE;
56 }
57 try {
58 monitor.beginTask(tr("Reading changesets..."));
59 StringBuffer sb = new StringBuffer();
60 sb.append("changesets?").append(query.getQueryString());
61 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
62 if (in == null)
63 return null;
64 monitor.indeterminateSubTask(tr("Downloading changesets ..."));
65 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
66 return changesets;
67 } catch(OsmTransferException e) {
68 throw e;
69 } catch(IllegalDataException e) {
70 throw new OsmTransferException(e);
71 } finally {
72 monitor.finishTask();
73 }
74 }
75
76 /**
77 * Reads the changeset with id <code>id</code> from the server
78 *
79 * @param id the changeset id. id > 0 required.
80 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
81 * @return the changeset read
82 * @throws OsmTransferException thrown if something goes wrong
83 * @throws IllegalArgumentException if id <= 0
84 */
85 public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
86 if (id <= 0)
87 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
88 if (monitor == null) {
89 monitor = NullProgressMonitor.INSTANCE;
90 }
91 try {
92 monitor.beginTask(tr("Reading changeset {0} ...",id));
93 StringBuffer sb = new StringBuffer();
94 sb.append("changeset/").append(id);
95 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
96 if (in == null)
97 return null;
98 monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
99 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
100 if (changesets == null || changesets.isEmpty())
101 return null;
102 return changesets.get(0);
103 } catch(OsmTransferException e) {
104 throw e;
105 } catch(IllegalDataException e) {
106 throw new OsmTransferException(e);
107 } finally {
108 monitor.finishTask();
109 }
110 }
111
112 /**
113 * Reads the changeset with id <code>id</code> from the server
114 *
115 * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
116 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
117 * @return the changeset read
118 * @throws OsmTransferException thrown if something goes wrong
119 * @throws IllegalArgumentException if id <= 0
120 */
121 public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
122 if (ids == null)
123 return Collections.emptyList();
124 if (monitor == null) {
125 monitor = NullProgressMonitor.INSTANCE;
126 }
127 try {
128 monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...",ids.size(),ids.size()));
129 monitor.setTicksCount(ids.size());
130 List<Changeset> ret = new ArrayList<Changeset>();
131 int i=0;
132 for (int id : ids) {
133 if (id <= 0) {
134 continue;
135 }
136 i++;
137 StringBuffer sb = new StringBuffer();
138 sb.append("changeset/").append(id);
139 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
140 if (in == null)
141 return null;
142 monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
143 List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
144 if (changesets == null || changesets.isEmpty()) {
145 continue;
146 }
147 ret.addAll(changesets);
148 monitor.worked(1);
149 }
150 return ret;
151 } catch(OsmTransferException e) {
152 throw e;
153 } catch(IllegalDataException e) {
154 throw new OsmTransferException(e);
155 } finally {
156 monitor.finishTask();
157 }
158 }
159
160 /**
161 * Downloads the content of a changeset
162 *
163 * @param id the changeset id. >0 required.
164 * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
165 * @return the changeset content
166 * @throws IllegalArgumentException thrown if id <= 0
167 * @throws OsmTransferException thrown if something went wrong
168 */
169 public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws IllegalArgumentException, OsmTransferException {
170 if (id <= 0)
171 throw new IllegalArgumentException(MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
172 if (monitor == null) {
173 monitor = NullProgressMonitor.INSTANCE;
174 }
175 try {
176 monitor.beginTask(tr("Downloading changeset content"));
177 StringBuffer sb = new StringBuffer();
178 sb.append("changeset/").append(id).append("/download");
179 InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
180 if (in == null)
181 return null;
182 monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
183 OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
184 ChangesetDataSet ds = parser.parse(monitor.createSubTaskMonitor(1, true));
185 return ds;
186 } catch(OsmDataParsingException e) {
187 throw new OsmTransferException(e);
188 } finally {
189 monitor.finishTask();
190 }
191 }
192}
Note: See TracBrowser for help on using the repository browser.