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

Last change on this file since 3129 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 4.4 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.OsmPrimitive;
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 static public 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 = new OsmWriter(writer, false, apiVersion);
37 osmwriter.setChangeset(changeset);
38 }
39
40 protected void write(OsmPrimitive p) {
41 if (p.isDeleted()) {
42 switchMode("delete");
43 osmwriter.setWithBody(false);
44 p.visit(osmwriter);
45 } else {
46 switchMode(p.isNew() ? "create" : "modify");
47 osmwriter.setWithBody(true);
48 p.visit(osmwriter);
49 }
50 }
51
52 private void switchMode(String newMode) {
53 if ((newMode != null && !newMode.equals(currentMode))||(newMode == null && currentMode != null)) {
54 if (currentMode != null) {
55 writer.print("</");
56 writer.print(currentMode);
57 writer.println(">");
58 }
59 if (newMode != null) {
60 writer.print("<");
61 writer.print(newMode);
62 writer.println(">");
63 }
64 currentMode = newMode;
65 }
66 }
67
68 /**
69 * Writes the prolog of the OsmChange document
70 *
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;
80 }
81
82 /**
83 * Appends a collection of {@see OsmPrimitive}s to the OsmChange document.
84 *
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 }
97 }
98
99 /**
100 * Appends an {@see OsmPrimitive} to the OsmChange document.
101 *
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
117 *
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() {
132 return swriter.toString();
133 }
134}
Note: See TracBrowser for help on using the repository browser.