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