/core/java/android/util/CharsetUtils.java

https://github.com/BhargzShukla/android_frameworks_base · Java · 198 lines · 59 code · 15 blank · 124 comment · 13 complexity · f8c389d3d3826abcc0e5b50907dce946 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.util;
  17. import android.os.Build;
  18. import android.text.TextUtils;
  19. import java.nio.charset.Charset;
  20. import java.nio.charset.IllegalCharsetNameException;
  21. import java.nio.charset.UnsupportedCharsetException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. /**
  25. * <p>
  26. * A class containing utility methods related to character sets. This
  27. * class is primarily useful for code that wishes to be vendor-aware
  28. * in its interpretation of Japanese charset names (used in DoCoMo,
  29. * KDDI, and SoftBank).
  30. * </p>
  31. *
  32. * <p>
  33. * <b>Note:</b> Developers will need to add an appropriate mapping for
  34. * each vendor-specific charset. You may need to modify the C libraries
  35. * like icu4c in order to let Android support an additional charset.
  36. * </p>
  37. *
  38. * @hide
  39. */
  40. public final class CharsetUtils {
  41. /**
  42. * name of the vendor "DoCoMo". <b>Note:</b> This isn't a public
  43. * constant, in order to keep this class from becoming a de facto
  44. * reference list of vendor names.
  45. */
  46. private static final String VENDOR_DOCOMO = "docomo";
  47. /**
  48. * Name of the vendor "KDDI".
  49. */
  50. private static final String VENDOR_KDDI = "kddi";
  51. /**
  52. * Name of the vendor "SoftBank".
  53. */
  54. private static final String VENDOR_SOFTBANK = "softbank";
  55. /**
  56. * Represents one-to-one mapping from a vendor name to a charset specific to the vendor.
  57. */
  58. private static final Map<String, String> sVendorShiftJisMap = new HashMap<String, String>();
  59. static {
  60. // These variants of Shift_JIS come from icu's mapping data (convrtrs.txt)
  61. sVendorShiftJisMap.put(VENDOR_DOCOMO, "docomo-shift_jis-2007");
  62. sVendorShiftJisMap.put(VENDOR_KDDI, "kddi-shift_jis-2007");
  63. sVendorShiftJisMap.put(VENDOR_SOFTBANK, "softbank-shift_jis-2007");
  64. }
  65. /**
  66. * This class is uninstantiable.
  67. */
  68. private CharsetUtils() {
  69. // This space intentionally left blank.
  70. }
  71. /**
  72. * Returns the name of the vendor-specific character set
  73. * corresponding to the given original character set name and
  74. * vendor. If there is no vendor-specific character set for the
  75. * given name/vendor pair, this returns the original character set name.
  76. *
  77. * @param charsetName the base character set name
  78. * @param vendor the vendor to specialize for. All characters should be lower-cased.
  79. * @return the specialized character set name, or {@code charsetName} if
  80. * there is no specialized name
  81. */
  82. public static String nameForVendor(String charsetName, String vendor) {
  83. if (!TextUtils.isEmpty(charsetName) && !TextUtils.isEmpty(vendor)) {
  84. // You can add your own mapping here.
  85. if (isShiftJis(charsetName)) {
  86. final String vendorShiftJis = sVendorShiftJisMap.get(vendor);
  87. if (vendorShiftJis != null) {
  88. return vendorShiftJis;
  89. }
  90. }
  91. }
  92. return charsetName;
  93. }
  94. /**
  95. * Returns the name of the vendor-specific character set
  96. * corresponding to the given original character set name and the
  97. * default vendor (that is, the targeted vendor of the device this
  98. * code is running on). This method merely calls through to
  99. * {@link #nameForVendor(String,String)}, passing the default vendor
  100. * as the second argument.
  101. *
  102. * @param charsetName the base character set name
  103. * @return the specialized character set name, or {@code charsetName} if
  104. * there is no specialized name
  105. */
  106. public static String nameForDefaultVendor(String charsetName) {
  107. return nameForVendor(charsetName, getDefaultVendor());
  108. }
  109. /**
  110. * Returns the vendor-specific character set corresponding to the
  111. * given original character set name and vendor. If there is no
  112. * vendor-specific character set for the given name/vendor pair,
  113. * this returns the character set corresponding to the original
  114. * name. The vendor name is matched case-insensitively. This
  115. * method merely calls {@code Charset.forName()} on a name
  116. * transformed by a call to {@link #nameForVendor(String,String)}.
  117. *
  118. * @param charsetName the base character set name
  119. * @param vendor the vendor to specialize for
  120. * @return the specialized character set, or the one corresponding
  121. * directly to {@code charsetName} if there is no specialized
  122. * variant
  123. * @throws UnsupportedCharsetException thrown if the named character
  124. * set is not supported by the system
  125. * @throws IllegalCharsetNameException thrown if {@code charsetName}
  126. * has invalid syntax
  127. */
  128. public static Charset charsetForVendor(String charsetName, String vendor)
  129. throws UnsupportedCharsetException, IllegalCharsetNameException {
  130. charsetName = nameForVendor(charsetName, vendor);
  131. return Charset.forName(charsetName);
  132. }
  133. /**
  134. * Returns the vendor-specific character set corresponding to the
  135. * given original character set name and default vendor (that is,
  136. * the targeted vendor of the device this code is running on).
  137. * This method merely calls through to {@link
  138. * #charsetForVendor(String,String)}, passing the default vendor
  139. * as the second argument.
  140. *
  141. * @param charsetName the base character set name
  142. * @return the specialized character set, or the one corresponding
  143. * directly to {@code charsetName} if there is no specialized
  144. * variant
  145. * @throws UnsupportedCharsetException thrown if the named character
  146. * set is not supported by the system
  147. * @throws IllegalCharsetNameException thrown if {@code charsetName}
  148. * has invalid syntax
  149. */
  150. public static Charset charsetForVendor(String charsetName)
  151. throws UnsupportedCharsetException, IllegalCharsetNameException {
  152. return charsetForVendor(charsetName, getDefaultVendor());
  153. }
  154. /**
  155. * Returns whether the given character set name indicates the Shift-JIS
  156. * encoding. Returns false if the name is null.
  157. *
  158. * @param charsetName the character set name
  159. * @return {@code true} if the name corresponds to Shift-JIS or
  160. * {@code false} if not
  161. */
  162. private static boolean isShiftJis(String charsetName) {
  163. // Bail quickly if the length doesn't match.
  164. if (charsetName == null) {
  165. return false;
  166. }
  167. int length = charsetName.length();
  168. if (length != 4 && length != 9) {
  169. return false;
  170. }
  171. return charsetName.equalsIgnoreCase("shift_jis")
  172. || charsetName.equalsIgnoreCase("shift-jis")
  173. || charsetName.equalsIgnoreCase("sjis");
  174. }
  175. /**
  176. * Gets the default vendor for this build.
  177. *
  178. * @return the default vendor name
  179. */
  180. private static String getDefaultVendor() {
  181. return Build.BRAND;
  182. }
  183. }