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