Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DefaultCharacterTranslatorSource |
|
| 2.0;2 |
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 | import java.util.HashMap; |
|
18 | import java.util.Map; |
|
19 | ||
20 | /** |
|
21 | * The default implementation of a character translator source. Returns a |
|
22 | * standard HTML translator that encodes everything that is non-safe or an HTML |
|
23 | * translator that encodes only non-safe ASCII symbols if the encoding is a |
|
24 | * unicode one. |
|
25 | * |
|
26 | * @author mb |
|
27 | * @since 4.0 |
|
28 | */ |
|
29 | 0 | public class DefaultCharacterTranslatorSource implements |
30 | ICharacterTranslatorSource |
|
31 | { |
|
32 | ||
33 | 0 | private static final ICharacterTranslator DEFAULT_TRANSLATOR = new MarkupCharacterTranslator(); |
34 | 0 | private static final ICharacterTranslator UNICODE_TRANSLATOR = new MarkupCharacterTranslator( |
35 | false); |
|
36 | ||
37 | private static final Map _translators; |
|
38 | ||
39 | static |
|
40 | { |
|
41 | 0 | _translators = new HashMap(); |
42 | 0 | _translators.put("UTF-8", UNICODE_TRANSLATOR); |
43 | 0 | _translators.put("UTF-7", UNICODE_TRANSLATOR); |
44 | 0 | _translators.put("UTF-16", UNICODE_TRANSLATOR); |
45 | 0 | _translators.put("UTF-16BE", UNICODE_TRANSLATOR); |
46 | 0 | _translators.put("UTF-16LE", UNICODE_TRANSLATOR); |
47 | 0 | } |
48 | ||
49 | /** |
|
50 | * Returns a translator that encodes all non-safe characters into their HTML |
|
51 | * equivalents. |
|
52 | * |
|
53 | * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getDefaultTranslator() |
|
54 | */ |
|
55 | public ICharacterTranslator getDefaultTranslator() |
|
56 | { |
|
57 | 0 | return DEFAULT_TRANSLATOR; |
58 | } |
|
59 | ||
60 | /** |
|
61 | * If the encoding is a Unicode one, returns a translator that encodes only |
|
62 | * the non-safe ASCII characters and leaves the others untouched. Otherwise, |
|
63 | * returns the default translator. |
|
64 | * |
|
65 | * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getTranslator(java.lang.String) |
|
66 | */ |
|
67 | public ICharacterTranslator getTranslator(String encoding) |
|
68 | { |
|
69 | 0 | ICharacterTranslator translator = (ICharacterTranslator) _translators |
70 | .get(encoding.toUpperCase()); |
|
71 | 0 | if (translator != null) return translator; |
72 | 0 | return getDefaultTranslator(); |
73 | } |
|
74 | ||
75 | } |