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  package org.apache.myfaces.util;
20  
21  import java.io.FilterWriter;
22  import java.io.IOException;
23  import java.io.Writer;
24  
25  /**
26   *
27   * @author Leonardo Uribe
28   */
29  public class CDataEndEscapeFilterWriter extends FilterWriter
30  {
31      private char c1;
32      private char c2;
33      private int pos;
34  
35      public CDataEndEscapeFilterWriter(Writer out)
36      {
37          super(out);
38          pos = 0;
39      }
40  
41      @Override
42      public void write(int c) throws IOException
43      {
44          super.write(c);
45          c1 = c2;
46          c2 = (char) c;
47          pos ++;
48          if (pos > 2)
49          {
50              if (c1 == ']' && c2 == ']' && c == '>')
51              {
52                  //"]]><![CDATA[]]]]><![CDATA[>"
53                  out.write("<![CDATA[]]]]><![CDATA[>");
54              }
55          }
56      }
57  
58      @Override
59      public void write(char[] cbuf, int off, int len) throws IOException
60      {
61          int index = off;
62          for (int i = 0; i < len; i++)
63          {
64              char c = cbuf[off+i];
65              if (c1 == ']' && c2 == ']' && c == '>')
66              {
67                  super.write(cbuf, index, i+1 - ( index - off ) ); 
68                  index = off+i+1;
69                  out.write("<![CDATA[]]]]><![CDATA[>");
70              }
71              c1 = c2;
72              c2 = (char) cbuf[off+i];
73              pos++;
74          }
75          if (index < off+len)
76          {
77              super.write(cbuf, index, off+len-index);
78          }
79      }
80  
81      @Override
82      public void write(String str, int off, int len) throws IOException
83      {
84          int index = off;
85          for (int i = 0; i < len; i++)
86          {
87              char c = str.charAt(off+i);
88              if (c1 == ']' && c2 == ']' && c == '>')
89              {
90                  super.write(str, index, i+1 - ( index - off ) );
91                  index = off+i+1;
92                  out.write("<![CDATA[]]]]><![CDATA[>");
93              }
94              c1 = c2;
95              c2 = (char) str.charAt(off+i);
96              pos++;
97          }
98          if (index < off+len)
99          {
100             super.write(str, index, off+len-index);
101         }
102     }
103 }