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 OsmChange format.
|
---|
16 | * @since 1071
|
---|
17 | */
|
---|
18 | public class OsmChangeBuilder {
|
---|
19 | /** Default OSM API version */
|
---|
20 | public static final String DEFAULT_API_VERSION = "0.6";
|
---|
21 |
|
---|
22 | private String currentMode;
|
---|
23 | private final PrintWriter writer;
|
---|
24 | private final StringWriter swriter;
|
---|
25 | private final OsmWriter osmwriter;
|
---|
26 | private String apiVersion = DEFAULT_API_VERSION;
|
---|
27 | private boolean prologWritten;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code OsmChangeBuilder}.
|
---|
31 | * @param changeset changeset
|
---|
32 | */
|
---|
33 | public OsmChangeBuilder(Changeset changeset) {
|
---|
34 | this(changeset, null /* default api version */);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Constructs a new {@code OsmChangeBuilder}.
|
---|
39 | * @param changeset changeset
|
---|
40 | * @param apiVersion OSM API version
|
---|
41 | */
|
---|
42 | public OsmChangeBuilder(Changeset changeset, String apiVersion) {
|
---|
43 | this.apiVersion = apiVersion == null ? DEFAULT_API_VERSION : apiVersion;
|
---|
44 | swriter = new StringWriter();
|
---|
45 | writer = new PrintWriter(swriter);
|
---|
46 | osmwriter = OsmWriterFactory.createOsmWriter(writer, false, apiVersion);
|
---|
47 | osmwriter.setChangeset(changeset);
|
---|
48 | osmwriter.setIsOsmChange(true);
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected void write(IPrimitive p) {
|
---|
52 | if (p.isDeleted()) {
|
---|
53 | switchMode("delete");
|
---|
54 | osmwriter.setWithBody(false);
|
---|
55 | p.accept(osmwriter);
|
---|
56 | } else {
|
---|
57 | switchMode(p.isNew() ? "create" : "modify");
|
---|
58 | osmwriter.setWithBody(true);
|
---|
59 | p.accept(osmwriter);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void switchMode(String newMode) {
|
---|
64 | if ((newMode != null && !newMode.equals(currentMode)) || (newMode == null && currentMode != null)) {
|
---|
65 | if (currentMode != null) {
|
---|
66 | writer.print("</");
|
---|
67 | writer.print(currentMode);
|
---|
68 | writer.println(">");
|
---|
69 | }
|
---|
70 | if (newMode != null) {
|
---|
71 | writer.print("<");
|
---|
72 | writer.print(newMode);
|
---|
73 | writer.println(">");
|
---|
74 | }
|
---|
75 | currentMode = newMode;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Writes the prolog of the OsmChange document
|
---|
81 | *
|
---|
82 | * @throws IllegalStateException if the prologs has already been written
|
---|
83 | */
|
---|
84 | public void start() {
|
---|
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\">");
|
---|
90 | prologWritten = true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Appends a collection of Primitives to the OsmChange document.
|
---|
95 | *
|
---|
96 | * @param primitives the collection of primitives. Ignored if null.
|
---|
97 | * @throws IllegalStateException if the prologs has not been written yet
|
---|
98 | * @see #start()
|
---|
99 | * @see #append(IPrimitive)
|
---|
100 | */
|
---|
101 | public void append(Collection<? extends IPrimitive> primitives) {
|
---|
102 | if (primitives == null)
|
---|
103 | return;
|
---|
104 | checkProlog();
|
---|
105 | for (IPrimitive p : primitives) {
|
---|
106 | write(p);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void checkProlog() {
|
---|
111 | if (!prologWritten)
|
---|
112 | throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Appends an Primitive to the OsmChange document.
|
---|
117 | *
|
---|
118 | * @param p the primitive. Ignored if null.
|
---|
119 | * @throws IllegalStateException if the prologs has not been written yet
|
---|
120 | * @see #start()
|
---|
121 | * @see #append(Collection)
|
---|
122 | */
|
---|
123 | public void append(IPrimitive p) {
|
---|
124 | if (p == null)
|
---|
125 | return;
|
---|
126 | checkProlog();
|
---|
127 | write(p);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Writes the epilog of the OsmChange document
|
---|
132 | *
|
---|
133 | * @throws IllegalStateException if the prologs has not been written yet
|
---|
134 | */
|
---|
135 | public void finish() {
|
---|
136 | checkProlog();
|
---|
137 | if (currentMode != null) {
|
---|
138 | writer.print("</");
|
---|
139 | writer.print(currentMode);
|
---|
140 | writer.println(">");
|
---|
141 | }
|
---|
142 | writer.println("</osmChange>");
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Returns XML document.
|
---|
147 | * @return XML document
|
---|
148 | */
|
---|
149 | public String getDocument() {
|
---|
150 | return swriter.toString();
|
---|
151 | }
|
---|
152 | }
|
---|