[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;
|
---|
| 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 12 |
|
---|
| 13 | /**
|
---|
| 14 | * Creates an OsmChange document from JOSM edits.
|
---|
[1169] | 15 | * See http://wiki.openstreetmap.org/index.php/OsmChange for a documentation of the
|
---|
[1071] | 16 | * OsmChange format.
|
---|
[1169] | 17 | *
|
---|
[1071] | 18 | */
|
---|
[2604] | 19 | public class OsmChangeBuilder {
|
---|
| 20 | static public final String DEFAULT_API_VERSION = "0.6";
|
---|
[1071] | 21 |
|
---|
[1523] | 22 | private String currentMode;
|
---|
| 23 | private PrintWriter writer;
|
---|
| 24 | private StringWriter swriter;
|
---|
| 25 | private OsmWriter osmwriter;
|
---|
[2604] | 26 | private String apiVersion = DEFAULT_API_VERSION;
|
---|
| 27 | private boolean prologWritten = false;
|
---|
[1169] | 28 |
|
---|
[2604] | 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;
|
---|
[1071] | 35 | writer = new PrintWriter(swriter = new StringWriter());
|
---|
[2604] | 36 | osmwriter = new OsmWriter(writer, false, apiVersion);
|
---|
[1523] | 37 | osmwriter.setChangeset(changeset);
|
---|
[1071] | 38 | }
|
---|
[1169] | 39 |
|
---|
[2604] | 40 | protected void write(OsmPrimitive p) {
|
---|
| 41 | if (p.isDeleted()) {
|
---|
[1071] | 42 | switchMode("delete");
|
---|
[1523] | 43 | osmwriter.setWithBody(false);
|
---|
[2604] | 44 | p.visit(osmwriter);
|
---|
[1071] | 45 | } else {
|
---|
[2604] | 46 | switchMode(p.isNew() ? "create" : "modify");
|
---|
[1523] | 47 | osmwriter.setWithBody(true);
|
---|
[2604] | 48 | p.visit(osmwriter);
|
---|
[1071] | 49 | }
|
---|
| 50 | }
|
---|
[1677] | 51 |
|
---|
[1071] | 52 | private void switchMode(String newMode) {
|
---|
| 53 | if ((newMode != null && !newMode.equals(currentMode))||(newMode == null && currentMode != null)) {
|
---|
| 54 | if (currentMode != null) {
|
---|
[2604] | 55 | writer.print("</");
|
---|
| 56 | writer.print(currentMode);
|
---|
| 57 | writer.println(">");
|
---|
[1071] | 58 | }
|
---|
| 59 | if (newMode != null) {
|
---|
[2604] | 60 | writer.print("<");
|
---|
| 61 | writer.print(newMode);
|
---|
| 62 | writer.println(">");
|
---|
[1071] | 63 | }
|
---|
| 64 | currentMode = newMode;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[1169] | 67 |
|
---|
[2604] | 68 | /**
|
---|
| 69 | * Writes the prolog of the OsmChange document
|
---|
[2711] | 70 | *
|
---|
[2604] | 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;
|
---|
[1071] | 80 | }
|
---|
[1169] | 81 |
|
---|
[2604] | 82 | /**
|
---|
| 83 | * Appends a collection of {@see OsmPrimitive}s to the OsmChange document.
|
---|
[2711] | 84 | *
|
---|
[2604] | 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(OsmPrimitive)
|
---|
| 89 | */
|
---|
| 90 | public void append(Collection<OsmPrimitive> 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 frst."));
|
---|
| 94 | for (OsmPrimitive p : primitives) {
|
---|
| 95 | write(p);
|
---|
| 96 | }
|
---|
[1071] | 97 | }
|
---|
[2604] | 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Appends an {@see OsmPrimitive} to the OsmChange document.
|
---|
[2711] | 101 | *
|
---|
[2604] | 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(OsmPrimitive p) {
|
---|
| 109 | if (p == null) return;
|
---|
| 110 | if (!prologWritten)
|
---|
| 111 | throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write frst."));
|
---|
| 112 | write(p);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /**
|
---|
| 116 | * Writes the epilog of the OsmChange document
|
---|
[2711] | 117 | *
|
---|
[2604] | 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 frst."));
|
---|
| 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() {
|
---|
[3366] | 132 | return swriter.toString();
|
---|
[2604] | 133 | }
|
---|
[1071] | 134 | }
|
---|