1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import java.io.PrintWriter;
|
---|
4 | import java.io.Writer;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.Map.Entry;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
9 | import org.openstreetmap.josm.data.osm.LineSegment;
|
---|
10 | import org.openstreetmap.josm.data.osm.Node;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
14 | import org.openstreetmap.josm.tools.XmlWriter;
|
---|
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 implements Visitor {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * The output writer to save the values to.
|
---|
25 | */
|
---|
26 | private PrintWriter out;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * The counter for new created objects. Starting at -1 and goes down.
|
---|
30 | */
|
---|
31 | private long newIdCounter = -1;
|
---|
32 | /**
|
---|
33 | * All newly created ids and their primitive that uses it. This is a back reference
|
---|
34 | * map to allow references to use the correnct primitives.
|
---|
35 | */
|
---|
36 | private HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>();
|
---|
37 |
|
---|
38 | private final boolean osmConform;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Output the data to the stream
|
---|
42 | * @param osmConform <code>true</code>, if the xml should be 100% osm conform. In this
|
---|
43 | * case, not all information can be retrieved later (as example, modified state
|
---|
44 | * is lost and id's remain 0 instead of decrementing from -1)
|
---|
45 | */
|
---|
46 | public static void output(Writer out, DataSet ds, boolean osmConform) {
|
---|
47 | OsmWriter writer = new OsmWriter(out, osmConform);
|
---|
48 | writer.out.println("<?xml version='1.0' encoding='UTF-8'?>");
|
---|
49 | writer.out.println("<osm version='0.3' generator='JOSM'>");
|
---|
50 | for (Node n : ds.nodes)
|
---|
51 | writer.visit(n);
|
---|
52 | for (LineSegment ls : ds.lineSegments)
|
---|
53 | writer.visit(ls);
|
---|
54 | for (Way w : ds.ways)
|
---|
55 | writer.visit(w);
|
---|
56 | writer.out.println("</osm>");
|
---|
57 | }
|
---|
58 |
|
---|
59 | public static void outputSingle(Writer out, OsmPrimitive osm, boolean osmConform) {
|
---|
60 | OsmWriter writer = new OsmWriter(out, osmConform);
|
---|
61 | writer.out.println(XmlWriter.header());
|
---|
62 | writer.out.println("<osm version='0.3' generator='JOSM'>");
|
---|
63 | osm.visit(writer);
|
---|
64 | writer.out.println("</osm>");
|
---|
65 | }
|
---|
66 |
|
---|
67 | private OsmWriter(Writer out, boolean osmConform) {
|
---|
68 | if (out instanceof PrintWriter)
|
---|
69 | this.out = (PrintWriter)out;
|
---|
70 | else
|
---|
71 | this.out = new PrintWriter(out);
|
---|
72 | this.osmConform = osmConform;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public void visit(Node n) {
|
---|
76 | addCommon(n, "node");
|
---|
77 | out.print(" lat='"+n.coor.lat()+"' lon='"+n.coor.lon()+"'");
|
---|
78 | addTags(n, "node", true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void visit(LineSegment ls) {
|
---|
82 | if (ls.incomplete)
|
---|
83 | return; // Do not write an incomplete line segment
|
---|
84 | addCommon(ls, "segment");
|
---|
85 | out.print(" from='"+getUsedId(ls.from)+"' to='"+getUsedId(ls.to)+"'");
|
---|
86 | addTags(ls, "segment", true);
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void visit(Way w) {
|
---|
90 | addCommon(w, "way");
|
---|
91 | out.println(">");
|
---|
92 | for (LineSegment ls : w.segments)
|
---|
93 | out.println(" <seg id='"+getUsedId(ls)+"' />");
|
---|
94 | addTags(w, "way", false);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Return the id for the given osm primitive (may access the usedId map)
|
---|
99 | */
|
---|
100 | private long getUsedId(OsmPrimitive osm) {
|
---|
101 | if (osm.id != 0)
|
---|
102 | return osm.id;
|
---|
103 | if (usedNewIds.containsKey(osm))
|
---|
104 | return usedNewIds.get(osm);
|
---|
105 | usedNewIds.put(osm, newIdCounter);
|
---|
106 | return osmConform ? 0 : newIdCounter--;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) {
|
---|
111 | if (osm.keys != null) {
|
---|
112 | if (tagOpen)
|
---|
113 | out.println(">");
|
---|
114 | for (Entry<String, String> e : osm.keys.entrySet())
|
---|
115 | out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) +
|
---|
116 | "' v='"+XmlWriter.encode(e.getValue())+ "' />");
|
---|
117 | out.println(" </" + tagname + ">");
|
---|
118 | } else if (tagOpen)
|
---|
119 | out.println(" />");
|
---|
120 | else
|
---|
121 | out.println(" </" + tagname + ">");
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Add the common part as the from of the tag as well as the id or the action tag.
|
---|
126 | */
|
---|
127 | private void addCommon(OsmPrimitive osm, String tagname) {
|
---|
128 | out.print(" <"+tagname+" id='"+getUsedId(osm)+"'");
|
---|
129 | if (!osmConform) {
|
---|
130 | String action = null;
|
---|
131 | if (osm.isDeleted())
|
---|
132 | action = "delete";
|
---|
133 | else if (osm.modified && osm.modifiedProperties)
|
---|
134 | action = "modify";
|
---|
135 | else if (osm.modified && !osm.modifiedProperties)
|
---|
136 | action = "modify/object";
|
---|
137 | else if (!osm.modified && osm.modifiedProperties)
|
---|
138 | action = "modify/property";
|
---|
139 | if (action != null)
|
---|
140 | out.print(" action='"+action+"'");
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|