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