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