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