| 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.InputStream;
|
|---|
| 8 | import java.text.MessageFormat;
|
|---|
| 9 | import java.util.ArrayList;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.Collections;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|---|
| 18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 20 | import org.openstreetmap.josm.tools.XmlParsingException;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 | public 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 | StringBuilder sb = new StringBuilder();
|
|---|
| 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 | return OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
|---|
| 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 | StringBuilder sb = new StringBuilder();
|
|---|
| 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<>();
|
|---|
| 131 | int i=0;
|
|---|
| 132 | for (int id : ids) {
|
|---|
| 133 | if (id <= 0) {
|
|---|
| 134 | continue;
|
|---|
| 135 | }
|
|---|
| 136 | i++;
|
|---|
| 137 | StringBuilder sb = new StringBuilder();
|
|---|
| 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 | StringBuilder sb = new StringBuilder();
|
|---|
| 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 | return parser.parse(monitor.createSubTaskMonitor(1, true));
|
|---|
| 185 | } catch(XmlParsingException e) {
|
|---|
| 186 | throw new OsmTransferException(e);
|
|---|
| 187 | } finally {
|
|---|
| 188 | monitor.finishTask();
|
|---|
| 189 | }
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|