[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[2613] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 6 |
|
---|
[7033] | 7 | import java.io.IOException;
|
---|
[2512] | 8 | import java.io.InputStream;
|
---|
[2852] | 9 | import java.text.MessageFormat;
|
---|
[2613] | 10 | import java.util.ArrayList;
|
---|
| 11 | import java.util.Collection;
|
---|
| 12 | import java.util.Collections;
|
---|
[2512] | 13 | import java.util.List;
|
---|
| 14 |
|
---|
| 15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
[2688] | 16 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
[2512] | 17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 18 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[2852] | 20 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12620] | 21 | import org.openstreetmap.josm.tools.Logging;
|
---|
[6906] | 22 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
[2512] | 23 |
|
---|
| 24 | /**
|
---|
[5832] | 25 | * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
|
---|
[2512] | 26 | *
|
---|
| 27 | */
|
---|
| 28 | public class OsmServerChangesetReader extends OsmServerReader {
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * don't use - not implemented!
|
---|
| 32 | */
|
---|
| 33 | @Override
|
---|
| 34 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 35 | return null;
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[7704] | 38 | protected final InputStream getChangesetInputStream(long id, boolean includeDiscussion, ProgressMonitor monitor)
|
---|
| 39 | throws OsmTransferException {
|
---|
[10242] | 40 | StringBuilder sb = new StringBuilder(48).append("changeset/").append(id);
|
---|
[7704] | 41 | if (includeDiscussion) {
|
---|
| 42 | sb.append("?include_discussion=true");
|
---|
| 43 | }
|
---|
| 44 | return getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[2512] | 47 | /**
|
---|
| 48 | * Queries a list
|
---|
| 49 | * @param query the query specification. Must not be null.
|
---|
[5266] | 50 | * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 51 | * @return the list of changesets read from the server
|
---|
[8291] | 52 | * @throws IllegalArgumentException if query is null
|
---|
[11110] | 53 | * @throws OsmTransferException if something goes wrong
|
---|
[2512] | 54 | */
|
---|
| 55 | public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2852] | 56 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
[7033] | 57 | List<Changeset> result = null;
|
---|
[2512] | 58 | if (monitor == null) {
|
---|
| 59 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 60 | }
|
---|
| 61 | try {
|
---|
| 62 | monitor.beginTask(tr("Reading changesets..."));
|
---|
[6822] | 63 | StringBuilder sb = new StringBuilder();
|
---|
[2512] | 64 | sb.append("changesets?").append(query.getQueryString());
|
---|
[7033] | 65 | try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
|
---|
| 66 | if (in == null)
|
---|
[10006] | 67 | return Collections.emptyList();
|
---|
[7033] | 68 | monitor.indeterminateSubTask(tr("Downloading changesets ..."));
|
---|
| 69 | result = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
| 70 | } catch (IOException e) {
|
---|
[12620] | 71 | Logging.warn(e);
|
---|
[7033] | 72 | }
|
---|
[8510] | 73 | } catch (OsmTransferException e) {
|
---|
[2512] | 74 | throw e;
|
---|
[8510] | 75 | } catch (IllegalDataException e) {
|
---|
[2512] | 76 | throw new OsmTransferException(e);
|
---|
| 77 | } finally {
|
---|
| 78 | monitor.finishTask();
|
---|
| 79 | }
|
---|
[7033] | 80 | return result;
|
---|
[2512] | 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
[7704] | 84 | * Reads the changeset with id <code>id</code> from the server.
|
---|
[2512] | 85 | *
|
---|
[7704] | 86 | * @param id the changeset id. id > 0 required.
|
---|
| 87 | * @param includeDiscussion determines if discussion comments must be downloaded or not
|
---|
[5266] | 88 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 89 | * @return the changeset read
|
---|
[8291] | 90 | * @throws OsmTransferException if something goes wrong
|
---|
[6830] | 91 | * @throws IllegalArgumentException if id <= 0
|
---|
[7704] | 92 | * @since 7704
|
---|
[2512] | 93 | */
|
---|
[7704] | 94 | public Changeset readChangeset(long id, boolean includeDiscussion, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2512] | 95 | if (id <= 0)
|
---|
[2852] | 96 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
|
---|
[2512] | 97 | if (monitor == null) {
|
---|
| 98 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 99 | }
|
---|
[7033] | 100 | Changeset result = null;
|
---|
[2512] | 101 | try {
|
---|
[8510] | 102 | monitor.beginTask(tr("Reading changeset {0} ...", id));
|
---|
[7704] | 103 | try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
|
---|
[7033] | 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) {
|
---|
[12620] | 112 | Logging.warn(e);
|
---|
[7033] | 113 | }
|
---|
[8510] | 114 | } catch (OsmTransferException e) {
|
---|
[2512] | 115 | throw e;
|
---|
[8510] | 116 | } catch (IllegalDataException e) {
|
---|
[2512] | 117 | throw new OsmTransferException(e);
|
---|
| 118 | } finally {
|
---|
| 119 | monitor.finishTask();
|
---|
| 120 | }
|
---|
[7033] | 121 | return result;
|
---|
[2512] | 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
[7704] | 125 | * Reads the changesets with id <code>ids</code> from the server.
|
---|
[2613] | 126 | *
|
---|
[7704] | 127 | * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
|
---|
| 128 | * @param includeDiscussion determines if discussion comments must be downloaded or not
|
---|
[5266] | 129 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2613] | 130 | * @return the changeset read
|
---|
[8291] | 131 | * @throws OsmTransferException if something goes wrong
|
---|
[6830] | 132 | * @throws IllegalArgumentException if id <= 0
|
---|
[7704] | 133 | * @since 7704
|
---|
[2613] | 134 | */
|
---|
[8540] | 135 | public List<Changeset> readChangesets(Collection<Integer> ids, boolean includeDiscussion, ProgressMonitor monitor)
|
---|
| 136 | throws OsmTransferException {
|
---|
[2613] | 137 | if (ids == null)
|
---|
| 138 | return Collections.emptyList();
|
---|
| 139 | if (monitor == null) {
|
---|
| 140 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 141 | }
|
---|
| 142 | try {
|
---|
[8510] | 143 | monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...", ids.size(), ids.size()));
|
---|
[2613] | 144 | monitor.setTicksCount(ids.size());
|
---|
[7005] | 145 | List<Changeset> ret = new ArrayList<>();
|
---|
[8510] | 146 | int i = 0;
|
---|
[6104] | 147 | for (int id : ids) {
|
---|
[2613] | 148 | if (id <= 0) {
|
---|
| 149 | continue;
|
---|
| 150 | }
|
---|
| 151 | i++;
|
---|
[7704] | 152 | try (InputStream in = getChangesetInputStream(id, includeDiscussion, monitor)) {
|
---|
[7033] | 153 | if (in == null)
|
---|
| 154 | return null;
|
---|
| 155 | monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
|
---|
| 156 | List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
| 157 | if (changesets == null || changesets.isEmpty()) {
|
---|
| 158 | continue;
|
---|
| 159 | }
|
---|
| 160 | ret.addAll(changesets);
|
---|
| 161 | } catch (IOException e) {
|
---|
[12620] | 162 | Logging.warn(e);
|
---|
[2613] | 163 | }
|
---|
| 164 | monitor.worked(1);
|
---|
| 165 | }
|
---|
| 166 | return ret;
|
---|
[8510] | 167 | } catch (OsmTransferException e) {
|
---|
[2613] | 168 | throw e;
|
---|
[8510] | 169 | } catch (IllegalDataException e) {
|
---|
[2613] | 170 | throw new OsmTransferException(e);
|
---|
| 171 | } finally {
|
---|
| 172 | monitor.finishTask();
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
[2688] | 177 | * Downloads the content of a changeset
|
---|
[2512] | 178 | *
|
---|
[6830] | 179 | * @param id the changeset id. > 0 required.
|
---|
[5266] | 180 | * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
|
---|
[2915] | 181 | * @return the changeset content
|
---|
[8291] | 182 | * @throws IllegalArgumentException if id <= 0
|
---|
| 183 | * @throws OsmTransferException if something went wrong
|
---|
[2512] | 184 | */
|
---|
[8291] | 185 | public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2688] | 186 | if (id <= 0)
|
---|
[8540] | 187 | throw new IllegalArgumentException(
|
---|
| 188 | MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
[2688] | 189 | if (monitor == null) {
|
---|
| 190 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 191 | }
|
---|
[7033] | 192 | ChangesetDataSet result = null;
|
---|
[2688] | 193 | try {
|
---|
| 194 | monitor.beginTask(tr("Downloading changeset content"));
|
---|
[10242] | 195 | StringBuilder sb = new StringBuilder(32).append("changeset/").append(id).append("/download");
|
---|
[7033] | 196 | try (InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true))) {
|
---|
| 197 | if (in == null)
|
---|
| 198 | return null;
|
---|
| 199 | monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
|
---|
| 200 | OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
|
---|
| 201 | result = parser.parse(monitor.createSubTaskMonitor(1, true));
|
---|
| 202 | } catch (IOException e) {
|
---|
[12620] | 203 | Logging.warn(e);
|
---|
[7033] | 204 | }
|
---|
[8510] | 205 | } catch (XmlParsingException e) {
|
---|
[2688] | 206 | throw new OsmTransferException(e);
|
---|
| 207 | } finally {
|
---|
| 208 | monitor.finishTask();
|
---|
| 209 | }
|
---|
[7033] | 210 | return result;
|
---|
[2512] | 211 | }
|
---|
| 212 | }
|
---|