source: josm/trunk/src/org/openstreetmap/josm/io/OsmChangeBuilder.java@ 8839

Last change on this file since 8839 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.PrintWriter;
7import java.io.StringWriter;
8import java.util.Collection;
9
10import org.openstreetmap.josm.data.osm.Changeset;
11import 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 */
19public class OsmChangeBuilder {
20 public static 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 osmwriter.setIsOsmChange(true);
39 }
40
41 protected void write(IPrimitive p) {
42 if (p.isDeleted()) {
43 switchMode("delete");
44 osmwriter.setWithBody(false);
45 p.accept(osmwriter);
46 } else {
47 switchMode(p.isNew() ? "create" : "modify");
48 osmwriter.setWithBody(true);
49 p.accept(osmwriter);
50 }
51 }
52
53 private void switchMode(String newMode) {
54 if ((newMode != null && !newMode.equals(currentMode)) || (newMode == null && currentMode != null)) {
55 if (currentMode != null) {
56 writer.print("</");
57 writer.print(currentMode);
58 writer.println(">");
59 }
60 if (newMode != null) {
61 writer.print("<");
62 writer.print(newMode);
63 writer.println(">");
64 }
65 currentMode = newMode;
66 }
67 }
68
69 /**
70 * Writes the prolog of the OsmChange document
71 *
72 * @throws IllegalStateException if the prologs has already been written
73 */
74 public void start() {
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;
81 }
82
83 /**
84 * Appends a collection of Primitives to the OsmChange document.
85 *
86 * @param primitives the collection of primitives. Ignored if null.
87 * @throws IllegalStateException if the prologs has not been written yet
88 * @see #start()
89 * @see #append(IPrimitive)
90 */
91 public void append(Collection<? extends IPrimitive> primitives) {
92 if (primitives == null) return;
93 if (!prologWritten)
94 throw new IllegalStateException(tr("Prolog of OsmChange document not written yet. Please write first."));
95 for (IPrimitive p : primitives) {
96 write(p);
97 }
98 }
99
100 /**
101 * Appends an Primitive to the OsmChange document.
102 *
103 * @param p the primitive. Ignored if null.
104 * @throws IllegalStateException if the prologs has not been written yet
105 * @see #start()
106 * @see #append(Collection)
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 if the prologs has not been written yet
119 */
120 public void finish() {
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}
Note: See TracBrowser for help on using the repository browser.