Source for gnu.CORBA.GIOP.CharSets_OSF

   1: /* CharSets_OSF.java --
   2:    Copyright (C) 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: 
  39: package gnu.CORBA.GIOP;
  40: 
  41: import java.nio.charset.Charset;
  42: 
  43: import java.util.Hashtable;
  44: import java.util.Iterator;
  45: import java.util.Set;
  46: 
  47: /**
  48:  * This class contains the codes, used to identify character sets
  49:  * in CORBA. These codes are defined in Open Software Foundation (OSF)
  50:  * code set registry.
  51:  *
  52:  * The name of this class specially sets "OSF" apart if somebody would start
  53:  * searching Open Software Foundation abbreviation.
  54:  *
  55:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  56:  */
  57: public class CharSets_OSF
  58: {
  59:   public static final int ASCII = 0x00010020;
  60:   public static final int ISO8859_1 = 0x00010001;
  61:   public static final int ISO8859_2 = 0x00010002;
  62:   public static final int ISO8859_3 = 0x00010003;
  63:   public static final int ISO8859_4 = 0x00010004;
  64:   public static final int ISO8859_5 = 0x00010005;
  65:   public static final int ISO8859_6 = 0x00010006;
  66:   public static final int ISO8859_7 = 0x00010007;
  67:   public static final int ISO8859_8 = 0x00010008;
  68:   public static final int ISO8859_9 = 0x00010009;
  69:   public static final int ISO8859_15_FDIS = 0x0001000F;
  70:   public static final int UTF8 = 0x05010001;
  71:   public static final int UTF16 = 0x00010109;
  72:   public static final int UCS2 = 0x00010100;
  73:   public static final int Cp1047 = 0x10020417;
  74:   public static final int Cp1250 = 0x100204E2;
  75:   public static final int Cp1251 = 0x100204E3;
  76:   public static final int Cp1252 = 0x100204E4;
  77:   public static final int Cp1253 = 0x100204E5;
  78:   public static final int Cp1254 = 0x100204E6;
  79:   public static final int Cp1255 = 0x100204E7;
  80:   public static final int Cp1256 = 0x100204E8;
  81:   public static final int Cp1257 = 0x100204E9;
  82:   public static final int Cp1363 = 0x10020553;
  83:   public static final int Cp1363C = 0x10020553;
  84:   public static final int Cp1381 = 0x10020565;
  85:   public static final int Cp1383 = 0x10020567;
  86:   public static final int Cp1386 = 0x1002056A;
  87:   public static final int Cp33722 = 0x100283BA;
  88:   public static final int Cp33722C = 0x100283BA;
  89:   public static final int Cp930 = 0x100203A2;
  90:   public static final int Cp943 = 0x100203AF;
  91:   public static final int Cp943C = 0x100203AF;
  92:   public static final int Cp949 = 0x100203B5;
  93:   public static final int Cp949C = 0x100203B5;
  94:   public static final int Cp950 = 0x100203B6;
  95:   public static final int Cp964 = 0x100203C4;
  96:   public static final int Cp970 = 0x100203CA;
  97:   public static final int EUC_JP = 0x00030010;
  98:   public static final int EUC_KR = 0x0004000A;
  99:   public static final int EUC_TW = 0x00050010;
 100: 
 101:   /**
 102:    * The native character set for the narrow character.
 103:    */
 104:   public static final int NATIVE_CHARACTER = ISO8859_1;
 105: 
 106:   /**
 107:    * The native character set for the wide character.
 108:    */
 109:   public static final int NATIVE_WIDE_CHARACTER = UTF16;
 110: 
 111:   /**
 112:    * Table to convert from the code to string name.
 113:    */
 114:   private static Hashtable code_to_string;
 115: 
 116:   /**
 117:    * Table to convert from the string name to code.
 118:    */
 119:   private static Hashtable string_to_code;
 120: 
 121:   /**
 122:    * Get the charset code from its name.
 123:    *
 124:    * @return the charset code of 0 if not defined.
 125:    */
 126:   public static int getCode(String name)
 127:   {
 128:     if (string_to_code == null)
 129:       makeMap();
 130: 
 131:     Integer code = (Integer) string_to_code.get(name);
 132:     return code == null ? 0 : code.intValue();
 133:   }
 134: 
 135:   /**
 136:    * Get the charset name from its code.
 137:    *
 138:    * @return the code set name or nullfor the unknown code set.
 139:    */
 140:   public static String getName(int code)
 141:   {
 142:     if (code_to_string == null)
 143:       makeMap();
 144:     return (String) code_to_string.get(new Integer(code));
 145:   }
 146: 
 147:   /**
 148:    * Get the list of supported char sets for that the CORBA codes are
 149:    * also known.
 150:    */
 151:   public static int[] getSupportedCharSets()
 152:   {
 153:     Set supported_sets = Charset.availableCharsets().keySet();
 154:     int[] supported = new int[ supported_sets.size() ];
 155:     Iterator iter = supported_sets.iterator();
 156: 
 157:     int i = 0;
 158:     int code;
 159:     while (iter.hasNext())
 160:       {
 161:         code = getCode(iter.next().toString());
 162:         if (code > 0)
 163:           supported [ i++ ] = code;
 164:       }
 165: 
 166:     // Truncate the unused part.
 167:     int[] f = new int[ i ];
 168:     System.arraycopy(supported, 0, f, 0, f.length);
 169: 
 170:     return f;
 171:   }
 172: 
 173:   /**
 174:    * Create a convertion map.
 175:    */
 176:   private static void makeMap()
 177:   {
 178:     code_to_string = new Hashtable();
 179:     string_to_code = new Hashtable();
 180: 
 181:     // Put standard char sets.
 182:     put(ASCII, "US-ASCII");
 183:     put(ISO8859_1, "ISO-8859-1");
 184:     put(UTF16, "UTF-16");
 185: 
 186:     // Put other known char sets.
 187:     put(ISO8859_2, "ISO-8859-2");
 188:     put(ISO8859_3, "ISO-8859-3");
 189:     put(ISO8859_4, "ISO-8859-4");
 190:     put(ISO8859_5, "ISO-8859-5");
 191:     put(ISO8859_6, "ISO-8859-6");
 192:     put(ISO8859_7, "ISO-8859-7");
 193:     put(ISO8859_8, "ISO-8859-8");
 194:     put(ISO8859_9, "ISO-8859-9");
 195: 
 196:     put(UTF8, "UTF-8");
 197:     put(UCS2, "UCS-2");
 198: 
 199:     put(ISO8859_15_FDIS, "ISO8859-15-FDIS");
 200: 
 201:     put(Cp1047, "Cp1047");
 202:     put(Cp1250, "Cp1250");
 203:     put(Cp1251, "Cp1251");
 204:     put(Cp1252, "Cp1252");
 205:     put(Cp1253, "Cp1253");
 206:     put(Cp1254, "Cp1254");
 207:     put(Cp1255, "Cp1255");
 208:     put(Cp1256, "Cp1256");
 209:     put(Cp1257, "Cp1257");
 210:     put(Cp1363, "Cp1363");
 211:     put(Cp1363C, "Cp1363C");
 212:     put(Cp1381, "Cp1381");
 213:     put(Cp1383, "Cp1383");
 214:     put(Cp1386, "Cp1386");
 215:     put(Cp33722, "Cp33722");
 216:     put(Cp33722C, "Cp33722C");
 217:     put(Cp930, "Cp930");
 218:     put(Cp943, "Cp943");
 219:     put(Cp943C, "Cp943C");
 220:     put(Cp949, "Cp949");
 221:     put(Cp949C, "Cp949C");
 222:     put(Cp950, "Cp950");
 223:     put(Cp964, "Cp964");
 224:     put(Cp970, "Cp970");
 225: 
 226:     put(EUC_JP, "EUC-JP");
 227:     put(EUC_KR, "EUC-KR");
 228:     put(EUC_TW, "EUC-TW");
 229:   }
 230: 
 231:   private static void put(int code, String name)
 232:   {
 233:     Integer ic = new Integer(code);
 234: 
 235:     code_to_string.put(ic, name);
 236:     string_to_code.put(name, ic);
 237:   }
 238: }