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 import java.nio.charset.Charset;
25
26 public final class HtmlWriterHelper extends WriterHelper {
27
28 private static final char[][] CHARS_TO_ESCAPE;
29
30 static {
31
32 CHARS_TO_ESCAPE = new char[0xA0][];
33
34
35
36
37 for (int i = 0; i < 0x20; i++) {
38 CHARS_TO_ESCAPE[i] = EMPTY;
39 }
40 for (int i = 0x7F; i < 0xA0; i++) {
41 CHARS_TO_ESCAPE[i] = EMPTY;
42 }
43
44 CHARS_TO_ESCAPE['\t'] = "	".toCharArray();
45 CHARS_TO_ESCAPE['\n'] = "
".toCharArray();
46 CHARS_TO_ESCAPE['\r'] = "
".toCharArray();
47
48
49 CHARS_TO_ESCAPE['\''] = "'".toCharArray();
50 CHARS_TO_ESCAPE['&'] = "&".toCharArray();
51 CHARS_TO_ESCAPE['<'] = "<".toCharArray();
52 CHARS_TO_ESCAPE['>'] = ">".toCharArray();
53
54
55
56
57
58 }
59
60
61
62
63 @Deprecated
64 public HtmlWriterHelper(final Writer out, final String characterEncoding) {
65 this(out, Charset.forName(characterEncoding));
66 }
67
68 public HtmlWriterHelper(final Writer out, final Charset charset) {
69 super(out, charset);
70 }
71
72 @Override
73 protected void writeEncodedValue(final char[] text, final int start,
74 final int length, final boolean isAttribute) throws IOException {
75
76 int localIndex = -1;
77
78 final int end = start + length;
79 for (int i = start; i < end; i++) {
80 final char ch = text[i];
81 if (ch >= CHARS_TO_ESCAPE.length || CHARS_TO_ESCAPE[ch] != null) {
82 localIndex = i;
83 break;
84 }
85 }
86 final Writer out = getOut();
87
88 if (localIndex == -1) {
89
90 out.write(text, start, length);
91 } else {
92
93 out.write(text, start, localIndex);
94
95 final ResponseWriterBuffer buffer = getBuffer();
96
97 for (int i = localIndex; i < end; i++) {
98 final char ch = text[i];
99
100
101 if (ch < CHARS_TO_ESCAPE.length) {
102 if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
103
104
105 buffer.addToBuffer('&');
106 } else if (CHARS_TO_ESCAPE[ch] != null) {
107 buffer.addToBuffer(CHARS_TO_ESCAPE[ch]);
108 } else {
109 buffer.addToBuffer(ch);
110 }
111 } else if (isUtf8()) {
112 buffer.addToBuffer(ch);
113 } else if (ch <= 0xff) {
114
115 buffer.flushBuffer();
116
117 out.write('&');
118 final char[] chars = ISO8859_1_ENTITIES[ch - 0xA0];
119 out.write(chars, 0, chars.length);
120 out.write(';');
121 } else {
122 buffer.flushBuffer();
123
124
125
126
127
128 writeDecRef(ch);
129 }
130 }
131
132 buffer.flushBuffer();
133 }
134 }
135 }