1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.io.Closeable;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.PrintWriter;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Helper class to use for xml outputting classes.
|
---|
12 | *
|
---|
13 | * @author imi
|
---|
14 | */
|
---|
15 | public class XmlWriter implements Closeable {
|
---|
16 |
|
---|
17 | protected final PrintWriter out;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Constructs a new {@code XmlWriter}.
|
---|
21 | * @param out print writer
|
---|
22 | */
|
---|
23 | public XmlWriter(PrintWriter out) {
|
---|
24 | this.out = out;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Flushes the stream.
|
---|
29 | */
|
---|
30 | public void flush() {
|
---|
31 | if (out != null) {
|
---|
32 | out.flush();
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
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 | */
|
---|
43 | public static String encode(String unencoded) {
|
---|
44 | return encode(unencoded, false);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Encode the given string in XML1.0 format.
|
---|
49 | * Optimized to fast pass strings that don't need encoding (normal case).
|
---|
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
|
---|
54 | * new JLabel("<html>&apos;</html>")
|
---|
55 | * literally as 6 character string, see #7558)
|
---|
56 | * @return XML1.0 string
|
---|
57 | */
|
---|
58 | public static String encode(String unencoded, boolean keepApos) {
|
---|
59 | StringBuilder buffer = null;
|
---|
60 | if (unencoded != null) {
|
---|
61 | for (int i = 0; i < unencoded.length(); ++i) {
|
---|
62 | String encS = null;
|
---|
63 | if (!keepApos || unencoded.charAt(i) != '\'') {
|
---|
64 | encS = XmlWriter.encoding.get(unencoded.charAt(i));
|
---|
65 | }
|
---|
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 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return (buffer == null) ? unencoded : buffer.toString();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * The output writer to save the values to.
|
---|
81 | */
|
---|
82 | private static final Map<Character, String> encoding = new HashMap<>();
|
---|
83 | static {
|
---|
84 | encoding.put('<', "<");
|
---|
85 | encoding.put('>', ">");
|
---|
86 | encoding.put('"', """);
|
---|
87 | encoding.put('\'', "'");
|
---|
88 | encoding.put('&', "&");
|
---|
89 | encoding.put('\n', "
");
|
---|
90 | encoding.put('\r', "
");
|
---|
91 | encoding.put('\t', "	");
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public void close() throws IOException {
|
---|
96 | if (out != null) {
|
---|
97 | out.close();
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|