|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xml.internal.serialize; |
|
|
|
import java.io.OutputStream; |
|
import java.io.OutputStreamWriter; |
|
import java.io.UnsupportedEncodingException; |
|
import java.io.Writer; |
|
import com.sun.org.apache.xerces.internal.util.EncodingMap; |
|
import java.nio.charset.Charset; |
|
import java.nio.charset.CharsetEncoder; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@Deprecated |
|
public class EncodingInfo { |
|
|
|
// name of encoding as registered with IANA; |
|
|
|
String ianaName; |
|
String javaName; |
|
int lastPrintable; |
|
|
|
|
|
CharsetEncoder fCharsetEncoder = null; |
|
|
|
|
|
boolean fHaveTriedCharsetEncoder = false; |
|
|
|
|
|
|
|
*/ |
|
public EncodingInfo(String ianaName, String javaName, int lastPrintable) { |
|
this.ianaName = ianaName; |
|
this.javaName = EncodingMap.getIANA2JavaMapping(ianaName); |
|
this.lastPrintable = lastPrintable; |
|
} |
|
|
|
|
|
|
|
*/ |
|
public String getIANAName() { |
|
return this.ianaName; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Writer getWriter(OutputStream output) |
|
throws UnsupportedEncodingException { |
|
|
|
if (javaName != null) |
|
return new OutputStreamWriter(output, javaName); |
|
javaName = EncodingMap.getIANA2JavaMapping(ianaName); |
|
if(javaName == null) |
|
|
|
return new OutputStreamWriter(output, "UTF8"); |
|
return new OutputStreamWriter(output, javaName); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean isPrintable(char ch) { |
|
if (ch <= this.lastPrintable) { |
|
return true; |
|
} |
|
return isPrintable0(ch); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private boolean isPrintable0(char ch) { |
|
|
|
|
|
if (fCharsetEncoder == null && !fHaveTriedCharsetEncoder) { |
|
|
|
try { |
|
Charset charset = java.nio.charset.Charset.forName(javaName); |
|
if (charset.canEncode()) { |
|
fCharsetEncoder = charset.newEncoder(); |
|
} |
|
|
|
else { |
|
fHaveTriedCharsetEncoder = true; |
|
} |
|
} |
|
catch (Exception e) { |
|
|
|
fHaveTriedCharsetEncoder = true; |
|
} |
|
} |
|
|
|
if (fCharsetEncoder != null) { |
|
try { |
|
return fCharsetEncoder.canEncode(ch); |
|
} |
|
catch (Exception e) { |
|
|
|
fCharsetEncoder = null; |
|
fHaveTriedCharsetEncoder = false; |
|
} |
|
} |
|
|
|
return false; |
|
} |
|
|
|
// is this an encoding name recognized by this JDK? |
|
|
|
public static void testJavaEncodingName(String name) throws UnsupportedEncodingException { |
|
final byte [] bTest = {(byte)'v', (byte)'a', (byte)'l', (byte)'i', (byte)'d'}; |
|
String s = new String(bTest, name); |
|
} |
|
|
|
} |