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

Last change on this file since 86 was 86, checked in by imi, 18 years ago
  • added conflicts and resolve conflict dialog

This is one of those "changed everything" checkpoint.

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