001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import java.io.Closeable;
005import java.io.IOException;
006import java.io.PrintWriter;
007import java.util.HashMap;
008import java.util.Map;
009
010/**
011 * Helper class to use for xml outputting classes.
012 *
013 * @author imi
014 */
015public class XmlWriter implements Closeable {
016
017    protected final PrintWriter out;
018
019    /**
020     * Constructs a new {@code XmlWriter}.
021     * @param out print writer
022     */
023    public XmlWriter(PrintWriter out) {
024        this.out = out;
025    }
026
027    /**
028     * Flushes the stream.
029     */
030    public void flush() {
031        if (out != null) {
032            out.flush();
033        }
034    }
035
036    /**
037     * Encode the given string in XML1.0 format.
038     * Optimized to fast pass strings that don't need encoding (normal case).
039     *
040     * @param unencoded the unencoded input string
041     * @return XML1.0 string
042     */
043    public static String encode(String unencoded) {
044        return encode(unencoded, false);
045    }
046
047    /**
048     * Encode the given string in XML1.0 format.
049     * Optimized to fast pass strings that don't need encoding (normal case).
050     *
051     * @param unencoded the unencoded input string
052     * @param keepApos true if apostrophe sign should stay as it is (in order to work around
053     * a Java bug that renders
054     *     new JLabel("<html>'</html>")
055     * literally as 6 character string, see #7558)
056     * @return XML1.0 string
057     */
058    public static String encode(String unencoded, boolean keepApos) {
059        StringBuilder buffer = null;
060        for (int i = 0; i < unencoded.length(); ++i) {
061            String encS = null;
062            if (!keepApos || unencoded.charAt(i) != '\'') {
063                encS = XmlWriter.encoding.get(unencoded.charAt(i));
064            }
065            if (encS != null) {
066                if (buffer == null) {
067                    buffer = new StringBuilder(unencoded.substring(0, i));
068                }
069                buffer.append(encS);
070            } else if (buffer != null) {
071                buffer.append(unencoded.charAt(i));
072            }
073        }
074        return (buffer == null) ? unencoded : buffer.toString();
075    }
076
077    /**
078     * The output writer to save the values to.
079     */
080    private static final Map<Character, String> encoding = new HashMap<>();
081    static {
082        encoding.put('<', "&lt;");
083        encoding.put('>', "&gt;");
084        encoding.put('"', "&quot;");
085        encoding.put('\'', "&apos;");
086        encoding.put('&', "&amp;");
087        encoding.put('\n', "&#xA;");
088        encoding.put('\r', "&#xD;");
089        encoding.put('\t', "&#x9;");
090    }
091
092    @Override
093    public void close() throws IOException {
094        if (out != null) {
095            out.close();
096        }
097    }
098}