001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.io.BufferedWriter;
005import java.io.OutputStream;
006import java.io.OutputStreamWriter;
007import java.io.PrintWriter;
008import java.nio.charset.StandardCharsets;
009
010import org.openstreetmap.josm.data.coor.LatLon;
011import org.openstreetmap.josm.data.notes.Note;
012import org.openstreetmap.josm.data.notes.NoteComment;
013import org.openstreetmap.josm.data.osm.NoteData;
014import org.openstreetmap.josm.data.osm.User;
015
016/**
017 * Class to write a collection of notes out to XML.
018 * The format is that of the note dump file with the addition of one
019 * attribute in the comment element to indicate if the comment is a new local
020 * comment that has not been uploaded to the OSM server yet.
021 * @since 7732
022 */
023public class NoteWriter extends XmlWriter {
024
025    /**
026     * Create a NoteWriter that will write to the given PrintWriter
027     * @param out PrintWriter to write XML to
028     */
029    public NoteWriter(PrintWriter out) {
030        super(out);
031    }
032
033    /**
034     * Create a NoteWriter that will write to a given OutputStream.
035     * @param out OutputStream to write XML to
036     */
037    public NoteWriter(OutputStream out) {
038        super(new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8))));
039    }
040
041    /**
042     * Write notes to designated output target
043     * @param data Note collection to write
044     */
045    public void write(NoteData data) {
046        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
047        out.println("<osm-notes>");
048        for (Note note : data.getNotes()) {
049            LatLon ll = note.getLatLon();
050            out.print("  <note ");
051            out.print("id=\"" + note.getId() + "\" ");
052            out.print("lat=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lat()) + "\" ");
053            out.print("lon=\"" + LatLon.cDdHighPrecisionFormatter.format(ll.lon()) + "\" ");
054            out.print("created_at=\"" + note.getCreatedAt() + "\" ");
055            if (note.getClosedAt() != null) {
056                out.print("closed_at=\"" + note.getClosedAt() + "\" ");
057            }
058
059            out.println(">");
060            for (NoteComment comment : note.getComments()) {
061                writeComment(comment);
062            }
063            out.println("  </note>");
064        }
065
066        out.println("</osm-notes>");
067        out.flush();
068    }
069
070    private void writeComment(NoteComment comment) {
071        out.print("    <comment");
072        out.print(" action=\"" + comment.getNoteAction() + "\" ");
073        out.print("timestamp=\"" + comment.getCommentTimestamp() + "\" ");
074        if (comment.getUser() != null && !comment.getUser().equals(User.getAnonymous())) {
075            out.print("uid=\"" + comment.getUser().getId() + "\" ");
076            out.print("user=\"" + encode(comment.getUser().getName()) + "\" ");
077        }
078        out.print("is_new=\"" + comment.isNew() + "\" ");
079        out.print(">");
080        out.print(encode(comment.getText(), false));
081        out.println("</comment>");
082    }
083}