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