Index: applications/editors/josm/plugins/reverter/src/reverter/ChangesetReverter.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/ChangesetReverter.java	(revision 23192)
+++ applications/editors/josm/plugins/reverter/src/reverter/ChangesetReverter.java	(revision 23273)
@@ -3,4 +3,5 @@
 import static org.openstreetmap.josm.tools.I18n.tr;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Iterator;
@@ -15,5 +16,4 @@
 import org.openstreetmap.josm.data.coor.LatLon;
 import org.openstreetmap.josm.data.osm.Changeset;
-import org.openstreetmap.josm.data.osm.ChangesetDataSet;
 import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.Node;
@@ -24,6 +24,4 @@
 import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
 import org.openstreetmap.josm.data.osm.Way;
-import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetDataSetEntry;
-import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
 import org.openstreetmap.josm.data.osm.history.HistoryNode;
 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
@@ -34,6 +32,10 @@
 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
 import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
-import org.openstreetmap.josm.io.OsmServerChangesetReader;
 import org.openstreetmap.josm.io.OsmTransferException;
+
+import reverter.corehacks.ChangesetDataSet;
+import reverter.corehacks.OsmServerChangesetReader;
+import reverter.corehacks.ChangesetDataSet.ChangesetDataSetEntry;
+import reverter.corehacks.ChangesetDataSet.ChangesetModificationType;
 
 /**
@@ -167,15 +169,17 @@
         try {
             for (HistoryOsmPrimitive entry : updated) {
-                rdr.ReadObject(entry.getPrimitiveId(), (int)entry.getVersion()-1,
+                rdr.ReadObject(entry.getPrimitiveId(), cds.getEarliestVersion(entry.getPrimitiveId())-1,
                         progressMonitor.createSubTaskMonitor(1, true));
                 if (progressMonitor.isCancelled()) return;
             }
             for (HistoryOsmPrimitive entry : deleted) {
-                rdr.ReadObject(entry.getPrimitiveId(), (int)entry.getVersion()-1,
+                rdr.ReadObject(entry.getPrimitiveId(), cds.getEarliestVersion(entry.getPrimitiveId())-1,
                         progressMonitor.createSubTaskMonitor(1, true));
                 if (progressMonitor.isCancelled()) return;
             }
             nds = rdr.parseOsm(progressMonitor.createSubTaskMonitor(1, true));
-            addMissingIds(nds.allPrimitives());
+            for (OsmPrimitive p : nds.allPrimitives()) {
+                if (!p.isIncomplete()) addMissingIds(Collections.singleton(p));
+            }
         } finally {
             progressMonitor.finishTask();
@@ -246,5 +250,5 @@
                 RelationMember historyMember = historyMembers.get(i);
                 if (!currentMember.getRole().equals(historyMember.getRole())) return false;
-                if (currentMember.getMember().getPrimitiveId().equals(new SimplePrimitiveId(
+                if (!currentMember.getMember().getPrimitiveId().equals(new SimplePrimitiveId(
                         historyMember.getPrimitiveId(),historyMember.getPrimitiveType()))) return false;
             }
@@ -263,5 +267,5 @@
         //////////////////////////////////////////////////////////////////////////
         // Create commands to restore/update all affected objects
-        DataSetToCmd merger = new DataSetToCmd(nds,ds);
+        DataSetCommandMerger merger = new DataSetCommandMerger(nds,ds);
         LinkedList<Command> cmds = merger.getCommandList();
 
Index: applications/editors/josm/plugins/reverter/src/reverter/DataSetCommandMerger.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/DataSetCommandMerger.java	(revision 23273)
+++ applications/editors/josm/plugins/reverter/src/reverter/DataSetCommandMerger.java	(revision 23273)
@@ -0,0 +1,163 @@
+package reverter;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.data.conflict.Conflict;
+import org.openstreetmap.josm.data.conflict.ConflictCollection;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.RelationMember;
+import org.openstreetmap.josm.data.osm.Way;
+
+/**
+ * Modified {@see org.openstreetmap.josm.data.osm.DataSetMerger} that
+ * produces list of commands instead of directly merging layers.
+ *
+ */
+final class DataSetCommandMerger {
+
+    /** the collection of conflicts created during merging */
+    private final ConflictCollection conflicts = new ConflictCollection();
+    /** the source dataset where primitives are merged from */
+    private final DataSet sourceDataSet;
+    private final DataSet targetDataSet;
+
+    private final LinkedList<Command> cmds = new LinkedList<Command>();
+
+    /**
+     * constructor
+     */
+    public DataSetCommandMerger(DataSet sourceDataSet, DataSet targetDataSet) {
+        this.sourceDataSet = sourceDataSet;
+        this.targetDataSet = targetDataSet;
+        merge();
+    }
+
+    private OsmPrimitive getMergeTarget(OsmPrimitive mergeSource) {
+        OsmPrimitive p = targetDataSet.getPrimitiveById(mergeSource.getId(), mergeSource.getType());
+        if (p == null)
+            throw new IllegalStateException(tr("Missing merge target of type {0} with id {1}",
+                    mergeSource.getType(), mergeSource.getUniqueId()));
+        return p;
+    }
+
+    private void mergePrimitive(OsmPrimitive source, OsmPrimitive target, OsmPrimitive newTarget) {
+        newTarget.mergeFrom(source);
+        newTarget.setOsmId(target.getId(), (int)target.getVersion());
+        newTarget.setVisible(target.isVisible());
+        newTarget.setDeleted(false);
+    }
+
+    /**
+     * Merges the source node onto its target node.
+     *
+     * @param source the source way
+     */
+    private void mergeNode(Node source) {
+        if (source.isIncomplete()) return;
+        if (!source.isVisible()) return;
+        Node target = (Node)getMergeTarget(source);
+
+        Node newTarget = new Node(target);
+        mergePrimitive(source, target, newTarget);
+        cmds.add(new ChangeCommand(target,newTarget));
+    }
+
+    /**
+     * Merges the source way onto its target way.
+     *
+     * @param source the source way
+     * @throws IllegalStateException thrown if no target way can be found for the source way
+     * @throws IllegalStateException thrown if there isn't a target node for one of the nodes in the source way
+     *
+     */
+    private void mergeWay(Way source) throws IllegalStateException {
+        if (source.isIncomplete()) return;
+        if (!source.isVisible()) return;
+        Way target = (Way)getMergeTarget(source);
+
+        List<Node> newNodes = new ArrayList<Node>(source.getNodesCount());
+        for (Node sourceNode : source.getNodes()) {
+            Node targetNode = (Node)getMergeTarget(sourceNode);
+            if (targetNode.isDeleted() && sourceNode.isIncomplete()
+                    && !conflicts.hasConflictForMy(targetNode)) {
+                conflicts.add(new Conflict<OsmPrimitive>(targetNode, sourceNode, true));
+                Node undeletedTargetNode = new Node(targetNode);
+                undeletedTargetNode.setDeleted(false);
+                cmds.add(new ChangeCommand(targetNode,undeletedTargetNode));
+            }
+            newNodes.add(targetNode);
+        }
+        Way newTarget = new Way(target);
+        mergePrimitive(source, target, newTarget);
+        newTarget.setNodes(newNodes);
+        cmds.add(new ChangeCommand(target,newTarget));
+    }
+
+    /**
+     * Merges the source relation onto the corresponding target relation.
+     * @param source the source relation
+     * @throws IllegalStateException thrown if there is no corresponding target relation
+     * @throws IllegalStateException thrown if there isn't a corresponding target object for one of the relation
+     * members in source
+     */
+    private void mergeRelation(Relation source) throws IllegalStateException {
+        if (source.isIncomplete()) return;
+        if (!source.isVisible()) return;
+        Relation target = (Relation) getMergeTarget(source);
+        LinkedList<RelationMember> newMembers = new LinkedList<RelationMember>();
+        for (RelationMember sourceMember : source.getMembers()) {
+            OsmPrimitive targetMember = getMergeTarget(sourceMember.getMember());
+            if (targetMember.isDeleted() && sourceMember.getMember().isIncomplete()
+                    && !conflicts.hasConflictForMy(targetMember)) {
+                conflicts.add(new Conflict<OsmPrimitive>(targetMember, sourceMember.getMember(), true));
+                OsmPrimitive undeletedTargetMember;
+                switch(targetMember.getType()) {
+                case NODE: undeletedTargetMember = new Node((Node)targetMember); break;
+                case WAY: undeletedTargetMember = new Way((Way)targetMember); break;
+                case RELATION: undeletedTargetMember = new Relation((Relation)targetMember); break;
+                default: throw new AssertionError();
+                }
+                undeletedTargetMember.setDeleted(false);
+                cmds.add(new ChangeCommand(targetMember,undeletedTargetMember));
+            }
+            newMembers.add(new RelationMember(sourceMember.getRole(), targetMember));
+        }
+        Relation newRelation = new Relation(target);
+        mergePrimitive(source, target, newRelation);
+        newRelation.setMembers(newMembers);
+        cmds.add(new ChangeCommand(target,newRelation));
+    }
+    private void merge() {
+        for (Node node: sourceDataSet.getNodes()) {
+            mergeNode(node);
+        }
+        for (Way way: sourceDataSet.getWays()) {
+            mergeWay(way);
+        }
+        for (Relation relation: sourceDataSet.getRelations()) {
+            mergeRelation(relation);
+        }
+    }
+
+    public LinkedList<Command> getCommandList() {
+        return cmds;
+    }
+
+    /**
+     * replies the map of conflicts
+     *
+     * @return the map of conflicts
+     */
+    public ConflictCollection getConflicts() {
+        return conflicts;
+    }
+}
Index: applications/editors/josm/plugins/reverter/src/reverter/DataSetToCmd.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/DataSetToCmd.java	(revision 23192)
+++ 	(revision )
@@ -1,163 +1,0 @@
-package reverter;
-
-import static org.openstreetmap.josm.tools.I18n.tr;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.openstreetmap.josm.command.ChangeCommand;
-import org.openstreetmap.josm.command.Command;
-import org.openstreetmap.josm.data.conflict.Conflict;
-import org.openstreetmap.josm.data.conflict.ConflictCollection;
-import org.openstreetmap.josm.data.osm.DataSet;
-import org.openstreetmap.josm.data.osm.Node;
-import org.openstreetmap.josm.data.osm.OsmPrimitive;
-import org.openstreetmap.josm.data.osm.Relation;
-import org.openstreetmap.josm.data.osm.RelationMember;
-import org.openstreetmap.josm.data.osm.Way;
-
-/**
- * Modified {@see org.openstreetmap.josm.data.osm.DataSetMerger} that
- * produces list of commands instead of directly merging layers.
- *
- */
-final class DataSetToCmd {
-
-    /** the collection of conflicts created during merging */
-    private final ConflictCollection conflicts = new ConflictCollection();
-    /** the source dataset where primitives are merged from */
-    private final DataSet sourceDataSet;
-    private final DataSet targetDataSet;
-
-    private final LinkedList<Command> cmds = new LinkedList<Command>();
-
-    /**
-     * constructor
-     */
-    public DataSetToCmd(DataSet sourceDataSet, DataSet targetDataSet) {
-        this.sourceDataSet = sourceDataSet;
-        this.targetDataSet = targetDataSet;
-        merge();
-    }
-
-    private OsmPrimitive getMergeTarget(OsmPrimitive mergeSource) {
-        OsmPrimitive p = targetDataSet.getPrimitiveById(mergeSource.getId(), mergeSource.getType());
-        if (p == null)
-            throw new IllegalStateException(tr("Missing merge target of type {0} with id {1}",
-                    mergeSource.getType(), mergeSource.getUniqueId()));
-        return p;
-    }
-
-    private void mergePrimitive(OsmPrimitive source, OsmPrimitive target, OsmPrimitive newTarget) {
-        newTarget.mergeFrom(source);
-        newTarget.setOsmId(target.getId(), (int)target.getVersion());
-        newTarget.setVisible(target.isVisible());
-        newTarget.setDeleted(false);
-    }
-
-    /**
-     * Merges the source node onto its target node.
-     *
-     * @param source the source way
-     */
-    private void mergeNode(Node source) {
-        if (source.isIncomplete()) return;
-        if (!source.isVisible()) return;
-        Node target = (Node)getMergeTarget(source);
-
-        Node newTarget = new Node(target);
-        mergePrimitive(source, target, newTarget);
-        cmds.add(new ChangeCommand(target,newTarget));
-    }
-
-    /**
-     * Merges the source way onto its target way.
-     *
-     * @param source the source way
-     * @throws IllegalStateException thrown if no target way can be found for the source way
-     * @throws IllegalStateException thrown if there isn't a target node for one of the nodes in the source way
-     *
-     */
-    private void mergeWay(Way source) throws IllegalStateException {
-        if (source.isIncomplete()) return;
-        if (!source.isVisible()) return;
-        Way target = (Way)getMergeTarget(source);
-
-        List<Node> newNodes = new ArrayList<Node>(source.getNodesCount());
-        for (Node sourceNode : source.getNodes()) {
-            Node targetNode = (Node)getMergeTarget(sourceNode);
-            if (targetNode.isDeleted() && sourceNode.isIncomplete()
-                    && !conflicts.hasConflictForMy(targetNode)) {
-                conflicts.add(new Conflict<OsmPrimitive>(targetNode, sourceNode, true));
-                Node undeletedTargetNode = new Node(targetNode);
-                undeletedTargetNode.setDeleted(false);
-                cmds.add(new ChangeCommand(targetNode,undeletedTargetNode));
-            }
-            newNodes.add(targetNode);
-        }
-        Way newTarget = new Way(target);
-        mergePrimitive(source, target, newTarget);
-        newTarget.setNodes(newNodes);
-        cmds.add(new ChangeCommand(target,newTarget));
-    }
-
-    /**
-     * Merges the source relation onto the corresponding target relation.
-     * @param source the source relation
-     * @throws IllegalStateException thrown if there is no corresponding target relation
-     * @throws IllegalStateException thrown if there isn't a corresponding target object for one of the relation
-     * members in source
-     */
-    private void mergeRelation(Relation source) throws IllegalStateException {
-        if (source.isIncomplete()) return;
-        if (!source.isVisible()) return;
-        Relation target = (Relation) getMergeTarget(source);
-        LinkedList<RelationMember> newMembers = new LinkedList<RelationMember>();
-        for (RelationMember sourceMember : source.getMembers()) {
-            OsmPrimitive targetMember = getMergeTarget(sourceMember.getMember());
-            if (targetMember.isDeleted() && sourceMember.getMember().isIncomplete()
-                    && !conflicts.hasConflictForMy(targetMember)) {
-                conflicts.add(new Conflict<OsmPrimitive>(targetMember, sourceMember.getMember(), true));
-                OsmPrimitive undeletedTargetMember;
-                switch(targetMember.getType()) {
-                case NODE: undeletedTargetMember = new Node((Node)targetMember); break;
-                case WAY: undeletedTargetMember = new Way((Way)targetMember); break;
-                case RELATION: undeletedTargetMember = new Relation((Relation)targetMember); break;
-                default: throw new AssertionError();
-                }
-                undeletedTargetMember.setDeleted(false);
-                cmds.add(new ChangeCommand(targetMember,undeletedTargetMember));
-            }
-            newMembers.add(new RelationMember(sourceMember.getRole(), targetMember));
-        }
-        Relation newRelation = new Relation(target);
-        mergePrimitive(source, target, newRelation);
-        newRelation.setMembers(newMembers);
-        cmds.add(new ChangeCommand(target,newRelation));
-    }
-    private void merge() {
-        for (Node node: sourceDataSet.getNodes()) {
-            mergeNode(node);
-        }
-        for (Way way: sourceDataSet.getWays()) {
-            mergeWay(way);
-        }
-        for (Relation relation: sourceDataSet.getRelations()) {
-            mergeRelation(relation);
-        }
-    }
-
-    public LinkedList<Command> getCommandList() {
-        return cmds;
-    }
-
-    /**
-     * replies the map of conflicts
-     *
-     * @return the map of conflicts
-     */
-    public ConflictCollection getConflicts() {
-        return conflicts;
-    }
-}
Index: applications/editors/josm/plugins/reverter/src/reverter/MultiOsmReader.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/MultiOsmReader.java	(revision 23192)
+++ 	(revision )
@@ -1,565 +1,0 @@
-package reverter;
-
-import static org.openstreetmap.josm.tools.I18n.tr;
-
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.text.MessageFormat;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.parsers.SAXParserFactory;
-
-import org.openstreetmap.josm.data.Bounds;
-import org.openstreetmap.josm.data.coor.LatLon;
-import org.openstreetmap.josm.data.osm.DataSet;
-import org.openstreetmap.josm.data.osm.DataSource;
-import org.openstreetmap.josm.data.osm.Node;
-import org.openstreetmap.josm.data.osm.NodeData;
-import org.openstreetmap.josm.data.osm.OsmPrimitive;
-import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
-import org.openstreetmap.josm.data.osm.PrimitiveData;
-import org.openstreetmap.josm.data.osm.PrimitiveId;
-import org.openstreetmap.josm.data.osm.Relation;
-import org.openstreetmap.josm.data.osm.RelationData;
-import org.openstreetmap.josm.data.osm.RelationMember;
-import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
-import org.openstreetmap.josm.data.osm.Storage;
-import org.openstreetmap.josm.data.osm.User;
-import org.openstreetmap.josm.data.osm.Way;
-import org.openstreetmap.josm.data.osm.WayData;
-import org.openstreetmap.josm.gui.progress.ProgressMonitor;
-import org.openstreetmap.josm.io.IllegalDataException;
-import org.openstreetmap.josm.io.OsmDataParsingException;
-import org.openstreetmap.josm.io.OsmReader;
-import org.openstreetmap.josm.tools.DateUtils;
-import org.xml.sax.Attributes;
-import org.xml.sax.InputSource;
-import org.xml.sax.Locator;
-import org.xml.sax.SAXException;
-import org.xml.sax.helpers.DefaultHandler;
-
-/**
- * Modified {@see org.openstreetmap.josm.io.OsmReader} that can handle multiple XML streams.
- *
- */
-public class MultiOsmReader {
-    static private final Logger logger = Logger.getLogger(OsmReader.class.getName());
-
-    /**
-     * The dataset to add parsed objects to.
-     */
-    private DataSet ds = new DataSet();
-
-    /**
-     * Replies the parsed data set
-     *
-     * @return the parsed data set
-     */
-    public DataSet getDataSet() {
-        return ds;
-    }
-
-    /** the map from external ids to read OsmPrimitives. External ids are
-     * longs too, but in contrast to internal ids negative values are used
-     * to identify primitives unknown to the OSM server
-     */
-    private Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
-
-    /**
-     * constructor (for private use only)
-     *
-     * @see #parseDataSet(InputStream, DataSet, ProgressMonitor)
-     * @see #parseDataSetOsm(InputStream, DataSet, ProgressMonitor)
-     */
-    public MultiOsmReader() {
-        externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
-    }
-
-    /**
-     * Used as a temporary storage for relation members, before they
-     * are resolved into pointers to real objects.
-     */
-    private static class RelationMemberData {
-        public OsmPrimitiveType type;
-        public long id;
-        public String role;
-    }
-
-    /**
-     * Data structure for the remaining way objects
-     */
-    private Map<Long, Collection<Long>> ways = new HashMap<Long, Collection<Long>>();
-
-    /**
-     * Data structure for relation objects
-     */
-    private Map<Long, Collection<RelationMemberData>> relations = new HashMap<Long, Collection<RelationMemberData>>();
-
-    private class Parser extends DefaultHandler {
-        private Locator locator;
-
-        @Override
-        public void setDocumentLocator(Locator locator) {
-            this.locator = locator;
-        }
-
-        protected void throwException(String msg) throws OsmDataParsingException{
-            throw new OsmDataParsingException(msg).rememberLocation(locator);
-        }
-        /**
-         * The current osm primitive to be read.
-         */
-        private OsmPrimitive currentPrimitive;
-        private long currentExternalId;
-        private String generator;
-        private Storage<String> internedStrings = new Storage<String>();
-
-        // Memory optimization - see #2312
-        private String intern(String s) {
-            String result = internedStrings.get(s);
-            if (result == null) {
-                internedStrings.put(s);
-                return s;
-            } else
-                return result;
-        }
-
-        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
-
-            if (qName.equals("osm")) {
-                if (atts == null) {
-                    throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
-                }
-                String v = atts.getValue("version");
-                if (v == null) {
-                    throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
-                }
-                if (!(v.equals("0.5") || v.equals("0.6"))) {
-                    throwException(tr("Unsupported version: {0}", v));
-                }
-                // save generator attribute for later use when creating DataSource objects
-                generator = atts.getValue("generator");
-                ds.setVersion(v);
-
-            } else if (qName.equals("bounds")) {
-                // new style bounds.
-                String minlon = atts.getValue("minlon");
-                String minlat = atts.getValue("minlat");
-                String maxlon = atts.getValue("maxlon");
-                String maxlat = atts.getValue("maxlat");
-                String origin = atts.getValue("origin");
-                if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
-                    if (origin == null) {
-                        origin = generator;
-                    }
-                    Bounds bounds = new Bounds(
-                            new LatLon(Double.parseDouble(minlat), Double.parseDouble(minlon)),
-                            new LatLon(Double.parseDouble(maxlat), Double.parseDouble(maxlon)));
-                    DataSource src = new DataSource(bounds, origin);
-                    ds.dataSources.add(src);
-                } else {
-                    throwException(tr(
-                            "Missing manadatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
-                            minlon, minlat, maxlon, maxlat, origin
-                    ));
-                }
-
-                // ---- PARSING NODES AND WAYS ----
-
-            } else if (qName.equals("node")) {
-                NodeData nd = new NodeData();
-                nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
-                readCommon(atts, nd);
-                Node n = new Node(nd.getId(), nd.getVersion());
-                n.load(nd);
-                externalIdMap.put(nd.getPrimitiveId(), n);
-                currentPrimitive = n;
-                currentExternalId = nd.getUniqueId();
-            } else if (qName.equals("way")) {
-                WayData wd = new WayData();
-                readCommon(atts, wd);
-                Way w = new Way(wd.getId(), wd.getVersion());
-                w.load(wd);
-                externalIdMap.put(wd.getPrimitiveId(), w);
-                ways.put(wd.getUniqueId(), new ArrayList<Long>());
-                currentPrimitive = w;
-                currentExternalId = wd.getUniqueId();
-            } else if (qName.equals("nd")) {
-                Collection<Long> list = ways.get(currentExternalId);
-                if (list == null) {
-                    throwException(
-                            tr("Found XML element <nd> not as direct child of element <way>.")
-                    );
-                }
-                if (atts.getValue("ref") == null) {
-                    throwException(
-                            tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId())
-                    );
-                }
-                long id = getLong(atts, "ref");
-                if (id == 0) {
-                    throwException(
-                            tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
-                    );
-                }
-                if (currentPrimitive.isDeleted()) {
-                    logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId()));
-                } else {
-                    list.add(id);
-                }
-
-                // ---- PARSING RELATIONS ----
-
-            } else if (qName.equals("relation")) {
-                RelationData rd = new RelationData();
-                readCommon(atts, rd);
-                Relation r = new Relation(rd.getId(), rd.getVersion());
-                r.load(rd);
-                externalIdMap.put(rd.getPrimitiveId(), r);
-                relations.put(rd.getUniqueId(), new LinkedList<RelationMemberData>());
-                currentPrimitive = r;
-                currentExternalId = rd.getUniqueId();
-            } else if (qName.equals("member")) {
-                Collection<RelationMemberData> list = relations.get(currentExternalId);
-                if (list == null) {
-                    throwException(
-                            tr("Found XML element <member> not as direct child of element <relation>.")
-                    );
-                }
-                RelationMemberData emd = new RelationMemberData();
-                String value = atts.getValue("ref");
-                if (value == null) {
-                    throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId()));
-                }
-                try {
-                    emd.id = Long.parseLong(value);
-                } catch(NumberFormatException e) {
-                    throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value));
-                }
-                value = atts.getValue("type");
-                if (value == null) {
-                    throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId())));
-                }
-                try {
-                    emd.type = OsmPrimitiveType.fromApiTypeName(value);
-                } catch(IllegalArgumentException e) {
-                    throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value));
-                }
-                value = atts.getValue("role");
-                emd.role = value;
-
-                if (emd.id == 0) {
-                    throwException(tr("Incomplete <member> specification with ref=0"));
-                }
-
-                if (currentPrimitive.isDeleted()) {
-                    logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId()));
-                } else {
-                    list.add(emd);
-                }
-
-                // ---- PARSING TAGS (applicable to all objects) ----
-
-            } else if (qName.equals("tag")) {
-                String key = atts.getValue("k");
-                String value = atts.getValue("v");
-                currentPrimitive.put(intern(key), intern(value));
-
-            } else {
-                System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName));
-            }
-        }
-
-        private double getDouble(Attributes atts, String value) {
-            return Double.parseDouble(atts.getValue(value));
-        }
-
-        private User createUser(String uid, String name) throws SAXException {
-            if (uid == null) {
-                if (name == null)
-                    return null;
-                return User.createLocalUser(name);
-            }
-            try {
-                long id = Long.parseLong(uid);
-                return User.createOsmUser(id, name);
-            } catch(NumberFormatException e) {
-                throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
-            }
-            return null;
-        }
-        /**
-         * Read out the common attributes from atts and put them into this.current.
-         */
-        void readCommon(Attributes atts, PrimitiveData current) throws SAXException {
-            current.setId(getLong(atts, "id"));
-            if (current.getUniqueId() == 0) {
-                throwException(tr("Illegal object with ID=0."));
-            }
-
-            String time = atts.getValue("timestamp");
-            if (time != null && time.length() != 0) {
-                current.setTimestamp(DateUtils.fromString(time));
-            }
-
-            // user attribute added in 0.4 API
-            String user = atts.getValue("user");
-            // uid attribute added in 0.6 API
-            String uid = atts.getValue("uid");
-            current.setUser(createUser(uid, user));
-
-            // visible attribute added in 0.4 API
-            String visible = atts.getValue("visible");
-            if (visible != null) {
-                current.setVisible(Boolean.parseBoolean(visible));
-            }
-
-            String versionString = atts.getValue("version");
-            int version = 0;
-            if (versionString != null) {
-                try {
-                    version = Integer.parseInt(versionString);
-                } catch(NumberFormatException e) {
-                    throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
-                }
-                if (ds.getVersion().equals("0.6")){
-                    if (version <= 0 && current.getUniqueId() > 0) {
-                        throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
-                    } else if (version < 0 && current.getUniqueId() <= 0) {
-                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.6"));
-                        version = 0;
-                    }
-                } else if (ds.getVersion().equals("0.5")) {
-                    if (version <= 0 && current.getUniqueId() > 0) {
-                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
-                        version = 1;
-                    } else if (version < 0 && current.getUniqueId() <= 0) {
-                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.5"));
-                        version = 0;
-                    }
-                } else {
-                    // should not happen. API version has been checked before
-                    throwException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion()));
-                }
-            } else {
-                // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6
-                //
-                if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.6")) {
-                    throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId())));
-                } else if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
-                    // default version in 0.5 files for existing primitives
-                    System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
-                    version= 1;
-                } else if (current.getUniqueId() <= 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
-                    // default version in 0.5 files for new primitives, no warning necessary. This is
-                    // (was) legal in API 0.5
-                    version= 0;
-                }
-            }
-            current.setVersion(version);
-
-            String action = atts.getValue("action");
-            if (action == null) {
-                // do nothing
-            } else if (action.equals("delete")) {
-                current.setDeleted(true);
-                current.setModified(true);
-            } else if (action.equals("modify")) {
-                current.setModified(true);
-            }
-
-            String v = atts.getValue("changeset");
-            if (v == null) {
-                current.setChangesetId(0);
-            } else {
-                try {
-                    current.setChangesetId(Integer.parseInt(v));
-                } catch(NumberFormatException e) {
-                    if (current.getUniqueId() <= 0) {
-                        // for a new primitive we just log a warning
-                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
-                        current.setChangesetId(0);
-                    } else {
-                        // for an existing primitive this is a problem
-                        throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
-                    }
-                }
-                if (current.getChangesetId() <=0) {
-                    if (current.getUniqueId() <= 0) {
-                        // for a new primitive we just log a warning
-                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
-                        current.setChangesetId(0);
-                    } else {
-                        // for an existing primitive this is a problem
-                        throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
-                    }
-                }
-            }
-        }
-
-        private long getLong(Attributes atts, String name) throws SAXException {
-            String value = atts.getValue(name);
-            if (value == null) {
-                throwException(tr("Missing required attribute ''{0}''.",name));
-            }
-            try {
-                return Long.parseLong(value);
-            } catch(NumberFormatException e) {
-                throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value));
-            }
-            return 0; // should not happen
-        }
-    }
-
-    /**
-     * Processes the ways after parsing. Rebuilds the list of nodes of each way and
-     * adds the way to the dataset
-     *
-     * @throws IllegalDataException thrown if a data integrity problem is detected
-     */
-    protected void processWaysAfterParsing() throws IllegalDataException {
-        for (Long externalWayId: ways.keySet()) {
-            Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
-            List<Node> wayNodes = new ArrayList<Node>();
-            for (long id : ways.get(externalWayId)) {
-                Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
-                if (n == null) {
-                    if (id <= 0)
-                        throw new IllegalDataException (
-                                tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.",
-                                        externalWayId,
-                                        id));
-                    // create an incomplete node if necessary
-                    //
-                    n = (Node)ds.getPrimitiveById(id,OsmPrimitiveType.NODE);
-                    if (n == null) {
-                        n = new Node(id);
-                        ds.addPrimitive(n);
-                    }
-                }
-                if (n.isDeleted()) {
-                    logger.warning(tr("Deleted node {0} is part of way {1}", id, w.getId()));
-                } else {
-                    wayNodes.add(n);
-                }
-            }
-            w.setNodes(wayNodes);
-            if (w.hasIncompleteNodes()) {
-                if (logger.isLoggable(Level.FINE)) {
-                    logger.fine(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.",
-                            externalWayId, w.getNodesCount()));
-                }
-            }
-            ds.addPrimitive(w);
-        }
-    }
-
-    /**
-     * Processes the parsed nodes after parsing. Just adds them to
-     * the dataset
-     *
-     */
-    protected void processNodesAfterParsing() {
-        for (OsmPrimitive primitive: externalIdMap.values()) {
-            if (primitive instanceof Node) {
-                this.ds.addPrimitive(primitive);
-            }
-        }
-    }
-
-    /**
-     * Completes the parsed relations with its members.
-     *
-     * @throws IllegalDataException thrown if a data integrity problem is detected, i.e. if a
-     * relation member refers to a local primitive which wasn't available in the data
-     *
-     */
-    private void processRelationsAfterParsing() throws IllegalDataException {
-
-        // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset
-        for (Long externalRelationId : relations.keySet()) {
-            Relation relation = (Relation) externalIdMap.get(
-                    new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
-            );
-            ds.addPrimitive(relation);
-        }
-
-        for (Long externalRelationId : relations.keySet()) {
-            Relation relation = (Relation) externalIdMap.get(
-                    new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
-            );
-            List<RelationMember> relationMembers = new ArrayList<RelationMember>();
-            for (RelationMemberData rm : relations.get(externalRelationId)) {
-                OsmPrimitive primitive = null;
-
-                // lookup the member from the map of already created primitives
-                primitive = externalIdMap.get(new SimplePrimitiveId(rm.id, rm.type));
-
-                if (primitive == null) {
-                    if (rm.id <= 0)
-                        // relation member refers to a primitive with a negative id which was not
-                        // found in the data. This is always a data integrity problem and we abort
-                        // with an exception
-                        //
-                        throw new IllegalDataException(
-                                tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.",
-                                        externalRelationId,
-                                        rm.id));
-
-                    // member refers to OSM primitive which was not present in the parsed data
-                    // -> create a new incomplete primitive and add it to the dataset
-                    //
-                    primitive = ds.getPrimitiveById(rm.id, rm.type);
-                    if (primitive == null) {
-                        switch (rm.type) {
-                        case NODE:
-                            primitive = new Node(rm.id); break;
-                        case WAY:
-                            primitive = new Way(rm.id); break;
-                        case RELATION:
-                            primitive = new Relation(rm.id); break;
-                        default: throw new AssertionError(); // can't happen
-                        }
-
-                        ds.addPrimitive(primitive);
-                        externalIdMap.put(new SimplePrimitiveId(rm.id, rm.type), primitive);
-                    }
-                }
-                if (primitive.isDeleted()) {
-                    logger.warning(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId()));
-                } else {
-                    relationMembers.add(new RelationMember(rm.role, primitive));
-                }
-            }
-            relation.setMembers(relationMembers);
-        }
-    }
-
-    public void AddData(InputStream source) throws IllegalDataException {
-        try {
-            InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
-            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
-        } catch(ParserConfigurationException e) {
-            throw new IllegalDataException(e.getMessage(), e);
-        } catch(SAXException e) {
-            throw new IllegalDataException(e.getMessage(), e);
-        } catch(Exception e) {
-            throw new IllegalDataException(e);
-        }
-    }
-    public void ProcessData() throws IllegalDataException {
-        processNodesAfterParsing();
-        processWaysAfterParsing();
-        processRelationsAfterParsing();
-    }
-
-
-}
Index: applications/editors/josm/plugins/reverter/src/reverter/OsmServerMultiObjectReader.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/OsmServerMultiObjectReader.java	(revision 23192)
+++ applications/editors/josm/plugins/reverter/src/reverter/OsmServerMultiObjectReader.java	(revision 23273)
@@ -13,4 +13,6 @@
 import org.openstreetmap.josm.io.OsmTransferException;
 import org.xml.sax.SAXException;
+
+import reverter.corehacks.MultiOsmReader;
 
 public class OsmServerMultiObjectReader extends OsmServerReader {
Index: applications/editors/josm/plugins/reverter/src/reverter/corehacks/ChangesetDataSet.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/corehacks/ChangesetDataSet.java	(revision 23273)
+++ applications/editors/josm/plugins/reverter/src/reverter/corehacks/ChangesetDataSet.java	(revision 23273)
@@ -0,0 +1,230 @@
+// License: GPL. For details, see LICENSE file.
+package reverter.corehacks;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import org.openstreetmap.josm.data.osm.PrimitiveId;
+import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
+
+/**
+ * A ChangesetDataSet holds the content of a changeset.
+ */
+public class ChangesetDataSet {
+
+    public static enum ChangesetModificationType {
+        CREATED,
+        UPDATED,
+        DELETED
+    }
+
+    public static interface ChangesetDataSetEntry {
+        public ChangesetModificationType getModificationType();
+        public HistoryOsmPrimitive getPrimitive();
+        public int getEarliestVersion();
+    }
+
+    final private Map<PrimitiveId, Integer> earliestVersions = new HashMap<PrimitiveId, Integer>();
+    final private Map<PrimitiveId, HistoryOsmPrimitive> primitives = new HashMap<PrimitiveId, HistoryOsmPrimitive>();
+    final private Map<PrimitiveId, ChangesetModificationType> modificationTypes = new HashMap<PrimitiveId, ChangesetModificationType>();
+
+    /**
+     * Remembers a history primitive with the given modification type
+     *
+     * @param primitive the primitive. Must not be null.
+     * @param cmt the modification type. Must not be null.
+     * @throws IllegalArgumentException thrown if primitive is null
+     * @throws IllegalArgumentException thrown if cmt is null
+     */
+    public void put(HistoryOsmPrimitive primitive, ChangesetModificationType cmt) throws IllegalArgumentException{
+        CheckParameterUtil.ensureParameterNotNull(primitive,"primitive");
+        CheckParameterUtil.ensureParameterNotNull(cmt,"cmt");
+        PrimitiveId pid = primitive.getPrimitiveId();
+        if (primitives.containsKey(pid)) {
+            // Save only latest versions of primitives for reverter
+            if (primitive.getVersion() < primitives.get(pid).getVersion()) {
+                Integer earliest = earliestVersions.get(pid);
+                if (earliest == null || primitive.getVersion() < earliest) {
+                    earliestVersions.put(pid, (int)primitive.getVersion());
+                    if (cmt == ChangesetModificationType.CREATED)
+                        modificationTypes.put(pid, cmt);
+                }
+                return;
+            } else {
+                if (modificationTypes.get(pid) == ChangesetModificationType.CREATED) {
+                    cmt = ChangesetModificationType.CREATED;
+                }
+                if (earliestVersions.get(pid) == null) {
+                    earliestVersions.put(pid, (int)primitives.get(pid).getVersion());
+                }
+            }
+        }
+        primitives.put(pid, primitive);
+        modificationTypes.put(pid, cmt);
+    }
+
+    /**
+     * Replies true if the changeset content contains the object with primitive <code>id</code>.
+     * @param id the id.
+     * @return true if the changeset content contains the object with primitive <code>id</code>
+     */
+    public boolean contains(PrimitiveId id) {
+        if (id == null) return false;
+        return primitives.containsKey(id);
+    }
+
+    /**
+     * Replies the modification type for the object with id <code>id</code>. Replies null, if id is null or
+     * if the object with id <code>id</code> isn't in the changeset content.
+     *
+     * @param id the id
+     * @return the modification type
+     */
+    public ChangesetModificationType getModificationType(PrimitiveId id) {
+        if (!contains(id)) return null;
+        return modificationTypes.get(id);
+    }
+
+    public int getEarliestVersion(PrimitiveId id) {
+        Integer earliestVersion = earliestVersions.get(id);
+        if (earliestVersion == null) earliestVersion = (int)primitives.get(id).getVersion();
+        return earliestVersion;
+    }
+
+    /**
+     * Replies true if the primitive with id <code>id</code> was created in this
+     * changeset. Replies false, if id is null.
+     *
+     * @param id the id
+     * @return true if the primitive with id <code>id</code> was created in this
+     * changeset.
+     */
+    public boolean isCreated(PrimitiveId id) {
+        if (!contains(id)) return false;
+        return ChangesetModificationType.CREATED.equals(getModificationType(id));
+    }
+
+    /**
+     * Replies true if the primitive with id <code>id</code> was updated in this
+     * changeset. Replies false, if id is null.
+     *
+     * @param id the id
+     * @return true if the primitive with id <code>id</code> was updated in this
+     * changeset.
+     */
+    public boolean isUpdated(PrimitiveId id) {
+        if (!contains(id)) return false;
+        return ChangesetModificationType.UPDATED.equals(getModificationType(id));
+    }
+
+    /**
+     * Replies true if the primitive with id <code>id</code> was deleted in this
+     * changeset. Replies false, if id is null.
+     *
+     * @param id the id
+     * @return true if the primitive with id <code>id</code> was deleted in this
+     * changeset.
+     */
+    public boolean isDeleted(PrimitiveId id) {
+        if (!contains(id)) return false;
+        return ChangesetModificationType.DELETED.equals(getModificationType(id));
+    }
+
+    /**
+     * Replies the set of primitives with a specific modification type
+     *
+     * @param cmt the modification type. Must not be null.
+     * @return the set of primitives
+     * @throws IllegalArgumentException thrown if cmt is null
+     */
+    public Set<HistoryOsmPrimitive> getPrimitivesByModificationType(ChangesetModificationType cmt) throws IllegalArgumentException {
+        CheckParameterUtil.ensureParameterNotNull(cmt,"cmt");
+        HashSet<HistoryOsmPrimitive> ret = new HashSet<HistoryOsmPrimitive>();
+        for (Entry<PrimitiveId, ChangesetModificationType> entry: modificationTypes.entrySet()) {
+            if (entry.getValue().equals(cmt)) {
+                ret.add(primitives.get(entry.getKey()));
+            }
+        }
+        return ret;
+    }
+
+    /**
+     * Replies the number of objects in the dataset
+     *
+     * @return the number of objects in the dataset
+     */
+    public int size() {
+        return primitives.size();
+    }
+
+    /**
+     * Replies the {@see HistoryOsmPrimitive} with id <code>id</code> from this
+     * dataset. null, if there is no such primitive in the data set.
+     *
+     * @param id the id
+     * @return  the {@see HistoryOsmPrimitive} with id <code>id</code> from this
+     * dataset
+     */
+    public HistoryOsmPrimitive getPrimitive(PrimitiveId id) {
+        if (id == null)  return null;
+        return primitives.get(id);
+    }
+
+    public Iterator<ChangesetDataSetEntry> iterator() {
+        return new DefaultIterator();
+    }
+
+    private static class DefaultChangesetDataSetEntry implements ChangesetDataSetEntry {
+        private final ChangesetModificationType modificationType;
+        private final HistoryOsmPrimitive primitive;
+        private final int earliestVersion;
+
+        public DefaultChangesetDataSetEntry(ChangesetModificationType modificationType, HistoryOsmPrimitive primitive, int earliestVersion) {
+            this.modificationType = modificationType;
+            this.primitive = primitive;
+            this.earliestVersion = earliestVersion;
+        }
+
+        public ChangesetModificationType getModificationType() {
+            return modificationType;
+        }
+
+        public HistoryOsmPrimitive getPrimitive() {
+            return primitive;
+        }
+
+        public int getEarliestVersion() {
+            return earliestVersion;
+        }
+}
+
+    private class DefaultIterator implements Iterator<ChangesetDataSetEntry> {
+        private final Iterator<Entry<PrimitiveId, ChangesetModificationType>> typeIterator;
+
+        public DefaultIterator() {
+            typeIterator = modificationTypes.entrySet().iterator();
+        }
+
+        public boolean hasNext() {
+            return typeIterator.hasNext();
+        }
+
+        public ChangesetDataSetEntry next() {
+            Entry<PrimitiveId, ChangesetModificationType> next = typeIterator.next();
+            ChangesetModificationType type = next.getValue();
+            HistoryOsmPrimitive primitive = primitives.get(next.getKey());
+            Integer earliestVersion = earliestVersions.get(next.getKey());
+            if (earliestVersion == null) earliestVersion = (int)primitive.getVersion();
+            return new DefaultChangesetDataSetEntry(type, primitive, earliestVersion);
+        }
+
+        public void remove() {
+            throw new UnsupportedOperationException();
+        }
+    }
+}
Index: applications/editors/josm/plugins/reverter/src/reverter/corehacks/MultiOsmReader.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/corehacks/MultiOsmReader.java	(revision 23273)
+++ applications/editors/josm/plugins/reverter/src/reverter/corehacks/MultiOsmReader.java	(revision 23273)
@@ -0,0 +1,565 @@
+package reverter.corehacks;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.DataSource;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.NodeData;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
+import org.openstreetmap.josm.data.osm.PrimitiveData;
+import org.openstreetmap.josm.data.osm.PrimitiveId;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.RelationData;
+import org.openstreetmap.josm.data.osm.RelationMember;
+import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
+import org.openstreetmap.josm.data.osm.Storage;
+import org.openstreetmap.josm.data.osm.User;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.WayData;
+import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.IllegalDataException;
+import org.openstreetmap.josm.io.OsmDataParsingException;
+import org.openstreetmap.josm.io.OsmReader;
+import org.openstreetmap.josm.tools.DateUtils;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Modified {@see org.openstreetmap.josm.io.OsmReader} that can handle multiple XML streams.
+ *
+ */
+public class MultiOsmReader {
+    static private final Logger logger = Logger.getLogger(OsmReader.class.getName());
+
+    /**
+     * The dataset to add parsed objects to.
+     */
+    private final DataSet ds = new DataSet();
+
+    /**
+     * Replies the parsed data set
+     *
+     * @return the parsed data set
+     */
+    public DataSet getDataSet() {
+        return ds;
+    }
+
+    /** the map from external ids to read OsmPrimitives. External ids are
+     * longs too, but in contrast to internal ids negative values are used
+     * to identify primitives unknown to the OSM server
+     */
+    private Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
+
+    /**
+     * constructor (for private use only)
+     *
+     * @see #parseDataSet(InputStream, DataSet, ProgressMonitor)
+     * @see #parseDataSetOsm(InputStream, DataSet, ProgressMonitor)
+     */
+    public MultiOsmReader() {
+        externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>();
+    }
+
+    /**
+     * Used as a temporary storage for relation members, before they
+     * are resolved into pointers to real objects.
+     */
+    private static class RelationMemberData {
+        public OsmPrimitiveType type;
+        public long id;
+        public String role;
+    }
+
+    /**
+     * Data structure for the remaining way objects
+     */
+    private final Map<Long, Collection<Long>> ways = new HashMap<Long, Collection<Long>>();
+
+    /**
+     * Data structure for relation objects
+     */
+    private final Map<Long, Collection<RelationMemberData>> relations = new HashMap<Long, Collection<RelationMemberData>>();
+
+    private class Parser extends DefaultHandler {
+        private Locator locator;
+
+        @Override
+        public void setDocumentLocator(Locator locator) {
+            this.locator = locator;
+        }
+
+        protected void throwException(String msg) throws OsmDataParsingException{
+            throw new OsmDataParsingException(msg).rememberLocation(locator);
+        }
+        /**
+         * The current osm primitive to be read.
+         */
+        private OsmPrimitive currentPrimitive;
+        private long currentExternalId;
+        private String generator;
+        private final Storage<String> internedStrings = new Storage<String>();
+
+        // Memory optimization - see #2312
+        private String intern(String s) {
+            String result = internedStrings.get(s);
+            if (result == null) {
+                internedStrings.put(s);
+                return s;
+            } else
+                return result;
+        }
+
+        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
+
+            if (qName.equals("osm")) {
+                if (atts == null) {
+                    throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
+                }
+                String v = atts.getValue("version");
+                if (v == null) {
+                    throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
+                }
+                if (!(v.equals("0.5") || v.equals("0.6"))) {
+                    throwException(tr("Unsupported version: {0}", v));
+                }
+                // save generator attribute for later use when creating DataSource objects
+                generator = atts.getValue("generator");
+                ds.setVersion(v);
+
+            } else if (qName.equals("bounds")) {
+                // new style bounds.
+                String minlon = atts.getValue("minlon");
+                String minlat = atts.getValue("minlat");
+                String maxlon = atts.getValue("maxlon");
+                String maxlat = atts.getValue("maxlat");
+                String origin = atts.getValue("origin");
+                if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
+                    if (origin == null) {
+                        origin = generator;
+                    }
+                    Bounds bounds = new Bounds(
+                            new LatLon(Double.parseDouble(minlat), Double.parseDouble(minlon)),
+                            new LatLon(Double.parseDouble(maxlat), Double.parseDouble(maxlon)));
+                    DataSource src = new DataSource(bounds, origin);
+                    ds.dataSources.add(src);
+                } else {
+                    throwException(tr(
+                            "Missing manadatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
+                            minlon, minlat, maxlon, maxlat, origin
+                    ));
+                }
+
+                // ---- PARSING NODES AND WAYS ----
+
+            } else if (qName.equals("node")) {
+                NodeData nd = new NodeData();
+                nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
+                readCommon(atts, nd);
+                Node n = new Node(nd.getId(), nd.getVersion());
+                n.load(nd);
+                externalIdMap.put(nd.getPrimitiveId(), n);
+                currentPrimitive = n;
+                currentExternalId = nd.getUniqueId();
+            } else if (qName.equals("way")) {
+                WayData wd = new WayData();
+                readCommon(atts, wd);
+                Way w = new Way(wd.getId(), wd.getVersion());
+                w.load(wd);
+                externalIdMap.put(wd.getPrimitiveId(), w);
+                ways.put(wd.getUniqueId(), new ArrayList<Long>());
+                currentPrimitive = w;
+                currentExternalId = wd.getUniqueId();
+            } else if (qName.equals("nd")) {
+                Collection<Long> list = ways.get(currentExternalId);
+                if (list == null) {
+                    throwException(
+                            tr("Found XML element <nd> not as direct child of element <way>.")
+                    );
+                }
+                if (atts.getValue("ref") == null) {
+                    throwException(
+                            tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId())
+                    );
+                }
+                long id = getLong(atts, "ref");
+                if (id == 0) {
+                    throwException(
+                            tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
+                    );
+                }
+                if (currentPrimitive.isDeleted()) {
+                    logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId()));
+                } else {
+                    list.add(id);
+                }
+
+                // ---- PARSING RELATIONS ----
+
+            } else if (qName.equals("relation")) {
+                RelationData rd = new RelationData();
+                readCommon(atts, rd);
+                Relation r = new Relation(rd.getId(), rd.getVersion());
+                r.load(rd);
+                externalIdMap.put(rd.getPrimitiveId(), r);
+                relations.put(rd.getUniqueId(), new LinkedList<RelationMemberData>());
+                currentPrimitive = r;
+                currentExternalId = rd.getUniqueId();
+            } else if (qName.equals("member")) {
+                Collection<RelationMemberData> list = relations.get(currentExternalId);
+                if (list == null) {
+                    throwException(
+                            tr("Found XML element <member> not as direct child of element <relation>.")
+                    );
+                }
+                RelationMemberData emd = new RelationMemberData();
+                String value = atts.getValue("ref");
+                if (value == null) {
+                    throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId()));
+                }
+                try {
+                    emd.id = Long.parseLong(value);
+                } catch(NumberFormatException e) {
+                    throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value));
+                }
+                value = atts.getValue("type");
+                if (value == null) {
+                    throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId())));
+                }
+                try {
+                    emd.type = OsmPrimitiveType.fromApiTypeName(value);
+                } catch(IllegalArgumentException e) {
+                    throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value));
+                }
+                value = atts.getValue("role");
+                emd.role = value;
+
+                if (emd.id == 0) {
+                    throwException(tr("Incomplete <member> specification with ref=0"));
+                }
+
+                if (currentPrimitive.isDeleted()) {
+                    logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId()));
+                } else {
+                    list.add(emd);
+                }
+
+                // ---- PARSING TAGS (applicable to all objects) ----
+
+            } else if (qName.equals("tag")) {
+                String key = atts.getValue("k");
+                String value = atts.getValue("v");
+                currentPrimitive.put(intern(key), intern(value));
+
+            } else {
+                System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName));
+            }
+        }
+
+        private double getDouble(Attributes atts, String value) {
+            return Double.parseDouble(atts.getValue(value));
+        }
+
+        private User createUser(String uid, String name) throws SAXException {
+            if (uid == null) {
+                if (name == null)
+                    return null;
+                return User.createLocalUser(name);
+            }
+            try {
+                long id = Long.parseLong(uid);
+                return User.createOsmUser(id, name);
+            } catch(NumberFormatException e) {
+                throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
+            }
+            return null;
+        }
+        /**
+         * Read out the common attributes from atts and put them into this.current.
+         */
+        void readCommon(Attributes atts, PrimitiveData current) throws SAXException {
+            current.setId(getLong(atts, "id"));
+            if (current.getUniqueId() == 0) {
+                throwException(tr("Illegal object with ID=0."));
+            }
+
+            String time = atts.getValue("timestamp");
+            if (time != null && time.length() != 0) {
+                current.setTimestamp(DateUtils.fromString(time));
+            }
+
+            // user attribute added in 0.4 API
+            String user = atts.getValue("user");
+            // uid attribute added in 0.6 API
+            String uid = atts.getValue("uid");
+            current.setUser(createUser(uid, user));
+
+            // visible attribute added in 0.4 API
+            String visible = atts.getValue("visible");
+            if (visible != null) {
+                current.setVisible(Boolean.parseBoolean(visible));
+            }
+
+            String versionString = atts.getValue("version");
+            int version = 0;
+            if (versionString != null) {
+                try {
+                    version = Integer.parseInt(versionString);
+                } catch(NumberFormatException e) {
+                    throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
+                }
+                if (ds.getVersion().equals("0.6")){
+                    if (version <= 0 && current.getUniqueId() > 0) {
+                        throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString));
+                    } else if (version < 0 && current.getUniqueId() <= 0) {
+                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.6"));
+                        version = 0;
+                    }
+                } else if (ds.getVersion().equals("0.5")) {
+                    if (version <= 0 && current.getUniqueId() > 0) {
+                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
+                        version = 1;
+                    } else if (version < 0 && current.getUniqueId() <= 0) {
+                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.5"));
+                        version = 0;
+                    }
+                } else {
+                    // should not happen. API version has been checked before
+                    throwException(tr("Unknown or unsupported API version. Got {0}.", ds.getVersion()));
+                }
+            } else {
+                // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6
+                //
+                if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.6")) {
+                    throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId())));
+                } else if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
+                    // default version in 0.5 files for existing primitives
+                    System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5"));
+                    version= 1;
+                } else if (current.getUniqueId() <= 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) {
+                    // default version in 0.5 files for new primitives, no warning necessary. This is
+                    // (was) legal in API 0.5
+                    version= 0;
+                }
+            }
+            current.setVersion(version);
+
+            String action = atts.getValue("action");
+            if (action == null) {
+                // do nothing
+            } else if (action.equals("delete")) {
+                current.setDeleted(true);
+                current.setModified(current.isVisible());
+            } else if (action.equals("modify")) {
+                current.setModified(true);
+            }
+
+            String v = atts.getValue("changeset");
+            if (v == null) {
+                current.setChangesetId(0);
+            } else {
+                try {
+                    current.setChangesetId(Integer.parseInt(v));
+                } catch(NumberFormatException e) {
+                    if (current.getUniqueId() <= 0) {
+                        // for a new primitive we just log a warning
+                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
+                        current.setChangesetId(0);
+                    } else {
+                        // for an existing primitive this is a problem
+                        throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
+                    }
+                }
+                if (current.getChangesetId() <=0) {
+                    if (current.getUniqueId() <= 0) {
+                        // for a new primitive we just log a warning
+                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId()));
+                        current.setChangesetId(0);
+                    } else {
+                        // for an existing primitive this is a problem
+                        throwException(tr("Illegal value for attribute ''changeset''. Got {0}.", v));
+                    }
+                }
+            }
+        }
+
+        private long getLong(Attributes atts, String name) throws SAXException {
+            String value = atts.getValue(name);
+            if (value == null) {
+                throwException(tr("Missing required attribute ''{0}''.",name));
+            }
+            try {
+                return Long.parseLong(value);
+            } catch(NumberFormatException e) {
+                throwException(tr("Illegal long value for attribute ''{0}''. Got ''{1}''.",name, value));
+            }
+            return 0; // should not happen
+        }
+    }
+
+    /**
+     * Processes the ways after parsing. Rebuilds the list of nodes of each way and
+     * adds the way to the dataset
+     *
+     * @throws IllegalDataException thrown if a data integrity problem is detected
+     */
+    protected void processWaysAfterParsing() throws IllegalDataException {
+        for (Long externalWayId: ways.keySet()) {
+            Way w = (Way)externalIdMap.get(new SimplePrimitiveId(externalWayId, OsmPrimitiveType.WAY));
+            List<Node> wayNodes = new ArrayList<Node>();
+            for (long id : ways.get(externalWayId)) {
+                Node n = (Node)externalIdMap.get(new SimplePrimitiveId(id, OsmPrimitiveType.NODE));
+                if (n == null) {
+                    if (id <= 0)
+                        throw new IllegalDataException (
+                                tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.",
+                                        externalWayId,
+                                        id));
+                    // create an incomplete node if necessary
+                    //
+                    n = (Node)ds.getPrimitiveById(id,OsmPrimitiveType.NODE);
+                    if (n == null) {
+                        n = new Node(id);
+                        ds.addPrimitive(n);
+                    }
+                }
+                if (n.isDeleted()) {
+                    logger.warning(tr("Deleted node {0} is part of way {1}", id, w.getId()));
+                } else {
+                    wayNodes.add(n);
+                }
+            }
+            w.setNodes(wayNodes);
+            if (w.hasIncompleteNodes()) {
+                if (logger.isLoggable(Level.FINE)) {
+                    logger.fine(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.",
+                            externalWayId, w.getNodesCount()));
+                }
+            }
+            ds.addPrimitive(w);
+        }
+    }
+
+    /**
+     * Processes the parsed nodes after parsing. Just adds them to
+     * the dataset
+     *
+     */
+    protected void processNodesAfterParsing() {
+        for (OsmPrimitive primitive: externalIdMap.values()) {
+            if (primitive instanceof Node) {
+                this.ds.addPrimitive(primitive);
+            }
+        }
+    }
+
+    /**
+     * Completes the parsed relations with its members.
+     *
+     * @throws IllegalDataException thrown if a data integrity problem is detected, i.e. if a
+     * relation member refers to a local primitive which wasn't available in the data
+     *
+     */
+    private void processRelationsAfterParsing() throws IllegalDataException {
+
+        // First add all relations to make sure that when relation reference other relation, the referenced will be already in dataset
+        for (Long externalRelationId : relations.keySet()) {
+            Relation relation = (Relation) externalIdMap.get(
+                    new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
+            );
+            ds.addPrimitive(relation);
+        }
+
+        for (Long externalRelationId : relations.keySet()) {
+            Relation relation = (Relation) externalIdMap.get(
+                    new SimplePrimitiveId(externalRelationId, OsmPrimitiveType.RELATION)
+            );
+            List<RelationMember> relationMembers = new ArrayList<RelationMember>();
+            for (RelationMemberData rm : relations.get(externalRelationId)) {
+                OsmPrimitive primitive = null;
+
+                // lookup the member from the map of already created primitives
+                primitive = externalIdMap.get(new SimplePrimitiveId(rm.id, rm.type));
+
+                if (primitive == null) {
+                    if (rm.id <= 0)
+                        // relation member refers to a primitive with a negative id which was not
+                        // found in the data. This is always a data integrity problem and we abort
+                        // with an exception
+                        //
+                        throw new IllegalDataException(
+                                tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.",
+                                        externalRelationId,
+                                        rm.id));
+
+                    // member refers to OSM primitive which was not present in the parsed data
+                    // -> create a new incomplete primitive and add it to the dataset
+                    //
+                    primitive = ds.getPrimitiveById(rm.id, rm.type);
+                    if (primitive == null) {
+                        switch (rm.type) {
+                        case NODE:
+                            primitive = new Node(rm.id); break;
+                        case WAY:
+                            primitive = new Way(rm.id); break;
+                        case RELATION:
+                            primitive = new Relation(rm.id); break;
+                        default: throw new AssertionError(); // can't happen
+                        }
+
+                        ds.addPrimitive(primitive);
+                        externalIdMap.put(new SimplePrimitiveId(rm.id, rm.type), primitive);
+                    }
+                }
+                if (primitive.isDeleted()) {
+                    logger.warning(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId()));
+                } else {
+                    relationMembers.add(new RelationMember(rm.role, primitive));
+                }
+            }
+            relation.setMembers(relationMembers);
+        }
+    }
+
+    public void AddData(InputStream source) throws IllegalDataException {
+        try {
+            InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
+            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
+        } catch(ParserConfigurationException e) {
+            throw new IllegalDataException(e.getMessage(), e);
+        } catch(SAXException e) {
+            throw new IllegalDataException(e.getMessage(), e);
+        } catch(Exception e) {
+            throw new IllegalDataException(e);
+        }
+    }
+    public void ProcessData() throws IllegalDataException {
+        processNodesAfterParsing();
+        processWaysAfterParsing();
+        processRelationsAfterParsing();
+    }
+
+
+}
Index: applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmChangesetContentParser.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmChangesetContentParser.java	(revision 23273)
+++ applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmChangesetContentParser.java	(revision 23273)
@@ -0,0 +1,334 @@
+// License: GPL. Copyright 2007 by Immanuel Scholz and others
+package reverter.corehacks;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParserFactory;
+
+import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
+import org.openstreetmap.josm.data.osm.history.HistoryNode;
+import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
+import org.openstreetmap.josm.data.osm.history.HistoryRelation;
+import org.openstreetmap.josm.data.osm.history.HistoryWay;
+import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
+import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.OsmDataParsingException;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.DateUtils;
+import org.xml.sax.Attributes;
+import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+import org.xml.sax.helpers.DefaultHandler;
+
+import reverter.corehacks.ChangesetDataSet.ChangesetModificationType;
+
+/**
+ * Parser for OSM changeset content.
+ *
+ */
+public class OsmChangesetContentParser {
+
+    private final InputSource source;
+    private final ChangesetDataSet data;
+
+    private class Parser extends DefaultHandler {
+
+        /** the current primitive to be read */
+        private HistoryOsmPrimitive currentPrimitive;
+        /** the current change modification type */
+        private ChangesetDataSet.ChangesetModificationType currentModificationType;
+
+        private Locator locator;
+
+        @Override
+        public void setDocumentLocator(Locator locator) {
+            this.locator = locator;
+        }
+
+        protected void throwException(String message) throws OsmDataParsingException {
+            throw new OsmDataParsingException(
+                    message
+            ).rememberLocation(locator);
+        }
+
+        protected void throwException(Exception e) throws OsmDataParsingException {
+            throw new OsmDataParsingException(
+                    e
+            ).rememberLocation(locator);
+        }
+
+        protected long getMandatoryAttributeLong(Attributes attr, String name) throws SAXException{
+            String v = attr.getValue(name);
+            if (v == null) {
+                throwException(tr("Missing mandatory attribute ''{0}''.", name));
+            }
+            Long l = 0l;
+            try {
+                l = Long.parseLong(v);
+            } catch(NumberFormatException e) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v));
+            }
+            if (l < 0) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v));
+            }
+            return l;
+        }
+
+        protected long getAttributeLong(Attributes attr, String name, long defaultValue) throws SAXException{
+            String v = attr.getValue(name);
+            if (v == null)
+                return defaultValue;
+            Long l = 0l;
+            try {
+                l = Long.parseLong(v);
+            } catch(NumberFormatException e) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v));
+            }
+            if (l < 0) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v));
+            }
+            return l;
+        }
+
+        protected Double getMandatoryAttributeDouble(Attributes attr, String name) throws SAXException{
+            String v = attr.getValue(name);
+            if (v == null) {
+                throwException(tr("Missing mandatory attribute ''{0}''.", name));
+            }
+            double d = 0.0;
+            try {
+                d = Double.parseDouble(v);
+            } catch(NumberFormatException e) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type double. Got ''{1}''.", name, v));
+            }
+            return d;
+        }
+
+        protected String getMandatoryAttributeString(Attributes attr, String name) throws SAXException{
+            String v = attr.getValue(name);
+            if (v == null) {
+                throwException(tr("Missing mandatory attribute ''{0}''.", name));
+            }
+            return v;
+        }
+
+        protected String getAttributeString(Attributes attr, String name, String defaultValue) {
+            String v = attr.getValue(name);
+            if (v == null)
+                return defaultValue;
+            return v;
+        }
+
+        protected boolean getMandatoryAttributeBoolean(Attributes attr, String name) throws SAXException{
+            String v = attr.getValue(name);
+            if (v == null) {
+                throwException(tr("Missing mandatory attribute ''{0}''.", name));
+            }
+            if ("true".equals(v)) return true;
+            if ("false".equals(v)) return false;
+            throwException(tr("Illegal value for mandatory attribute ''{0}'' of type boolean. Got ''{1}''.", name, v));
+            // not reached
+            return false;
+        }
+
+        protected  HistoryOsmPrimitive createPrimitive(Attributes atts, OsmPrimitiveType type) throws SAXException {
+            long id = getMandatoryAttributeLong(atts,"id");
+            long version = getMandatoryAttributeLong(atts,"version");
+            long changesetId = getMandatoryAttributeLong(atts,"changeset");
+            boolean visible= getMandatoryAttributeBoolean(atts, "visible");
+            long uid = getAttributeLong(atts, "uid",-1);
+            String user = getAttributeString(atts, "user", tr("<anonymous>"));
+            String v = getMandatoryAttributeString(atts, "timestamp");
+            Date timestamp = DateUtils.fromString(v);
+            HistoryOsmPrimitive primitive = null;
+            if (type.equals(OsmPrimitiveType.NODE)) {
+                double lat = getMandatoryAttributeDouble(atts, "lat");
+                double lon = getMandatoryAttributeDouble(atts, "lon");
+                primitive = new HistoryNode(
+                        id,version,visible,user,uid,changesetId,timestamp, new LatLon(lat,lon)
+                );
+
+            } else if (type.equals(OsmPrimitiveType.WAY)) {
+                primitive = new HistoryWay(
+                        id,version,visible,user,uid,changesetId,timestamp
+                );
+            }if (type.equals(OsmPrimitiveType.RELATION)) {
+                primitive = new HistoryRelation(
+                        id,version,visible,user,uid,changesetId,timestamp
+                );
+            }
+            return primitive;
+        }
+
+        protected void startNode(Attributes atts) throws SAXException {
+            currentPrimitive= createPrimitive(atts, OsmPrimitiveType.NODE);
+        }
+
+        protected void startWay(Attributes atts) throws SAXException {
+            currentPrimitive= createPrimitive(atts, OsmPrimitiveType.WAY);
+        }
+        protected void startRelation(Attributes atts) throws SAXException {
+            currentPrimitive= createPrimitive(atts, OsmPrimitiveType.RELATION);
+        }
+
+        protected void handleTag(Attributes atts) throws SAXException {
+            String key= getMandatoryAttributeString(atts, "k");
+            String value= getMandatoryAttributeString(atts, "v");
+            currentPrimitive.put(key,value);
+        }
+
+        protected void handleNodeReference(Attributes atts) throws SAXException {
+            long ref = getMandatoryAttributeLong(atts, "ref");
+            ((HistoryWay)currentPrimitive).addNode(ref);
+        }
+
+        protected void handleMember(Attributes atts) throws SAXException {
+            long ref = getMandatoryAttributeLong(atts, "ref");
+            String v = getMandatoryAttributeString(atts, "type");
+            OsmPrimitiveType type = null;
+            try {
+                type = OsmPrimitiveType.fromApiTypeName(v);
+            } catch(IllegalArgumentException e) {
+                throwException(tr("Illegal value for mandatory attribute ''{0}'' of type OsmPrimitiveType. Got ''{1}''.", "type", v));
+            }
+            String role = getMandatoryAttributeString(atts, "role");
+            org.openstreetmap.josm.data.osm.history.RelationMember member = new org.openstreetmap.josm.data.osm.history.RelationMember(role, type,ref);
+            ((HistoryRelation)currentPrimitive).addMember(member);
+        }
+
+        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
+            if (qName.equals("node")) {
+                startNode(atts);
+            } else if (qName.equals("way")) {
+                startWay(atts);
+            } else if (qName.equals("relation")) {
+                startRelation(atts);
+            } else if (qName.equals("tag")) {
+                handleTag(atts);
+            } else if (qName.equals("nd")) {
+                handleNodeReference(atts);
+            } else if (qName.equals("member")) {
+                handleMember(atts);
+            } else if (qName.equals("osmChange")) {
+                // do nothing
+            } else if (qName.equals("create")) {
+                currentModificationType = ChangesetModificationType.CREATED;
+            } else if (qName.equals("modify")) {
+                currentModificationType = ChangesetModificationType.UPDATED;
+            } else if (qName.equals("delete")) {
+                currentModificationType = ChangesetModificationType.DELETED;
+            } else {
+                System.err.println(tr("Warning: unsupported start element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
+            }
+        }
+
+        @Override
+        public void endElement(String uri, String localName, String qName) throws SAXException {
+            if (qName.equals("node")
+                    || qName.equals("way")
+                    || qName.equals("relation")) {
+                if (currentModificationType == null) {
+                    throwException(tr("Illegal document structure. Found node, way, or relation outside of ''create'', ''modify'', or ''delete''."));
+                }
+                data.put(currentPrimitive, currentModificationType);
+            } else if (qName.equals("osmChange")) {
+                // do nothing
+            } else if (qName.equals("create")) {
+                currentModificationType = null;
+            } else if (qName.equals("modify")) {
+                currentModificationType = null;
+            } else if (qName.equals("delete")) {
+                currentModificationType = null;
+            } else if (qName.equals("tag")) {
+                // do nothing
+            } else if (qName.equals("nd")) {
+                // do nothing
+            } else if (qName.equals("member")) {
+                // do nothing
+            } else {
+                System.err.println(tr("Warning: unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
+            }
+        }
+
+        @Override
+        public void error(SAXParseException e) throws SAXException {
+            throwException(e);
+        }
+
+        @Override
+        public void fatalError(SAXParseException e) throws SAXException {
+            throwException(e);
+        }
+    }
+
+    /**
+     * Create a parser
+     *
+     * @param source the input stream with the changeset content as XML document. Must not be null.
+     * @throws IllegalArgumentException thrown if source is null.
+     */
+    public OsmChangesetContentParser(InputStream source) throws UnsupportedEncodingException {
+        CheckParameterUtil.ensureParameterNotNull(source, "source");
+        this.source = new InputSource(new InputStreamReader(source, "UTF-8"));
+        data = new ChangesetDataSet();
+    }
+
+    public OsmChangesetContentParser(String source) {
+        CheckParameterUtil.ensureParameterNotNull(source, "source");
+        this.source = new InputSource(new StringReader(source));
+        data = new ChangesetDataSet();
+    }
+
+    /**
+     * Parses the content
+     *
+     * @param progressMonitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE}
+     * if null
+     * @return the parsed data
+     * @throws OsmDataParsingException thrown if something went wrong. Check for chained
+     * exceptions.
+     */
+    public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws OsmDataParsingException {
+        if (progressMonitor == null) {
+            progressMonitor = NullProgressMonitor.INSTANCE;
+        }
+        try {
+            progressMonitor.beginTask("");
+            progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
+            SAXParserFactory.newInstance().newSAXParser().parse(source, new Parser());
+        } catch(OsmDataParsingException e){
+            throw e;
+        } catch (ParserConfigurationException e) {
+            throw new OsmDataParsingException(e);
+        } catch(SAXException e) {
+            throw new OsmDataParsingException(e);
+        } catch(IOException e) {
+            throw new OsmDataParsingException(e);
+        } finally {
+            progressMonitor.finishTask();
+        }
+        return data;
+    }
+
+    /**
+     * Parses the content from the input source
+     *
+     * @return the parsed data
+     * @throws OsmDataParsingException thrown if something went wrong. Check for chained
+     * exceptions.
+     */
+    public ChangesetDataSet parse() throws OsmDataParsingException {
+        return parse(null);
+    }
+}
Index: applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmServerChangesetReader.java
===================================================================
--- applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmServerChangesetReader.java	(revision 23273)
+++ applications/editors/josm/plugins/reverter/src/reverter/corehacks/OsmServerChangesetReader.java	(revision 23273)
@@ -0,0 +1,202 @@
+// License: GPL. For details, see LICENSE file.
+package reverter.corehacks;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+import static org.openstreetmap.josm.tools.I18n.trn;
+
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.openstreetmap.josm.data.osm.Changeset;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
+import org.openstreetmap.josm.gui.progress.ProgressMonitor;
+import org.openstreetmap.josm.io.ChangesetQuery;
+import org.openstreetmap.josm.io.IllegalDataException;
+import org.openstreetmap.josm.io.OsmChangesetParser;
+import org.openstreetmap.josm.io.OsmDataParsingException;
+import org.openstreetmap.josm.io.OsmServerReader;
+import org.openstreetmap.josm.io.OsmTransferException;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
+
+/**
+ * Reads the history of an {@see OsmPrimitive} from the OSM API server.
+ *
+ */
+public class OsmServerChangesetReader extends OsmServerReader {
+
+    /**
+     * constructor
+     *
+     */
+    public OsmServerChangesetReader(){
+        setDoAuthenticate(false);
+    }
+
+    /**
+     * don't use - not implemented!
+     *
+     */
+    @Override
+    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
+        return null;
+    }
+
+    /**
+     * Queries a list
+     * @param query  the query specification. Must not be null.
+     * @param monitor a progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
+     * @return the list of changesets read from the server
+     * @throws IllegalArgumentException thrown if query is null
+     * @throws OsmTransferException thrown if something goes wrong w
+     */
+    public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
+        CheckParameterUtil.ensureParameterNotNull(query, "query");
+        if (monitor == null) {
+            monitor = NullProgressMonitor.INSTANCE;
+        }
+        try {
+            monitor.beginTask(tr("Reading changesets..."));
+            StringBuffer sb = new StringBuffer();
+            sb.append("changesets?").append(query.getQueryString());
+            InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
+            if (in == null)
+                return null;
+            monitor.indeterminateSubTask(tr("Downloading changesets ..."));
+            List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
+            return changesets;
+        } catch(OsmTransferException e) {
+            throw e;
+        } catch(IllegalDataException e) {
+            throw new OsmTransferException(e);
+        } finally {
+            monitor.finishTask();
+        }
+    }
+
+    /**
+     * Reads the changeset with id <code>id</code> from the server
+     *
+     * @param id  the changeset id. id > 0 required.
+     * @param monitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
+     * @return the changeset read
+     * @throws OsmTransferException thrown if something goes wrong
+     * @throws IllegalArgumentException if id <= 0
+     */
+    public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
+        if (id <= 0)
+            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
+        if (monitor == null) {
+            monitor = NullProgressMonitor.INSTANCE;
+        }
+        try {
+            monitor.beginTask(tr("Reading changeset {0} ...",id));
+            StringBuffer sb = new StringBuffer();
+            sb.append("changeset/").append(id);
+            InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
+            if (in == null)
+                return null;
+            monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
+            List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
+            if (changesets == null || changesets.isEmpty())
+                return null;
+            return changesets.get(0);
+        } catch(OsmTransferException e) {
+            throw e;
+        } catch(IllegalDataException e) {
+            throw new OsmTransferException(e);
+        } finally {
+            monitor.finishTask();
+        }
+    }
+
+    /**
+     * Reads the changeset with id <code>id</code> from the server
+     *
+     * @param ids  the list of ids. Ignored if null. Only load changesets for ids > 0.
+     * @param monitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
+     * @return the changeset read
+     * @throws OsmTransferException thrown if something goes wrong
+     * @throws IllegalArgumentException if id <= 0
+     */
+    public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
+        if (ids == null)
+            return Collections.emptyList();
+        if (monitor == null) {
+            monitor = NullProgressMonitor.INSTANCE;
+        }
+        try {
+            monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...",ids.size(),ids.size()));
+            monitor.setTicksCount(ids.size());
+            List<Changeset> ret = new ArrayList<Changeset>();
+            int i=0;
+            for (Iterator<Integer> it = ids.iterator(); it.hasNext(); ) {
+                int id = it.next();
+                if (id <= 0) {
+                    continue;
+                }
+                i++;
+                StringBuffer sb = new StringBuffer();
+                sb.append("changeset/").append(id);
+                InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
+                if (in == null)
+                    return null;
+                monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i,ids.size(), id));
+                List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
+                if (changesets == null || changesets.isEmpty()) {
+                    continue;
+                }
+                ret.addAll(changesets);
+                monitor.worked(1);
+            }
+            return ret;
+        } catch(OsmTransferException e) {
+            throw e;
+        } catch(IllegalDataException e) {
+            throw new OsmTransferException(e);
+        } finally {
+            monitor.finishTask();
+        }
+    }
+
+    /**
+     * Downloads the content of a changeset
+     *
+     * @param id the changeset id. >0 required.
+     * @param monitor the progress monitor. {@see NullProgressMonitor#INSTANCE} assumed if null.
+     * @return the changeset content
+     * @throws IllegalArgumentException thrown if id <= 0
+     * @throws OsmTransferException thrown if something went wrong
+     */
+    public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws IllegalArgumentException, OsmTransferException {
+        if (id <= 0)
+            throw new IllegalArgumentException(MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
+        if (monitor == null) {
+            monitor = NullProgressMonitor.INSTANCE;
+        }
+        try {
+            monitor.beginTask(tr("Downloading changeset content"));
+            StringBuffer sb = new StringBuffer();
+            sb.append("changeset/").append(id).append("/download");
+            InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
+            if (in == null)
+                return null;
+            monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
+            OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
+            ChangesetDataSet ds = parser.parse(monitor.createSubTaskMonitor(1, true));
+            return ds;
+        } catch(UnsupportedEncodingException e) {
+            throw new OsmTransferException(e);
+        } catch(OsmDataParsingException e) {
+            throw new OsmTransferException(e);
+        } finally {
+            monitor.finishTask();
+        }
+    }
+}
