| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize.charcode; |
| 9 |
|
| 10 |
import net.sf.saxon.trans.XPathException; |
| 11 |
|
| 12 |
import javax.xml.transform.OutputKeys; |
| 13 |
import java.nio.charset.Charset; |
| 14 |
import java.nio.charset.IllegalCharsetNameException; |
| 15 |
import java.nio.charset.UnsupportedCharsetException; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.Map; |
| 18 |
import java.util.Properties; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class CharacterSetFactory { |
| 34 |
|
| 35 |
private HashMap<String, CharacterSet> characterSets = new HashMap<String, CharacterSet>(10); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public CharacterSetFactory() { |
| 41 |
HashMap<String, CharacterSet> c = characterSets; |
| 42 |
UTF8CharacterSet utf8 = UTF8CharacterSet.getInstance(); |
| 43 |
c.put("utf8", utf8); |
| 44 |
UTF16CharacterSet utf16 = UTF16CharacterSet.getInstance(); |
| 45 |
c.put("utf16", utf16); |
| 46 |
ASCIICharacterSet acs = ASCIICharacterSet.getInstance(); |
| 47 |
c.put("ascii", acs); |
| 48 |
c.put("iso646", acs); |
| 49 |
c.put("usascii", acs); |
| 50 |
ISO88591CharacterSet lcs = ISO88591CharacterSet.getInstance(); |
| 51 |
c.put("iso88591", lcs); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public void setCharacterSetImplementation( String encoding, CharacterSet charSet) { |
| 62 |
characterSets.put(normalizeCharsetName(encoding), charSet); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private static String normalizeCharsetName(String name) { |
| 73 |
return name.replace("-", "").replace("_", "").toLowerCase(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public CharacterSet getCharacterSet(Properties details) |
| 84 |
throws XPathException { |
| 85 |
|
| 86 |
String encoding = details.getProperty(OutputKeys.ENCODING); |
| 87 |
if (encoding == null) { |
| 88 |
return UTF8CharacterSet.getInstance(); |
| 89 |
} |
| 90 |
return getCharacterSet(encoding); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public CharacterSet getCharacterSet(String encoding) |
| 100 |
throws XPathException { |
| 101 |
|
| 102 |
if (encoding == null) { |
| 103 |
return UTF8CharacterSet.getInstance(); |
| 104 |
} else { |
| 105 |
String encodingKey = normalizeCharsetName(encoding); |
| 106 |
CharacterSet cs = characterSets.get(encodingKey); |
| 107 |
if (cs != null) { |
| 108 |
return cs; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
Charset charset; |
| 114 |
try { |
| 115 |
charset = Charset.forName(encoding); |
| 116 |
CharacterSet res = JavaCharacterSet.makeCharSet(charset); |
| 117 |
characterSets.put(encodingKey, res); |
| 118 |
return res; |
| 119 |
} catch (IllegalCharsetNameException err) { |
| 120 |
XPathException e = new XPathException("Invalid encoding name: " + encoding); |
| 121 |
e.setErrorCode("SESU0007"); |
| 122 |
throw e; |
| 123 |
} catch (UnsupportedCharsetException err) { |
| 124 |
XPathException e = new XPathException("Unknown encoding requested: " + encoding); |
| 125 |
e.setErrorCode("SESU0007"); |
| 126 |
throw e; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public static void main(String[] args) throws Exception { |
| 139 |
System.err.println("Available Character Sets in the java.nio package for this Java VM:"); |
| 140 |
for (String s : Charset.availableCharsets().keySet()) { |
| 141 |
System.err.println(" " + s); |
| 142 |
} |
| 143 |
System.err.println("Registered Character Sets in Saxon:"); |
| 144 |
CharacterSetFactory factory = new CharacterSetFactory(); |
| 145 |
for (Map.Entry e : factory.characterSets.entrySet()) { |
| 146 |
System.err.println(" " + e.getKey() + " = " + e.getValue().getClass().getName()); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
|