Coverage Report - org.apache.myfaces.util.CDataEndEscapeFilterWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
CDataEndEscapeFilterWriter
0%
0/37
0%
0/28
4.5
 
 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  0
         super(out);
 38  0
         pos = 0;
 39  0
     }
 40  
 
 41  
     @Override
 42  
     public void write(int c) throws IOException
 43  
     {
 44  0
         super.write(c);
 45  0
         c1 = c2;
 46  0
         c2 = (char) c;
 47  0
         pos ++;
 48  0
         if (pos > 2)
 49  
         {
 50  0
             if (c1 == ']' && c2 == ']' && c == '>')
 51  
             {
 52  
                 //"]]><![CDATA[]]]]><![CDATA[>"
 53  0
                 out.write("<![CDATA[]]]]><![CDATA[>");
 54  
             }
 55  
         }
 56  0
     }
 57  
 
 58  
     @Override
 59  
     public void write(char[] cbuf, int off, int len) throws IOException
 60  
     {
 61  0
         int index = off;
 62  0
         for (int i = 0; i < len; i++)
 63  
         {
 64  0
             char c = cbuf[off+i];
 65  0
             if (c1 == ']' && c2 == ']' && c == '>')
 66  
             {
 67  0
                 super.write(cbuf, index, i+1 - ( index - off ) ); 
 68  0
                 index = off+i+1;
 69  0
                 out.write("<![CDATA[]]]]><![CDATA[>");
 70  
             }
 71  0
             c1 = c2;
 72  0
             c2 = c;
 73  0
             pos++;
 74  
         }
 75  0
         if (index < off+len)
 76  
         {
 77  0
             super.write(cbuf, index, off+len-index);
 78  
         }
 79  0
     }
 80  
 
 81  
     @Override
 82  
     public void write(String str, int off, int len) throws IOException
 83  
     {
 84  0
         int index = off;
 85  0
         for (int i = 0; i < len; i++)
 86  
         {
 87  0
             char c = str.charAt(off+i);
 88  0
             if (c1 == ']' && c2 == ']' && c == '>')
 89  
             {
 90  0
                 super.write(str, index, i+1 - ( index - off ) );
 91  0
                 index = off+i+1;
 92  0
                 out.write("<![CDATA[]]]]><![CDATA[>");
 93  
             }
 94  0
             c1 = c2;
 95  0
             c2 = c;
 96  0
             pos++;
 97  
         }
 98  0
         if (index < off+len)
 99  
         {
 100  0
             super.write(str, index, off+len-index);
 101  
         }
 102  0
     }
 103  
 }