Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/native2ascii/Main.java
32270 views
/*1* Copyright (c) 1996, 2012, 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*/2425/*26*/2728/*29Currently javac and load() method in java.util.Properties30supports only Latin1 encoding input.31But in Asian platforms programmer or message translator32uses the editor which support othere than latin1 encoding33to specify their native language string.34So if programmer or message translator wants to use other than35Latin1 character in his/her program source or properties file36they must convert the file to ASCII plus \udddd notation.37(javac/load() modification is not appropriate due to38time constraints for JDK1.1)39This utility is for the purpose of that conversion.4041NAME42native2ascii - convert native encoding file to ascii file43include \udddd Unicode notation4445SYNOPSIS46native2ascii [options] [inputfile [outputfile]]4748DESCRIPTION49If outputfile is not described standard output is used as50output file, and if inputfile is not also described51stardard input is used as input file.5253Options5455-reverse56convert ascii with \udddd notation to native encoding5758-encoding encoding_name59Specify the encoding name which is used by conversion.608859_[1 - 9], JIS, EUCJIS, SJIS is currently supported.61Default encoding is taken from System property "file.encoding".6263*/6465package sun.tools.native2ascii;6667import java.io.*;68import java.util.*;69import java.text.MessageFormat;70import java.nio.charset.CharsetEncoder;71import java.nio.charset.Charset;72import java.nio.charset.IllegalCharsetNameException;73import java.io.UnsupportedEncodingException;74import java.nio.charset.UnsupportedCharsetException;7576/**77* Main program of the native2ascii78*/7980public class Main {8182String inputFileName = null;83String outputFileName = null;84File tempFile = null;85boolean reverse = false;86static String encodingString = null;87static String defaultEncoding = null;88static CharsetEncoder encoder = null;8990/**91* Run the converter92*/93public synchronized boolean convert(String argv[]) {94List<String> v = new ArrayList<>(2);95File outputFile = null;96boolean createOutputFile = false;9798// Parse arguments99for (int i = 0; i < argv.length; i++) {100if (argv[i].equals("-encoding")) {101if ((i + 1) < argv.length) {102encodingString = argv[++i];103} else {104error(getMsg("err.bad.arg"));105usage();106return false;107}108} else if (argv[i].equals("-reverse")) {109reverse = true;110} else {111if (v.size() > 1) {112usage();113return false;114}115v.add(argv[i]);116}117}118119if (encodingString == null) {120defaultEncoding = Charset.defaultCharset().name();121}122char[] lineBreak = System.getProperty("line.separator").toCharArray();123124try {125initializeConverter();126127if (v.size() == 1) {128inputFileName = v.get(0);129}130131if (v.size() == 2) {132inputFileName = v.get(0);133outputFileName = v.get(1);134createOutputFile = true;135}136137if (createOutputFile) {138outputFile = new File(outputFileName);139if (outputFile.exists() && !outputFile.canWrite()) {140throw new Exception(formatMsg("err.cannot.write", outputFileName));141}142}143144if (reverse) {145try (BufferedReader reader = getA2NInput(inputFileName);146Writer osw = getA2NOutput(outputFileName);) {147String line;148while ((line = reader.readLine()) != null) {149osw.write(line.toCharArray());150osw.write(lineBreak);151if (outputFileName == null) { // flush stdout152osw.flush();153}154}155}156} else {157// N2A158try (BufferedReader in = getN2AInput(inputFileName);159BufferedWriter out = getN2AOutput(outputFileName);) {160String inLine;161while ((inLine = in.readLine()) != null) {162out.write(inLine.toCharArray());163out.write(lineBreak);164if (outputFileName == null) { // flush stdout165out.flush();166}167}168}169}170171// Since we are done rename temporary file to desired output file172if (createOutputFile) {173if (outputFile.exists()) {174// Some win32 platforms can't handle atomic175// rename if source and target file paths are176// identical. To make things simple we just unconditionally177// delete the target file before calling renameTo()178outputFile.delete();179}180tempFile.renameTo(outputFile);181}182} catch (Exception e) {183error(e.toString());184return false;185}186187return true;188}189190private void error(String msg){191System.out.println(msg);192}193194private void usage(){195System.out.println(getMsg("usage"));196}197198199private BufferedReader getN2AInput(String inFile) throws Exception {200201InputStream forwardIn;202if (inFile == null)203forwardIn = System.in;204else {205File f = new File(inFile);206if (!f.canRead()){207throw new Exception(formatMsg("err.cannot.read", f.getName()));208}209210try {211forwardIn = new FileInputStream(inFile);212} catch (IOException e) {213throw new Exception(formatMsg("err.cannot.read", f.getName()));214}215}216217BufferedReader r = (encodingString != null) ?218new BufferedReader(new InputStreamReader(forwardIn,219encodingString)) :220new BufferedReader(new InputStreamReader(forwardIn));221return r;222}223224225private BufferedWriter getN2AOutput(String outFile) throws Exception {226Writer output;227BufferedWriter n2aOut;228229if (outFile == null)230output = new OutputStreamWriter(System.out,"US-ASCII");231232else {233File f = new File(outFile);234235File tempDir = f.getParentFile();236237if (tempDir == null)238tempDir = new File(System.getProperty("user.dir"));239240tempFile = File.createTempFile("_N2A",241".TMP",242tempDir);243tempFile.deleteOnExit();244245try {246output = new FileWriter(tempFile);247} catch (IOException e){248throw new Exception(formatMsg("err.cannot.write", tempFile.getName()));249}250}251252n2aOut = new BufferedWriter(new N2AFilter(output));253return n2aOut;254}255256private BufferedReader getA2NInput(String inFile) throws Exception {257Reader in;258BufferedReader reader;259260if (inFile == null)261in = new InputStreamReader(System.in, "US-ASCII");262else {263File f = new File(inFile);264if (!f.canRead()){265throw new Exception(formatMsg("err.cannot.read", f.getName()));266}267268try {269in = new FileReader(inFile);270} catch (Exception e) {271throw new Exception(formatMsg("err.cannot.read", f.getName()));272}273}274275reader = new BufferedReader(new A2NFilter(in));276return reader;277}278279private Writer getA2NOutput(String outFile) throws Exception {280281OutputStreamWriter w = null;282OutputStream output = null;283284if (outFile == null)285output = System.out;286else {287File f = new File(outFile);288289File tempDir = f.getParentFile();290if (tempDir == null)291tempDir = new File(System.getProperty("user.dir"));292tempFile = File.createTempFile("_N2A",293".TMP",294tempDir);295tempFile.deleteOnExit();296297try {298output = new FileOutputStream(tempFile);299} catch (IOException e){300throw new Exception(formatMsg("err.cannot.write", tempFile.getName()));301}302}303304w = (encodingString != null) ?305new OutputStreamWriter(output, encodingString) :306new OutputStreamWriter(output);307308return (w);309}310311private static Charset lookupCharset(String csName) {312if (Charset.isSupported(csName)) {313try {314return Charset.forName(csName);315} catch (UnsupportedCharsetException x) {316throw new Error(x);317}318}319return null;320}321322public static boolean canConvert(char ch) {323return (encoder != null && encoder.canEncode(ch));324}325326private static void initializeConverter() throws UnsupportedEncodingException {327Charset cs = null;328329try {330cs = (encodingString == null) ?331lookupCharset(defaultEncoding):332lookupCharset(encodingString);333334encoder = (cs != null) ?335cs.newEncoder() :336null;337} catch (IllegalCharsetNameException e) {338throw new Error(e);339}340}341342private static ResourceBundle rsrc;343344static {345try {346rsrc = ResourceBundle.getBundle(347"sun.tools.native2ascii.resources.MsgNative2ascii");348} catch (MissingResourceException e) {349throw new Error("Missing message file.");350}351}352353private String getMsg(String key) {354try {355return (rsrc.getString(key));356} catch (MissingResourceException e) {357throw new Error("Error in message file format.");358}359}360361private String formatMsg(String key, String arg) {362String msg = getMsg(key);363return MessageFormat.format(msg, arg);364}365366367/**368* Main program369*/370public static void main(String argv[]){371Main converter = new Main();372System.exit(converter.convert(argv) ? 0 : 1);373}374}375376377