1/* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package android.support.v7.mms.pdu; 19 20import java.io.UnsupportedEncodingException; 21import java.util.HashMap; 22 23public class CharacterSets { 24 /** 25 * IANA assigned MIB enum numbers. 26 * 27 * From wap-230-wsp-20010705-a.pdf 28 * Any-charset = <Octet 128> 29 * Equivalent to the special RFC2616 charset value "*" 30 */ 31 public static final int ANY_CHARSET = 0x00; 32 public static final int US_ASCII = 0x03; 33 public static final int ISO_8859_1 = 0x04; 34 public static final int ISO_8859_2 = 0x05; 35 public static final int ISO_8859_3 = 0x06; 36 public static final int ISO_8859_4 = 0x07; 37 public static final int ISO_8859_5 = 0x08; 38 public static final int ISO_8859_6 = 0x09; 39 public static final int ISO_8859_7 = 0x0A; 40 public static final int ISO_8859_8 = 0x0B; 41 public static final int ISO_8859_9 = 0x0C; 42 public static final int SHIFT_JIS = 0x11; 43 public static final int UTF_8 = 0x6A; 44 public static final int BIG5 = 0x07EA; 45 public static final int UCS2 = 0x03E8; 46 public static final int UTF_16 = 0x03F7; 47 48 /** 49 * If the encoding of given data is unsupported, use UTF_8 to decode it. 50 */ 51 public static final int DEFAULT_CHARSET = UTF_8; 52 53 /** 54 * Array of MIB enum numbers. 55 */ 56 private static final int[] MIBENUM_NUMBERS = { 57 ANY_CHARSET, 58 US_ASCII, 59 ISO_8859_1, 60 ISO_8859_2, 61 ISO_8859_3, 62 ISO_8859_4, 63 ISO_8859_5, 64 ISO_8859_6, 65 ISO_8859_7, 66 ISO_8859_8, 67 ISO_8859_9, 68 SHIFT_JIS, 69 UTF_8, 70 BIG5, 71 UCS2, 72 UTF_16, 73 }; 74 75 /** 76 * The Well-known-charset Mime name. 77 */ 78 public static final String MIMENAME_ANY_CHARSET = "*"; 79 public static final String MIMENAME_US_ASCII = "us-ascii"; 80 public static final String MIMENAME_ISO_8859_1 = "iso-8859-1"; 81 public static final String MIMENAME_ISO_8859_2 = "iso-8859-2"; 82 public static final String MIMENAME_ISO_8859_3 = "iso-8859-3"; 83 public static final String MIMENAME_ISO_8859_4 = "iso-8859-4"; 84 public static final String MIMENAME_ISO_8859_5 = "iso-8859-5"; 85 public static final String MIMENAME_ISO_8859_6 = "iso-8859-6"; 86 public static final String MIMENAME_ISO_8859_7 = "iso-8859-7"; 87 public static final String MIMENAME_ISO_8859_8 = "iso-8859-8"; 88 public static final String MIMENAME_ISO_8859_9 = "iso-8859-9"; 89 public static final String MIMENAME_SHIFT_JIS = "shift_JIS"; 90 public static final String MIMENAME_UTF_8 = "utf-8"; 91 public static final String MIMENAME_BIG5 = "big5"; 92 public static final String MIMENAME_UCS2 = "iso-10646-ucs-2"; 93 public static final String MIMENAME_UTF_16 = "utf-16"; 94 95 public static final String DEFAULT_CHARSET_NAME = MIMENAME_UTF_8; 96 97 /** 98 * Array of the names of character sets. 99 */ 100 private static final String[] MIME_NAMES = { 101 MIMENAME_ANY_CHARSET, 102 MIMENAME_US_ASCII, 103 MIMENAME_ISO_8859_1, 104 MIMENAME_ISO_8859_2, 105 MIMENAME_ISO_8859_3, 106 MIMENAME_ISO_8859_4, 107 MIMENAME_ISO_8859_5, 108 MIMENAME_ISO_8859_6, 109 MIMENAME_ISO_8859_7, 110 MIMENAME_ISO_8859_8, 111 MIMENAME_ISO_8859_9, 112 MIMENAME_SHIFT_JIS, 113 MIMENAME_UTF_8, 114 MIMENAME_BIG5, 115 MIMENAME_UCS2, 116 MIMENAME_UTF_16, 117 }; 118 119 private static final HashMap<Integer, String> MIBENUM_TO_NAME_MAP; 120 private static final HashMap<String, Integer> NAME_TO_MIBENUM_MAP; 121 122 static { 123 // Create the HashMaps. 124 MIBENUM_TO_NAME_MAP = new HashMap<Integer, String>(); 125 NAME_TO_MIBENUM_MAP = new HashMap<String, Integer>(); 126 assert(MIBENUM_NUMBERS.length == MIME_NAMES.length); 127 int count = MIBENUM_NUMBERS.length - 1; 128 for(int i = 0; i <= count; i++) { 129 MIBENUM_TO_NAME_MAP.put(MIBENUM_NUMBERS[i], MIME_NAMES[i]); 130 NAME_TO_MIBENUM_MAP.put(MIME_NAMES[i], MIBENUM_NUMBERS[i]); 131 } 132 } 133 134 private CharacterSets() {} // Non-instantiatable 135 136 /** 137 * Map an MIBEnum number to the name of the charset which this number 138 * is assigned to by IANA. 139 * 140 * @param mibEnumValue An IANA assigned MIBEnum number. 141 * @return The name string of the charset. 142 * @throws UnsupportedEncodingException 143 */ 144 public static String getMimeName(int mibEnumValue) 145 throws UnsupportedEncodingException { 146 String name = MIBENUM_TO_NAME_MAP.get(mibEnumValue); 147 if (name == null) { 148 throw new UnsupportedEncodingException(); 149 } 150 return name; 151 } 152 153 /** 154 * Map a well-known charset name to its assigned MIBEnum number. 155 * 156 * @param mimeName The charset name. 157 * @return The MIBEnum number assigned by IANA for this charset. 158 * @throws UnsupportedEncodingException 159 */ 160 public static int getMibEnumValue(String mimeName) 161 throws UnsupportedEncodingException { 162 if(null == mimeName) { 163 return -1; 164 } 165 166 Integer mibEnumValue = NAME_TO_MIBENUM_MAP.get(mimeName); 167 if (mibEnumValue == null) { 168 throw new UnsupportedEncodingException(); 169 } 170 return mibEnumValue; 171 } 172} 173