|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
MarkupCharacterTranslator.java | 100% | 75% | 60% | 77.8% |
|
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 |
{ "<", "<" }, |
|
36 |
{ ">", ">" }, |
|
37 |
{ "&", "&" } |
|
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 | 144 |
public MarkupCharacterTranslator(boolean encodeNonAscii) |
53 |
{ |
|
54 | 144 |
this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR);
|
55 |
} |
|
56 |
|
|
57 | 145 |
public MarkupCharacterTranslator(boolean encodeNonAscii, ICharacterMatcher safeMatcher, ICharacterTranslator entityTranslator) |
58 |
{ |
|
59 | 145 |
_encodeNonAscii = encodeNonAscii; |
60 | 145 |
_safeMatcher = safeMatcher; |
61 | 145 |
_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 | 272450 |
public String translate(char ch) { |
75 | 272450 |
if (ch >= 128 && !_encodeNonAscii)
|
76 | 32 |
return null; |
77 |
|
|
78 | 272418 |
if (_safeMatcher.matches(ch))
|
79 | 271458 |
return null; |
80 |
|
|
81 | 960 |
String entity = _entityTranslator.translate(ch); |
82 | 960 |
if (entity != null) |
83 | 946 |
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 |
} |
|
90 |
|
|