Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/charsetmapping/Utils.java
32287 views
/*1* Copyright (c) 2008, 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;2627import java.io.File;28import java.io.InputStream;29import java.io.InputStreamReader;30import java.io.OutputStream;31import java.io.BufferedReader;32import java.io.IOException;33import java.util.regex.Matcher;34import java.util.regex.Pattern;35import java.util.Scanner;36import java.util.Formatter;3738public class Utils {3940public final static char UNMAPPABLE_DECODING = '\uFFFD';41public final static int UNMAPPABLE_ENCODING = 0xFFFD;4243public static class Entry {44public int bs; //byte sequence reps45public int cp; //Unicode codepoint46public int cp2; //CC of composite4748public Entry () {}49public Entry (int bytes, int cp, int cp2) {50this.bs = bytes;51this.cp = cp;52this.cp2 = cp2;53}54}5556public static class Parser {57static final Pattern basic = Pattern.compile("(?:0x)?(\\p{XDigit}++)\\s++(?:0x)?(\\p{XDigit}++)?\\s*+.*");58static final int gBS = 1;59static final int gCP = 2;60static final int gCP2 = 3;6162BufferedReader reader;63boolean closed;64Matcher matcher;65int gbs, gcp, gcp2;6667public Parser (InputStream in, Pattern p, int gbs, int gcp, int gcp2)68throws IOException69{70this.reader = new BufferedReader(new InputStreamReader(in));71this.closed = false;72this.matcher = p.matcher("");73this.gbs = gbs;74this.gcp = gcp;75this.gcp2 = gcp2;76}7778public Parser (InputStream in, Pattern p) throws IOException {79this(in, p, gBS, gCP, gCP2);80}8182public Parser (InputStream in) throws IOException {83this(in, basic, gBS, gCP, gCP2);84}8586protected boolean isDirective(String line) {87return line.startsWith("#");88}8990protected Entry parse(Matcher matcher, Entry mapping) {91mapping.bs = Integer.parseInt(matcher.group(gbs), 16);92mapping.cp = Integer.parseInt(matcher.group(gcp), 16);93if (gcp2 <= matcher.groupCount() &&94matcher.group(gcp2) != null)95mapping.cp2 = Integer.parseInt(matcher.group(gcp2), 16);96else97mapping.cp2 = 0;98return mapping;99}100101public Entry next() throws Exception {102return next(new Entry());103}104105// returns null and closes the input stream if the eof has beenreached.106public Entry next(Entry mapping) throws Exception {107if (closed)108return null;109String line;110while ((line = reader.readLine()) != null) {111if (isDirective(line))112continue;113matcher.reset(line);114if (!matcher.lookingAt()) {115//System.out.println("Missed: " + line);116continue;117}118return parse(matcher, mapping);119}120reader.close();121closed = true;122return null;123}124}125126public static class Output {127private Formatter out;128129public Output(Formatter out) {130this.out = out;131}132133public void close() {134out.close();135}136137private void toChar(String fmt, char c) {138switch (c) {139case '\b':140out.format("\\b"); break;141case '\t':142out.format("\\t"); break;143case '\n':144out.format("\\n"); break;145case '\f':146out.format("\\f"); break;147case '\r':148out.format("\\r"); break;149case '\"':150out.format("\\\""); break;151case '\'':152out.format("\\'"); break;153case '\\':154out.format("\\\\"); break;155default:156out.format(fmt, c & 0xffff);157}158}159160public void format(String fmt, Object ... args) {161out.format(fmt, args);162}163164public void format(char[] cc, int off, int end, String closure) {165while (off < end) {166out.format(" \"");167for (int j = 0; j < 8; j++) {168if (off == end)169break;170toChar("\\u%04X", cc[off++]);171}172if (off == end)173out.format("\" %s%n", closure);174else175out.format("\" + %n");176}177}178179public void format(char[] cc, String closure) {180format(cc, 0, cc.length, closure);181}182183public void format(char[] db, int b1, int b2Min, int b2Max,184String closure)185{186char[] cc = new char[b2Max - b2Min + 1];187int off = 0;188for (int b2 = b2Min; b2 <= b2Max; b2++) {189cc[off++] = db[(b1 << 8) | b2];190}191format(cc, 0, cc.length, closure);192}193194public void format(char[] date) {195int off = 0;196int end = date.length;197while (off < end) {198out.format(" ");199for (int j = 0; j < 8 && off < end; j++) {200toChar("'\\u%04X',", date[off++]);201}202out.format("%n");203}204}205}206207public static String getCopyright(File f) throws IOException {208Scanner s = new Scanner(f, "ISO-8859-1");209StringBuilder sb = new StringBuilder();210while (s.hasNextLine()) {211String ln = s.nextLine();212sb.append(ln + "\n");213// assume we have the copyright as the first comment214if (ln.matches("^\\s\\*\\/$"))215break;216}217s.close();218return sb.toString();219}220}221222223