blob: e3d03e078fc358dcb7abaaf756b3621f99a57ff0 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.nio.cs;
27
28import java.lang.ref.SoftReference;
29import java.nio.charset.Charset;
30import java.nio.charset.spi.CharsetProvider;
31import java.util.ArrayList;
32import java.util.TreeMap;
33import java.util.Iterator;
34import java.util.Locale;
35import java.util.Map;
36import sun.misc.ASCIICaseInsensitiveComparator;
37
38
39/**
40 * Abstract base class for charset providers.
41 *
42 * @author Mark Reinhold
43 */
44
45public class AbstractCharsetProvider
46 extends CharsetProvider
47{
48
49 /* Maps canonical names to class names
50 */
51 private Map classMap
52 = new TreeMap(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
53
54 /* Maps alias names to canonical names
55 */
56 private Map aliasMap
57 = new TreeMap(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
58
59 /* Maps canonical names to alias-name arrays
60 */
61 private Map aliasNameMap
62 = new TreeMap(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
63
64 /* Maps canonical names to soft references that hold cached instances
65 */
66 private Map cache
67 = new TreeMap(ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
68
69 private String packagePrefix;
70
71 protected AbstractCharsetProvider() {
72 packagePrefix = "sun.nio.cs";
73 }
74
75 protected AbstractCharsetProvider(String pkgPrefixName) {
76 packagePrefix = pkgPrefixName;
77 }
78
79 /* Add an entry to the given map, but only if no mapping yet exists
80 * for the given name.
81 */
82 private static void put(Map m, String name, Object value) {
83 if (!m.containsKey(name))
84 m.put(name, value);
85 }
86
87 private static void remove(Map m, String name) {
88 Object x = m.remove(name);
89 assert (x != null);
90 }
91
92 /* Declare support for the given charset
93 */
94 protected void charset(String name, String className, String[] aliases) {
95 synchronized (this) {
96 put(classMap, name, className);
97 for (int i = 0; i < aliases.length; i++)
98 put(aliasMap, aliases[i], name);
99 put(aliasNameMap, name, aliases);
100 cache.clear();
101 }
102 }
103
104 protected void deleteCharset(String name, String[] aliases) {
105 synchronized (this) {
106 remove(classMap, name);
107 for (int i = 0; i < aliases.length; i++)
108 remove(aliasMap, aliases[i]);
109 remove(aliasNameMap, name);
110 cache.clear();
111 }
112 }
113
114 /* Late initialization hook, needed by some providers
115 */
116 protected void init() { }
117
118 private String canonicalize(String charsetName) {
119 String acn = (String)aliasMap.get(charsetName);
120 return (acn != null) ? acn : charsetName;
121 }
122
123 private Charset lookup(String csn) {
124
125 // Check cache first
126 SoftReference sr = (SoftReference)cache.get(csn);
127 if (sr != null) {
128 Charset cs = (Charset)sr.get();
129 if (cs != null)
130 return cs;
131 }
132
133 // Do we even support this charset?
134 String cln = (String)classMap.get(csn);
135
136 if (cln == null)
137 return null;
138
139 // Instantiate the charset and cache it
140 try {
141
142 Class c = Class.forName(packagePrefix + "." + cln,
143 true,
144 this.getClass().getClassLoader());
145
146 Charset cs = (Charset)c.newInstance();
147 cache.put(csn, new SoftReference(cs));
148 return cs;
149 } catch (ClassNotFoundException x) {
150 return null;
151 } catch (IllegalAccessException x) {
152 return null;
153 } catch (InstantiationException x) {
154 return null;
155 }
156 }
157
158 public final Charset charsetForName(String charsetName) {
159 synchronized (this) {
160 init();
161 return lookup(canonicalize(charsetName));
162 }
163 }
164
165 public final Iterator<Charset> charsets() {
166
167 final ArrayList ks;
168 synchronized (this) {
169 init();
170 ks = new ArrayList(classMap.keySet());
171 }
172
173 return new Iterator<Charset>() {
174 Iterator i = ks.iterator();
175
176 public boolean hasNext() {
177 return i.hasNext();
178 }
179
180 public Charset next() {
181 String csn = (String)i.next();
182 return lookup(csn);
183 }
184
185 public void remove() {
186 throw new UnsupportedOperationException();
187 }
188 };
189 }
190
191 public final String[] aliases(String charsetName) {
192 synchronized (this) {
193 init();
194 return (String[])aliasNameMap.get(charsetName);
195 }
196 }
197
198}