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