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