Coverage Report - org.apache.tapestry.util.text.MarkupCharacterTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
MarkupCharacterTranslator
0%
0/25
0%
0/8
2.4
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.util.text;
 16  
 
 17  
 /**
 18  
  * An object that encodes a character according to rules of the HTML
 19  
  * specification, so that it will be properly parsed by a browser irrespectively
 20  
  * of the character encoding used in the HTML output.
 21  
  *
 22  
  * @author mb
 23  
  * @since 4.0
 24  
  */
 25  
 public class MarkupCharacterTranslator implements ICharacterTranslator {
 26  
 
 27  
     private static final String SAFE_CHARACTERS = "01234567890"
 28  
                                                   + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 29  
                                                   + "\t\n\r !#$%'()*+,-./:;=?@[\\]^_`{|}~";
 30  
 
 31  0
     private static final String[][] ENTITIES = {
 32  
             {"\"", """},
 33  
             {"<", "&lt;"}, {">", "&gt;"}, {"&", "&amp;"}
 34  
     };
 35  
 
 36  0
     private static final ICharacterMatcher SAFE_MATCHER = new AsciiCharacterMatcher(SAFE_CHARACTERS);
 37  0
     private static final ICharacterTranslator ENTITY_TRANSLATOR = new AsciiCharacterTranslator(ENTITIES);
 38  
 
 39  
     private boolean _encodeNonAscii;
 40  
     private ICharacterMatcher _safeMatcher;
 41  
     private ICharacterTranslator _entityTranslator;
 42  
 
 43  
     public MarkupCharacterTranslator()
 44  
     {
 45  0
         this(true);
 46  0
     }
 47  
 
 48  
     public MarkupCharacterTranslator(boolean encodeNonAscii)
 49  
     {
 50  0
         this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR);
 51  0
     }
 52  
 
 53  
     public MarkupCharacterTranslator(boolean encodeNonAscii,
 54  
                                      ICharacterMatcher safeMatcher, ICharacterTranslator entityTranslator)
 55  0
     {
 56  0
         _encodeNonAscii = encodeNonAscii;
 57  0
         _safeMatcher = safeMatcher;
 58  0
         _entityTranslator = entityTranslator;
 59  0
     }
 60  
 
 61  
     public MarkupCharacterTranslator(boolean encodeNonAscii,
 62  
                                      String safeCharacters, String[][] entities)
 63  0
     {
 64  0
         _encodeNonAscii = encodeNonAscii;
 65  0
         _safeMatcher = new AsciiCharacterMatcher(safeCharacters);
 66  0
         _entityTranslator = new AsciiCharacterTranslator(entities);
 67  0
     }
 68  
 
 69  
     /**
 70  
      * @see ICharacterTranslator#translate(char)
 71  
      */
 72  
     public String translate(char ch)
 73  
     {
 74  
         // IE and Firefox do not handle characters between 128 and 159 well,
 75  
         // so they have to be quoted as well
 76  0
         if (ch >= 160 && !_encodeNonAscii)
 77  0
             return null;
 78  
 
 79  0
         if (_safeMatcher.matches(ch))
 80  0
             return null;
 81  
 
 82  0
         String entity = _entityTranslator.translate(ch);
 83  0
         if (entity != null)
 84  0
             return entity;
 85  
 
 86  
         // needs to use a NumberFormat here to be fully compliant,
 87  
         // but this is accepted fine by the browsers
 88  0
         return "&#" + (int) ch + ";";
 89  
     }
 90  
 }