[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 |
|
---|
[2512] | 7 | import java.io.InputStream;
|
---|
[2852] | 8 | import java.text.MessageFormat;
|
---|
[2613] | 9 | import java.util.ArrayList;
|
---|
| 10 | import java.util.Collection;
|
---|
| 11 | import java.util.Collections;
|
---|
[2512] | 12 | import java.util.List;
|
---|
| 13 |
|
---|
| 14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
[2688] | 15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
[2512] | 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[2852] | 19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[6906] | 20 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
[2512] | 21 |
|
---|
| 22 | /**
|
---|
[5832] | 23 | * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
|
---|
[2512] | 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.
|
---|
[5266] | 48 | * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 49 | * @return the list of changesets read from the server
|
---|
| 50 | * @throws IllegalArgumentException thrown if query is null
|
---|
[2781] | 51 | * @throws OsmTransferException thrown if something goes wrong w
|
---|
[2512] | 52 | */
|
---|
| 53 | public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2852] | 54 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
[2512] | 55 | if (monitor == null) {
|
---|
| 56 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 57 | }
|
---|
| 58 | try {
|
---|
| 59 | monitor.beginTask(tr("Reading changesets..."));
|
---|
[6822] | 60 | StringBuilder sb = new StringBuilder();
|
---|
[2512] | 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 ..."));
|
---|
[6792] | 66 | return OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
[2512] | 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 | /**
|
---|
[2613] | 77 | * Reads the changeset with id <code>id</code> from the server
|
---|
[2512] | 78 | *
|
---|
[6830] | 79 | * @param id the changeset id. id > 0 required.
|
---|
[5266] | 80 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 81 | * @return the changeset read
|
---|
| 82 | * @throws OsmTransferException thrown if something goes wrong
|
---|
[6830] | 83 | * @throws IllegalArgumentException if id <= 0
|
---|
[2512] | 84 | */
|
---|
| 85 | public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
|
---|
| 86 | if (id <= 0)
|
---|
[2852] | 87 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
|
---|
[2512] | 88 | if (monitor == null) {
|
---|
| 89 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 90 | }
|
---|
| 91 | try {
|
---|
| 92 | monitor.beginTask(tr("Reading changeset {0} ...",id));
|
---|
[6822] | 93 | StringBuilder sb = new StringBuilder();
|
---|
[2512] | 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 | /**
|
---|
[2613] | 113 | * Reads the changeset with id <code>id</code> from the server
|
---|
| 114 | *
|
---|
[6830] | 115 | * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
|
---|
[5266] | 116 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2613] | 117 | * @return the changeset read
|
---|
| 118 | * @throws OsmTransferException thrown if something goes wrong
|
---|
[6830] | 119 | * @throws IllegalArgumentException if id <= 0
|
---|
[2613] | 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;
|
---|
[6104] | 132 | for (int id : ids) {
|
---|
[2613] | 133 | if (id <= 0) {
|
---|
| 134 | continue;
|
---|
| 135 | }
|
---|
| 136 | i++;
|
---|
[6822] | 137 | StringBuilder sb = new StringBuilder();
|
---|
[2613] | 138 | sb.append("changeset/").append(id);
|
---|
| 139 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
| 140 | if (in == null)
|
---|
| 141 | return null;
|
---|
[6104] | 142 | monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
|
---|
[2613] | 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 | /**
|
---|
[2688] | 161 | * Downloads the content of a changeset
|
---|
[2512] | 162 | *
|
---|
[6830] | 163 | * @param id the changeset id. > 0 required.
|
---|
[5266] | 164 | * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
|
---|
[2915] | 165 | * @return the changeset content
|
---|
[6830] | 166 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
[2688] | 167 | * @throws OsmTransferException thrown if something went wrong
|
---|
[2512] | 168 | */
|
---|
[2688] | 169 | public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws IllegalArgumentException, OsmTransferException {
|
---|
| 170 | if (id <= 0)
|
---|
[2852] | 171 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
[2688] | 172 | if (monitor == null) {
|
---|
| 173 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 174 | }
|
---|
| 175 | try {
|
---|
| 176 | monitor.beginTask(tr("Downloading changeset content"));
|
---|
[6822] | 177 | StringBuilder sb = new StringBuilder();
|
---|
[2688] | 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);
|
---|
[6792] | 184 | return parser.parse(monitor.createSubTaskMonitor(1, true));
|
---|
[6906] | 185 | } catch(XmlParsingException e) {
|
---|
[2688] | 186 | throw new OsmTransferException(e);
|
---|
| 187 | } finally {
|
---|
| 188 | monitor.finishTask();
|
---|
| 189 | }
|
---|
[2512] | 190 | }
|
---|
| 191 | }
|
---|