Debian Codegraph @

net/sf/saxon/serialize/charcode/CharacterSetFactory.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * This class delivers a CharacterSet object for a given named encoding.
22 * <p>It maintains a mapping from character set names to class names, and a separate mapping
23 * from character set names to instances of those classes. This means that a class is not
24 * actually instantiated until the encoding is used, but once instantiated, the same instance
25 * is used whenever that encoding is used again in the same Configuration.</p>
26 * <p>Note that the purpose of the CharacterSet object is only to record which Unicode
27 * characters are represented in the encoding, so that non-encodable characters can
28 * be represented as XML or HTML character references. The actual translation from Unicode
29 * codepoints to bytes in the chosen encoding is left to the Java IO library.</p>
30 */
31
32
33 public class CharacterSetFactory {
34
35 private HashMap<String, CharacterSet> characterSets = new HashMap<String, CharacterSet>(10);
36
37 /**
38 * Class has a single instance per Configuration
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 * Register an implementation of a character set, using the class name
56 *
57 * @param encoding the name of the character set
58 * @param charSet the name of a class that implements {@link net.sf.saxon.serialize.charcode.CharacterSet}
59 */
60
61 public void setCharacterSetImplementation(/*@NotNull*/ String encoding, CharacterSet charSet) {
62 characterSets.put(normalizeCharsetName(encoding), charSet);
63 }
64
65 /**
66 * Normalize the name of a character set
67 *
68 * @param name the character set name
69 * @return the normalized name (removes hyphens and underscores and converts to lower-case)
70 */
71
72 private static String normalizeCharsetName(String name) {
73 return name.replace("-", "").replace("_", "").toLowerCase();
74 }
75
76 /**
77 * Make a CharacterSet appropriate to the encoding
78 *
79 * @param details the serialization properties
80 * @return the constructed CharacterSet
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 * Make a CharacterSet appropriate to the encoding
95 * @param encoding the required encoding
96 * @return the constructed CharacterSet
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 // Otherwise see if the Java VM knows anything about the character set
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 * Main program is a utility to give a list of the character sets supported
133 * by the Java VM
134 *
135 * @param args command line arguments (none needed)
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