[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.
|
---|
[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 {
|
---|
[6889] | 20 | public static 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());
|
---|
[4645] | 36 | osmwriter = OsmWriterFactory.createOsmWriter(writer, false, apiVersion);
|
---|
[1523] | 37 | osmwriter.setChangeset(changeset);
|
---|
[5589] | 38 | osmwriter.setIsOsmChange(true);
|
---|
[1071] | 39 | }
|
---|
[1169] | 40 |
|
---|
[4100] | 41 | protected void write(IPrimitive p) {
|
---|
[2604] | 42 | if (p.isDeleted()) {
|
---|
[1071] | 43 | switchMode("delete");
|
---|
[1523] | 44 | osmwriter.setWithBody(false);
|
---|
[6009] | 45 | p.accept(osmwriter);
|
---|
[1071] | 46 | } else {
|
---|
[2604] | 47 | switchMode(p.isNew() ? "create" : "modify");
|
---|
[1523] | 48 | osmwriter.setWithBody(true);
|
---|
[6009] | 49 | p.accept(osmwriter);
|
---|
[1071] | 50 | }
|
---|
| 51 | }
|
---|
[1677] | 52 |
|
---|
[1071] | 53 | private void switchMode(String newMode) {
|
---|
| 54 | if ((newMode != null && !newMode.equals(currentMode))||(newMode == null && currentMode != null)) {
|
---|
| 55 | if (currentMode != null) {
|
---|
[2604] | 56 | writer.print("</");
|
---|
| 57 | writer.print(currentMode);
|
---|
| 58 | writer.println(">");
|
---|
[1071] | 59 | }
|
---|
| 60 | if (newMode != null) {
|
---|
[2604] | 61 | writer.print("<");
|
---|
| 62 | writer.print(newMode);
|
---|
| 63 | writer.println(">");
|
---|
[1071] | 64 | }
|
---|
| 65 | currentMode = newMode;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
[1169] | 68 |
|
---|
[2604] | 69 | /**
|
---|
| 70 | * Writes the prolog of the OsmChange document
|
---|
[2711] | 71 | *
|
---|
[2604] | 72 | * @throws IllegalStateException thrown if the prologs has already been written
|
---|
| 73 | */
|
---|
| 74 | public void start() throws IllegalStateException{
|
---|
| 75 | if (prologWritten)
|
---|
| 76 | throw new IllegalStateException(tr("Prolog of OsmChange document already written. Please write only once."));
|
---|
| 77 | writer.print("<osmChange version=\"");
|
---|
| 78 | writer.print(apiVersion);
|
---|
| 79 | writer.println("\" generator=\"JOSM\">");
|
---|
| 80 | prologWritten=true;
|
---|
[1071] | 81 | }
|
---|
[1169] | 82 |
|
---|
[2604] | 83 | /**
|
---|
[4100] | 84 | * Appends a collection of Primitives to the OsmChange document.
|
---|
[2711] | 85 | *
|
---|
[2604] | 86 | * @param primitives the collection of primitives. Ignored if null.
|
---|
| 87 | * @throws IllegalStateException thrown if the prologs has not been written yet
|
---|
| 88 | * @see #start()
|
---|
[4100] | 89 | * @see #append(IPrimitive)
|
---|
[2604] | 90 | */
|
---|
[4100] | 91 | public void append(Collection<? extends IPrimitive> primitives) throws IllegalStateException{
|
---|
[2604] | 92 | if (primitives == null) return;
|
---|
| 93 | if (!prologWritten)
|
---|
[4310] | 94 | throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
|
---|
[4100] | 95 | for (IPrimitive p : primitives) {
|
---|
[2604] | 96 | write(p);
|
---|
| 97 | }
|
---|
[1071] | 98 | }
|
---|
[2604] | 99 |
|
---|
| 100 | /**
|
---|
[4100] | 101 | * Appends an Primitive to the OsmChange document.
|
---|
[2711] | 102 | *
|
---|
[2604] | 103 | * @param p the primitive. Ignored if null.
|
---|
| 104 | * @throws IllegalStateException thrown if the prologs has not been written yet
|
---|
| 105 | * @see #start()
|
---|
| 106 | * @see #append(Collection)
|
---|
| 107 |
|
---|
| 108 | */
|
---|
[4100] | 109 | public void append(IPrimitive p) {
|
---|
[2604] | 110 | if (p == null) return;
|
---|
| 111 | if (!prologWritten)
|
---|
[4310] | 112 | throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
|
---|
[2604] | 113 | write(p);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * Writes the epilog of the OsmChange document
|
---|
[2711] | 118 | *
|
---|
[2604] | 119 | * @throws IllegalStateException thrown if the prologs has not been written yet
|
---|
| 120 | */
|
---|
| 121 | public void finish() throws IllegalStateException {
|
---|
| 122 | if (!prologWritten)
|
---|
[4310] | 123 | throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
|
---|
[2604] | 124 | if (currentMode != null) {
|
---|
| 125 | writer.print("</");
|
---|
| 126 | writer.print(currentMode);
|
---|
| 127 | writer.println(">");
|
---|
| 128 | }
|
---|
| 129 | writer.println("</osmChange>");
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public String getDocument() {
|
---|
[3366] | 133 | return swriter.toString();
|
---|
[2604] | 134 | }
|
---|
[1071] | 135 | }
|
---|