25 26 package build.tools.charsetmapping; 27 28 import java.io.*; 29 import java.util.Arrays; 30 import java.util.ArrayList; 31 import java.util.Scanner; 32 import java.util.Formatter; 33 import java.util.regex.*; 34 import java.nio.charset.*; 35 import static build.tools.charsetmapping.Utils.*; 36 37 public class HKSCS { 38 39 // HKSCS2001.map has the third column for "UnicodeAlternate", which 40 // is for c->b non-roundtrip mapping. 41 // For HKSCS2008, those non-roundtrip mappings are in .nr file 42 private static Pattern hkscs = 43 Pattern.compile("(?:0x)?+(\\p{XDigit}++)\\s++(?:0x|U\\+)?+(\\p{XDigit}++)?\\s*+(?:0x|U\\+)?(\\p{XDigit}++)?\\s*+.*"); 44 45 static void genClass(String args[]) throws Exception { 46 47 // hkscs2008 48 genClass0(new FileInputStream(new File(args[0], "HKSCS2008.map")), 49 new FileInputStream(new File(args[0], "HKSCS2008.c2b")), 50 new PrintStream(new File(args[1], "HKSCSMapping.java"), 51 "ISO-8859-1"), 52 "HKSCSMapping", 53 getCopyright(new File(args[3]))); 54 55 56 // xp2001 57 genClass0(new FileInputStream(new File(args[0], "HKSCS_XP.map")), 58 null, 59 new PrintStream(new File(args[1], "HKSCS_XPMapping.java"), 60 "ISO-8859-1"), 61 "HKSCS_XPMapping", 62 getCopyright(new File(args[3]))); 63 64 // hkscs2001 65 genClass0(new FileInputStream(new File(args[0], "HKSCS2001.map")), 66 new FileInputStream(new File(args[0], "HKSCS2001.c2b")), 67 new PrintStream(new File(args[1], "HKSCS2001Mapping.java"), 68 "ISO-8859-1"), 69 "HKSCS2001Mapping", 70 getCopyright(new File(args[3]))); 71 } 72 73 static void genClass0(InputStream isB2C, 74 InputStream isC2B, 75 PrintStream ps, 76 String clzName, 77 String copyright) 78 throws Exception 79 { 80 // ranges of byte1 and byte2, something should come from a "config" file 81 int b1Min = 0x87; 82 int b1Max = 0xfe; 83 int b2Min = 0x40; 84 int b2Max = 0xfe; 85 86 try { 87 char[] bmp = new char[0x10000]; 88 char[] supp = new char[0x10000]; 89 90 boolean[] b2cBmp = new boolean[0x100]; 91 boolean[] b2cSupp = new boolean[0x100]; 92 // pua should be in range of e000-f8ff. Expand 93 // it to 0xf93b becase the hkscs2001.c2b has 94 // the f920-f93b filled 95 //char[] pua = new char[0xF8FF - 0xE000 + 1]; 96 char[] pua = new char[0xF93b - 0xE000 + 1]; 115 if (e.cp2 != 0 && e.cp2 >= 0xe000 && e.cp2 <= 0xf8ff) { 116 hasPua = true; 117 pua[e.cp2 - 0xE000] = (char)e.bs; 118 } 119 } 120 121 if (isC2B != null) { 122 p = new Parser(isC2B, hkscs); 123 e = null; 124 while ((e = p.next()) != null) { 125 pua[e.cp - 0xE000] = (char)e.bs; 126 } 127 hasPua = true; 128 } 129 130 StringBuilder sb = new StringBuilder(); 131 Output out = new Output(new Formatter(sb)); 132 133 out.format(copyright); 134 out.format("%n// -- This file was mechanically generated: Do not edit! -- //%n"); 135 out.format("package sun.nio.cs.ext;%n%n"); 136 out.format("class %s {%n%n", clzName); 137 138 /* hardcoded in sun.nio.cs.ext.HKSCS.java 139 out.format(" final static int b1Min = 0x%x;%n", b1Min); 140 out.format(" final static int b1Max = 0x%x;%n", b1Max); 141 out.format(" final static int b2Min = 0x%x;%n", b2Min); 142 out.format(" final static int b2Max = 0x%x;%n", b2Max); 143 */ 144 145 // bmp tables 146 out.format("%n static final String[] b2cBmpStr = new String[] {%n"); 147 for (int i = 0; i < 0x100; i++) { 148 if (b2cBmp[i]) 149 out.format(bmp, i, b2Min, b2Max, ","); 150 else 151 out.format(" null,%n"); //unmappable segments 152 } 153 out.format(" };%n"); 154 155 // supp tables 156 out.format("%n static final String[] b2cSuppStr ="); 157 if (hasSupp) { 158 out.format(" new String[] {%n"); 159 for (int i = 0; i < 0x100; i++) { 160 if (b2cSupp[i]) 161 out.format(supp, i, b2Min, b2Max, ","); 162 else 163 out.format(" null,%n"); //unmappable segments 164 } 165 out.format(" };%n"); 166 } else { 167 out.format(" null;%n"); 168 } 169 170 // private area tables 171 out.format("%n final static String pua ="); 172 if (hasPua) { 173 out.format("%n"); 174 out.format(pua, 0, pua.length, ";"); 175 } else { 176 out.format(" null;%n"); 177 } 178 out.format("%n"); 179 out.format("}"); 180 181 out.close(); 182 183 ps.println(sb.toString()); 184 ps.close(); 185 186 } catch (Exception x) { 187 x.printStackTrace(); 188 } 189 } 190 } | 25 26 package build.tools.charsetmapping; 27 28 import java.io.*; 29 import java.util.Arrays; 30 import java.util.ArrayList; 31 import java.util.Scanner; 32 import java.util.Formatter; 33 import java.util.regex.*; 34 import java.nio.charset.*; 35 import static build.tools.charsetmapping.Utils.*; 36 37 public class HKSCS { 38 39 // HKSCS2001.map has the third column for "UnicodeAlternate", which 40 // is for c->b non-roundtrip mapping. 41 // For HKSCS2008, those non-roundtrip mappings are in .nr file 42 private static Pattern hkscs = 43 Pattern.compile("(?:0x)?+(\\p{XDigit}++)\\s++(?:0x|U\\+)?+(\\p{XDigit}++)?\\s*+(?:0x|U\\+)?(\\p{XDigit}++)?\\s*+.*"); 44 45 static void genClass2008(String srcDir, String dstDir, String pkgName) 46 throws Exception 47 { 48 // hkscs2008 49 genClass0(new FileInputStream(new File(srcDir, "HKSCS2008.map")), 50 new FileInputStream(new File(srcDir, "HKSCS2008.c2b")), 51 new PrintStream(new File(dstDir, "HKSCSMapping.java"), 52 "ISO-8859-1"), 53 pkgName, 54 "HKSCSMapping", 55 true, 56 ""); 57 } 58 59 static void genClassXP(String srcDir, String dstDir, String pkgName) 60 throws Exception 61 { 62 genClass0(new FileInputStream(new File(srcDir, "HKSCS_XP.map")), 63 null, 64 new PrintStream(new File(dstDir, "HKSCS_XPMapping.java"), 65 "ISO-8859-1"), 66 pkgName, 67 "HKSCS_XPMapping", 68 false, 69 ""); 70 } 71 72 static void genClass2001(String args[]) throws Exception { 73 // hkscs2001 74 genClass0(new FileInputStream(new File(args[0], "HKSCS2001.map")), 75 new FileInputStream(new File(args[0], "HKSCS2001.c2b")), 76 new PrintStream(new File(args[1], "HKSCS2001Mapping.java"), 77 "ISO-8859-1"), 78 "sun.nio.cs.ext", 79 "HKSCS2001Mapping", 80 false, 81 getCopyright(new File(args[3]))); 82 } 83 84 static void genClass0(InputStream isB2C, 85 InputStream isC2B, 86 PrintStream ps, 87 String pkgName, 88 String clzName, 89 boolean isPublic, 90 String copyright) 91 throws Exception 92 { 93 // ranges of byte1 and byte2, something should come from a "config" file 94 int b1Min = 0x87; 95 int b1Max = 0xfe; 96 int b2Min = 0x40; 97 int b2Max = 0xfe; 98 99 try { 100 char[] bmp = new char[0x10000]; 101 char[] supp = new char[0x10000]; 102 103 boolean[] b2cBmp = new boolean[0x100]; 104 boolean[] b2cSupp = new boolean[0x100]; 105 // pua should be in range of e000-f8ff. Expand 106 // it to 0xf93b becase the hkscs2001.c2b has 107 // the f920-f93b filled 108 //char[] pua = new char[0xF8FF - 0xE000 + 1]; 109 char[] pua = new char[0xF93b - 0xE000 + 1]; 128 if (e.cp2 != 0 && e.cp2 >= 0xe000 && e.cp2 <= 0xf8ff) { 129 hasPua = true; 130 pua[e.cp2 - 0xE000] = (char)e.bs; 131 } 132 } 133 134 if (isC2B != null) { 135 p = new Parser(isC2B, hkscs); 136 e = null; 137 while ((e = p.next()) != null) { 138 pua[e.cp - 0xE000] = (char)e.bs; 139 } 140 hasPua = true; 141 } 142 143 StringBuilder sb = new StringBuilder(); 144 Output out = new Output(new Formatter(sb)); 145 146 out.format(copyright); 147 out.format("%n// -- This file was mechanically generated: Do not edit! -- //%n"); 148 out.format("package %s;%n%n", pkgName); 149 out.format("%sclass %s {%n%n", isPublic ? "public " : "", clzName); 150 151 /* hardcoded in sun.nio.cs.ext.HKSCS.java 152 out.format(" final static int b1Min = 0x%x;%n", b1Min); 153 out.format(" final static int b1Max = 0x%x;%n", b1Max); 154 out.format(" final static int b2Min = 0x%x;%n", b2Min); 155 out.format(" final static int b2Max = 0x%x;%n", b2Max); 156 */ 157 158 // bmp tables 159 out.format("%n %sstatic final String[] b2cBmpStr = new String[] {%n", 160 isPublic ? "public " : ""); 161 for (int i = 0; i < 0x100; i++) { 162 if (b2cBmp[i]) 163 out.format(bmp, i, b2Min, b2Max, ","); 164 else 165 out.format(" null,%n"); //unmappable segments 166 } 167 out.format(" };%n"); 168 169 // supp tables 170 out.format("%n %sstatic final String[] b2cSuppStr =", 171 isPublic ? "public " : ""); 172 if (hasSupp) { 173 out.format(" new String[] {%n"); 174 for (int i = 0; i < 0x100; i++) { 175 if (b2cSupp[i]) 176 out.format(supp, i, b2Min, b2Max, ","); 177 else 178 out.format(" null,%n"); //unmappable segments 179 } 180 out.format(" };%n"); 181 } else { 182 out.format(" null;%n"); 183 } 184 185 // private area tables 186 out.format("%n %sfinal static String pua =", 187 isPublic ? "public " : ""); 188 if (hasPua) { 189 out.format("%n"); 190 out.format(pua, 0, pua.length, ";"); 191 } else { 192 out.format(" null;%n"); 193 } 194 out.format("%n"); 195 out.format("}"); 196 197 out.close(); 198 199 ps.println(sb.toString()); 200 ps.close(); 201 202 } catch (Exception x) { 203 x.printStackTrace(); 204 } 205 } 206 } |