001: /*
002: * Copyright 1999-2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.jasig.portal.serialize;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.util.Hashtable;
021: import java.util.Locale;
022:
023: import org.apache.xerces.util.EncodingMap;
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 36559 2006-04-28 18:38:13Z bjohnson $
032: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
033: */
034: public class Encodings {
035:
036: /**
037: * The last printable character for unknown encodings.
038: */
039: static final int DEFAULT_LAST_PRINTABLE = 0x7F;
040:
041: // last printable character for Unicode-compatible encodings
042: static final int LAST_PRINTABLE_UNICODE = 0xffff;
043: // unicode-compliant encodings; can express plane 0
044: static final String[] UNICODE_ENCODINGS = { "Unicode",
045: "UnicodeBig", "UnicodeLittle", "GB2312", "UTF8", "UTF-16", };
046: // default (Java) encoding if none supplied:
047: static final String DEFAULT_ENCODING = "UTF8";
048:
049: // note that the size of this Hashtable
050: // is bounded by the number of encodings recognized by EncodingMap;
051: // therefore it poses no static mutability risk.
052: static Hashtable _encodings = new Hashtable();
053:
054: /**
055: * @param encoding a MIME charset name, or null.
056: */
057: static EncodingInfo getEncodingInfo(String encoding,
058: boolean allowJavaNames) throws UnsupportedEncodingException {
059: EncodingInfo eInfo = null;
060: if (encoding == null) {
061: if ((eInfo = (EncodingInfo) _encodings
062: .get(DEFAULT_ENCODING)) != null)
063: return eInfo;
064: eInfo = new EncodingInfo(EncodingMap
065: .getJava2IANAMapping(DEFAULT_ENCODING),
066: DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
067: _encodings.put(DEFAULT_ENCODING, eInfo);
068: return eInfo;
069: }
070: // need to convert it to upper case:
071: encoding = encoding.toUpperCase(Locale.ENGLISH);
072: String jName = EncodingMap.getIANA2JavaMapping(encoding);
073: if (jName == null) {
074: // see if the encoding passed in is a Java encoding name.
075: if (allowJavaNames) {
076: EncodingInfo.testJavaEncodingName(encoding);
077: if ((eInfo = (EncodingInfo) _encodings.get(encoding)) != null)
078: return eInfo;
079: // is it known to be unicode-compliant?
080: int i = 0;
081: for (; i < UNICODE_ENCODINGS.length; i++) {
082: if (UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
083: eInfo = new EncodingInfo(EncodingMap
084: .getJava2IANAMapping(encoding),
085: encoding, LAST_PRINTABLE_UNICODE);
086: break;
087: }
088: }
089: if (i == UNICODE_ENCODINGS.length) {
090: eInfo = new EncodingInfo(EncodingMap
091: .getJava2IANAMapping(encoding), encoding,
092: DEFAULT_LAST_PRINTABLE);
093: }
094: _encodings.put(encoding, eInfo);
095: return eInfo;
096: } else {
097: throw new UnsupportedEncodingException(encoding);
098: }
099: }
100: if ((eInfo = (EncodingInfo) _encodings.get(jName)) != null)
101: return eInfo;
102: // have to create one...
103: // is it known to be unicode-compliant?
104: int i = 0;
105: for (; i < UNICODE_ENCODINGS.length; i++) {
106: if (UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
107: eInfo = new EncodingInfo(encoding, jName,
108: LAST_PRINTABLE_UNICODE);
109: break;
110: }
111: }
112: if (i == UNICODE_ENCODINGS.length) {
113: eInfo = new EncodingInfo(encoding, jName,
114: DEFAULT_LAST_PRINTABLE);
115: }
116: _encodings.put(jName, eInfo);
117: return eInfo;
118: }
119:
120: static final String JIS_DANGER_CHARS = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
121: + "\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
122: + "\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
123:
124: }
|