KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > util > text > DefaultCharacterTranslatorSource


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 JavaDoc;
18 import java.util.Map JavaDoc;
19
20 /**
21  * The default implementation of a character translator source.
22  * Returns a standard HTML translator that encodes everything that is non-safe
23  * or an HTML translator that encodes only non-safe ASCII symbols
24  * if the encoding is a unicode one.
25  *
26  * @author mb
27  * @since 4.0
28  */

29 public class DefaultCharacterTranslatorSource implements ICharacterTranslatorSource
30 {
31     private static final ICharacterTranslator DEFAULT_TRANSLATOR = new MarkupCharacterTranslator();
32     private static final ICharacterTranslator UNICODE_TRANSLATOR = new MarkupCharacterTranslator(false);
33
34     private final static Map JavaDoc _translators;
35     
36     static {
37         _translators = new HashMap JavaDoc();
38         _translators.put("UTF-8", UNICODE_TRANSLATOR);
39         _translators.put("UTF-7", UNICODE_TRANSLATOR);
40         _translators.put("UTF-16", UNICODE_TRANSLATOR);
41         _translators.put("UTF-16BE", UNICODE_TRANSLATOR);
42         _translators.put("UTF-16LE", UNICODE_TRANSLATOR);
43     }
44     
45     /**
46      * Returns a translator that encodes all non-safe characters into their HTML equivalents.
47      *
48      * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getDefaultTranslator()
49      */

50     public ICharacterTranslator getDefaultTranslator() {
51         return DEFAULT_TRANSLATOR;
52     }
53
54     /**
55      * If the encoding is a Unicode one, returns a translator that encodes only the
56      * non-safe ASCII characters and leaves the others untouched.
57      * Otherwise, returns the default translator.
58      *
59      * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getTranslator(java.lang.String)
60      */

61     public ICharacterTranslator getTranslator(String JavaDoc encoding) {
62         ICharacterTranslator translator = (ICharacterTranslator) _translators.get(encoding.toUpperCase());
63         if (translator != null)
64             return translator;
65         return getDefaultTranslator();
66     }
67     
68 }
69
Popular Tags