1 /**
2 *
3 * Copyright 2006 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 /*
18 * The Apache Software License, Version 1.1
19 *
20 *
21 * Copyright (c) 1999 The Apache Software Foundation. All rights
22 * reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 *
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 *
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in
33 * the documentation and/or other materials provided with the
34 * distribution.
35 *
36 * 3. The end-user documentation included with the redistribution,
37 * if any, must include the following acknowledgment:
38 * "This product includes software developed by the
39 * Apache Software Foundation (http://www.apache.org/)."
40 * Alternately, this acknowledgment may appear in the software itself,
41 * if and wherever such third-party acknowledgments normally appear.
42 *
43 * 4. The names "Xerces" and "Apache Software Foundation" must
44 * not be used to endorse or promote products derived from this
45 * software without prior written permission. For written
46 * permission, please contact apache@apache.org.
47 *
48 * 5. Products derived from this software may not be called "Apache",
49 * nor may "Apache" appear in their name, without prior written
50 * permission of the Apache Software Foundation.
51 *
52 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
53 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
54 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
56 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
59 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
60 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
61 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
62 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 * ====================================================================
65 *
66 * This software consists of voluntary contributions made by many
67 * individuals on behalf of the Apache Software Foundation and was
68 * originally based on software copyright (c) 1999, International
69 * Business Machines, Inc., http://www.apache.org. For more
70 * information on the Apache Software Foundation, please see
71 * <http://www.apache.org/>.
72 */
73
74 /*
75 * This code has been borrowed from the Apache Xerces project. We're copying the code to
76 * keep from adding a dependency on Xerces in the Geronimo kernel.
77 */
78
79 package org.apache.geronimo.system.configuration;
80
81 /**
82 * Provides information about encodings. Depends on the Java runtime
83 * to provides writers for the different encodings, but can be used
84 * to override encoding names and provide the last printable character
85 * for each encoding.
86 *
87 * @version $Id: Encodings.java 410741 2006-06-01 04:35:48Z jsisson $
88 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
89 */
90 public class Encodings
91 {
92
93
94 /**
95 * The last printable character for unknown encodings.
96 */
97 static final int DefaultLastPrintable = 0x7F;
98
99 /**
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 }