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

Last change on this file since 17379 was 17333, checked in by Don-vip, 3 years ago

see #20129 - Fix typos and misspellings in the code (patch by gaben)

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