make/src/classes/build/tools/charsetmapping/SBCS.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  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 SBCS {
  38 
  39     public static void genClass(String args[]) throws Exception {
  40 
  41         Scanner s = new Scanner(new File(args[0], args[2]));
  42         while (s.hasNextLine()) {
  43             String line = s.nextLine();
  44             if (line.startsWith("#") || line.length() == 0)
  45                 continue;
  46             String[] fields = line.split("\\s+");
  47             if (fields.length < 5) {
  48                 System.err.println("Misconfiged sbcs line <" + line + ">?");
  49                 continue;
  50             }
  51             String clzName = fields[0];
  52             String csName  = fields[1];
  53             String hisName = fields[2];
  54             boolean isASCII = Boolean.valueOf(fields[3]);
  55             String pkgName  = fields[4];
  56             System.out.printf("%s,%s,%s,%b,%s%n", clzName, csName, hisName, isASCII, pkgName);
  57 
  58             genClass0(args[0], args[1], "SingleByte-X.java.template",
  59                       clzName, csName, hisName, pkgName, isASCII);
  60         }
  61     }
  62 
  63     private static void toString(char[] sb, int off, int end,
  64                                  Formatter out, String closure,
  65                                  boolean comment) {
  66         while (off < end) {
  67             out.format("        \"");
  68             for (int j = 0; j < 8; j++) {
  69                 if (off == end)
  70                     break;
  71                 char c = sb[off++];
  72                 switch (c) {
  73                 case '\b':
  74                     out.format("\\b"); break;
  75                 case '\t':
  76                     out.format("\\t"); break;
  77                 case '\n':
  78                     out.format("\\n"); break;
  79                 case '\f':
  80                     out.format("\\f"); break;
  81                 case '\r':
  82                     out.format("\\r"); break;
  83                 case '\"':
  84                     out.format("\\\""); break;
  85                 case '\'':
  86                     out.format("\\'"); break;
  87                 case '\\':
  88                     out.format("\\\\"); break;
  89                 default:
  90                     out.format("\\u%04X", c & 0xffff);
  91                 }
  92             }
  93             if (comment) {
  94                 if (off == end)
  95                     out.format("\" %s      // 0x%02x - 0x%02x%n",
  96                                closure, off-8, off-1);
  97                 else
  98                     out.format("\" +      // 0x%02x - 0x%02x%n",
  99                                off-8, off-1);
 100             } else {
 101                 if (off == end)
 102                     out.format("\"%s%n", closure);
 103                 else
 104                     out.format("\" +%n");
 105             }
 106         }
 107     }
 108 
 109     static Pattern sbmap = Pattern.compile("0x(\\p{XDigit}++)\\s++(?:U\\+|0x)?(\\p{XDigit}++)(?:\\s++#.*)?");
 110 
 111     private static void genClass0(String srcDir, String dstDir,
 112                                   String template,
 113                                   String clzName,
 114                                   String csName,
 115                                   String hisName,
 116                                   String pkgName,
 117                                   boolean isASCII)
 118         throws Exception
 119     {






 120         StringBuilder b2cSB = new StringBuilder();
 121         StringBuilder b2cNRSB = new StringBuilder();
 122         StringBuilder c2bNRSB = new StringBuilder();
 123 
 124         char[] sb = new char[0x100];
 125         char[] c2bIndex = new char[0x100];
 126         int    c2bOff = 0;
 127         Arrays.fill(sb, UNMAPPABLE_DECODING);
 128         Arrays.fill(c2bIndex, UNMAPPABLE_DECODING);
 129 
 130         // (1)read in .map to parse all b->c entries
 131         FileInputStream in = new FileInputStream(
 132                                  new File(srcDir, clzName + ".map"));
 133         Parser p = new Parser(in, sbmap);
 134         Entry  e = null;
 135 
 136         while ((e = p.next()) != null) {
 137             sb[e.bs] = (char)e.cp;
 138             if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {
 139                 c2bOff += 0x100;


 249                 line = line.replace("$B2CTABLE$", b2c);
 250             }
 251             if (line.indexOf("$C2BLENGTH$") != -1) {
 252                 line = line.replace("$C2BLENGTH$", "0x" + Integer.toString(c2bOff, 16));
 253             }
 254             if (line.indexOf("$NONROUNDTRIP_B2C$") != -1) {
 255                 if (b2cNR.length() == 0)
 256                     continue;
 257                 line = line.replace("$NONROUNDTRIP_B2C$", b2cNR);
 258             }
 259 
 260             if (line.indexOf("$NONROUNDTRIP_C2B$") != -1) {
 261                 if (c2bNR.length() == 0)
 262                     continue;
 263                 line = line.replace("$NONROUNDTRIP_C2B$", c2bNR);
 264             }
 265             out.println(line);
 266         }
 267         out.close();
 268     }














































 269 }
   1 /*
   2  * Copyright (c) 2008, 2015, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  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.Pattern;

  34 import static build.tools.charsetmapping.Utils.*;
  35 
  36 public class SBCS {
  37 






































































  38     static Pattern sbmap = Pattern.compile("0x(\\p{XDigit}++)\\s++(?:U\\+|0x)?(\\p{XDigit}++)(?:\\s++#.*)?");
  39 
  40     public static void genClass(Charset cs,
  41                                 String srcDir, String dstDir, String template)





  42         throws Exception
  43     {
  44         String clzName = cs.clzName;
  45         String csName  = cs.csName;
  46         String hisName = cs.hisName;
  47         String pkgName = cs.pkgName;
  48         boolean isASCII = cs.isASCII;
  49 
  50         StringBuilder b2cSB = new StringBuilder();
  51         StringBuilder b2cNRSB = new StringBuilder();
  52         StringBuilder c2bNRSB = new StringBuilder();
  53 
  54         char[] sb = new char[0x100];
  55         char[] c2bIndex = new char[0x100];
  56         int    c2bOff = 0;
  57         Arrays.fill(sb, UNMAPPABLE_DECODING);
  58         Arrays.fill(c2bIndex, UNMAPPABLE_DECODING);
  59 
  60         // (1)read in .map to parse all b->c entries
  61         FileInputStream in = new FileInputStream(
  62                                  new File(srcDir, clzName + ".map"));
  63         Parser p = new Parser(in, sbmap);
  64         Entry  e = null;
  65 
  66         while ((e = p.next()) != null) {
  67             sb[e.bs] = (char)e.cp;
  68             if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {
  69                 c2bOff += 0x100;


 179                 line = line.replace("$B2CTABLE$", b2c);
 180             }
 181             if (line.indexOf("$C2BLENGTH$") != -1) {
 182                 line = line.replace("$C2BLENGTH$", "0x" + Integer.toString(c2bOff, 16));
 183             }
 184             if (line.indexOf("$NONROUNDTRIP_B2C$") != -1) {
 185                 if (b2cNR.length() == 0)
 186                     continue;
 187                 line = line.replace("$NONROUNDTRIP_B2C$", b2cNR);
 188             }
 189 
 190             if (line.indexOf("$NONROUNDTRIP_C2B$") != -1) {
 191                 if (c2bNR.length() == 0)
 192                     continue;
 193                 line = line.replace("$NONROUNDTRIP_C2B$", c2bNR);
 194             }
 195             out.println(line);
 196         }
 197         out.close();
 198     }
 199 
 200     private static void toString(char[] sb, int off, int end,
 201                                  Formatter out, String closure, boolean comment)
 202     {
 203         while (off < end) {
 204             out.format("        \"");
 205             for (int j = 0; j < 8; j++) {
 206                 if (off == end)
 207                     break;
 208                 char c = sb[off++];
 209                 switch (c) {
 210                 case '\b':
 211                     out.format("\\b"); break;
 212                 case '\t':
 213                     out.format("\\t"); break;
 214                 case '\n':
 215                     out.format("\\n"); break;
 216                 case '\f':
 217                     out.format("\\f"); break;
 218                 case '\r':
 219                     out.format("\\r"); break;
 220                 case '\"':
 221                     out.format("\\\""); break;
 222                 case '\'':
 223                     out.format("\\'"); break;
 224                 case '\\':
 225                     out.format("\\\\"); break;
 226                 default:
 227                     out.format("\\u%04X", c & 0xffff);
 228                 }
 229             }
 230             if (comment) {
 231                 if (off == end)
 232                     out.format("\" %s      // 0x%02x - 0x%02x%n",
 233                                closure, off-8, off-1);
 234                 else
 235                     out.format("\" +      // 0x%02x - 0x%02x%n",
 236                                off-8, off-1);
 237             } else {
 238                 if (off == end)
 239                     out.format("\"%s%n", closure);
 240                 else
 241                     out.format("\" +%n");
 242             }
 243         }
 244     }
 245 }