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

Last change on this file since 13509 was 13485, checked in by Don-vip, 6 years ago

see #8039, see #10456 - keep layer download/upload/locked flags when saving to .osm file

  • Property svn:eol-style set to native
File size: 13.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.PrintWriter;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.Comparator;
10import java.util.Date;
11import java.util.List;
12import java.util.Map.Entry;
13
14import org.openstreetmap.josm.data.DataSource;
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
17import org.openstreetmap.josm.data.osm.AbstractPrimitive;
18import org.openstreetmap.josm.data.osm.Changeset;
19import org.openstreetmap.josm.data.osm.DataSet;
20import org.openstreetmap.josm.data.osm.DataSet.DownloadPolicy;
21import org.openstreetmap.josm.data.osm.DataSet.UploadPolicy;
22import org.openstreetmap.josm.data.osm.INode;
23import org.openstreetmap.josm.data.osm.IPrimitive;
24import org.openstreetmap.josm.data.osm.IRelation;
25import org.openstreetmap.josm.data.osm.IWay;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.Relation;
29import org.openstreetmap.josm.data.osm.Tagged;
30import org.openstreetmap.josm.data.osm.Way;
31import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor;
32import org.openstreetmap.josm.tools.date.DateUtils;
33
34/**
35 * Save the dataset into a stream as osm intern xml format. This is not using any xml library for storing.
36 * @author imi
37 * @since 59
38 */
39public class OsmWriter extends XmlWriter implements PrimitiveVisitor {
40
41 /** Default OSM API version */
42 public static final String DEFAULT_API_VERSION = "0.6";
43
44 private final boolean osmConform;
45 private boolean withBody = true;
46 private boolean withVisible = true;
47 private boolean isOsmChange;
48 private String version;
49 private Changeset changeset;
50
51 /**
52 * Constructs a new {@code OsmWriter}.
53 * Do not call this directly. Use {@link OsmWriterFactory} instead.
54 * @param out print writer
55 * @param osmConform if {@code true}, prevents modification attributes to be written to the common part
56 * @param version OSM API version (0.6)
57 */
58 protected OsmWriter(PrintWriter out, boolean osmConform, String version) {
59 super(out);
60 this.osmConform = osmConform;
61 this.version = version == null ? DEFAULT_API_VERSION : version;
62 }
63
64 /**
65 * Sets whether body must be written.
66 * @param wb if {@code true} body will be written.
67 */
68 public void setWithBody(boolean wb) {
69 this.withBody = wb;
70 }
71
72 /**
73 * Sets whether 'visible' attribute must be written.
74 * @param wv if {@code true} 'visible' attribute will be written.
75 * @since 12019
76 */
77 public void setWithVisible(boolean wv) {
78 this.withVisible = wv;
79 }
80
81 public void setIsOsmChange(boolean isOsmChange) {
82 this.isOsmChange = isOsmChange;
83 }
84
85 public void setChangeset(Changeset cs) {
86 this.changeset = cs;
87 }
88
89 public void setVersion(String v) {
90 this.version = v;
91 }
92
93 /**
94 * Writes OSM header with normal download and upload policies.
95 */
96 public void header() {
97 header(DownloadPolicy.NORMAL, UploadPolicy.NORMAL);
98 }
99
100 /**
101 * Writes OSM header with normal download policy and given upload policy.
102 * @deprecated Use {@link #header(DownloadPolicy, UploadPolicy)} instead
103 * @param upload upload policy
104 */
105 @Deprecated
106 public void header(UploadPolicy upload) {
107 header(DownloadPolicy.NORMAL, upload);
108 }
109
110 /**
111 * Writes OSM header with given download upload policies.
112 * @param download download policy
113 * @param upload upload policy
114 * @since 13485
115 */
116 public void header(DownloadPolicy download, UploadPolicy upload) {
117 header(download, upload, false);
118 }
119
120 private void header(DownloadPolicy download, UploadPolicy upload, boolean locked) {
121 out.println("<?xml version='1.0' encoding='UTF-8'?>");
122 out.print("<osm version='");
123 out.print(version);
124 if (download != null && download != DownloadPolicy.NORMAL) {
125 out.print("' download='");
126 out.print(download.getXmlFlag());
127 }
128 if (upload != null && upload != UploadPolicy.NORMAL) {
129 out.print("' upload='");
130 out.print(upload.getXmlFlag());
131 }
132 if (locked) {
133 out.print("' locked=true");
134 }
135 out.println("' generator='JOSM'>");
136 }
137
138 /**
139 * Writes OSM footer.
140 */
141 public void footer() {
142 out.println("</osm>");
143 }
144
145 /**
146 * Sorts {@code -1} &rarr; {@code -infinity}, then {@code +1} &rarr; {@code +infinity}
147 */
148 protected static final Comparator<AbstractPrimitive> byIdComparator = (o1, o2) -> {
149 final long i1 = o1.getUniqueId();
150 final long i2 = o2.getUniqueId();
151 if (i1 < 0 && i2 < 0) {
152 return Long.compare(i2, i1);
153 } else {
154 return Long.compare(i1, i2);
155 }
156 };
157
158 protected <T extends OsmPrimitive> Collection<T> sortById(Collection<T> primitives) {
159 List<T> result = new ArrayList<>(primitives.size());
160 result.addAll(primitives);
161 result.sort(byIdComparator);
162 return result;
163 }
164
165 /**
166 * Writes the full OSM file for the given data set (header, data sources, osm data, footer).
167 * @param data OSM data set
168 * @since 12800
169 */
170 public void write(DataSet data) {
171 header(data.getDownloadPolicy(), data.getUploadPolicy(), data.isLocked());
172 writeDataSources(data);
173 writeContent(data);
174 footer();
175 }
176
177 /**
178 * Writes the contents of the given dataset (nodes, then ways, then relations)
179 * @param ds The dataset to write
180 */
181 public void writeContent(DataSet ds) {
182 setWithVisible(UploadPolicy.NORMAL.equals(ds.getUploadPolicy()));
183 writeNodes(ds.getNodes());
184 writeWays(ds.getWays());
185 writeRelations(ds.getRelations());
186 }
187
188 /**
189 * Writes the given nodes sorted by id
190 * @param nodes The nodes to write
191 * @since 5737
192 */
193 public void writeNodes(Collection<Node> nodes) {
194 for (Node n : sortById(nodes)) {
195 if (shouldWrite(n)) {
196 visit(n);
197 }
198 }
199 }
200
201 /**
202 * Writes the given ways sorted by id
203 * @param ways The ways to write
204 * @since 5737
205 */
206 public void writeWays(Collection<Way> ways) {
207 for (Way w : sortById(ways)) {
208 if (shouldWrite(w)) {
209 visit(w);
210 }
211 }
212 }
213
214 /**
215 * Writes the given relations sorted by id
216 * @param relations The relations to write
217 * @since 5737
218 */
219 public void writeRelations(Collection<Relation> relations) {
220 for (Relation r : sortById(relations)) {
221 if (shouldWrite(r)) {
222 visit(r);
223 }
224 }
225 }
226
227 protected boolean shouldWrite(OsmPrimitive osm) {
228 return !osm.isNewOrUndeleted() || !osm.isDeleted();
229 }
230
231 public void writeDataSources(DataSet ds) {
232 for (DataSource s : ds.getDataSources()) {
233 out.println(" <bounds minlat='"
234 + DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMin())
235 +"' minlon='"
236 + DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMin())
237 +"' maxlat='"
238 + DecimalDegreesCoordinateFormat.INSTANCE.latToString(s.bounds.getMax())
239 +"' maxlon='"
240 + DecimalDegreesCoordinateFormat.INSTANCE.lonToString(s.bounds.getMax())
241 +"' origin='"+XmlWriter.encode(s.origin)+"' />");
242 }
243 }
244
245 void writeLatLon(LatLon ll) {
246 if (ll != null) {
247 out.print(" lat='"+LatLon.cDdHighPecisionFormatter.format(ll.lat())+
248 "' lon='"+LatLon.cDdHighPecisionFormatter.format(ll.lon())+'\'');
249 }
250 }
251
252 @Override
253 public void visit(INode n) {
254 if (n.isIncomplete()) return;
255 addCommon(n, "node");
256 if (!withBody) {
257 out.println("/>");
258 } else {
259 writeLatLon(n.getCoor());
260 addTags(n, "node", true);
261 }
262 }
263
264 @Override
265 public void visit(IWay w) {
266 if (w.isIncomplete()) return;
267 addCommon(w, "way");
268 if (!withBody) {
269 out.println("/>");
270 } else {
271 out.println(">");
272 for (int i = 0; i < w.getNodesCount(); ++i) {
273 out.println(" <nd ref='"+w.getNodeId(i) +"' />");
274 }
275 addTags(w, "way", false);
276 }
277 }
278
279 @Override
280 public void visit(IRelation e) {
281 if (e.isIncomplete()) return;
282 addCommon(e, "relation");
283 if (!withBody) {
284 out.println("/>");
285 } else {
286 out.println(">");
287 for (int i = 0; i < e.getMembersCount(); ++i) {
288 out.print(" <member type='");
289 out.print(e.getMemberType(i).getAPIName());
290 out.println("' ref='"+e.getMemberId(i)+"' role='" +
291 XmlWriter.encode(e.getRole(i)) + "' />");
292 }
293 addTags(e, "relation", false);
294 }
295 }
296
297 public void visit(Changeset cs) {
298 out.print(" <changeset id='"+cs.getId()+'\'');
299 if (cs.getUser() != null) {
300 out.print(" user='"+ XmlWriter.encode(cs.getUser().getName()) +'\'');
301 out.print(" uid='"+cs.getUser().getId() +'\'');
302 }
303 Date createdDate = cs.getCreatedAt();
304 if (createdDate != null) {
305 out.print(" created_at='"+DateUtils.fromDate(createdDate) +'\'');
306 }
307 Date closedDate = cs.getClosedAt();
308 if (closedDate != null) {
309 out.print(" closed_at='"+DateUtils.fromDate(closedDate) +'\'');
310 }
311 out.print(" open='"+ (cs.isOpen() ? "true" : "false") +'\'');
312 if (cs.getMin() != null) {
313 out.print(" min_lon='"+ DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMin()) +'\'');
314 out.print(" min_lat='"+ DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMin()) +'\'');
315 }
316 if (cs.getMax() != null) {
317 out.print(" max_lon='"+ DecimalDegreesCoordinateFormat.INSTANCE.lonToString(cs.getMin()) +'\'');
318 out.print(" max_lat='"+ DecimalDegreesCoordinateFormat.INSTANCE.latToString(cs.getMin()) +'\'');
319 }
320 out.println(">");
321 addTags(cs, "changeset", false); // also writes closing </changeset>
322 }
323
324 protected static final Comparator<Entry<String, String>> byKeyComparator = (o1, o2) -> o1.getKey().compareTo(o2.getKey());
325
326 protected void addTags(Tagged osm, String tagname, boolean tagOpen) {
327 if (osm.hasKeys()) {
328 if (tagOpen) {
329 out.println(">");
330 }
331 List<Entry<String, String>> entries = new ArrayList<>(osm.getKeys().entrySet());
332 entries.sort(byKeyComparator);
333 for (Entry<String, String> e : entries) {
334 out.println(" <tag k='"+ XmlWriter.encode(e.getKey()) +
335 "' v='"+XmlWriter.encode(e.getValue())+ "' />");
336 }
337 out.println(" </" + tagname + '>');
338 } else if (tagOpen) {
339 out.println(" />");
340 } else {
341 out.println(" </" + tagname + '>');
342 }
343 }
344
345 /**
346 * Add the common part as the form of the tag as well as the XML attributes
347 * id, action, user, and visible.
348 * @param osm osm primitive
349 * @param tagname XML tag matching osm primitive (node, way, relation)
350 */
351 protected void addCommon(IPrimitive osm, String tagname) {
352 out.print(" <"+tagname);
353 if (osm.getUniqueId() != 0) {
354 out.print(" id='"+ osm.getUniqueId()+'\'');
355 } else
356 throw new IllegalStateException(tr("Unexpected id 0 for osm primitive found"));
357 if (!isOsmChange) {
358 if (!osmConform) {
359 String action = null;
360 if (osm.isDeleted()) {
361 action = "delete";
362 } else if (osm.isModified()) {
363 action = "modify";
364 }
365 if (action != null) {
366 out.print(" action='"+action+'\'');
367 }
368 }
369 if (!osm.isTimestampEmpty()) {
370 out.print(" timestamp='"+DateUtils.fromTimestamp(osm.getRawTimestamp())+'\'');
371 }
372 // user and visible added with 0.4 API
373 if (osm.getUser() != null) {
374 if (osm.getUser().isLocalUser()) {
375 out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+'\'');
376 } else if (osm.getUser().isOsmUser()) {
377 // uid added with 0.6
378 out.print(" uid='"+ osm.getUser().getId()+'\'');
379 out.print(" user='"+XmlWriter.encode(osm.getUser().getName())+'\'');
380 }
381 }
382 if (withVisible) {
383 out.print(" visible='"+osm.isVisible()+'\'');
384 }
385 }
386 if (osm.getVersion() != 0) {
387 out.print(" version='"+osm.getVersion()+'\'');
388 }
389 if (this.changeset != null && this.changeset.getId() != 0) {
390 out.print(" changeset='"+this.changeset.getId()+'\'');
391 } else if (osm.getChangesetId() > 0 && !osm.isNew()) {
392 out.print(" changeset='"+osm.getChangesetId()+'\'');
393 }
394 }
395}
Note: See TracBrowser for help on using the repository browser.