PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/test/tools/launcher/UnicodeTest.java

#
Java | 197 lines | 134 code | 18 blank | 45 comment | 11 complexity | 449577428eb198f55937c05beb43b0a1 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /*
  24. *
  25. *
  26. * Used by UnicodeTest.sh.
  27. *
  28. * This class creates Java source files using Unicode characters
  29. * that test the limits of what's possible
  30. * - in situations where the platform encoding imposes limits
  31. * (command line arguments, non-Unicode file system)
  32. * - in situations where full Unicode is supported
  33. * (file system access in UTF-8 locales and on Windows 2000++,
  34. * jar file contents)
  35. *
  36. * @author Norbert Lindenberg
  37. */
  38. import java.io.FileOutputStream;
  39. import java.io.OutputStreamWriter;
  40. import java.nio.charset.Charset;
  41. import java.util.Locale;
  42. public class UnicodeTest {
  43. public static void main(String[] args) throws Exception {
  44. String commandLineClassNameSuffix = commandLineClassNameSuffix();
  45. String commandLineClassName = "ClassA" + commandLineClassNameSuffix;
  46. String manifestClassName;
  47. if (hasUnicodeFileSystem()) {
  48. manifestClassName = "ClassB" + unicode;
  49. } else {
  50. manifestClassName = "ClassB" + commandLineClassNameSuffix;
  51. }
  52. generateSource(commandLineClassName, manifestClassName);
  53. generateSource(manifestClassName, commandLineClassName);
  54. generateManifest(manifestClassName);
  55. System.out.println(commandLineClassName);
  56. }
  57. private static final String fileSeparator = System.getProperty("file.separator");
  58. private static final String osName = System.getProperty("os.name");
  59. private static final String defaultEncoding = Charset.defaultCharset().name();
  60. // language names taken from java.util.Locale.getDisplayLanguage for the respective language
  61. private static final String arabic = "\u0627\u0644\u0639\u0631\u0628\u064a\u0629";
  62. private static final String s_chinese = "\u4e2d\u6587";
  63. private static final String t_chinese = "\u4e2d\u6587";
  64. private static final String russian = "\u0440\u0443\u0441\u0441\u043A\u0438\u0439";
  65. private static final String hindi = "\u0939\u093f\u0902\u0926\u0940";
  66. private static final String greek = "\u03b5\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac";
  67. private static final String hebrew = "\u05e2\u05d1\u05e8\u05d9\u05ea";
  68. private static final String japanese = "\u65e5\u672c\u8a9e";
  69. private static final String korean = "\ud55c\uad6d\uc5b4";
  70. private static final String lithuanian = "Lietuvi\u0173";
  71. private static final String czech = "\u010de\u0161tina";
  72. private static final String turkish = "T\u00fcrk\u00e7e";
  73. private static final String spanish = "espa\u00f1ol";
  74. private static final String thai = "\u0e44\u0e17\u0e22";
  75. private static final String unicode = arabic + s_chinese + t_chinese
  76. + russian + hindi + greek + hebrew + japanese + korean
  77. + lithuanian + czech + turkish + spanish + thai;
  78. private static String commandLineClassNameSuffix() {
  79. // Mapping from main platform encodings to language names
  80. // for Unix and Windows, respectively. Use empty suffix
  81. // for Windows encodings where OEM encoding differs.
  82. // Use null if encoding isn't used.
  83. String[][] names = {
  84. { "UTF-8", unicode, "" },
  85. { "windows-1256", null, "" },
  86. { "iso-8859-6", arabic, null },
  87. { "GBK", s_chinese, s_chinese },
  88. { "GB18030", s_chinese, s_chinese },
  89. { "GB2312", s_chinese, null },
  90. { "x-windows-950", null, t_chinese },
  91. { "x-MS950-HKSCS", null, t_chinese },
  92. { "x-euc-tw", t_chinese, null },
  93. { "Big5", t_chinese, null },
  94. { "Big5-HKSCS", t_chinese, null },
  95. { "windows-1251", null, "" },
  96. { "iso-8859-5", russian, null },
  97. { "koi8-r", russian, null },
  98. { "windows-1253", null, "" },
  99. { "iso-8859-7", greek, null },
  100. { "windows-1255", null, "" },
  101. { "iso8859-8", hebrew, null },
  102. { "windows-31j", null, japanese },
  103. { "x-eucJP-Open", japanese, null },
  104. { "x-EUC-JP-LINUX", japanese, null },
  105. { "x-pck", japanese, null },
  106. { "x-windows-949", null, korean },
  107. { "euc-kr", korean, null },
  108. { "windows-1257", null, "" },
  109. { "iso-8859-13", lithuanian, null },
  110. { "windows-1250", null, "" },
  111. { "iso-8859-2", czech, null },
  112. { "windows-1254", null, "" },
  113. { "iso-8859-9", turkish, null },
  114. { "windows-1252", null, "" },
  115. { "iso-8859-1", spanish, null },
  116. { "iso-8859-15", spanish, null },
  117. { "x-windows-874", null, thai },
  118. { "tis-620", thai, null },
  119. };
  120. int column;
  121. if (osName.startsWith("Windows")) {
  122. column = 2;
  123. } else {
  124. column = 1;
  125. }
  126. for (int i = 0; i < names.length; i++) {
  127. if (names[i][0].equalsIgnoreCase(defaultEncoding)) {
  128. return names[i][column];
  129. }
  130. }
  131. return "";
  132. }
  133. private static boolean hasUnicodeFileSystem() {
  134. if (osName.startsWith("Windows")) {
  135. return ! osName.startsWith("Windows 9") &&
  136. ! osName.equals("Windows Me");
  137. } else {
  138. return defaultEncoding.equalsIgnoreCase("UTF-8");
  139. }
  140. }
  141. private static void generateSource(String thisClass, String otherClass) throws Exception {
  142. String fileName = "UnicodeTest-src" + fileSeparator + thisClass + ".java";
  143. OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
  144. out.write("public class " + thisClass + " {\n");
  145. out.write(" public static void main(String[] args) {\n");
  146. out.write(" if (!" + otherClass + "." + otherClass.toLowerCase() + "().equals(\"" + otherClass + "\")) {\n");
  147. out.write(" throw new RuntimeException();\n");
  148. out.write(" }\n");
  149. out.write(" }\n");
  150. out.write(" public static String " + thisClass.toLowerCase() + "() {\n");
  151. out.write(" return \"" + thisClass + "\";\n");
  152. out.write(" }\n");
  153. out.write("}\n");
  154. out.close();
  155. }
  156. private static void generateManifest(String mainClass) throws Exception {
  157. String fileName = "UnicodeTest-src" + fileSeparator + "MANIFEST.MF";
  158. FileOutputStream out = new FileOutputStream(fileName);
  159. out.write("Manifest-Version: 1.0\n".getBytes("UTF-8"));
  160. // Header lines are limited to 72 bytes.
  161. // The manifest spec doesn't say we have to break at character boundaries,
  162. // so we rudely break at byte boundaries.
  163. byte[] headerBytes = ("Main-Class: " + mainClass + "\n").getBytes("UTF-8");
  164. if (headerBytes.length <= 72) {
  165. out.write(headerBytes);
  166. } else {
  167. out.write(headerBytes, 0, 72);
  168. int start = 72;
  169. while (headerBytes.length > start) {
  170. out.write((byte) '\n');
  171. out.write((byte) ' ');
  172. int count = Math.min(71, headerBytes.length - start);
  173. out.write(headerBytes, start, count);
  174. start += count;
  175. }
  176. }
  177. out.close();
  178. }
  179. }