KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > serialize > Encodings


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.xml.serialize;
19
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import org.apache.xerces.util.EncodingMap;
26
27
28 /**
29  * Provides information about encodings. Depends on the Java runtime
30  * to provides writers for the different encodings, but can be used
31  * to override encoding names and provide the last printable character
32  * for each encoding.
33  *
34  * @version $Id: Encodings.java,v 1.10 2005/06/24 01:32:04 mrglavas Exp $
35  * @author <a HREF="mailto:arkin@intalio.com">Assaf Arkin</a>
36  */

37 public class Encodings
38 {
39
40
41     /**
42      * The last printable character for unknown encodings.
43      */

44     static final int DEFAULT_LAST_PRINTABLE = 0x7F;
45
46     // last printable character for Unicode-compatible encodings
47
static final int LAST_PRINTABLE_UNICODE = 0xffff;
48     // unicode-compliant encodings; can express plane 0
49
static final String JavaDoc[] UNICODE_ENCODINGS = {
50         "Unicode", "UnicodeBig", "UnicodeLittle", "GB2312", "UTF8", "UTF-16",
51     };
52     // default (Java) encoding if none supplied:
53
static final String JavaDoc DEFAULT_ENCODING = "UTF8";
54
55     // note that the size of this Hashtable
56
// is bounded by the number of encodings recognized by EncodingMap;
57
// therefore it poses no static mutability risk.
58
static Hashtable JavaDoc _encodings = new Hashtable JavaDoc();
59
60     /**
61      * @param encoding a MIME charset name, or null.
62      */

63     static EncodingInfo getEncodingInfo(String JavaDoc encoding, boolean allowJavaNames) throws UnsupportedEncodingException JavaDoc {
64         EncodingInfo eInfo = null;
65         if (encoding == null) {
66             if((eInfo = (EncodingInfo)_encodings.get(DEFAULT_ENCODING)) != null)
67                 return eInfo;
68             eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(DEFAULT_ENCODING), DEFAULT_ENCODING, LAST_PRINTABLE_UNICODE);
69             _encodings.put(DEFAULT_ENCODING, eInfo);
70             return eInfo;
71         }
72         // need to convert it to upper case:
73
encoding = encoding.toUpperCase(Locale.ENGLISH);
74         String JavaDoc jName = EncodingMap.getIANA2JavaMapping(encoding);
75         if(jName == null) {
76             // see if the encoding passed in is a Java encoding name.
77
if(allowJavaNames ) {
78                 EncodingInfo.testJavaEncodingName(encoding);
79                 if((eInfo = (EncodingInfo)_encodings.get(encoding)) != null)
80                     return eInfo;
81                 // is it known to be unicode-compliant?
82
int i=0;
83                 for(; i<UNICODE_ENCODINGS.length; i++) {
84                     if(UNICODE_ENCODINGS[i].equalsIgnoreCase(encoding)) {
85                         eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, LAST_PRINTABLE_UNICODE);
86                         break;
87                     }
88                 }
89                 if(i == UNICODE_ENCODINGS.length) {
90                     eInfo = new EncodingInfo(EncodingMap.getJava2IANAMapping(encoding), encoding, DEFAULT_LAST_PRINTABLE);
91                 }
92                 _encodings.put(encoding, eInfo);
93                 return eInfo;
94             } else {
95                 throw new UnsupportedEncodingException JavaDoc(encoding);
96             }
97         }
98         if ((eInfo = (EncodingInfo)_encodings.get(jName)) != null)
99             return eInfo;
100         // have to create one...
101
// is it known to be unicode-compliant?
102
int i=0;
103         for(; i<UNICODE_ENCODINGS.length; i++) {
104             if(UNICODE_ENCODINGS[i].equalsIgnoreCase(jName)) {
105                 eInfo = new EncodingInfo(encoding, jName, LAST_PRINTABLE_UNICODE);
106                 break;
107             }
108         }
109         if(i == UNICODE_ENCODINGS.length) {
110             eInfo = new EncodingInfo(encoding, jName, DEFAULT_LAST_PRINTABLE);
111         }
112         _encodings.put(jName, eInfo);
113         return eInfo;
114     }
115
116     static final String JavaDoc JIS_DANGER_CHARS
117     = "\\\u007e\u007f\u00a2\u00a3\u00a5\u00ac"
118     +"\u2014\u2015\u2016\u2026\u203e\u203e\u2225\u222f\u301c"
119     +"\uff3c\uff5e\uffe0\uffe1\uffe2\uffe3";
120
121 }
122
Popular Tags