1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.PrintWriter;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.Comparator;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map.Entry;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
18 | import org.openstreetmap.josm.data.osm.INode;
|
---|
19 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
20 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
21 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
22 | import org.openstreetmap.josm.data.osm.Node;
|
---|
23 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
24 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
25 | import org.openstreetmap.josm.data.osm.Tagged;
|
---|
26 | import org.openstreetmap.josm.data.osm.Way;
|
---|
27 | import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
|
---|
28 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Save the dataset into a stream as osm intern xml format. This is not using any
|
---|
32 | * xml library for storing.
|
---|
33 | * @author imi
|
---|
34 | */
|
---|
35 | public class OsmWriter extends XmlWriter implements PrimitiveVisitor {
|
---|
36 |
|
---|
37 | public static final String DEFAULT_API_VERSION = "0.6";
|
---|
38 |
|
---|
39 | private boolean osmConform;
|
---|
40 | private boolean withBody = true;
|
---|
41 | private String version;
|
---|
42 | private Changeset changeset;
|
---|
43 |
|
---|
44 | public OsmWriter(PrintWriter out, boolean osmConform, String version) {
|
---|
45 | super(out);
|
---|
46 | this.osmConform = osmConform;
|
---|
47 | this.version = (version == null ? DEFAULT_API_VERSION : version);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void setWithBody(boolean wb) {
|
---|
51 | this.withBody = wb;
|
---|
52 | }
|
---|
53 | public void setChangeset(Changeset cs) {
|
---|
54 | this.changeset = cs;
|
---|
55 | }
|
---|
56 | public void setVersion(String v) {
|
---|
57 | this.version = v;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void header() {
|
---|
61 | out.println("<?xml version='1.0' encoding='UTF-8'?>");
|
---|
62 | out.print("<osm version='");
|
---|
63 | out.print(version);
|
---|
64 | out.println("' generator='JOSM'>");
|
---|
65 | }
|
---|
66 | public void footer() {
|
---|
67 | out.println("</osm>");
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static final Comparator<OsmPrimitive> byIdComparator = new Comparator<OsmPrimitive>() {
|
---|
71 | @Override public int compare(OsmPrimitive o1, OsmPrimitive o2) {
|
---|
72 | return (o1.getUniqueId()<o2.getUniqueId() ? -1 : (o1.getUniqueId()==o2.getUniqueId() ? 0 : 1));
|
---|
73 | }
|
---|
74 | };
|
---|
75 |
|
---|
76 | private Collection<OsmPrimitive> sortById(Collection<? extends OsmPrimitive> primitives) {
|
---|
77 | List<OsmPrimitive> result = new ArrayList<OsmPrimitive>(primitives.size());
|
---|
78 | result.addAll(primitives);
|
---|
79 | Collections.sort(result, byIdComparator);
|
---|
80 | return result;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void writeContent(DataSet ds) {
|
---|
84 | for (OsmPrimitive n : sortById(ds.getNodes())) {
|
---|
85 | if (shouldWrite(n)) {
|
---|
86 | visit((Node)n);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | for (OsmPrimitive w : sortById(ds.getWays())) {
|
---|
90 | if (shouldWrite(w)) {
|
---|
91 | visit((Way)w);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | for (OsmPrimitive e: sortById(ds.getRelations())) {
|
---|
95 | if (shouldWrite(e)) {
|
---|
96 | visit((Relation)e);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | private boolean shouldWrite(OsmPrimitive osm) {
|
---|
102 | return !osm.isNewOrUndeleted() || !osm.isDeleted();
|
---|
103 | }
|
---|
104 |
|
---|
105 | public void writeDataSources(DataSet ds) {
|
---|
106 | for (DataSource s : ds.dataSources) {
|
---|
107 | out.println(" <bounds minlat='"
|
---|
108 | + s.bounds.getMin().lat()+"' minlon='"
|
---|
109 | + s.bounds.getMin().lon()+"' maxlat='"
|
---|
110 | + s.bounds.getMax().lat()+"' maxlon='"
|
---|
111 | + s.bounds.getMax().lon()
|
---|
112 | +"' origin='"+XmlWriter.encode(s.origin)+"' />");
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public void visit(INode n) {
|
---|
118 | if (n.isIncomplete()) return;
|
---|
119 | addCommon(n, "node");
|
---|
120 | out.print(" lat='"+n.getCoor().lat()+"' lon='"+n.getCoor().lon()+"'");
|
---|
121 | if (!withBody) {
|
---|
122 | out.println("/>");
|
---|
123 | } else {
|
---|
124 | addTags(n, "node", true);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | public void visit(IWay w) {
|
---|
130 | if (w.isIncomplete()) return;
|
---|
131 | addCommon(w, "way");
|
---|
132 | if (!withBody) {
|
---|
133 | out.println("/>");
|
---|
134 | } else {
|
---|
135 | out.println(">");
|
---|
136 | for (int i=0; i<w.getNodesCount(); ++i) {
|
---|
137 | out.println(" <nd ref='"+w.getNodeId(i) +"' />");
|
---|
138 | }
|
---|
139 | addTags(w, "way", false);
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public void visit(IRelation e) {
|
---|
145 | if (e.isIncomplete()) return;
|
---|
146 | addCommon(e, "relation");
|
---|
147 | if (!withBody) {
|
---|
148 | out.println("/>");
|
---|
149 | } else {
|
---|
150 | out.println(">");
|
---|
151 | for (int i=0; i<e.getMembersCount(); ++i) {
|
---|
152 | out.print(" <member type='");
|
---|
153 | out.print(e.getMemberType(i));
|
---|
154 | out.println("' ref='"+e.getMemberId(i)+"' role='" +
|
---|
155 | XmlWriter.encode(e.getRole(i)) + "' />");
|
---|
156 | }
|
---|
157 | addTags(e, "relation", false);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public void visit(Changeset cs) {
|
---|
162 | out.print(" <changeset ");
|
---|
163 | out.print(" id='"+cs.getId()+"'");
|
---|
164 | if (cs.getUser() != null) {
|
---|
165 | out.print(" user='"+cs.getUser().getName() +"'");
|
---|
166 | out.print(" uid='"+cs.getUser().getId() +"'");
|
---|
167 | }
|
---|
168 | if (cs.getCreatedAt() != null) {
|
---|
169 | out.print(" created_at='"+DateUtils.fromDate(cs.getCreatedAt()) +"'");
|
---|
170 | }
|
---|
171 | if (cs.getClosedAt() != null) {
|
---|
172 | out.print(" closed_at='"+DateUtils.fromDate(cs.getClosedAt()) +"'");
|
---|
173 | }
|
---|
174 | out.print(" open='"+ (cs.isOpen() ? "true" : "false") +"'");
|
---|
175 | if (cs.getMin() != null) {
|
---|
176 | out.print(" min_lon='"+ cs.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
|
---|
177 | out.print(" min_lat='"+ cs.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
|
---|
178 | }
|
---|
179 | if (cs.getMax() != null) {
|
---|
180 | out.print(" max_lon='"+ cs.getMin().lonToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
|
---|
181 | out.print(" max_lat='"+ cs.getMin().latToString(CoordinateFormat.DECIMAL_DEGREES) +"'");
|
---|
182 | }
|
---|
183 | out.println(">");
|
---|
184 | addTags(cs, "changeset", false); // also writes closing </changeset>
|
---|
185 | }
|
---|
186 |
|
---|
187 | private static final Comparator<Entry<String, String>> byKeyComparator = new Comparator<Entry<String,String>>() {
|
---|
188 | @Override public int compare(Entry<String, String> o1, Entry<String, String> o2) {
|
---|
189 | return o1.getKey().compareTo(o2.getKey());
|
---|
190 | }
|
---|
191 | };
|
---|
192 |
|
---|
193 | private void addTags(Tagged osm, String tagname, boolean tagOpen) {
|
---|
194 | if (osm.hasKeys()) {
|
---|
195 | if (tagOpen) {
|
---|
196 | out.println(">");
|
---|
197 | }
|
---|
198 | List<Entry<String, String>> entries = new ArrayList<Entry<String,String>>(osm.getKeys().entrySet());
|
---|
199 | Collections.sort(entries, byKeyComparator);
|
---|
200 | for (Entry<String, String> e : entries) {
|
---|
201 | if ((osm instanceof Changeset) || !("created_by".equals(e.getKey()))) {
|
---|
202 | out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) +
|
---|
203 | "' v='"+XmlWriter.encode(e.getValue())+ "' />");
|
---|
204 | }
|
---|
205 | }
|
---|
206 | out.println(" </" + tagname + ">");
|
---|
207 | } else if (tagOpen) {
|
---|
208 | out.println(" />");
|
---|
209 | } else {
|
---|
210 | out.println(" </" + tagname + ">");
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Add the common part as the form of the tag as well as the XML attributes
|
---|
216 | * id, action, user, and visible.
|
---|
217 | */
|
---|
218 | private void addCommon(IPrimitive osm, String tagname) {
|
---|
219 | out.print(" <"+tagname);
|
---|
220 | if (osm.getUniqueId() != 0) {
|
---|
221 | out.print(" id='"+ osm.getUniqueId()+"'");
|
---|
222 | } else
|
---|
223 | throw new IllegalStateException(tr("Unexpected id 0 for osm primitive found"));
|
---|
224 | if (!osmConform) {
|
---|
225 | String action = null;
|
---|
226 | if (osm.isDeleted()) {
|
---|
227 | action = "delete";
|
---|
228 | } else if (osm.isModified()) {
|
---|
229 | action = "modify";
|
---|
230 | }
|
---|
231 | if (action != null) {
|
---|
232 | out.print(" action='"+action+"'");
|
---|
233 | }
|
---|
234 | }
|
---|
235 | if (!osm.isTimestampEmpty()) {
|
---|
236 | out.print(" timestamp='"+DateUtils.fromDate(osm.getTimestamp())+"'");
|
---|
237 | }
|
---|
238 | // user and visible added with 0.4 API
|
---|
239 | if (osm.getUser() != null) {
|
---|
240 | if(osm.getUser().isLocalUser()) {
|
---|
241 | out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+"'");
|
---|
242 | } else if (osm.getUser().isOsmUser()) {
|
---|
243 | // uid added with 0.6
|
---|
244 | out.print(" uid='"+ osm.getUser().getId()+"'");
|
---|
245 | out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+"'");
|
---|
246 | }
|
---|
247 | }
|
---|
248 | out.print(" visible='"+osm.isVisible()+"'");
|
---|
249 | if (osm.getVersion() != 0) {
|
---|
250 | out.print(" version='"+osm.getVersion()+"'");
|
---|
251 | }
|
---|
252 | if (this.changeset != null && this.changeset.getId() != 0) {
|
---|
253 | out.print(" changeset='"+this.changeset.getId()+"'" );
|
---|
254 | } else if (osm.getChangesetId() > 0 && !osm.isNew()) {
|
---|
255 | out.print(" changeset='"+osm.getChangesetId()+"'" );
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | public void close() {
|
---|
260 | out.close();
|
---|
261 | }
|
---|
262 |
|
---|
263 | public void flush() {
|
---|
264 | out.flush();
|
---|
265 | }
|
---|
266 | }
|
---|