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