Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 89   Methods: 5
NCLOC: 50   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MarkupCharacterTranslator.java 100% 75% 60% 77.8%
coverage coverage
 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 specification,
 19    * so that it will be properly parsed by a browser irrespectively of the character
 20    * 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 =
 28    "01234567890"
 29    + "abcdefghijklmnopqrstuvwxyz"
 30    + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 31    + "\t\n\r !#$%'()*+,-./:;=?@[\\]^_`{|}~";
 32   
 33    private static final String[][] ENTITIES = {
 34    { "\"", """ },
 35    { "<", "&lt;" },
 36    { ">", "&gt;" },
 37    { "&", "&amp;" }
 38    };
 39   
 40    private static final ICharacterMatcher SAFE_MATCHER = new AsciiCharacterMatcher(SAFE_CHARACTERS);
 41    private static final ICharacterTranslator ENTITY_TRANSLATOR = new AsciiCharacterTranslator(ENTITIES);
 42   
 43    private boolean _encodeNonAscii;
 44    private ICharacterMatcher _safeMatcher;
 45    private ICharacterTranslator _entityTranslator;
 46   
 47  0 public MarkupCharacterTranslator()
 48    {
 49  0 this(true);
 50    }
 51   
 52  156 public MarkupCharacterTranslator(boolean encodeNonAscii)
 53    {
 54  156 this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR);
 55    }
 56   
 57  157 public MarkupCharacterTranslator(boolean encodeNonAscii, ICharacterMatcher safeMatcher, ICharacterTranslator entityTranslator)
 58    {
 59  157 _encodeNonAscii = encodeNonAscii;
 60  157 _safeMatcher = safeMatcher;
 61  157 _entityTranslator = entityTranslator;
 62    }
 63   
 64  0 public MarkupCharacterTranslator(boolean encodeNonAscii, String safeCharacters, String[][] entities)
 65    {
 66  0 _encodeNonAscii = encodeNonAscii;
 67  0 _safeMatcher = new AsciiCharacterMatcher(safeCharacters);
 68  0 _entityTranslator = new AsciiCharacterTranslator(entities);
 69    }
 70   
 71    /**
 72    * @see org.apache.tapestry.util.text.IMarkupCharacterTranslator#translateAttribute(char)
 73    */
 74  265312 public String translate(char ch) {
 75  265312 if (ch >= 128 && !_encodeNonAscii)
 76  32 return null;
 77   
 78  265280 if (_safeMatcher.matches(ch))
 79  264327 return null;
 80   
 81  953 String entity = _entityTranslator.translate(ch);
 82  953 if (entity != null)
 83  939 return entity;
 84   
 85    // needs to use a NumberFormat here to be fully compliant,
 86    // but this is accepted fine by the browsers
 87  14 return "&#" + (int) ch + ";";
 88    }
 89    }