286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Encodings.java,v 1.3 2005/09/28 13:49:04 pvedula Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.serializer;
286N/A
286N/Aimport java.io.InputStream;
286N/Aimport java.io.OutputStream;
286N/Aimport java.io.OutputStreamWriter;
286N/Aimport java.io.UnsupportedEncodingException;
286N/Aimport java.io.Writer;
286N/Aimport java.io.BufferedWriter;
286N/Aimport java.net.URL;
286N/Aimport java.util.Enumeration;
286N/Aimport java.util.HashMap;
286N/Aimport java.util.Properties;
286N/Aimport java.util.StringTokenizer;
566N/Aimport java.io.IOException;
566N/Aimport java.net.MalformedURLException;
566N/Aimport java.nio.charset.Charset;
566N/Aimport java.nio.charset.IllegalCharsetNameException;
566N/Aimport java.nio.charset.UnsupportedCharsetException;
566N/Aimport java.util.Collections;
566N/Aimport java.util.Map;
566N/Aimport java.util.Map.Entry;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.utils.SecuritySupport;
286N/A
286N/A/**
286N/A * Provides information about encodings. Depends on the Java runtime
286N/A * to provides writers for the different encodings, but can be used
286N/A * to override encoding names and provide the last printable character
286N/A * for each encoding.
286N/A *
286N/A * @version $Revision: 1.11 $ $Date: 2010-11-01 04:34:44 $
286N/A * @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
286N/A */
286N/A
286N/Apublic final class Encodings extends Object
286N/A{
286N/A
286N/A /**
286N/A * The last printable character for unknown encodings.
286N/A */
286N/A private static final int m_defaultLastPrintable = 0x7F;
286N/A
286N/A /**
286N/A * Standard filename for properties file with encodings data.
286N/A */
286N/A private static final String ENCODINGS_FILE = "com/sun/org/apache/xml/internal/serializer/Encodings.properties";
286N/A
286N/A /**
286N/A * Standard filename for properties file with encodings data.
286N/A */
286N/A private static final String ENCODINGS_PROP = "com.sun.org.apache.xalan.internal.serialize.encodings";
286N/A
286N/A
286N/A /**
286N/A * Returns a writer for the specified encoding based on
286N/A * an output stream.
286N/A *
286N/A * @param output The output stream
286N/A * @param encoding The encoding
286N/A * @return A suitable writer
286N/A * @throws UnsupportedEncodingException There is no convertor
286N/A * to support this encoding
286N/A */
286N/A static Writer getWriter(OutputStream output, String encoding)
286N/A throws UnsupportedEncodingException
286N/A {
286N/A
566N/A final EncodingInfo ei = _encodingInfos.findEncoding(toUpperCaseFast(encoding));
566N/A if (ei != null) {
566N/A try {
566N/A return new BufferedWriter(new OutputStreamWriter(
566N/A output, ei.javaName));
566N/A } catch (UnsupportedEncodingException usee) {
566N/A // keep trying
286N/A }
286N/A }
286N/A
566N/A return new BufferedWriter(new OutputStreamWriter(output, encoding));
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Returns the last printable character for an unspecified
286N/A * encoding.
286N/A *
286N/A * @return the default size
286N/A */
286N/A public static int getLastPrintable()
286N/A {
286N/A return m_defaultLastPrintable;
286N/A }
286N/A
286N/A
286N/A
286N/A /**
286N/A * Returns the EncodingInfo object for the specified
286N/A * encoding.
286N/A * <p>
286N/A * This is not a public API.
286N/A *
286N/A * @param encoding The encoding
286N/A * @return The object that is used to determine if
286N/A * characters are in the given encoding.
286N/A * @xsl.usage internal
286N/A */
286N/A static EncodingInfo getEncodingInfo(String encoding)
286N/A {
286N/A EncodingInfo ei;
286N/A
286N/A String normalizedEncoding = toUpperCaseFast(encoding);
566N/A ei = _encodingInfos.findEncoding(normalizedEncoding);
286N/A if (ei == null) {
286N/A // We shouldn't have to do this, but just in case.
566N/A try {
566N/A // This may happen if the caller tries to use
566N/A // an encoding that wasn't registered in the
566N/A // (java name)->(preferred mime name) mapping file.
566N/A // In that case we attempt to load the charset for the
566N/A // given encoding, and if that succeeds - we create a new
566N/A // EncodingInfo instance - assuming the canonical name
566N/A // of the charset can be used as the mime name.
566N/A final Charset c = Charset.forName(encoding);
566N/A final String name = c.name();
566N/A ei = new EncodingInfo(name, name);
566N/A _encodingInfos.putEncoding(normalizedEncoding, ei);
566N/A } catch (IllegalCharsetNameException | UnsupportedCharsetException x) {
566N/A ei = new EncodingInfo(null,null);
566N/A }
286N/A }
286N/A
286N/A return ei;
286N/A }
286N/A
286N/A /**
286N/A * A fast and cheap way to uppercase a String that is
286N/A * only made of printable ASCII characters.
286N/A * <p>
286N/A * This is not a public API.
286N/A * @param s a String of ASCII characters
286N/A * @return an uppercased version of the input String,
286N/A * possibly the same String.
286N/A * @xsl.usage internal
286N/A */
286N/A static private String toUpperCaseFast(final String s) {
286N/A
286N/A boolean different = false;
286N/A final int mx = s.length();
286N/A char[] chars = new char[mx];
286N/A for (int i=0; i < mx; i++) {
286N/A char ch = s.charAt(i);
286N/A // is the character a lower case ASCII one?
286N/A if ('a' <= ch && ch <= 'z') {
286N/A // a cheap and fast way to uppercase that is good enough
286N/A ch = (char) (ch + ('A' - 'a'));
286N/A different = true; // the uppercased String is different
286N/A }
286N/A chars[i] = ch;
286N/A }
286N/A
286N/A // A little optimization, don't call String.valueOf() if
286N/A // the uppercased string is the same as the input string.
286N/A final String upper;
286N/A if (different)
286N/A upper = String.valueOf(chars);
286N/A else
286N/A upper = s;
286N/A
286N/A return upper;
286N/A }
286N/A
286N/A /** The default encoding, ISO style, ISO style. */
286N/A static final String DEFAULT_MIME_ENCODING = "UTF-8";
286N/A
286N/A /**
286N/A * Get the proper mime encoding. From the XSLT recommendation: "The encoding
286N/A * attribute specifies the preferred encoding to use for outputting the result
286N/A * tree. XSLT processors are required to respect values of UTF-8 and UTF-16.
286N/A * For other values, if the XSLT processor does not support the specified
286N/A * encoding it may signal an error; if it does not signal an error it should
286N/A * use UTF-8 or UTF-16 instead. The XSLT processor must not use an encoding
286N/A * whose name does not match the EncName production of the XML Recommendation
286N/A * [XML]. If no encoding attribute is specified, then the XSLT processor should
286N/A * use either UTF-8 or UTF-16."
286N/A *
286N/A * @param encoding Reference to java-style encoding string, which may be null,
286N/A * in which case a default will be found.
286N/A *
286N/A * @return The ISO-style encoding string, or null if failure.
286N/A */
286N/A static String getMimeEncoding(String encoding)
286N/A {
286N/A
286N/A if (null == encoding)
286N/A {
286N/A try
286N/A {
286N/A
286N/A // Get the default system character encoding. This may be
286N/A // incorrect if they passed in a writer, but right now there
286N/A // seems to be no way to get the encoding from a writer.
524N/A encoding = SecuritySupport.getSystemProperty("file.encoding", "UTF8");
286N/A
286N/A if (null != encoding)
286N/A {
286N/A
286N/A /*
286N/A * See if the mime type is equal to UTF8. If you don't
286N/A * do that, then convertJava2MimeEncoding will convert
286N/A * 8859_1 to "ISO-8859-1", which is not what we want,
286N/A * I think, and I don't think I want to alter the tables
286N/A * to convert everything to UTF-8.
286N/A */
286N/A String jencoding =
286N/A (encoding.equalsIgnoreCase("Cp1252")
286N/A || encoding.equalsIgnoreCase("ISO8859_1")
286N/A || encoding.equalsIgnoreCase("8859_1")
286N/A || encoding.equalsIgnoreCase("UTF8"))
286N/A ? DEFAULT_MIME_ENCODING
286N/A : convertJava2MimeEncoding(encoding);
286N/A
286N/A encoding =
286N/A (null != jencoding) ? jencoding : DEFAULT_MIME_ENCODING;
286N/A }
286N/A else
286N/A {
286N/A encoding = DEFAULT_MIME_ENCODING;
286N/A }
286N/A }
286N/A catch (SecurityException se)
286N/A {
286N/A encoding = DEFAULT_MIME_ENCODING;
286N/A }
286N/A }
286N/A else
286N/A {
286N/A encoding = convertJava2MimeEncoding(encoding);
286N/A }
286N/A
286N/A return encoding;
286N/A }
286N/A
286N/A /**
286N/A * Try the best we can to convert a Java encoding to a XML-style encoding.
286N/A *
286N/A * @param encoding non-null reference to encoding string, java style.
286N/A *
286N/A * @return ISO-style encoding string.
286N/A */
286N/A private static String convertJava2MimeEncoding(String encoding)
286N/A {
566N/A final EncodingInfo enc =
566N/A _encodingInfos.getEncodingFromJavaKey(toUpperCaseFast(encoding));
286N/A if (null != enc)
286N/A return enc.name;
286N/A return encoding;
286N/A }
286N/A
286N/A /**
286N/A * Try the best we can to convert a Java encoding to a XML-style encoding.
286N/A *
286N/A * @param encoding non-null reference to encoding string, java style.
286N/A *
286N/A * @return ISO-style encoding string.
286N/A */
286N/A public static String convertMime2JavaEncoding(String encoding)
286N/A {
566N/A final EncodingInfo info = _encodingInfos.findEncoding(toUpperCaseFast(encoding));
566N/A return info != null ? info.javaName : encoding;
286N/A }
286N/A
566N/A // Using an inner static class here prevent initialization races
566N/A // where the hash maps could be used before they were populated.
566N/A //
566N/A private final static class EncodingInfos {
566N/A // These maps are final and not modified after initialization.
566N/A private final Map<String, EncodingInfo> _encodingTableKeyJava = new HashMap<>();
566N/A private final Map<String, EncodingInfo> _encodingTableKeyMime = new HashMap<>();
566N/A // This map will be added to after initialization: make sure it's
566N/A // thread-safe. This map should not be used frequently - only in cases
566N/A // where the mapping requested was not declared in the Encodings.properties
566N/A // file.
566N/A private final Map<String, EncodingInfo> _encodingDynamicTable =
566N/A Collections.synchronizedMap(new HashMap<String, EncodingInfo>());
566N/A
566N/A private EncodingInfos() {
566N/A loadEncodingInfo();
566N/A }
566N/A
566N/A // Opens the file/resource containing java charset name -> preferred mime
566N/A // name mapping and returns it as an InputStream.
566N/A private InputStream openEncodingsFileStream() throws MalformedURLException, IOException {
286N/A String urlString = null;
286N/A InputStream is = null;
286N/A
566N/A try {
524N/A urlString = SecuritySupport.getSystemProperty(ENCODINGS_PROP, "");
566N/A } catch (SecurityException e) {
286N/A }
286N/A
286N/A if (urlString != null && urlString.length() > 0) {
286N/A URL url = new URL(urlString);
286N/A is = url.openStream();
286N/A }
286N/A
286N/A if (is == null) {
286N/A is = SecuritySupport.getResourceAsStream(ENCODINGS_FILE);
286N/A }
566N/A return is;
566N/A }
286N/A
566N/A // Loads the Properties resource containing the mapping:
566N/A // java charset name -> preferred mime name
566N/A // and returns it.
566N/A private Properties loadProperties() throws MalformedURLException, IOException {
286N/A Properties props = new Properties();
566N/A final InputStream is = openEncodingsFileStream();
566N/A try {
566N/A if (is != null) {
566N/A props.load(is);
566N/A } else {
566N/A // Seems to be no real need to force failure here, let the
566N/A // system do its best... The issue is not really very critical,
566N/A // and the output will be in any case _correct_ though maybe not
566N/A // always human-friendly... :)
566N/A // But maybe report/log the resource problem?
566N/A // Any standard ways to report/log errors (in static context)?
566N/A }
566N/A } finally {
566N/A if (is != null) {
566N/A is.close();
566N/A }
286N/A }
566N/A return props;
566N/A }
286N/A
566N/A // Parses the mime list associated to a java charset name.
566N/A // The first mime name in the list is supposed to be the preferred
566N/A // mime name.
566N/A private String[] parseMimeTypes(String val) {
566N/A int pos = val.indexOf(' ');
566N/A //int lastPrintable;
566N/A if (pos < 0) {
566N/A // Maybe report/log this problem?
566N/A // "Last printable character not defined for encoding " +
566N/A // mimeName + " (" + val + ")" ...
566N/A return new String[] { val };
566N/A //lastPrintable = 0x00FF;
566N/A }
566N/A //lastPrintable =
566N/A // Integer.decode(val.substring(pos).trim()).intValue();
566N/A StringTokenizer st =
566N/A new StringTokenizer(val.substring(0, pos), ",");
566N/A String[] values = new String[st.countTokens()];
566N/A for (int i=0; st.hasMoreTokens(); i++) {
566N/A values[i] = st.nextToken();
566N/A }
566N/A return values;
566N/A }
566N/A
566N/A // This method here attempts to find the canonical charset name for the
566N/A // the given name - which is supposed to be either a java name or a mime
566N/A // name.
566N/A // For that, it attempts to load the charset using the given name, and
566N/A // then returns the charset's canonical name.
566N/A // If the charset could not be loaded from the given name,
566N/A // the method returns null.
566N/A private String findCharsetNameFor(String name) {
566N/A try {
566N/A return Charset.forName(name).name();
566N/A } catch (Exception x) {
566N/A return null;
286N/A }
566N/A }
566N/A
566N/A // This method here attempts to find the canonical charset name for the
566N/A // the set javaName+mimeNames - which are supposed to all refer to the
566N/A // same charset.
566N/A // For that it attempts to load the charset using the javaName, and if
566N/A // not found, attempts again using each of the mime names in turn.
566N/A // If the charset could be loaded from the javaName, then the javaName
566N/A // itself is returned as charset name. Otherwise, each of the mime names
566N/A // is tried in turn, until a charset can be loaded from one of the names,
566N/A // and the loaded charset's canonical name is returned.
566N/A // If no charset can be loaded from either the javaName or one of the
566N/A // mime names, then null is returned.
566N/A //
566N/A // Note that the returned name is the 'java' name that will be used in
566N/A // instances of EncodingInfo.
566N/A // This is important because EncodingInfo uses that 'java name' later on
566N/A // in calls to String.getBytes(javaName).
566N/A // As it happens, sometimes only one element of the set mime names/javaName
566N/A // is known by Charset: sometimes only one of the mime names is known,
566N/A // sometime only the javaName is known, sometimes all are known.
566N/A //
566N/A // By using this method here, we fix the problem where one of the mime
566N/A // names is known but the javaName is unknown, by associating the charset
566N/A // loaded from one of the mime names with the unrecognized javaName.
566N/A //
566N/A // When none of the mime names or javaName are known - there's not much we can
566N/A // do... It can mean that this encoding is not supported for this
566N/A // OS. If such a charset is ever use it will result in having all characters
566N/A // escaped.
566N/A //
566N/A private String findCharsetNameFor(String javaName, String[] mimes) {
566N/A String cs = findCharsetNameFor(javaName);
566N/A if (cs != null) return javaName;
566N/A for (String m : mimes) {
566N/A cs = findCharsetNameFor(m);
566N/A if (cs != null) break;
566N/A }
566N/A return cs;
566N/A }
566N/A
566N/A /**
566N/A * Loads a list of all the supported encodings.
566N/A *
566N/A * System property "encodings" formatted using URL syntax may define an
566N/A * external encodings list. Thanks to Sergey Ushakov for the code
566N/A * contribution!
566N/A */
566N/A private void loadEncodingInfo() {
566N/A try {
566N/A // load (java name)->(preferred mime name) mapping.
566N/A final Properties props = loadProperties();
566N/A
566N/A // create instances of EncodingInfo from the loaded mapping
566N/A Enumeration keys = props.keys();
566N/A Map<String, EncodingInfo> canonicals = new HashMap<>();
566N/A while (keys.hasMoreElements()) {
566N/A final String javaName = (String) keys.nextElement();
566N/A final String[] mimes = parseMimeTypes(props.getProperty(javaName));
566N/A
566N/A final String charsetName = findCharsetNameFor(javaName, mimes);
566N/A if (charsetName != null) {
566N/A final String kj = toUpperCaseFast(javaName);
566N/A final String kc = toUpperCaseFast(charsetName);
566N/A for (int i = 0; i < mimes.length; ++i) {
566N/A final String mimeName = mimes[i];
566N/A final String km = toUpperCaseFast(mimeName);
566N/A EncodingInfo info = new EncodingInfo(mimeName, charsetName);
566N/A _encodingTableKeyMime.put(km, info);
566N/A if (!canonicals.containsKey(kc)) {
566N/A // canonicals will map the charset name to
566N/A // the info containing the prefered mime name
566N/A // (the preferred mime name is the first mime
566N/A // name in the list).
566N/A canonicals.put(kc, info);
566N/A _encodingTableKeyJava.put(kc, info);
566N/A }
566N/A _encodingTableKeyJava.put(kj, info);
566N/A }
566N/A } else {
566N/A // None of the java or mime names on the line were
566N/A // recognized => this charset is not supported?
286N/A }
286N/A }
566N/A
566N/A // Fix up the _encodingTableKeyJava so that the info mapped to
566N/A // the java name contains the preferred mime name.
566N/A // (a given java name can correspond to several mime name,
566N/A // but we want the _encodingTableKeyJava to point to the
566N/A // preferred mime name).
566N/A for (Entry<String, EncodingInfo> e : _encodingTableKeyJava.entrySet()) {
566N/A e.setValue(canonicals.get(toUpperCaseFast(e.getValue().javaName)));
566N/A }
566N/A
566N/A } catch (java.net.MalformedURLException mue) {
566N/A throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(mue);
566N/A } catch (java.io.IOException ioe) {
566N/A throw new com.sun.org.apache.xml.internal.serializer.utils.WrappedRuntimeException(ioe);
286N/A }
286N/A }
566N/A
566N/A EncodingInfo findEncoding(String normalizedEncoding) {
566N/A EncodingInfo info = _encodingTableKeyJava.get(normalizedEncoding);
566N/A if (info == null) {
566N/A info = _encodingTableKeyMime.get(normalizedEncoding);
566N/A }
566N/A if (info == null) {
566N/A info = _encodingDynamicTable.get(normalizedEncoding);
566N/A }
566N/A return info;
286N/A }
566N/A
566N/A EncodingInfo getEncodingFromMimeKey(String normalizedMimeName) {
566N/A return _encodingTableKeyMime.get(normalizedMimeName);
566N/A }
566N/A
566N/A EncodingInfo getEncodingFromJavaKey(String normalizedJavaName) {
566N/A return _encodingTableKeyJava.get(normalizedJavaName);
566N/A }
566N/A
566N/A void putEncoding(String key, EncodingInfo info) {
566N/A _encodingDynamicTable.put(key, info);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Return true if the character is the high member of a surrogate pair.
286N/A * <p>
286N/A * This is not a public API.
286N/A * @param ch the character to test
286N/A * @xsl.usage internal
286N/A */
286N/A static boolean isHighUTF16Surrogate(char ch) {
286N/A return ('\uD800' <= ch && ch <= '\uDBFF');
286N/A }
286N/A /**
286N/A * Return true if the character is the low member of a surrogate pair.
286N/A * <p>
286N/A * This is not a public API.
286N/A * @param ch the character to test
286N/A * @xsl.usage internal
286N/A */
286N/A static boolean isLowUTF16Surrogate(char ch) {
286N/A return ('\uDC00' <= ch && ch <= '\uDFFF');
286N/A }
286N/A /**
286N/A * Return the unicode code point represented by the high/low surrogate pair.
286N/A * <p>
286N/A * This is not a public API.
286N/A * @param highSurrogate the high char of the high/low pair
286N/A * @param lowSurrogate the low char of the high/low pair
286N/A * @xsl.usage internal
286N/A */
286N/A static int toCodePoint(char highSurrogate, char lowSurrogate) {
286N/A int codePoint =
286N/A ((highSurrogate - 0xd800) << 10)
286N/A + (lowSurrogate - 0xdc00)
286N/A + 0x10000;
286N/A return codePoint;
286N/A }
286N/A /**
286N/A * Return the unicode code point represented by the char.
286N/A * A bit of a dummy method, since all it does is return the char,
286N/A * but as an int value.
286N/A * <p>
286N/A * This is not a public API.
286N/A * @param ch the char.
286N/A * @xsl.usage internal
286N/A */
286N/A static int toCodePoint(char ch) {
286N/A int codePoint = ch;
286N/A return codePoint;
286N/A }
286N/A
566N/A private final static EncodingInfos _encodingInfos = new EncodingInfos();
566N/A
286N/A}