source: josm/trunk/src/org/openstreetmap/josm/io/OsmChangeBuilder.java @ 5241

Revision 4645, 4.4 KB checked in by framm, 5 months ago (diff)

Added three new hooks for plugins, allowing plugins to mess with JOSM's server I/O. Plugins can now add postprocessors to server reading (so they may change what JOSM receives from the OSM server), and they can add postprocessors to server writing (so they can react to an upload action *after* it happened and after IDs for new objects have been assigned - this is in addition to the existing UploadHook which is a server writing preprocessor). Thirdly, plugins can now substitute their own version of OsmWriter. Taken together, these changes make it possible for a plugin to introduce extra information from a non-OSM source into the JOSM editing process, and upon upload split away that extra information and send it to another destination. - Also made minor changes to CredentialDialog to allow plugins to subclass the dialog in case they need their own authentication.

  • Property svn:eol-style set to native
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.PrintWriter;
7import java.io.StringWriter;
8import java.util.Collection;
9
10import org.openstreetmap.josm.data.osm.Changeset;
11import org.openstreetmap.josm.data.osm.IPrimitive;
12
13/**
14 * Creates an OsmChange document from JOSM edits.
15 * See http://wiki.openstreetmap.org/index.php/OsmChange for a documentation of the
16 * OsmChange format.
17 *
18 */
19public class OsmChangeBuilder {
20    static public final String DEFAULT_API_VERSION = "0.6";
21
22    private String currentMode;
23    private PrintWriter writer;
24    private StringWriter swriter;
25    private OsmWriter osmwriter;
26    private String apiVersion = DEFAULT_API_VERSION;
27    private boolean prologWritten = false;
28
29    public OsmChangeBuilder(Changeset changeset) {
30        this(changeset, null /* default api version */);
31    }
32
33    public OsmChangeBuilder(Changeset changeset, String apiVersion) {
34        this.apiVersion = apiVersion == null ? DEFAULT_API_VERSION : apiVersion;
35        writer = new PrintWriter(swriter = new StringWriter());
36        osmwriter = OsmWriterFactory.createOsmWriter(writer, false, apiVersion);
37        osmwriter.setChangeset(changeset);
38    }
39
40    protected void write(IPrimitive p) {
41        if (p.isDeleted()) {
42            switchMode("delete");
43            osmwriter.setWithBody(false);
44            p.visit(osmwriter);
45        } else {
46            switchMode(p.isNew() ? "create" : "modify");
47            osmwriter.setWithBody(true);
48            p.visit(osmwriter);
49        }
50    }
51
52    private void switchMode(String newMode) {
53        if ((newMode != null && !newMode.equals(currentMode))||(newMode == null && currentMode != null)) {
54            if (currentMode != null) {
55                writer.print("</");
56                writer.print(currentMode);
57                writer.println(">");
58            }
59            if (newMode != null) {
60                writer.print("<");
61                writer.print(newMode);
62                writer.println(">");
63            }
64            currentMode = newMode;
65        }
66    }
67
68    /**
69     * Writes the prolog of the OsmChange document
70     *
71     * @throws IllegalStateException thrown if the prologs has already been written
72     */
73    public void start() throws IllegalStateException{
74        if (prologWritten)
75            throw new IllegalStateException(tr("Prolog of OsmChange document already written. Please write only once."));
76        writer.print("<osmChange version=\"");
77        writer.print(apiVersion);
78        writer.println("\" generator=\"JOSM\">");
79        prologWritten=true;
80    }
81
82    /**
83     * Appends a collection of Primitives to the OsmChange document.
84     *
85     * @param primitives the collection of primitives. Ignored if null.
86     * @throws IllegalStateException thrown if the prologs has not been written yet
87     * @see #start()
88     * @see #append(IPrimitive)
89     */
90    public void append(Collection<? extends IPrimitive> primitives) throws IllegalStateException{
91        if (primitives == null) return;
92        if (!prologWritten)
93            throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
94        for (IPrimitive p : primitives) {
95            write(p);
96        }
97    }
98
99    /**
100     * Appends an Primitive to the OsmChange document.
101     *
102     * @param p the primitive. Ignored if null.
103     * @throws IllegalStateException thrown if the prologs has not been written yet
104     * @see #start()
105     * @see #append(Collection)
106
107     */
108    public void append(IPrimitive p) {
109        if (p == null) return;
110        if (!prologWritten)
111            throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
112        write(p);
113    }
114
115    /**
116     * Writes the epilog of the OsmChange document
117     *
118     * @throws IllegalStateException thrown if the prologs has not been written yet
119     */
120    public void finish() throws IllegalStateException {
121        if (!prologWritten)
122            throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
123        if (currentMode != null) {
124            writer.print("</");
125            writer.print(currentMode);
126            writer.println(">");
127        }
128        writer.println("</osmChange>");
129    }
130
131    public String getDocument() {
132        return swriter.toString();
133    }
134}
Note: See TracBrowser for help on using the repository browser.