source: josm/trunk/src/org/openstreetmap/josm/io/NoteWriter.java@ 18636

Last change on this file since 18636 was 17712, checked in by simon04, 3 years ago

see #14176 - Migrate Note/NoteComment to Instant

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.BufferedWriter;
5import java.io.OutputStream;
6import java.io.OutputStreamWriter;
7import java.io.PrintWriter;
8import java.nio.charset.StandardCharsets;
9
10import org.openstreetmap.josm.data.coor.LatLon;
11import org.openstreetmap.josm.data.notes.Note;
12import org.openstreetmap.josm.data.notes.NoteComment;
13import org.openstreetmap.josm.data.osm.NoteData;
14import org.openstreetmap.josm.data.osm.User;
15
16/**
17 * Class to write a collection of notes out to XML.
18 * The format is that of the note dump file with the addition of one
19 * attribute in the comment element to indicate if the comment is a new local
20 * comment that has not been uploaded to the OSM server yet.
21 * @since 7732
22 */
23public class NoteWriter extends XmlWriter {
24
25 /**
26 * Create a NoteWriter that will write to the given PrintWriter
27 * @param out PrintWriter to write XML to
28 */
29 public NoteWriter(PrintWriter out) {
30 super(out);
31 }
32
33 /**
34 * Create a NoteWriter that will write to a given OutputStream.
35 * @param out OutputStream to write XML to
36 */
37 public NoteWriter(OutputStream out) {
38 super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))));
39 }
40
41 /**
42 * Write notes to designated output target
43 * @param data Note collection to write
44 */
45 public void write(NoteData data) {
46 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
47 out.println("<osm-notes>");
48 for (Note note : data.getNotes()) {
49 LatLon ll = note.getLatLon();
50 out.print(" <note ");
51 out.print("id=\"" + note.getId() + "\" ");
52 out.print("lat=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lat()) + "\" ");
53 out.print("lon=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lon()) + "\" ");
54 out.print("created_at=\"" + note.getCreatedAt() + "\" ");
55 if (note.getClosedAt() != null) {
56 out.print("closed_at=\"" + note.getClosedAt() + "\" ");
57 }
58
59 out.println(">");
60 for (NoteComment comment : note.getComments()) {
61 writeComment(comment);
62 }
63 out.println(" </note>");
64 }
65
66 out.println("</osm-notes>");
67 out.flush();
68 }
69
70 private void writeComment(NoteComment comment) {
71 out.print(" <comment");
72 out.print(" action=\"" + comment.getNoteAction() + "\" ");
73 out.print("timestamp=\"" + comment.getCommentTimestamp() + "\" ");
74 if (comment.getUser() != null && !comment.getUser().equals(User.getAnonymous())) {
75 out.print("uid=\"" + comment.getUser().getId() + "\" ");
76 out.print("user=\"" + encode(comment.getUser().getName()) + "\" ");
77 }
78 out.print("is_new=\"" + comment.isNew() + "\" ");
79 out.print(">");
80 out.print(encode(comment.getText(), false));
81 out.println("</comment>");
82 }
83}
Note: See TracBrowser for help on using the repository browser.