jaxp/src/com/sun/org/apache/xml/internal/serialize/Encodings.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xml/internal/serialize/Encodings.java@7f561c08de6b
child 25834 aba3efbf4ec5
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2002,2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.serialize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.io.UnsupportedEncodingException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import java.util.Locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xerces.internal.util.EncodingMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * Provides information about encodings. Depends on the Java runtime
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * to provides writers for the different encodings, but can be used
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * to override encoding names and provide the last printable character
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * for each encoding.
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
public class Encodings
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
     * The last printable character for unknown encodings.
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    static final int DEFAULT_LAST_PRINTABLE = 0x7F;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
    // last printable character for Unicode-compatible encodings
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
    static final int LAST_PRINTABLE_UNICODE = 0xffff;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    // unicode-compliant encodings; can express plane 0
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
    static final String[] UNICODE_ENCODINGS = {
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
        "Unicode", "UnicodeBig", "UnicodeLittle", "GB2312", "UTF8", "UTF-16",
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    };
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
    // default (Java) encoding if none supplied:
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    static final String DEFAULT_ENCODING = "UTF8";
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    // note that the size of this Hashtable
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    // is bounded by the number of encodings recognized by EncodingMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    // therefore it poses no static mutability risk.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    static Hashtable _encodings = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     * @param encoding a MIME charset name, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    static EncodingInfo getEncodingInfo(String encoding, boolean allowJavaNames) throws UnsupportedEncodingException {
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        EncodingInfo eInfo = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
        if (encoding == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
            if((eInfo = (EncodingInfo)_encodings.get(DEFAULT_ENCODING)) != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
                return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
            eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(DEFAULT_ENCODING), DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
            _encodings.put(DEFAULT_ENCODING, eInfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
            return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
        // need to convert it to upper case:
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
        encoding = encoding.toUpperCase(Locale.ENGLISH);
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
        String jName = EncodingMap.getIANA2JavaMapping(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
        if(jName == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
            // see if the encoding passed in is a Java encoding name.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            if(allowJavaNames ) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
                EncodingInfo.testJavaEncodingName(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
                if((eInfo = (EncodingInfo)_encodings.get(encoding)) != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
                    return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                // is it known to be unicode-compliant?
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                int i=0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
                for(; i<UNICODE_ENCODINGS.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                    if(UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
                        eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, LAST_PRINTABLE_UNICODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
                        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                if(i == UNICODE_ENCODINGS.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
                    eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, DEFAULT_LAST_PRINTABLE);
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
                _encodings.put(encoding, eInfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
            } else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                throw new UnsupportedEncodingException(encoding);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        if ((eInfo = (EncodingInfo)_encodings.get(jName)) != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
            return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
        // have to create one...
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        // is it known to be unicode-compliant?
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        int i=0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        for(; i<UNICODE_ENCODINGS.length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
            if(UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
                eInfo = new EncodingInfo(encoding, jName, LAST_PRINTABLE_UNICODE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
                break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        if(i == UNICODE_ENCODINGS.length) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            eInfo = new EncodingInfo(encoding, jName, DEFAULT_LAST_PRINTABLE);
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
        _encodings.put(jName, eInfo);
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        return eInfo;
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    static final String JIS_DANGER_CHARS
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    +"\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    +"\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
}