Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/charsetmapping/DBCS.java
32287 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.charsetmapping;26import java.io.*;27import java.util.Arrays;28import java.util.ArrayList;29import java.util.Scanner;30import java.util.Formatter;31import java.util.regex.*;32import java.nio.charset.*;33import static build.tools.charsetmapping.Utils.*;3435public class DBCS {36// pattern used by this class to read in mapping table37static Pattern mPattern = Pattern.compile("(?:0x)?(\\p{XDigit}++)\\s++(?:0x)?(\\p{XDigit}++)(?:\\s++#.*)?");3839public static void genClass(String args[]) throws Exception {4041Scanner s = new Scanner(new File(args[0], args[2]));42while (s.hasNextLine()) {43String line = s.nextLine();44if (line.startsWith("#") || line.length() == 0)45continue;46String[] fields = line.split("\\s+");47if (fields.length < 10) {48System.err.println("Misconfiged sbcs line <" + line + ">?");49continue;50}51String clzName = fields[0];52String csName = fields[1];53String hisName = ("null".equals(fields[2]))?null:fields[2];54String type = fields[3].toUpperCase();55if ("BASIC".equals(type))56type = "";57else58type = "_" + type;59String pkgName = fields[4];60boolean isASCII = Boolean.valueOf(fields[5]);61int b1Min = toInteger(fields[6]);62int b1Max = toInteger(fields[7]);63int b2Min = toInteger(fields[8]);64int b2Max = toInteger(fields[9]);65System.out.printf("%s,%s,%s,%b,%s%n", clzName, csName, hisName, isASCII, pkgName);66genClass0(args[0], args[1], "DoubleByte-X.java.template",67clzName, csName, hisName, pkgName,68isASCII, type,69b1Min, b1Max, b2Min, b2Max);70}71}7273static int toInteger(String s) {74if (s.startsWith("0x") || s.startsWith("0X"))75return Integer.valueOf(s.substring(2), 16);76else77return Integer.valueOf(s);78}7980private static void genClass0(String srcDir, String dstDir, String template,81String clzName,82String csName,83String hisName,84String pkgName,85boolean isASCII,86String type,87int b1Min, int b1Max,88int b2Min, int b2Max)89throws Exception90{9192StringBuilder b2cSB = new StringBuilder();93StringBuilder b2cNRSB = new StringBuilder();94StringBuilder c2bNRSB = new StringBuilder();9596char[] db = new char[0x10000];97char[] c2bIndex = new char[0x100];98int c2bOff = 0x100; // first 0x100 for unmappable segs99100Arrays.fill(db, UNMAPPABLE_DECODING);101Arrays.fill(c2bIndex, UNMAPPABLE_DECODING);102103char[] b2cIndex = new char[0x100];104Arrays.fill(b2cIndex, UNMAPPABLE_DECODING);105106// (1)read in .map to parse all b->c entries107FileInputStream in = new FileInputStream(new File(srcDir, clzName + ".map"));108Parser p = new Parser(in, mPattern);109Entry e = null;110while ((e = p.next()) != null) {111db[e.bs] = (char)e.cp;112113if (e.bs > 0x100 && // db114b2cIndex[e.bs>>8] == UNMAPPABLE_DECODING) {115b2cIndex[e.bs>>8] = 1;116}117118if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {119c2bOff += 0x100;120c2bIndex[e.cp>>8] = 1;121}122}123Output out = new Output(new Formatter(b2cSB));124out.format("%n static final String b2cSBStr =%n");125out.format(db, 0x00, 0x100, ";");126127out.format("%n static final String[] b2cStr = {%n");128for (int i = 0; i < 0x100; i++) {129if (b2cIndex[i] == UNMAPPABLE_DECODING) {130out.format(" null,%n"); //unmappable segments131} else {132out.format(db, i, b2Min, b2Max, ",");133}134}135136out.format(" };%n");137out.close();138139// (2)now parse the .nr file which includes "b->c" non-roundtrip entries140File f = new File(srcDir, clzName + ".nr");141if (f.exists()) {142StringBuilder sb = new StringBuilder();143in = new FileInputStream(f);144p = new Parser(in, mPattern);145e = null;146while ((e = p.next()) != null) {147// A <b,c> pair148sb.append((char)e.bs);149sb.append((char)e.cp);150}151char[] nr = sb.toString().toCharArray();152out = new Output(new Formatter(b2cNRSB));153out.format("String b2cNR =%n");154out.format(nr, 0, nr.length, ";");155out.close();156} else {157b2cNRSB.append("String b2cNR = null;");158}159160// (3)finally the .c2b file which includes c->b non-roundtrip entries161f = new File(srcDir, clzName + ".c2b");162if (f.exists()) {163StringBuilder sb = new StringBuilder();164in = new FileInputStream(f);165p = new Parser(in, mPattern);166e = null;167while ((e = p.next()) != null) {168// A <b,c> pair169if (c2bIndex[e.cp>>8] == UNMAPPABLE_DECODING) {170c2bOff += 0x100;171c2bIndex[e.cp>>8] = 1;172}173sb.append((char)e.bs);174sb.append((char)e.cp);175}176char[] nr = sb.toString().toCharArray();177out = new Output(new Formatter(c2bNRSB));178out.format("String c2bNR =%n");179out.format(nr, 0, nr.length, ";");180out.close();181} else {182c2bNRSB.append("String c2bNR = null;");183}184185// (4)it's time to generate the source file186String b2c = b2cSB.toString();187String b2cNR = b2cNRSB.toString();188String c2bNR = c2bNRSB.toString();189190Scanner s = new Scanner(new File(srcDir, template));191PrintStream ops = new PrintStream(new FileOutputStream(192new File(dstDir, clzName + ".java")));193if (hisName == null)194hisName = "";195196// (5) c2b replacement, only used for JIs0208/0212, which197// are two pure db charsets so default '3f' does not work198// TBD: move this into configuration file199String c2bRepl = "";200if (clzName.startsWith("JIS_X_0208")) {201c2bRepl = "new byte[]{ (byte)0x21, (byte)0x29 },";202} else if (clzName.startsWith("JIS_X_0212")) {203c2bRepl = "new byte[]{ (byte)0x22, (byte)0x44 },";204} else if (clzName.startsWith("IBM300")) {205c2bRepl = "new byte[]{ (byte)0x42, (byte)0x6f },";206}207208while (s.hasNextLine()) {209String line = s.nextLine();210if (line.indexOf("$") == -1) {211ops.println(line);212continue;213}214line = line.replace("$PACKAGE$" , pkgName)215.replace("$IMPLEMENTS$", (hisName == null)?216"" : "implements HistoricallyNamedCharset")217.replace("$NAME_CLZ$", clzName)218.replace("$NAME_ALIASES$",219"sun.nio.cs".equals(pkgName) ?220"StandardCharsets.aliases_" + clzName :221"ExtendedCharsets.aliasesFor(\"" + csName + "\")")222.replace("$NAME_CS$" , csName)223.replace("$CONTAINS$",224"MS932".equals(clzName)?225"return ((cs.name().equals(\"US-ASCII\")) || (cs instanceof JIS_X_0201) || (cs instanceof " + clzName + "));":226(isASCII ?227"return ((cs.name().equals(\"US-ASCII\")) || (cs instanceof " + clzName + "));":228"return (cs instanceof " + clzName + ");"))229.replace("$HISTORICALNAME$",230(hisName == null)? "" :231" public String historicalName() { return \"" + hisName + "\"; }")232.replace("$DECTYPE$", type)233.replace("$ENCTYPE$", type)234.replace("$B1MIN$" , "0x" + Integer.toString(b1Min, 16))235.replace("$B1MAX$" , "0x" + Integer.toString(b1Max, 16))236.replace("$B2MIN$" , "0x" + Integer.toString(b2Min, 16))237.replace("$B2MAX$" , "0x" + Integer.toString(b2Max, 16))238.replace("$B2C$", b2c)239.replace("$C2BLENGTH$", "0x" + Integer.toString(c2bOff, 16))240.replace("$NONROUNDTRIP_B2C$", b2cNR)241.replace("$NONROUNDTRIP_C2B$", c2bNR)242.replace("$ENC_REPLACEMENT$", c2bRepl);243244ops.println(line);245}246ops.close();247}248}249250251