1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.internal.util;
21
22 import java.io.IOException;
23 import java.io.Writer;
24
25 public final class HtmlWriterUtils extends WriterUtils {
26
27 private static final char[][] CHARS_TO_ESCAPE;
28
29 static {
30
31 CHARS_TO_ESCAPE = new char[0xA0][];
32
33 for (int i = 0; i < 0x20; i++) {
34 CHARS_TO_ESCAPE[i] = EMPTY;
35 }
36
37 CHARS_TO_ESCAPE['\t'] = "	".toCharArray();
38 CHARS_TO_ESCAPE['\n'] = " ".toCharArray();
39 CHARS_TO_ESCAPE['\r'] = " ".toCharArray();
40
41 CHARS_TO_ESCAPE['"'] = """.toCharArray();
42 CHARS_TO_ESCAPE['&'] = "&".toCharArray();
43 CHARS_TO_ESCAPE['<'] = "<".toCharArray();
44 CHARS_TO_ESCAPE['>'] = ">".toCharArray();
45
46 CHARS_TO_ESCAPE[0x7F] = EMPTY;
47
48 for (int i = 0x80; i < 0xA0; i++) {
49 CHARS_TO_ESCAPE[i] = EMPTY;
50 }
51
52
53 }
54
55 public HtmlWriterUtils(final Writer out, final String characterEncoding) {
56 super(out, characterEncoding);
57 }
58
59 @Override
60 protected void writeEncodedValue(final char[] text, final int start,
61 final int length, final boolean isAttribute) throws IOException {
62
63 int localIndex = -1;
64
65 final int end = start + length;
66 for (int i = start; i < end; i++) {
67 char ch = text[i];
68 if (ch >= CHARS_TO_ESCAPE.length || CHARS_TO_ESCAPE[ch] != null) {
69 localIndex = i;
70 break;
71 }
72 }
73 final Writer out = getOut();
74
75 if (localIndex == -1) {
76
77 out.write(text, start, length);
78 } else {
79
80 out.write(text, start, localIndex);
81
82 final ResponseWriterBuffer buffer = getBuffer();
83
84 for (int i = localIndex; i < end; i++) {
85 final char ch = text[i];
86
87
88 if (ch < CHARS_TO_ESCAPE.length) {
89 if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
90
91
92 buffer.addToBuffer('&');
93 } else if (CHARS_TO_ESCAPE[ch] != null) {
94 buffer.addToBuffer(CHARS_TO_ESCAPE[ch]);
95 } else {
96 buffer.addToBuffer(ch);
97 }
98 } else if (isUtf8()) {
99 buffer.addToBuffer(ch);
100 } else if (ch <= 0xff) {
101
102 buffer.flushBuffer();
103
104 out.write('&');
105 char[] chars = ISO8859_1_ENTITIES[ch - 0xA0];
106 out.write(chars, 0, chars.length);
107 out.write(';');
108 } else {
109 buffer.flushBuffer();
110
111
112
113
114
115 writeDecRef(ch);
116 }
117 }
118
119 buffer.flushBuffer();
120 }
121 }
122 }