Source for gnu.java.nio.charset.Provider

   1: /* Provider.java --
   2:    Copyright (C) 2002, 2005, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.nio.charset;
  39: 
  40: import java.nio.charset.Charset;
  41: import java.nio.charset.spi.CharsetProvider;
  42: import java.security.AccessController;
  43: import java.security.PrivilegedAction;
  44: import java.util.Collections;
  45: import java.util.HashMap;
  46: import java.util.Iterator;
  47: 
  48: /**
  49:  * Charset provider for the required charsets.  Used by
  50:  * {@link Charset#charsetForName} and * {@link Charset#availableCharsets}.
  51:  *
  52:  * Note: This class is a privileged class, because it can be instantiated without
  53:  * requiring the RuntimePermission("charsetProvider"). There is a check in
  54:  * java.nio.charset.spi.CharsetProvider to skip the security check if the provider
  55:  * is an instance of this class.
  56:  *
  57:  * @author Jesse Rosenstock
  58:  * @author Robert Schuster (thebohemian@gmx.net)
  59:  * @see Charset
  60:  */
  61: public final class Provider extends CharsetProvider
  62: {
  63:   private static Provider singleton;
  64: 
  65:   /**
  66:    * Map from charset name to charset canonical name. The strings
  67:    * are all lower-case to allow case-insensitive retrieval of
  68:    * Charset instances.
  69:    */
  70:   private final HashMap<String, String> canonicalNames;
  71: 
  72:   /**
  73:    * Map from lower-case canonical name to Charset.
  74:    * TODO: We may want to use soft references.  We would then need to keep
  75:    * track of the class name to regenerate the object.
  76:    */
  77:   private final HashMap<String, Charset> charsets;
  78: 
  79:   /**
  80:    * We don't load all available charsets at the start
  81:    */
  82:   private boolean extendedLoaded;
  83: 
  84:   // Package private to avoid an accessor method in PrivilegedAction below.
  85:   Provider ()
  86:   {
  87:     extendedLoaded = false;
  88:     canonicalNames = new HashMap<String,String> ();
  89:     charsets = new HashMap<String,Charset> ();
  90: 
  91:     // US-ASCII aka ISO646-US
  92:     addCharset (new US_ASCII ());
  93: 
  94:     // ISO-8859-1 aka ISO-LATIN-1
  95:     addCharset (new ISO_8859_1 ());
  96: 
  97:     // UTF-8
  98:     addCharset (new UTF_8 ());
  99: 
 100:     // UTF-16BE
 101:     addCharset (new UTF_16BE ());
 102: 
 103:     // UTF-16LE
 104:     addCharset (new UTF_16LE ());
 105: 
 106:     // UTF-16
 107:     addCharset (new UTF_16 ());
 108: 
 109:     // UTF-16LE (marked)
 110:     addCharset (new UnicodeLittle ());
 111: 
 112:     // Windows-1250 aka cp-1250 (East European)
 113:     addCharset (new Windows1250 ());
 114: 
 115:     // Windows-1251 (Cyrillic)
 116:     addCharset (new Windows1251 ());
 117: 
 118:     // Windows-1252 aka cp-1252 (Latin-1)
 119:     addCharset (new Windows1252 ());
 120: 
 121:     // Windows-1253 (Greek)
 122:     addCharset (new Windows1253 ());
 123: 
 124:     // Windows-1254 (Turkish)
 125:     addCharset (new Windows1254 ());
 126: 
 127:     // Windows-1257 (Baltic)
 128:     addCharset (new Windows1257 ());
 129: 
 130:     // ISO-8859-2 aka ISO-LATIN-2
 131:     addCharset (new ISO_8859_2 ());
 132: 
 133:     // ISO-8859-4 aka ISO-LATIN-4
 134:     addCharset (new ISO_8859_4 ());
 135: 
 136:     // ISO-8859-5 (Cyrillic)
 137:     addCharset (new ISO_8859_5 ());
 138: 
 139:     // ISO-8859-7 (Greek)
 140:     addCharset (new ISO_8859_7 ());
 141: 
 142:     // ISO-8859-9 aka ISO-LATIN-5
 143:     addCharset (new ISO_8859_9 ());
 144: 
 145:     // ISO-8859-13 aka ISO-LATIN-7
 146:     addCharset (new ISO_8859_13 ());
 147: 
 148:     // ISO-8859-15 aka ISO-LATIN-9
 149:     addCharset (new ISO_8859_15 ());
 150: 
 151:     // KOI8 (Cyrillic)
 152:     addCharset (new KOI_8 ());
 153:   }
 154: 
 155:  /**
 156:   * Load non-mandatory charsets.
 157:   */
 158:   private synchronized void loadExtended ()
 159:   {
 160:     if (extendedLoaded)
 161:       return;
 162: 
 163:     addCharset (new ISO_8859_3 ());    // ISO-8859-3 aka ISO-LATIN-3
 164:     addCharset (new ISO_8859_6 ());    // ISO-8859-6 (Arabic)
 165:     addCharset (new ISO_8859_8 ());    // ISO-8859-8 (Hebrew)
 166: 
 167:     // Some more codepages
 168:     addCharset (new Cp424());
 169:     addCharset (new Cp437());
 170:     addCharset (new Cp737());
 171:     addCharset (new Cp775());
 172:     addCharset (new Cp850());
 173:     addCharset (new Cp852());
 174:     addCharset (new Cp855()); // IBM Cyrillic
 175:     addCharset (new Cp857()); // IBM Turkish
 176:     addCharset (new Cp860()); // MSDOS Portugese
 177:     addCharset (new Cp861()); // MSDOS Icelandic
 178:     addCharset (new Cp862()); // PC Hebrew
 179:     addCharset (new Cp863()); // MSDOS Can. French
 180:     addCharset (new Cp864()); // PC Arabic
 181:     addCharset (new Cp865()); // MSDOS Nordic
 182:     addCharset (new Cp866()); // MSDOS Russian
 183:     addCharset (new Cp869()); // IBM modern Greek
 184:     addCharset (new Cp874()); // IBM Thai
 185: 
 186:     addCharset (new MacCentralEurope());
 187:     addCharset (new MacCroatian());
 188:     addCharset (new MacCyrillic());
 189:     addCharset (new MacDingbat());
 190:     addCharset (new MacGreek());
 191:     addCharset (new MacIceland());
 192:     addCharset (new MacRoman());
 193:     addCharset (new MacRomania());
 194:     addCharset (new MacSymbol());
 195:     addCharset (new MacThai());
 196:     addCharset (new MacTurkish());
 197:     addCharset (new MS874());
 198: 
 199:     addCharset (new Windows1255());
 200:     addCharset (new Windows1256());
 201:     addCharset (new Windows1258());
 202: 
 203:     extendedLoaded = true;
 204:   }
 205: 
 206:   public Iterator<Charset> charsets ()
 207:   {
 208:     loadExtended();
 209:     return Collections.unmodifiableCollection (charsets.values ())
 210:                       .iterator ();
 211:   }
 212: 
 213:   /**
 214:    * Returns a Charset instance by converting the given
 215:    * name to lower-case, looking up the canonical charset
 216:    * name and finally looking up the Charset with that name.
 217:    *
 218:    * <p>The lookup is therefore case-insensitive.</p>
 219:    *
 220:    *  @returns The Charset having <code>charsetName</code>
 221:    *  as its alias or null if no such Charset exist.
 222:    */
 223:   public Charset charsetForName (String charsetName)
 224:   {
 225:     Charset cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase()));
 226:     if (cs == null)
 227:      {
 228:        loadExtended();
 229:        cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase()));
 230:      }
 231:     return cs;
 232:   }
 233: 
 234:   /**
 235:    * Puts a Charset under its canonical name into the 'charsets' map.
 236:    * Then puts a mapping from all its alias names to the canonical name.
 237:    *
 238:    * <p>All names are converted to lower-case</p>.
 239:    *
 240:    * @param cs
 241:    */
 242:   private void addCharset (Charset cs)
 243:   {
 244:     String canonicalName = cs.name().toLowerCase();
 245:     charsets.put (canonicalName, cs);
 246: 
 247:     /* Adds a mapping between the canonical name
 248:      * itself making a lookup using that name
 249:      * no special case.
 250:      */
 251:     canonicalNames.put(canonicalName, canonicalName);
 252: 
 253:     for (Iterator<String> i = cs.aliases ().iterator (); i.hasNext (); )
 254:       canonicalNames.put (((String) i.next()).toLowerCase(), canonicalName);
 255:   }
 256: 
 257:   public static synchronized Provider provider ()
 258:   {
 259:     // The default provider is safe to instantiate.
 260:     if (singleton == null)
 261:       singleton = AccessController.doPrivileged
 262:         (new PrivilegedAction<Provider>()
 263:           {
 264:             public Provider run()
 265:             {
 266:               return new Provider();
 267:             }
 268:           });
 269:     return singleton;
 270:   }
 271: }