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

Last change on this file since 90 was 90, checked in by imi, 18 years ago
  • fixed that toggle dialog buttons are sync with the dialogs
  • added search for timestamp (e.g. timestamp:06-3-25)
File size: 4.3 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.PrintWriter;
4import java.io.Writer;
5import java.util.HashMap;
6import java.util.Map.Entry;
7
8import org.openstreetmap.josm.data.osm.DataSet;
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.Segment;
12import org.openstreetmap.josm.data.osm.Way;
13import org.openstreetmap.josm.data.osm.visitor.Visitor;
14import 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 */
21public 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 (Segment ls : ds.segments)
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(Segment ls) {
82 if (ls.incomplete)
83 return; // Do not write an incomplete 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 (Segment 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.deleted)
132 action = "delete";
133 else if (osm.modified)
134 action = "modify";
135 if (action != null)
136 out.print(" action='"+action+"'");
137 }
138 if (osm.timestamp != null) {
139 String time = osm.getTimeStr();
140 out.print(" timestamp='"+time+"'");
141 }
142 }
143}
Note: See TracBrowser for help on using the repository browser.