001    // Copyright Aug 4, 2006 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    package org.apache.tapestry.util.text;
015    
016    
017    /**
018     * Handles escaping of special characters as per the XML spec section 2.2.
019     * 
020     * @author lquijano
021     */
022    public final class XmlCharacterTranslator extends MarkupCharacterTranslator {
023        
024        /** Default constructor. */
025        public XmlCharacterTranslator() {
026            super(true);
027        }
028        
029        /**
030         * Translates the character.
031         * 
032         * <p>
033         *  XML spec section 2.2
034         *  Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] |
035         *  [#xE000-#xFFFD] |
036         *  [#x10000-#x10FFFF]
037         *  any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
038         *  </p>
039         */
040        public String translate(char ch) {
041            if (ch == 0x09 || ch == 0x0a || ch == 0x0d
042                    || (ch >= 0x20 && ch <= 0xd7ff)
043                    || (ch >= 0xe000 && ch <= 0xfffd)
044                    || (ch >= 0x10000 && ch <= 0x10ffff)) {
045                return super.translate(ch);
046            }
047    
048            return "";
049        }
050    }