View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // init lookup table
32      CHARS_TO_ESCAPE = new char[0xA0][];
33  
34      for (int i = 0; i < 0x20; i++) {
35        CHARS_TO_ESCAPE[i] = EMPTY; // Control characters
36      }
37  
38      CHARS_TO_ESCAPE['\t'] = "&#09;".toCharArray(); // Horizontal tabulator
39      CHARS_TO_ESCAPE['\n'] = "&#10;".toCharArray(); // Line feed
40      CHARS_TO_ESCAPE['\r'] = "&#13;".toCharArray(); // Carriage return
41  
42      CHARS_TO_ESCAPE['"'] = "&quot;".toCharArray();
43      CHARS_TO_ESCAPE['&'] = "&amp;".toCharArray();
44      CHARS_TO_ESCAPE['<'] = "&lt;".toCharArray();
45      CHARS_TO_ESCAPE['>'] = "&gt;".toCharArray();
46      CHARS_TO_ESCAPE['\\'] = "\\\\".toCharArray();
47  
48      CHARS_TO_ESCAPE[0x7F] = EMPTY; // Delete
49  
50      for (int i = 0x80; i < 0xA0; i++) {
51        CHARS_TO_ESCAPE[i] = EMPTY; // Control characters
52      }
53  
54      // all "normal" character positions contains null
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        // no need to escape
79        out.write(text, start, length);
80      } else {
81        // write until localIndex and then encode the remainder
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          // Tilde or less...
90          if (ch < CHARS_TO_ESCAPE.length) {
91            if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
92              // HTML 4.0, section B.7.1: ampersands followed by
93              // an open brace don't get escaped
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           // ISO-8859-1 entities: encode as needed
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           // Double-byte characters to encode.
114           // PENDING: when outputting to an encoding that
115           // supports double-byte characters (UTF-8, for example),
116           // we should not be encoding
117           writeDecRef(ch);
118         }
119       }
120 
121       buffer.flushBuffer();
122     }
123   }
124 }