Coverage Report - org.apache.myfaces.shared.renderkit.html.util.UnicodeEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
UnicodeEncoder
43%
29/67
39%
19/48
8
 
 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.shared.renderkit.html.util;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.Writer;
 23  
 
 24  
 /**
 25  
  * Converts characters outside of latin-1 set in a string to numeric character references.
 26  
  * 
 27  
  */
 28  0
 public abstract class UnicodeEncoder
 29  
 {
 30  
     /**
 31  
      * Encodes the given string, so that it can be used within a html page.
 32  
      * @param string the string to convert
 33  
      */
 34  
     public static String encode (String string)
 35  
     {
 36  3
         if (string == null)
 37  
         {
 38  0
             return "";
 39  
         }
 40  
 
 41  3
         StringBuilder sb = null;
 42  
         char c;
 43  7
         for (int i = 0; i < string.length (); ++i)
 44  
         {
 45  4
             c = string.charAt(i);
 46  4
             if (((int)c) >= 0x80)
 47  
             {
 48  3
                 if( sb == null )
 49  
                 {
 50  3
                     sb = new StringBuilder( string.length()+4 );
 51  3
                     sb.append( string.substring(0,i) );
 52  
                 }
 53  
                 //encode all non basic latin characters
 54  3
                 sb.append("&#");
 55  3
                 sb.append((int)c);
 56  3
                 sb.append(";");
 57  
             }
 58  1
             else if( sb != null )
 59  
             {
 60  0
                 sb.append(c);
 61  
             }
 62  
         }
 63  
 
 64  3
         return sb != null ? sb.toString() : string;
 65  
     }
 66  
     
 67  
     public static void encode (Writer writer, String string) throws IOException
 68  
     {
 69  9
         if (string == null)
 70  
         {
 71  0
             return;
 72  
         }
 73  
 
 74  9
         int start = 0;
 75  
         char c;
 76  141
         for (int i = 0; i < string.length (); ++i)
 77  
         {
 78  132
             c = string.charAt(i);
 79  132
             if (((int)c) >= 0x80)
 80  
             {
 81  4
                 if (start < i)
 82  
                 {
 83  2
                     writer.write(string, start, i-start);
 84  
                 }
 85  4
                 start = i+1;
 86  
                 //encode all non basic latin characters
 87  4
                 writer.write("&#");
 88  4
                 writer.write(Integer.toString((int)c));
 89  4
                 writer.write(";");
 90  
             }
 91  
         }
 92  
 
 93  9
         if (start == 0)
 94  
         {
 95  5
             writer.write(string);
 96  
         }
 97  4
         else if (start < string.length())
 98  
         {
 99  2
             writer.write(string,start,string.length()-start);
 100  
         }
 101  9
     }
 102  
 
 103  
     public static void encode (Writer writer, char[] cbuf, int off, int len) throws IOException
 104  
     {
 105  0
         if (cbuf == null)
 106  
         {
 107  0
             return;
 108  
         }
 109  
 
 110  0
         int start = off;
 111  
         char c;
 112  0
         for (int i = off; i < off+len; ++i)
 113  
         {
 114  0
             c = cbuf[i];
 115  0
             if (((int)c) >= 0x80)
 116  
             {
 117  0
                 if (start < i)
 118  
                 {
 119  0
                     writer.write(cbuf, start, i-start);
 120  
                 }
 121  0
                 start = i+1;
 122  
                 //encode all non basic latin characters
 123  0
                 writer.write("&#");
 124  0
                 writer.write(Integer.toString((int)c));
 125  0
                 writer.write(";");
 126  
             }
 127  
         }
 128  
 
 129  0
         if (start == off)
 130  
         {
 131  0
             writer.write(cbuf, off, len);
 132  
         }
 133  0
         else if (start < off+len)
 134  
         {
 135  0
             writer.write(cbuf,start,off+len-start);
 136  
         }
 137  0
     }
 138  
     
 139  
     public static void encode (Writer writer, String cbuf, int off, int len) throws IOException
 140  
     {
 141  0
         if (cbuf == null)
 142  
         {
 143  0
             return;
 144  
         }
 145  
 
 146  0
         int start = off;
 147  
         char c;
 148  0
         for (int i = off; i < off+len; ++i)
 149  
         {
 150  0
             c = cbuf.charAt(i);
 151  0
             if (((int)c) >= 0x80)
 152  
             {
 153  0
                 if (start < i)
 154  
                 {
 155  0
                     writer.write(cbuf, start, i-start);
 156  
                 }
 157  0
                 start = i+1;
 158  
                 //encode all non basic latin characters
 159  0
                 writer.write("&#");
 160  0
                 writer.write(Integer.toString((int)c));
 161  0
                 writer.write(";");
 162  
             }
 163  
         }
 164  
 
 165  0
         if (start == off)
 166  
         {
 167  0
             writer.write(cbuf, off, len);
 168  
         }
 169  0
         else if (start < off+len)
 170  
         {
 171  0
             writer.write(cbuf,start,off+len-start);
 172  
         }
 173  0
     }
 174  
 }