|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com 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 | 149 | public MarkupCharacterTranslator(boolean encodeNonAscii) |
53 | { | |
54 | 149 | this(encodeNonAscii, SAFE_MATCHER, ENTITY_TRANSLATOR); |
55 | } | |
56 | ||
57 | 150 | public MarkupCharacterTranslator(boolean encodeNonAscii, ICharacterMatcher safeMatcher, ICharacterTranslator entityTranslator) |
58 | { | |
59 | 150 | _encodeNonAscii = encodeNonAscii; |
60 | 150 | _safeMatcher = safeMatcher; |
61 | 150 | _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 | 264318 | public String translate(char ch) { |
75 | 264318 | if (ch >= 128 && !_encodeNonAscii) |
76 | 32 | return null; |
77 | ||
78 | 264286 | if (_safeMatcher.matches(ch)) |
79 | 263343 | return null; |
80 | ||
81 | 943 | String entity = _entityTranslator.translate(ch); |
82 | 943 | if (entity != null) |
83 | 929 | 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 | } |
|