001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.util.text;
016    
017    import java.util.HashMap;
018    import java.util.Map;
019    
020    /**
021     * The default implementation of a character translator source. Returns a
022     * standard HTML translator that encodes everything that is non-safe or an HTML
023     * translator that encodes only non-safe ASCII symbols if the encoding is a
024     * unicode one.
025     * 
026     * @author mb
027     * @since 4.0
028     */
029    public class DefaultCharacterTranslatorSource implements
030            ICharacterTranslatorSource
031    {
032    
033        private static final ICharacterTranslator DEFAULT_TRANSLATOR = new MarkupCharacterTranslator();
034        private static final ICharacterTranslator UNICODE_TRANSLATOR = new MarkupCharacterTranslator(
035                false);
036    
037        private static final Map _translators;
038    
039        static
040        {
041            _translators = new HashMap();
042            _translators.put("UTF-8", UNICODE_TRANSLATOR);
043            _translators.put("UTF-7", UNICODE_TRANSLATOR);
044            _translators.put("UTF-16", UNICODE_TRANSLATOR);
045            _translators.put("UTF-16BE", UNICODE_TRANSLATOR);
046            _translators.put("UTF-16LE", UNICODE_TRANSLATOR);
047        }
048    
049        /**
050         * Returns a translator that encodes all non-safe characters into their HTML
051         * equivalents.
052         * 
053         * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getDefaultTranslator()
054         */
055        public ICharacterTranslator getDefaultTranslator()
056        {
057            return DEFAULT_TRANSLATOR;
058        }
059    
060        /**
061         * If the encoding is a Unicode one, returns a translator that encodes only
062         * the non-safe ASCII characters and leaves the others untouched. Otherwise,
063         * returns the default translator.
064         * 
065         * @see org.apache.tapestry.util.text.ICharacterTranslatorSource#getTranslator(java.lang.String)
066         */
067        public ICharacterTranslator getTranslator(String encoding)
068        {
069            ICharacterTranslator translator = (ICharacterTranslator) _translators
070                    .get(encoding.toUpperCase());
071            if (translator != null) return translator;
072            return getDefaultTranslator();
073        }
074    
075    }