Ticket #17459: add-reverter-core-hacks.patch

File add-reverter-core-hacks.patch, 9.0 KB (added by GerdP, 6 years ago)

patch for core

  • src/org/openstreetmap/josm/data/osm/ChangesetDataSet.java

     
    1515 * A ChangesetDataSet holds the content of a changeset.
    1616 */
    1717public class ChangesetDataSet {
     18    private final boolean useEarliestVersion;
    1819
    1920    /**
     21     * Constructs a new {@code ChangesetDataSet}
     22     * For primitives which appear with different versions the last version is kept.
     23     */
     24    public ChangesetDataSet() {
     25        this.useEarliestVersion = false;
     26    }
     27
     28    /**
     29     * Constructs a new {@code ChangesetDataSet}
     30     * @param useEarliestVersion if true, keep the first version of a primitive, else the last version is kept.
     31     */
     32    public ChangesetDataSet(boolean useEarliestVersion) {
     33        this.useEarliestVersion = useEarliestVersion;
     34    }
     35
     36    /**
    2037     * Type of primitive modification.
    2138     */
    2239    public enum ChangesetModificationType {
     
    6077    public void put(HistoryOsmPrimitive primitive, ChangesetModificationType cmt) {
    6178        CheckParameterUtil.ensureParameterNotNull(primitive, "primitive");
    6279        CheckParameterUtil.ensureParameterNotNull(cmt, "cmt");
    63         primitives.put(primitive.getPrimitiveId(), primitive);
    64         modificationTypes.put(primitive.getPrimitiveId(), cmt);
     80        if (!useEarliestVersion || !primitives.containsKey(primitive)) {
     81            primitives.put(primitive.getPrimitiveId(), primitive);
     82            modificationTypes.put(primitive.getPrimitiveId(), cmt);
     83        }
    6584    }
    6685
    6786    /**
  • src/org/openstreetmap/josm/io/AbstractParser.java

     
    2828    /** the current primitive to be read */
    2929    protected HistoryOsmPrimitive currentPrimitive;
    3030    protected Locator locator;
     31    /** if true, replace user information in input by anonymous user */
     32    protected boolean useAnonymousUser;
    3133
    3234    @Override
    3335    public void setDocumentLocator(Locator locator) {
     
    111113        long changesetId = changeset != null ? changeset : 0L;
    112114        boolean visible = getMandatoryAttributeBoolean(atts, "visible");
    113115
    114         Long uid = getAttributeLong(atts, "uid");
    115         String userStr = atts.getValue("user");
    116         User user;
    117         if (userStr != null) {
    118             if (uid != null) {
    119                 user = User.createOsmUser(uid, userStr);
    120                 user.setPreferredName(userStr);
    121             } else {
    122                 user = User.createLocalUser(userStr);
     116        User user = null;
     117        if (!useAnonymousUser) {
     118            Long uid = getAttributeLong(atts, "uid");
     119            String userStr = atts.getValue("user");
     120            if (userStr != null) {
     121                if (uid != null) {
     122                    user = User.createOsmUser(uid, userStr);
     123                    user.setPreferredName(userStr);
     124                } else {
     125                    user = User.createLocalUser(userStr);
     126                }
    123127            }
    124         } else {
     128        }
     129        if (user == null) {
    125130            user = User.getAnonymous();
    126131        }
    127132
  • src/org/openstreetmap/josm/io/OsmChangesetContentParser.java

     
    3131public class OsmChangesetContentParser {
    3232
    3333    private final InputSource source;
    34     private final ChangesetDataSet data = new ChangesetDataSet();
     34    private final ChangesetDataSet data;
    3535
    3636    private class Parser extends AbstractParser {
     37        Parser(boolean useAnonymousUser) {
     38            this.useAnonymousUser = useAnonymousUser;
     39        }
    3740
    3841        /** the current change modification type */
    3942        private ChangesetDataSet.ChangesetModificationType currentModificationType;
     
    115118    }
    116119
    117120    /**
     121     * Constructs a new {@code OsmChangesetContentParser} which keeps the oldest version if
     122     * multiple versions of a primitive appear in one changeset.
     123     *
     124     * @param source the input stream with the changeset content as XML document. Must not be null.
     125     * @throws IllegalArgumentException if source is {@code null}.
     126     */
     127    public OsmChangesetContentParser(InputStream source) {
     128        this(source, false);
     129    }
     130
     131    /**
    118132     * Constructs a new {@code OsmChangesetContentParser}.
    119133     *
    120134     * @param source the input stream with the changeset content as XML document. Must not be null.
     135     * @param useEarliestVersion if true, keep the first version of a primitive, else the last version is kept.
    121136     * @throws IllegalArgumentException if source is {@code null}.
     137     * @since xxx
    122138     */
    123     @SuppressWarnings("resource")
    124     public OsmChangesetContentParser(InputStream source) {
     139    public OsmChangesetContentParser(InputStream source, boolean useEarliestVersion) {
    125140        CheckParameterUtil.ensureParameterNotNull(source, "source");
    126141        this.source = new InputSource(new InputStreamReader(source, StandardCharsets.UTF_8));
     142        data = new ChangesetDataSet(useEarliestVersion);
    127143    }
    128144
    129145    /**
     
    135151    public OsmChangesetContentParser(String source) {
    136152        CheckParameterUtil.ensureParameterNotNull(source, "source");
    137153        this.source = new InputSource(new StringReader(source));
     154        data = new ChangesetDataSet();
    138155    }
    139156
    140157    /**
     
    146163     * exceptions.
    147164     */
    148165    public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws XmlParsingException {
     166        return parse(progressMonitor, false);
     167    }
     168
     169    /**
     170     * Parses the content.
     171     *
     172     * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
     173     * @param useAnonymousUser if true, replace all user information with the anonymous user
     174     * @return the parsed data
     175     * @throws XmlParsingException if something went wrong. Check for chained
     176     * exceptions.
     177     */
     178    public ChangesetDataSet parse(ProgressMonitor progressMonitor, boolean useAnonymousUser) throws XmlParsingException {
    149179        if (progressMonitor == null) {
    150180            progressMonitor = NullProgressMonitor.INSTANCE;
    151181        }
     
    152182        try {
    153183            progressMonitor.beginTask("");
    154184            progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
    155             XmlUtils.parseSafeSAX(source, new Parser());
     185            XmlUtils.parseSafeSAX(source, new Parser(useAnonymousUser));
    156186        } catch (XmlParsingException e) {
    157187            throw e;
    158188        } catch (ParserConfigurationException | SAXException | IOException e) {
     
    171201     * exceptions.
    172202     */
    173203    public ChangesetDataSet parse() throws XmlParsingException {
    174         return parse(null);
     204        return parse(null, false);
    175205    }
    176206}
  • src/org/openstreetmap/josm/io/OsmServerChangesetReader.java

     
    2626 *
    2727 */
    2828public class OsmServerChangesetReader extends OsmServerReader {
     29    final boolean useAnonymousUser;
     30    final boolean useEarliestVersion;
    2931
     32
    3033    /**
     34     * Constructs a new {@code OsmServerChangesetReader} with default settings.
     35     */
     36    public OsmServerChangesetReader() {
     37        this(false, false);
     38    }
     39
     40    /**
     41     * Constructs a new {@code OsmServerChangesetReader}
     42     * @param useAnonymousUser if true, replace all user information with the anonymous user
     43     * @param useEarliestVersion if true, keep the first version of a primitive, else the last version is kept.
     44     * @since xxx
     45     */
     46    public OsmServerChangesetReader(boolean useAnonymousUser, boolean useEarliestVersion) {
     47        super();
     48        this.useAnonymousUser = useAnonymousUser;
     49        this.useEarliestVersion = useEarliestVersion;
     50    }
     51
     52    /**
    3153     * don't use - not implemented!
    3254     */
    3355    @Override
     
    197219                if (in == null)
    198220                    return null;
    199221                monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
    200                 OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
    201                 result = parser.parse(monitor.createSubTaskMonitor(1, true));
     222                OsmChangesetContentParser parser = new OsmChangesetContentParser(in, useEarliestVersion);
     223                result = parser.parse(monitor.createSubTaskMonitor(1, true), useAnonymousUser);
    202224            } catch (IOException e) {
    203225                Logging.warn(e);
    204226            }