source: josm/trunk/src/org/openstreetmap/josm/io/XmlWriter.java@ 13647

Last change on this file since 13647 was 12537, checked in by Don-vip, 7 years ago

PMD - VariableNamingConventions

  • Property svn:eol-style set to native
File size: 2.8 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[143]2package org.openstreetmap.josm.io;
[72]3
[5874]4import java.io.Closeable;
5import java.io.IOException;
[143]6import java.io.PrintWriter;
[72]7import java.util.HashMap;
[6316]8import java.util.Map;
[72]9
10/**
11 * Helper class to use for xml outputting classes.
[1169]12 *
[72]13 * @author imi
14 */
[5874]15public class XmlWriter implements Closeable {
[72]16
[5874]17 protected final PrintWriter out;
[1677]18
[9231]19 /**
20 * Constructs a new {@code XmlWriter}.
21 * @param out print writer
22 */
[1523]23 public XmlWriter(PrintWriter out) {
[1169]24 this.out = out;
25 }
[1677]26
[5874]27 /**
28 * Flushes the stream.
29 */
[4645]30 public void flush() {
[5874]31 if (out != null) {
32 out.flush();
33 }
[4645]34 }
35
[9231]36 /**
37 * Encode the given string in XML1.0 format.
38 * Optimized to fast pass strings that don't need encoding (normal case).
39 *
40 * @param unencoded the unencoded input string
41 * @return XML1.0 string
42 */
[5146]43 public static String encode(String unencoded) {
44 return encode(unencoded, false);
45 }
46
[1169]47 /**
48 * Encode the given string in XML1.0 format.
49 * Optimized to fast pass strings that don't need encoding (normal case).
[5146]50 *
51 * @param unencoded the unencoded input string
52 * @param keepApos true if apostrophe sign should stay as it is (in order to work around
53 * a Java bug that renders
[6830]54 * new JLabel("<html>'</html>")
[5146]55 * literally as 6 character string, see #7558)
[8929]56 * @return XML1.0 string
[1169]57 */
[5146]58 public static String encode(String unencoded, boolean keepApos) {
[1169]59 StringBuilder buffer = null;
[9828]60 if (unencoded != null) {
61 for (int i = 0; i < unencoded.length(); ++i) {
62 String encS = null;
63 if (!keepApos || unencoded.charAt(i) != '\'') {
[12537]64 encS = ENCODING.get(unencoded.charAt(i));
[4645]65 }
[9828]66 if (encS != null) {
67 if (buffer == null) {
68 buffer = new StringBuilder(unencoded.substring(0, i));
69 }
70 buffer.append(encS);
71 } else if (buffer != null) {
72 buffer.append(unencoded.charAt(i));
73 }
[4645]74 }
[1169]75 }
76 return (buffer == null) ? unencoded : buffer.toString();
77 }
[72]78
[1169]79 /**
80 * The output writer to save the values to.
81 */
[12537]82 private static final Map<Character, String> ENCODING = new HashMap<>();
[1169]83 static {
[12537]84 ENCODING.put('<', "&lt;");
85 ENCODING.put('>', "&gt;");
86 ENCODING.put('"', "&quot;");
87 ENCODING.put('\'', "&apos;");
88 ENCODING.put('&', "&amp;");
89 ENCODING.put('\n', "&#xA;");
90 ENCODING.put('\r', "&#xD;");
91 ENCODING.put('\t', "&#x9;");
[1169]92 }
[6070]93
[5874]94 @Override
95 public void close() throws IOException {
96 if (out != null) {
97 out.close();
98 }
99 }
[72]100}
Note: See TracBrowser for help on using the repository browser.