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: package org.apache.xml.serialize;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.util.Hashtable;
022: import java.util.Locale;
023:
024: import org.apache.xerces.util.EncodingMap;
025:
026: /**
027: * Provides information about encodings. Depends on the Java runtime
028: * to provides writers for the different encodings, but can be used
029: * to override encoding names and provide the last printable character
030: * for each encoding.
031: *
032: * @deprecated This class was deprecated in Xerces 2.9.0. It is recommended
033: * that new applications use the DOM Level 3 LSSerializer or JAXP's Transformation
034: * API for XML (TrAX) for serializing XML. See the Xerces documentation for more
035: * information.
036: * @version $Id: Encodings.java 476047 2006-11-17 04:27:45Z mrglavas $
037: * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
038: */
039: public class Encodings {
040:
041: /**
042: * The last printable character for unknown encodings.
043: */
044: static final int DEFAULT_LAST_PRINTABLE = 0x7F;
045:
046: // last printable character for Unicode-compatible encodings
047: static final int LAST_PRINTABLE_UNICODE = 0xffff;
048: // unicode-compliant encodings; can express plane 0
049: static final String[] UNICODE_ENCODINGS = { "Unicode",
050: "UnicodeBig", "UnicodeLittle", "GB2312", "UTF8", "UTF-16", };
051: // default (Java) encoding if none supplied:
052: static final String DEFAULT_ENCODING = "UTF8";
053:
054: // note that the size of this Hashtable
055: // is bounded by the number of encodings recognized by EncodingMap;
056: // therefore it poses no static mutability risk.
057: static Hashtable _encodings = new Hashtable();
058:
059: /**
060: * @param encoding a MIME charset name, or null.
061: */
062: static EncodingInfo getEncodingInfo(String encoding,
063: boolean allowJavaNames) throws UnsupportedEncodingException {
064: EncodingInfo eInfo = null;
065: if (encoding == null) {
066: if ((eInfo = (EncodingInfo) _encodings
067: .get(DEFAULT_ENCODING)) != null)
068: return eInfo;
069: eInfo = new EncodingInfo(EncodingMap
070: .getJava2IANAMapping(DEFAULT_ENCODING),
071: DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
072: _encodings.put(DEFAULT_ENCODING, eInfo);
073: return eInfo;
074: }
075: // need to convert it to upper case:
076: encoding = encoding.toUpperCase(Locale.ENGLISH);
077: String jName = EncodingMap.getIANA2JavaMapping(encoding);
078: if (jName == null) {
079: // see if the encoding passed in is a Java encoding name.
080: if (allowJavaNames) {
081: EncodingInfo.testJavaEncodingName(encoding);
082: if ((eInfo = (EncodingInfo) _encodings.get(encoding)) != null)
083: return eInfo;
084: // is it known to be unicode-compliant?
085: int i = 0;
086: for (; i < UNICODE_ENCODINGS.length; i++) {
087: if (UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
088: eInfo = new EncodingInfo(EncodingMap
089: .getJava2IANAMapping(encoding),
090: encoding, LAST_PRINTABLE_UNICODE);
091: break;
092: }
093: }
094: if (i == UNICODE_ENCODINGS.length) {
095: eInfo = new EncodingInfo(EncodingMap
096: .getJava2IANAMapping(encoding), encoding,
097: DEFAULT_LAST_PRINTABLE);
098: }
099: _encodings.put(encoding, eInfo);
100: return eInfo;
101: }
102: throw new UnsupportedEncodingException(encoding);
103: }
104: if ((eInfo = (EncodingInfo) _encodings.get(jName)) != null)
105: return eInfo;
106: // have to create one...
107: // is it known to be unicode-compliant?
108: int i = 0;
109: for (; i < UNICODE_ENCODINGS.length; i++) {
110: if (UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
111: eInfo = new EncodingInfo(encoding, jName,
112: LAST_PRINTABLE_UNICODE);
113: break;
114: }
115: }
116: if (i == UNICODE_ENCODINGS.length) {
117: eInfo = new EncodingInfo(encoding, jName,
118: DEFAULT_LAST_PRINTABLE);
119: }
120: _encodings.put(jName, eInfo);
121: return eInfo;
122: }
123:
124: static final String JIS_DANGER_CHARS = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
125: + "\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
126: + "\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
127:
128: }
|