1 package gboat2.base.bridge.util.xml;
2
3 import java.io.IOException;
4 import java.io.Writer;
5
6 import com.sun.org.apache.xml.internal.utils.XML11Char;
7 import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
8
9
10
11
12
13
14
15 public class XmlCharacterEscapeHandler implements CharacterEscapeHandler {
16
17
18
19
20
21
22 @Override
23 public void escape(char[] buf, int start, int len, boolean isAttValue, Writer out) throws IOException {
24 for (int i = start; i < (start + len); i++) {
25 char ch = buf[i];
26
27
28 if (ch == '&') {
29 out.write("&");
30 continue;
31 }
32
33 if (ch == '<') {
34 out.write("<");
35 continue;
36 }
37
38 if (ch == '>') {
39 out.write(">");
40 continue;
41 }
42
43 if ((ch == '"') && isAttValue) {
44
45
46 out.write(""");
47 continue;
48 }
49
50 if ((ch == '\'') && isAttValue) {
51 out.write("'");
52 continue;
53 }
54
55
56 if (ch > 0x7F) {
57
58 out.write("&#x");
59 out.write(Integer.toHexString(ch));
60 out.write(";");
61 continue;
62 }
63
64
65 if (XML11Char.isXML11Valid(ch)) {
66 out.write(ch);
67 }
68 }
69 }
70 }