001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. 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 /*
019 * This code has been borrowed from the Apache Xerces project. We're copying the code to
020 * keep from adding a dependency on Xerces in the Geronimo kernel.
021 */
022
023 package org.apache.geronimo.system.configuration;
024
025 /**
026 * Provides information about encodings. Depends on the Java runtime
027 * to provides writers for the different encodings, but can be used
028 * to override encoding names and provide the last printable character
029 * for each encoding.
030 *
031 * @version $Id: Encodings.java 482336 2006-12-04 20:12:19Z kevan $
032 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
033 */
034 public class Encodings
035 {
036
037
038 /**
039 * The last printable character for unknown encodings.
040 */
041 static final int DefaultLastPrintable = 0x7F;
042
043 /**
044 * @param encoding a MIME charset name, or null.
045 */
046 static EncodingInfo getEncodingInfo(String encoding) {
047 if (encoding == null)
048 return new EncodingInfo(null, DefaultLastPrintable);
049 for (int i = 0; i < _encodings.length; i++) {
050 if (_encodings[i].name.equalsIgnoreCase(encoding))
051 return _encodings[i];
052 }
053 return new SieveEncodingInfo(encoding, DefaultLastPrintable);
054 }
055
056 static final String JIS_DANGER_CHARS
057 = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
058 +"\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
059 +"\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
060
061 /**
062 * Constructs a list of all the supported encodings.
063 */
064 private static final EncodingInfo[] _encodings = new EncodingInfo[] {
065 new EncodingInfo("ASCII", 0x7F),
066 new EncodingInfo("US-ASCII", 0x7F),
067 new EncodingInfo("ISO-8859-1", 0xFF),
068 new EncodingInfo("ISO-8859-2", 0xFF),
069 new EncodingInfo("ISO-8859-3", 0xFF),
070 new EncodingInfo("ISO-8859-4", 0xFF),
071 new EncodingInfo("ISO-8859-5", 0xFF),
072 new EncodingInfo("ISO-8859-6", 0xFF),
073 new EncodingInfo("ISO-8859-7", 0xFF),
074 new EncodingInfo("ISO-8859-8", 0xFF),
075 new EncodingInfo("ISO-8859-9", 0xFF),
076 /**
077 * Does JDK's converter supprt surrogates?
078 * A Java encoding name "UTF-8" is suppoted by JDK 1.2 or later.
079 */
080 new EncodingInfo("UTF-8", "UTF8", 0x10FFFF),
081 /**
082 * JDK 1.1 supports "Shift_JIS" as an alias of "SJIS".
083 * But JDK 1.2 treats "Shift_JIS" as an alias of "MS932".
084 * The JDK 1.2's behavior is invalid against IANA registrations.
085 */
086 new SieveEncodingInfo("Shift_JIS", "SJIS", 0x7F, JIS_DANGER_CHARS),
087 /**
088 * "MS932" is supported by JDK 1.2 or later.
089 */
090 new SieveEncodingInfo("Windows-31J", "MS932", 0x7F, JIS_DANGER_CHARS),
091 new SieveEncodingInfo("EUC-JP", null, 0x7F, JIS_DANGER_CHARS),
092 new SieveEncodingInfo("ISO-2022-JP", null, 0x7F, JIS_DANGER_CHARS),
093 };
094 }