KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serialize > Encodings


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58
59 package com.sun.org.apache.xml.internal.serialize;
60
61
62 import java.io.UnsupportedEncodingException JavaDoc;
63 import java.util.Hashtable JavaDoc;
64 import java.util.Locale JavaDoc;
65
66 import com.sun.org.apache.xerces.internal.util.EncodingMap;
67
68
69 /**
70  * Provides information about encodings. Depends on the Java runtime
71  * to provides writers for the different encodings, but can be used
72  * to override encoding names and provide the last printable character
73  * for each encoding.
74  *
75  * @version $Id: Encodings.java,v 1.8 2003/07/18 16:47:22 mrglavas Exp $
76  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
77  */

78 public class Encodings
79 {
80
81
82     /**
83      * The last printable character for unknown encodings.
84      */

85     static final int DEFAULT_LAST_PRINTABLE = 0x7F;
86
87     // last printable character for Unicode-compatible encodings
88
static final int LAST_PRINTABLE_UNICODE = 0xffff;
89     // unicode-compliant encodings; can express plane 0
90
static final String JavaDoc[] UNICODE_ENCODINGS = {
91         "Unicode", "UnicodeBig", "UnicodeLittle", "GB2312", "UTF8",
92     };
93     // default (Java) encoding if none supplied:
94
static final String JavaDoc DEFAULT_ENCODING = "UTF8";
95
96     // note that the size of this Hashtable
97
// is bounded by the number of encodings recognized by EncodingMap;
98
// therefore it poses no static mutability risk.
99
static Hashtable JavaDoc _encodings = new Hashtable JavaDoc();
100
101     /**
102      * @param encoding a MIME charset name, or null.
103      */

104     static EncodingInfo getEncodingInfo(String JavaDoc encoding, boolean allowJavaNames) throws UnsupportedEncodingException JavaDoc {
105         EncodingInfo eInfo = null;
106         if (encoding == null) {
107             if((eInfo = (EncodingInfo)_encodings.get(DEFAULT_ENCODING)) != null)
108                 return eInfo;
109             eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(DEFAULT_ENCODING), DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
110             _encodings.put(DEFAULT_ENCODING, eInfo);
111             return eInfo;
112         }
113         // need to convert it to upper case:
114
encoding = encoding.toUpperCase(Locale.ENGLISH);
115         String JavaDoc jName = EncodingMap.getIANA2JavaMapping(encoding);
116         if(jName == null) {
117             // see if the encoding passed in is a Java encoding name.
118
if(allowJavaNames ) {
119                 EncodingInfo.testJavaEncodingName(encoding);
120                 if((eInfo = (EncodingInfo)_encodings.get(encoding)) != null)
121                     return eInfo;
122                 // is it known to be unicode-compliant?
123
int i=0;
124                 for(; i<UNICODE_ENCODINGS.length; i++) {
125                     if(UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
126                         eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, LAST_PRINTABLE_UNICODE);
127                         break;
128                     }
129                 }
130                 if(i == UNICODE_ENCODINGS.length) {
131                     eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, DEFAULT_LAST_PRINTABLE);
132                 }
133                 _encodings.put(encoding, eInfo);
134                 return eInfo;
135             } else {
136                 throw new UnsupportedEncodingException JavaDoc(encoding);
137             }
138         }
139         if ((eInfo = (EncodingInfo)_encodings.get(jName)) != null)
140             return eInfo;
141         // have to create one...
142
// is it known to be unicode-compliant?
143
int i=0;
144         for(; i<UNICODE_ENCODINGS.length; i++) {
145             if(UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
146                 eInfo = new EncodingInfo(encoding, jName, LAST_PRINTABLE_UNICODE);
147                 break;
148             }
149         }
150         if(i == UNICODE_ENCODINGS.length) {
151             eInfo = new EncodingInfo(encoding, jName, DEFAULT_LAST_PRINTABLE);
152         }
153         _encodings.put(jName, eInfo);
154         return eInfo;
155     }
156
157     static final String JavaDoc JIS_DANGER_CHARS
158     = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
159     +"\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
160     +"\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
161
162 }
163
Popular Tags