1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.BufferedWriter;
|
---|
5 | import java.io.OutputStream;
|
---|
6 | import java.io.OutputStreamWriter;
|
---|
7 | import java.io.PrintWriter;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.text.DateFormat;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.notes.Note;
|
---|
12 | import org.openstreetmap.josm.data.notes.NoteComment;
|
---|
13 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
14 | import org.openstreetmap.josm.data.osm.User;
|
---|
15 | import 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 | */
|
---|
23 | public class NoteWriter extends XmlWriter {
|
---|
24 |
|
---|
25 | private final DateFormat iso8601Format = DateUtils.newIsoDateTimeFormat();
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Create a NoteWriter that will write to the given PrintWriter
|
---|
29 | * @param out PrintWriter to write XML to
|
---|
30 | */
|
---|
31 | public NoteWriter(PrintWriter out) {
|
---|
32 | super(out);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Create a NoteWriter that will write to a given OutputStream.
|
---|
37 | * @param out OutputStream to write XML to
|
---|
38 | */
|
---|
39 | public NoteWriter(OutputStream out) {
|
---|
40 | super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Write notes to designated output target
|
---|
45 | * @param data Note collection to write
|
---|
46 | */
|
---|
47 | public void write(NoteData data) {
|
---|
48 | out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
|
---|
49 | out.println("<osm-notes>");
|
---|
50 | for (Note note : data.getNotes()) {
|
---|
51 | out.print(" <note ");
|
---|
52 | out.print("id=\"" + note.getId() + "\" ");
|
---|
53 | out.print("lat=\"" + note.getLatLon().lat() + "\" ");
|
---|
54 | out.print("lon=\"" + note.getLatLon().lon() + "\" ");
|
---|
55 | out.print("created_at=\"" + iso8601Format.format(note.getCreatedAt()) + "\" ");
|
---|
56 | if (note.getClosedAt() != null) {
|
---|
57 | out.print("closed_at=\"" + iso8601Format.format(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=\"" + iso8601Format.format(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 | }
|
---|