| 1 | // License: GPL. For details, see LICENSE file. |
|---|
| 2 | package org.openstreetmap.josm.io; |
|---|
| 3 | |
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
|---|
| 5 | |
|---|
| 6 | import java.io.PrintWriter; |
|---|
| 7 | import java.io.StringWriter; |
|---|
| 8 | import java.util.Collection; |
|---|
| 9 | |
|---|
| 10 | import org.openstreetmap.josm.data.osm.Changeset; |
|---|
| 11 | import 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 | */ |
|---|
| 19 | public 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 | } |
|---|