source: josm/trunk/src/org/openstreetmap/josm/io/OsmWriter.java@ 729

Last change on this file since 729 was 627, checked in by framm, 16 years ago
  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import java.io.PrintWriter;
5import java.util.HashMap;
6import java.util.Map.Entry;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.DataSource;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.data.osm.Node;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.data.osm.visitor.Visitor;
18
19/**
20 * Save the dataset into a stream as osm intern xml format. This is not using any
21 * xml library for storing.
22 * @author imi
23 */
24public class OsmWriter extends XmlWriter implements Visitor {
25
26 /**
27 * The counter for new created objects. Starting at -1 and goes down.
28 */
29 private long newIdCounter = -1;
30 /**
31 * All newly created ids and their primitive that uses it. This is a back reference
32 * map to allow references to use the correnct primitives.
33 */
34 private HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>();
35
36 private final boolean osmConform;
37 private final Changeset changeset;
38
39 public abstract static class Osm implements OsmWriterInterface {
40 public void header(PrintWriter out) {
41 out.print("<osm version='");
42 out.print(Main.pref.get("osm-server.version", "0.5"));
43 out.println("' generator='JOSM'>");
44 }
45 public void footer(PrintWriter out) {
46 out.println("</osm>");
47 }
48 }
49
50 // simple helper to write the object's class to the out stream
51 private Visitor typeWriteVisitor = new Visitor() {
52 public void visit(Node n) { out.print("node"); }
53 public void visit(Way w) { out.print("way"); }
54 public void visit(Relation e) { out.print("relation"); }
55 };
56
57 /**
58 * An output writer for function output that writes everything of the given dataset into
59 * the xml
60 */
61 public static final class All extends Osm {
62 private final DataSet ds;
63 private final boolean osmConform;
64
65 /**
66 * Construct an writer function
67 * @param osmConform <code>true</code>, if the xml should be 100% osm conform. In this
68 * case, not all information can be retrieved later (as example, modified state
69 * is lost and id's remain 0 instead of decrementing from -1)
70 */
71 public All(DataSet ds, boolean osmConform) {
72 this.ds = ds;
73 this.osmConform = osmConform;
74 }
75
76 public void write(PrintWriter out) {
77 Visitor writer = new OsmWriter(out, osmConform, null);
78 for (Node n : ds.nodes)
79 if (shouldWrite(n))
80 writer.visit(n);
81 for (Way w : ds.ways)
82 if (shouldWrite(w))
83 writer.visit(w);
84 for (Relation e : ds.relations)
85 if (shouldWrite(e))
86 writer.visit(e);
87 }
88
89 private boolean shouldWrite(OsmPrimitive osm) {
90 return osm.id != 0 || !osm.deleted;
91 }
92
93 @Override public void header(PrintWriter out) {
94 super.header(out);
95 for (DataSource s : ds.dataSources) {
96 out.print(" <bound box='"+
97 s.bounds.min.lat()+","+
98 s.bounds.min.lon()+","+
99 s.bounds.max.lat()+","+
100 s.bounds.max.lon()+"' ");
101 out.println("origin='"+XmlWriter.encode(s.origin)+"' />");
102 }
103 }
104 }
105
106 /**
107 * An output writer for functino output that writes only one specific primitive into
108 * the xml
109 */
110 public static final class Single extends Osm {
111 private final OsmPrimitive osm;
112 private final boolean osmConform;
113 private final Changeset changeset;
114
115 public Single(OsmPrimitive osm, boolean osmConform, Changeset changeset) {
116 this.osm = osm;
117 this.osmConform = osmConform;
118 this.changeset = changeset;
119 }
120
121 public void write(PrintWriter out) {
122 osm.visit(new OsmWriter(out, osmConform, changeset));
123 }
124 }
125
126 private OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) {
127 super(out);
128 this.osmConform = osmConform;
129 this.changeset = changeset;
130 }
131
132 public void visit(Node n) {
133 if (n.incomplete) return;
134 addCommon(n, "node");
135 out.print(" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'");
136 addTags(n, "node", true);
137 }
138
139 public void visit(Way w) {
140 if (w.incomplete) return;
141 addCommon(w, "way");
142 out.println(">");
143 for (Node n : w.nodes)
144 out.println(" <nd ref='"+getUsedId(n)+"' />");
145 addTags(w, "way", false);
146 }
147
148 public void visit(Relation e) {
149 if (e.incomplete) return;
150 addCommon(e, "relation");
151 out.println(">");
152 for (RelationMember em : e.members) {
153 out.print(" <member type='");
154 em.member.visit(typeWriteVisitor);
155 out.println("' ref='"+getUsedId(em.member)+"' role='" +
156 XmlWriter.encode(em.role) + "' />");
157 }
158 addTags(e, "relation", false);
159 }
160
161
162 /**
163 * Return the id for the given osm primitive (may access the usedId map)
164 */
165 private long getUsedId(OsmPrimitive osm) {
166 if (osm.id != 0)
167 return osm.id;
168 if (usedNewIds.containsKey(osm))
169 return usedNewIds.get(osm);
170 usedNewIds.put(osm, newIdCounter);
171 return osmConform ? 0 : newIdCounter--;
172 }
173
174
175 private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) {
176 if (osm.keys != null) {
177 if (tagOpen)
178 out.println(">");
179 for (Entry<String, String> e : osm.keys.entrySet())
180 out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) +
181 "' v='"+XmlWriter.encode(e.getValue())+ "' />");
182 out.println(" </" + tagname + ">");
183 } else if (tagOpen)
184 out.println(" />");
185 else
186 out.println(" </" + tagname + ">");
187 }
188
189 /**
190 * Add the common part as the form of the tag as well as the XML attributes
191 * id, action, user, and visible.
192 */
193 private void addCommon(OsmPrimitive osm, String tagname) {
194 out.print(" <"+tagname+" id='"+getUsedId(osm)+"'");
195 if (!osmConform) {
196 String action = null;
197 if (osm.deleted)
198 action = "delete";
199 else if (osm.modified)
200 action = "modify";
201 if (action != null)
202 out.print(" action='"+action+"'");
203 }
204 if (osm.timestamp != null) {
205 out.print(" timestamp='"+osm.timestamp+"'");
206 }
207 // user and visible added with 0.4 API
208 if (osm.user != null) {
209 out.print(" user='"+XmlWriter.encode(osm.user.name)+"'");
210 }
211 out.print(" visible='"+osm.visible+"'");
212 if( osm.version != -1 )
213 out.print( " old_version='"+osm.version+"'");
214 if( this.changeset != null && this.changeset.id != 0)
215 out.print( " changeset='"+this.changeset.id+"'" );
216 }
217}
Note: See TracBrowser for help on using the repository browser.