Source for gnu.java.nio.charset.Provider

   1: /* Provider.java -- 
   2:    Copyright (C) 2002, 2005 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.util.Collections;
  43: import java.util.HashMap;
  44: import java.util.Iterator;
  45: 
  46: /**
  47:  * Charset provider for the required charsets.  Used by
  48:  * {@link Charset#charsetForName} and * {@link Charset#availableCharsets}.
  49:  *
  50:  * @author Jesse Rosenstock
  51:  * @author Robert Schuster (thebohemian@gmx.net)
  52:  * @see Charset
  53:  */
  54: public final class Provider extends CharsetProvider
  55: {
  56:   private static Provider singleton;
  57: 
  58:   static
  59:   {
  60:     synchronized (Provider.class)
  61:       {
  62:         singleton = null;
  63:       }
  64:   }
  65: 
  66:   /**
  67:    * Map from charset name to charset canonical name. The strings
  68:    * are all lower-case to allow case-insensitive retrieval of
  69:    * Charset instances. 
  70:    */
  71:   private final HashMap canonicalNames;
  72: 
  73:   /**
  74:    * Map from lower-case canonical name to Charset.
  75:    * TODO: We may want to use soft references.  We would then need to keep
  76:    * track of the class name to regenerate the object.
  77:    */
  78:   private final HashMap charsets;
  79: 
  80:   /**
  81:    * We don't load all available charsets at the start
  82:    */
  83:   private boolean extendedLoaded;
  84: 
  85:   private Provider ()
  86:   {
  87:     extendedLoaded = false;
  88:     canonicalNames = new HashMap ();
  89:     charsets = new HashMap ();
  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 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 Cp855()); // IBM Cyrillic
 169:     addCharset (new Cp857()); // IBM Turkish
 170:     addCharset (new Cp860()); // MSDOS Portugese
 171:     addCharset (new Cp861()); // MSDOS Icelandic
 172:     addCharset (new Cp862()); // PC Hebrew
 173:     addCharset (new Cp863()); // MSDOS Can. French
 174:     addCharset (new Cp864()); // PC Arabic
 175:     addCharset (new Cp865()); // MSDOS Nordic
 176:     addCharset (new Cp866()); // MSDOS Russian
 177:     addCharset (new Cp869()); // IBM modern Greek
 178:     addCharset (new Cp874()); // IBM Thai
 179:     extendedLoaded = true;
 180:   }
 181: 
 182:   public Iterator charsets ()
 183:   {
 184:     loadExtended();
 185:     return Collections.unmodifiableCollection (charsets.values ())
 186:                       .iterator ();
 187:   }
 188: 
 189:   /**
 190:    * Returns a Charset instance by converting the given
 191:    * name to lower-case, looking up the canonical charset
 192:    * name and finally looking up the Charset with that name.
 193:    * 
 194:    * <p>The lookup is therefore case-insensitive.</p>
 195:    * 
 196:    *  @returns The Charset having <code>charsetName</code>
 197:    *  as its alias or null if no such Charset exist.
 198:    */
 199:   public Charset charsetForName (String charsetName)
 200:   {
 201:     Charset cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase()));
 202:     if(cs == null && !extendedLoaded)
 203:      {
 204:        loadExtended();
 205:        cs = (Charset) charsets.get(canonicalNames.get(charsetName.toLowerCase()));
 206:      }
 207:     return cs;
 208:   }
 209: 
 210:   /**
 211:    * Puts a Charset under its canonical name into the 'charsets' map.
 212:    * Then puts a mapping from all its alias names to the canonical name.
 213:    * 
 214:    * <p>All names are converted to lower-case</p>.
 215:    * 
 216:    * @param cs
 217:    */
 218:   private void addCharset (Charset cs)
 219:   {
 220:     String canonicalName = cs.name().toLowerCase();
 221:     charsets.put (canonicalName, cs);
 222:     
 223:     /* Adds a mapping between the canonical name
 224:      * itself making a lookup using that name
 225:      * no special case.
 226:      */  
 227:     canonicalNames.put(canonicalName, canonicalName);
 228: 
 229:     for (Iterator i = cs.aliases ().iterator (); i.hasNext (); )
 230:       canonicalNames.put (((String) i.next()).toLowerCase(), canonicalName);
 231:   }
 232: 
 233:   public static synchronized Provider provider ()
 234:   {
 235:     if (singleton == null)
 236:       singleton = new Provider ();
 237:     return singleton;
 238:   }
 239: }