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['\''] = "&#39;".toCharArray();
43      CHARS_TO_ESCAPE['"'] = "\\\"".toCharArray();
44      CHARS_TO_ESCAPE['&'] = "&amp;".toCharArray();
45      CHARS_TO_ESCAPE['<'] = "&lt;".toCharArray();
46      CHARS_TO_ESCAPE['>'] = "&gt;".toCharArray();
47      CHARS_TO_ESCAPE['\\'] = "\\\\".toCharArray();
48  
49      CHARS_TO_ESCAPE[0x7F] = EMPTY; // Delete
50  
51      for (int i = 0x80; i < 0xA0; i++) {
52        CHARS_TO_ESCAPE[i] = EMPTY; // Control characters
53      }
54  
55      // all "normal" character positions contains null
56    }
57  
58    public JsonWriterUtils(final Writer out, final String characterEncoding) {
59      super(out, characterEncoding);
60    }
61  
62    @Override
63    protected void writeEncodedValue(final char[] text, final int start,
64        final int length, final boolean isAttribute) throws IOException {
65  
66      int localIndex = -1;
67  
68      final int end = start + length;
69      for (int i = start; i < end; i++) {
70        final char ch = text[i];
71        if (ch >= CHARS_TO_ESCAPE.length || CHARS_TO_ESCAPE[ch] != null) {
72          localIndex = i;
73          break;
74        }
75      }
76      final Writer out = getOut();
77  
78      if (localIndex == -1) {
79        // no need to escape
80        out.write(text, start, length);
81      } else {
82        // write until localIndex and then encode the remainder
83        out.write(text, start, localIndex);
84  
85        final ResponseWriterBuffer buffer = getBuffer();
86  
87        for (int i = localIndex; i < end; i++) {
88          final char ch = text[i];
89  
90          // Tilde or less...
91          if (ch < CHARS_TO_ESCAPE.length) {
92            if (isAttribute && ch == '&' && (i + 1 < end) && text[i + 1] == '{') {
93              // HTML 4.0, section B.7.1: ampersands followed by
94              // an open brace don't get escaped
95              buffer.addToBuffer('&');
96            } else if (CHARS_TO_ESCAPE[ch] != null) {
97              buffer.addToBuffer(CHARS_TO_ESCAPE[ch]);
98            } else {
99              buffer.addToBuffer(ch);
100           }
101         } else if (isUtf8()) {
102           buffer.addToBuffer(ch);
103         } else if (ch <= 0xff) {
104           // ISO-8859-1 entities: encode as needed
105           buffer.flushBuffer();
106 
107           out.write('&');
108           final char[] chars = ISO8859_1_ENTITIES[ch - 0xA0];
109           out.write(chars, 0, chars.length);
110           out.write(';');
111         } else {
112           buffer.flushBuffer();
113 
114           // Double-byte characters to encode.
115           // PENDING: when outputting to an encoding that
116           // supports double-byte characters (UTF-8, for example),
117           // we should not be encoding
118           writeDecRef(ch);
119         }
120       }
121 
122       buffer.flushBuffer();
123     }
124   }
125 }