[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;
|
---|
[2512] | 20 |
|
---|
| 21 | /**
|
---|
[5832] | 22 | * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
|
---|
[2512] | 23 | *
|
---|
| 24 | */
|
---|
| 25 | public 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.
|
---|
[5266] | 47 | * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 48 | * @return the list of changesets read from the server
|
---|
| 49 | * @throws IllegalArgumentException thrown if query is null
|
---|
[2781] | 50 | * @throws OsmTransferException thrown if something goes wrong w
|
---|
[2512] | 51 | */
|
---|
| 52 | public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
|
---|
[2852] | 53 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
[2512] | 54 | if (monitor == null) {
|
---|
| 55 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 56 | }
|
---|
| 57 | try {
|
---|
| 58 | monitor.beginTask(tr("Reading changesets..."));
|
---|
[6822] | 59 | StringBuilder sb = new StringBuilder();
|
---|
[2512] | 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 ..."));
|
---|
[6792] | 65 | return OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
[2512] | 66 | } catch(OsmTransferException e) {
|
---|
| 67 | throw e;
|
---|
| 68 | } catch(IllegalDataException e) {
|
---|
| 69 | throw new OsmTransferException(e);
|
---|
| 70 | } finally {
|
---|
| 71 | monitor.finishTask();
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
[2613] | 76 | * Reads the changeset with id <code>id</code> from the server
|
---|
[2512] | 77 | *
|
---|
| 78 | * @param id the changeset id. id > 0 required.
|
---|
[5266] | 79 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2512] | 80 | * @return the changeset read
|
---|
| 81 | * @throws OsmTransferException thrown if something goes wrong
|
---|
| 82 | * @throws IllegalArgumentException if id <= 0
|
---|
| 83 | */
|
---|
| 84 | public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
|
---|
| 85 | if (id <= 0)
|
---|
[2852] | 86 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
|
---|
[2512] | 87 | if (monitor == null) {
|
---|
| 88 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 89 | }
|
---|
| 90 | try {
|
---|
| 91 | monitor.beginTask(tr("Reading changeset {0} ...",id));
|
---|
[6822] | 92 | StringBuilder sb = new StringBuilder();
|
---|
[2512] | 93 | sb.append("changeset/").append(id);
|
---|
| 94 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
| 95 | if (in == null)
|
---|
| 96 | return null;
|
---|
| 97 | monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
|
---|
| 98 | List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
| 99 | if (changesets == null || changesets.isEmpty())
|
---|
| 100 | return null;
|
---|
| 101 | return changesets.get(0);
|
---|
| 102 | } catch(OsmTransferException e) {
|
---|
| 103 | throw e;
|
---|
| 104 | } catch(IllegalDataException e) {
|
---|
| 105 | throw new OsmTransferException(e);
|
---|
| 106 | } finally {
|
---|
| 107 | monitor.finishTask();
|
---|
| 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
[2613] | 112 | * Reads the changeset with id <code>id</code> from the server
|
---|
| 113 | *
|
---|
| 114 | * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
|
---|
[5266] | 115 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2613] | 116 | * @return the changeset read
|
---|
| 117 | * @throws OsmTransferException thrown if something goes wrong
|
---|
| 118 | * @throws IllegalArgumentException if id <= 0
|
---|
| 119 | */
|
---|
| 120 | public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
|
---|
| 121 | if (ids == null)
|
---|
| 122 | return Collections.emptyList();
|
---|
| 123 | if (monitor == null) {
|
---|
| 124 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 125 | }
|
---|
| 126 | try {
|
---|
| 127 | monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...",ids.size(),ids.size()));
|
---|
| 128 | monitor.setTicksCount(ids.size());
|
---|
| 129 | List<Changeset> ret = new ArrayList<Changeset>();
|
---|
| 130 | int i=0;
|
---|
[6104] | 131 | for (int id : ids) {
|
---|
[2613] | 132 | if (id <= 0) {
|
---|
| 133 | continue;
|
---|
| 134 | }
|
---|
| 135 | i++;
|
---|
[6822] | 136 | StringBuilder sb = new StringBuilder();
|
---|
[2613] | 137 | sb.append("changeset/").append(id);
|
---|
| 138 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
| 139 | if (in == null)
|
---|
| 140 | return null;
|
---|
[6104] | 141 | monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
|
---|
[2613] | 142 | List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
| 143 | if (changesets == null || changesets.isEmpty()) {
|
---|
| 144 | continue;
|
---|
| 145 | }
|
---|
| 146 | ret.addAll(changesets);
|
---|
| 147 | monitor.worked(1);
|
---|
| 148 | }
|
---|
| 149 | return ret;
|
---|
| 150 | } catch(OsmTransferException e) {
|
---|
| 151 | throw e;
|
---|
| 152 | } catch(IllegalDataException e) {
|
---|
| 153 | throw new OsmTransferException(e);
|
---|
| 154 | } finally {
|
---|
| 155 | monitor.finishTask();
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | /**
|
---|
[2688] | 160 | * Downloads the content of a changeset
|
---|
[2512] | 161 | *
|
---|
[2915] | 162 | * @param id the changeset id. >0 required.
|
---|
[5266] | 163 | * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
|
---|
[2915] | 164 | * @return the changeset content
|
---|
[2688] | 165 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
| 166 | * @throws OsmTransferException thrown if something went wrong
|
---|
[2512] | 167 | */
|
---|
[2688] | 168 | public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws IllegalArgumentException, OsmTransferException {
|
---|
| 169 | if (id <= 0)
|
---|
[2852] | 170 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
[2688] | 171 | if (monitor == null) {
|
---|
| 172 | monitor = NullProgressMonitor.INSTANCE;
|
---|
| 173 | }
|
---|
| 174 | try {
|
---|
| 175 | monitor.beginTask(tr("Downloading changeset content"));
|
---|
[6822] | 176 | StringBuilder sb = new StringBuilder();
|
---|
[2688] | 177 | sb.append("changeset/").append(id).append("/download");
|
---|
| 178 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
| 179 | if (in == null)
|
---|
| 180 | return null;
|
---|
| 181 | monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
|
---|
| 182 | OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
|
---|
[6792] | 183 | return parser.parse(monitor.createSubTaskMonitor(1, true));
|
---|
[2688] | 184 | } catch(OsmDataParsingException e) {
|
---|
| 185 | throw new OsmTransferException(e);
|
---|
| 186 | } finally {
|
---|
| 187 | monitor.finishTask();
|
---|
| 188 | }
|
---|
[2512] | 189 | }
|
---|
| 190 | }
|
---|