Fields Summary |
---|
private static final int | m_defaultLastPrintableThe last printable character for unknown encodings. |
private static final String | ENCODINGS_FILEStandard filename for properties file with encodings data. |
private static final String | ENCODINGS_PROPStandard filename for properties file with encodings data. |
static final String | DEFAULT_MIME_ENCODINGThe default encoding, ISO style, ISO style. |
private static final Hashtable | _encodingTableKeyJava |
private static final Hashtable | _encodingTableKeyMime |
private static final EncodingInfo[] | _encodings |
Methods Summary |
---|
private static java.lang.String | convertJava2MimeEncoding(java.lang.String encoding)Try the best we can to convert a Java encoding to a XML-style encoding.
EncodingInfo enc =
(EncodingInfo) _encodingTableKeyJava.get(encoding.toUpperCase());
if (null != enc)
return enc.name;
return encoding;
|
public static java.lang.String | convertMime2JavaEncoding(java.lang.String encoding)Try the best we can to convert a Java encoding to a XML-style encoding.
for (int i = 0; i < _encodings.length; ++i)
{
if (_encodings[i].name.equalsIgnoreCase(encoding))
{
return _encodings[i].javaName;
}
}
return encoding;
|
static com.sun.org.apache.xml.internal.serializer.EncodingInfo | getEncodingInfo(java.lang.String encoding)Returns the EncodingInfo object for the specified
encoding.
This is not a public API.
EncodingInfo ei;
String normalizedEncoding = toUpperCaseFast(encoding);
ei = (EncodingInfo) _encodingTableKeyJava.get(normalizedEncoding);
if (ei == null)
ei = (EncodingInfo) _encodingTableKeyMime.get(normalizedEncoding);
if (ei == null) {
// We shouldn't have to do this, but just in case.
ei = new EncodingInfo(null,null);
}
return ei;
|
public static int | getLastPrintable()Returns the last printable character for an unspecified
encoding.
return m_defaultLastPrintable;
|
static java.lang.String | getMimeEncoding(java.lang.String encoding)Get the proper mime encoding. From the XSLT recommendation: "The encoding
attribute specifies the preferred encoding to use for outputting the result
tree. XSLT processors are required to respect values of UTF-8 and UTF-16.
For other values, if the XSLT processor does not support the specified
encoding it may signal an error; if it does not signal an error it should
use UTF-8 or UTF-16 instead. The XSLT processor must not use an encoding
whose name does not match the EncName production of the XML Recommendation
[XML]. If no encoding attribute is specified, then the XSLT processor should
use either UTF-8 or UTF-16."
if (null == encoding)
{
try
{
// Get the default system character encoding. This may be
// incorrect if they passed in a writer, but right now there
// seems to be no way to get the encoding from a writer.
encoding = System.getProperty("file.encoding", "UTF8");
if (null != encoding)
{
/*
* See if the mime type is equal to UTF8. If you don't
* do that, then convertJava2MimeEncoding will convert
* 8859_1 to "ISO-8859-1", which is not what we want,
* I think, and I don't think I want to alter the tables
* to convert everything to UTF-8.
*/
String jencoding =
(encoding.equalsIgnoreCase("Cp1252")
|| encoding.equalsIgnoreCase("ISO8859_1")
|| encoding.equalsIgnoreCase("8859_1")
|| encoding.equalsIgnoreCase("UTF8"))
? DEFAULT_MIME_ENCODING
: convertJava2MimeEncoding(encoding);
encoding =
(null != jencoding) ? jencoding : DEFAULT_MIME_ENCODING;
}
else
{
encoding = DEFAULT_MIME_ENCODING;
}
}
catch (SecurityException se)
{
encoding = DEFAULT_MIME_ENCODING;
}
}
else
{
encoding = convertJava2MimeEncoding(encoding);
}
return encoding;
|
static java.io.Writer | getWriter(java.io.OutputStream output, java.lang.String encoding)Returns a writer for the specified encoding based on
an output stream.
for (int i = 0; i < _encodings.length; ++i)
{
if (_encodings[i].name.equalsIgnoreCase(encoding))
{
try
{
return new OutputStreamWriter(
output,
_encodings[i].javaName);
}
catch (java.lang.IllegalArgumentException iae) // java 1.1.8
{
// keep trying
}
catch (UnsupportedEncodingException usee)
{
// keep trying
}
}
}
try
{
return new OutputStreamWriter(output, encoding);
}
catch (java.lang.IllegalArgumentException iae) // java 1.1.8
{
throw new UnsupportedEncodingException(encoding);
}
|
static boolean | isHighUTF16Surrogate(char ch)Return true if the character is the high member of a surrogate pair.
This is not a public API.
return ('\uD800" <= ch && ch <= '\uDBFF");
|
static boolean | isLowUTF16Surrogate(char ch)Return true if the character is the low member of a surrogate pair.
This is not a public API.
return ('\uDC00" <= ch && ch <= '\uDFFF");
|
private static com.sun.org.apache.xml.internal.serializer.EncodingInfo[] | loadEncodingInfo()Load a list of all the supported encodings.
System property "encodings" formatted using URL syntax may define an
external encodings list. Thanks to Sergey Ushakov for the code
contribution!
URL url = null;
try
{
String urlString = null;
InputStream is = null;
try
{
urlString = System.getProperty(ENCODINGS_PROP, "");
}
catch (SecurityException e)
{
}
if (urlString != null && urlString.length() > 0) {
url = new URL(urlString);
is = url.openStream();
}
if (is == null) {
SecuritySupport ss = SecuritySupport.getInstance();
is = ss.getResourceAsStream(ObjectFactory.findClassLoader(),
ENCODINGS_FILE);
}
Properties props = new Properties();
if (is != null) {
props.load(is);
is.close();
} else {
// Seems to be no real need to force failure here, let the
// system do its best... The issue is not really very critical,
// and the output will be in any case _correct_ though maybe not
// always human-friendly... :)
// But maybe report/log the resource problem?
// Any standard ways to report/log errors (in static context)?
}
int totalEntries = props.size();
int totalMimeNames = 0;
Enumeration keys = props.keys();
for (int i = 0; i < totalEntries; ++i)
{
String javaName = (String) keys.nextElement();
String val = props.getProperty(javaName);
totalMimeNames++;
int pos = val.indexOf(' ");
for (int j = 0; j < pos; ++j)
if (val.charAt(j) == ',")
totalMimeNames++;
}
EncodingInfo[] ret = new EncodingInfo[totalMimeNames];
int j = 0;
keys = props.keys();
for (int i = 0; i < totalEntries; ++i)
{
String javaName = (String) keys.nextElement();
String val = props.getProperty(javaName);
int pos = val.indexOf(' ");
String mimeName;
//int lastPrintable;
if (pos < 0)
{
// Maybe report/log this problem?
// "Last printable character not defined for encoding " +
// mimeName + " (" + val + ")" ...
mimeName = val;
//lastPrintable = 0x00FF;
}
else
{
//lastPrintable =
// Integer.decode(val.substring(pos).trim()).intValue();
StringTokenizer st =
new StringTokenizer(val.substring(0, pos), ",");
for (boolean first = true;
st.hasMoreTokens();
first = false)
{
mimeName = st.nextToken();
ret[j] =
new EncodingInfo(mimeName, javaName);
_encodingTableKeyMime.put(
mimeName.toUpperCase(),
ret[j]);
if (first)
_encodingTableKeyJava.put(
javaName.toUpperCase(),
ret[j]);
j++;
}
}
}
return ret;
}
catch (java.net.MalformedURLException mue)
{
throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(mue);
}
catch (java.io.IOException ioe)
{
throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(ioe);
}
|
static int | toCodePoint(char highSurrogate, char lowSurrogate)Return the unicode code point represented by the high/low surrogate pair.
This is not a public API.
int codePoint =
((highSurrogate - 0xd800) << 10)
+ (lowSurrogate - 0xdc00)
+ 0x10000;
return codePoint;
|
static int | toCodePoint(char ch)Return the unicode code point represented by the char.
A bit of a dummy method, since all it does is return the char,
but as an int value.
This is not a public API.
int codePoint = ch;
return codePoint;
|
private static java.lang.String | toUpperCaseFast(java.lang.String s)A fast and cheap way to uppercase a String that is
only made of printable ASCII characters.
This is not a public API.
boolean different = false;
final int mx = s.length();
char[] chars = new char[mx];
for (int i=0; i < mx; i++) {
char ch = s.charAt(i);
// is the character a lower case ASCII one?
if ('a" <= ch && ch <= 'z") {
// a cheap and fast way to uppercase that is good enough
ch = (char) (ch + ('A" - 'a"));
different = true; // the uppercased String is different
}
chars[i] = ch;
}
// A little optimization, don't call String.valueOf() if
// the uppercased string is the same as the input string.
final String upper;
if (different)
upper = String.valueOf(chars);
else
upper = s;
return upper;
|