001    /**
002     *
003     * Copyright 2006 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    /*
018     * The Apache Software License, Version 1.1
019     *
020     *
021     * Copyright (c) 1999 The Apache Software Foundation.  All rights 
022     * reserved.
023     *
024     * Redistribution and use in source and binary forms, with or without
025     * modification, are permitted provided that the following conditions
026     * are met:
027     *
028     * 1. Redistributions of source code must retain the above copyright
029     *    notice, this list of conditions and the following disclaimer. 
030     *
031     * 2. Redistributions in binary form must reproduce the above copyright
032     *    notice, this list of conditions and the following disclaimer in
033     *    the documentation and/or other materials provided with the
034     *    distribution.
035     *
036     * 3. The end-user documentation included with the redistribution,
037     *    if any, must include the following acknowledgment:  
038     *       "This product includes software developed by the
039     *        Apache Software Foundation (http://www.apache.org/)."
040     *    Alternately, this acknowledgment may appear in the software itself,
041     *    if and wherever such third-party acknowledgments normally appear.
042     *
043     * 4. The names "Xerces" and "Apache Software Foundation" must
044     *    not be used to endorse or promote products derived from this
045     *    software without prior written permission. For written 
046     *    permission, please contact apache@apache.org.
047     *
048     * 5. Products derived from this software may not be called "Apache",
049     *    nor may "Apache" appear in their name, without prior written
050     *    permission of the Apache Software Foundation.
051     *
052     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
053     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
054     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
055     * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
056     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
057     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
058     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
059     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
060     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
061     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
062     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
063     * SUCH DAMAGE.
064     * ====================================================================
065     *
066     * This software consists of voluntary contributions made by many
067     * individuals on behalf of the Apache Software Foundation and was
068     * originally based on software copyright (c) 1999, International
069     * Business Machines, Inc., http://www.apache.org.  For more
070     * information on the Apache Software Foundation, please see
071     * <http://www.apache.org/>.
072     */
073    
074    /*
075     * This code has been borrowed from the Apache Xerces project. We're copying the code to
076     * keep from adding a dependency on Xerces in the Geronimo kernel.
077     */
078    
079    package org.apache.geronimo.system.configuration;
080    
081    /**
082     * Provides information about encodings. Depends on the Java runtime
083     * to provides writers for the different encodings, but can be used
084     * to override encoding names and provide the last printable character
085     * for each encoding.
086     *
087     * @version $Id: Encodings.java 410741 2006-06-01 04:35:48Z jsisson $
088     * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
089     */
090    public class Encodings
091    {
092    
093    
094        /**
095         * The last printable character for unknown encodings.
096         */
097        static final int DefaultLastPrintable = 0x7F;
098    
099        /**
100         * @param encoding a MIME charset name, or null.
101         */
102        static EncodingInfo getEncodingInfo(String encoding) {
103            if (encoding == null)
104                return new EncodingInfo(null, DefaultLastPrintable);
105            for (int i = 0;  i < _encodings.length;  i++) {
106                if (_encodings[i].name.equalsIgnoreCase(encoding))
107                    return _encodings[i];
108            }
109            return new SieveEncodingInfo(encoding, DefaultLastPrintable);
110        }
111    
112        static final String JIS_DANGER_CHARS
113        = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
114        +"\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
115        +"\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
116    
117        /**
118         * Constructs a list of all the supported encodings.
119         */
120        private static final EncodingInfo[] _encodings = new EncodingInfo[] {
121            new EncodingInfo("ASCII", 0x7F),
122            new EncodingInfo("US-ASCII", 0x7F),
123            new EncodingInfo("ISO-8859-1", 0xFF),
124            new EncodingInfo("ISO-8859-2", 0xFF),
125            new EncodingInfo("ISO-8859-3", 0xFF),
126            new EncodingInfo("ISO-8859-4", 0xFF),
127            new EncodingInfo("ISO-8859-5", 0xFF),
128            new EncodingInfo("ISO-8859-6", 0xFF),
129            new EncodingInfo("ISO-8859-7", 0xFF),
130            new EncodingInfo("ISO-8859-8", 0xFF),
131            new EncodingInfo("ISO-8859-9", 0xFF),
132            /**
133             * Does JDK's converter supprt surrogates?
134             * A Java encoding name "UTF-8" is suppoted by JDK 1.2 or later.
135             */
136            new EncodingInfo("UTF-8", "UTF8", 0x10FFFF),
137            /**
138             * JDK 1.1 supports "Shift_JIS" as an alias of "SJIS".
139             * But JDK 1.2 treats "Shift_JIS" as an alias of "MS932".
140             * The JDK 1.2's behavior is invalid against IANA registrations.
141             */
142            new SieveEncodingInfo("Shift_JIS", "SJIS", 0x7F, JIS_DANGER_CHARS),
143            /**
144             * "MS932" is supported by JDK 1.2 or later.
145             */
146            new SieveEncodingInfo("Windows-31J", "MS932", 0x7F, JIS_DANGER_CHARS),
147            new SieveEncodingInfo("EUC-JP", null, 0x7F, JIS_DANGER_CHARS),
148            new SieveEncodingInfo("ISO-2022-JP", null, 0x7F, JIS_DANGER_CHARS),
149        };
150    }