jdk/src/share/classes/sun/tools/native2ascii/Main.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8560 f2abd715d39b
child 13041 8477cb6992be
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8560
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
        Currently javac and load() method in java.util.Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
        supports only Latin1 encoding input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
        But in Asian platforms programmer or message translator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
        uses the editor which support othere than latin1 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        to specify their native language string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
        So if programmer or message translator wants to use other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        Latin1 character in his/her program source or properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        they must convert the file to ASCII plus \udddd notation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        (javac/load() modification is not appropriate due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
         time constraints for JDK1.1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        This utility is for the purpose of that conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    NAME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        native2ascii - convert native encoding file to ascii file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                       include \udddd Unicode notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    SYNOPSIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        native2ascii [options] [inputfile [outputfile]]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    DESCRIPTION
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        If outputfile is not described standard output is used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        output file, and if inputfile is not also described
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        stardard input is used as input file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Options
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        -reverse
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
           convert ascii with \udddd notation to native encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        -encoding encoding_name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           Specify the encoding name which is used by conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           8859_[1 - 9], JIS, EUCJIS, SJIS is currently supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
           Default encoding is taken from System property "file.encoding".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
package sun.tools.native2ascii;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import java.nio.charset.IllegalCharsetNameException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
import sun.tools.native2ascii.A2NFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
import sun.tools.native2ascii.N2AFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * Main program of the native2ascii
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class Main {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    String inputFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    String outputFileName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    File tempFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    boolean reverse = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static String encodingString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static String defaultEncoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    static CharsetEncoder encoder = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Run the converter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public synchronized boolean convert(String argv[]){
8560
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
    97
        List<String> v = new ArrayList<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        File outputFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        boolean createOutputFile = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Parse arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (int i = 0; i < argv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (argv[i].equals("-encoding")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                if ((i + 1) < argv.length){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    encodingString = argv[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    error(getMsg("err.bad.arg"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            } else if (argv[i].equals("-reverse")){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                reverse = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (v.size() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                }
8560
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
   118
                v.add(argv[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (encodingString == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
           defaultEncoding = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        char[] lineBreak = System.getProperty("line.separator").toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            initializeConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            if (v.size() == 1)
8560
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
   129
                inputFileName = v.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (v.size() == 2) {
8560
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
   132
                inputFileName = v.get(0);
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
   133
                outputFileName = v.get(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                createOutputFile = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (createOutputFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                outputFile = new File(outputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    if (outputFile.exists() && !outputFile.canWrite()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        throw new Exception(formatMsg("err.cannot.write", outputFileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (reverse){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                BufferedReader reader = getA2NInput(inputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                Writer osw = getA2NOutput(outputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                String line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                while ((line = reader.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    osw.write(line.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    osw.write(lineBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (outputFileName == null) { // flush stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        osw.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                reader.close();  // Close the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                osw.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
             //N2A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                String inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                BufferedReader in = getN2AInput(inputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                BufferedWriter out = getN2AOutput(outputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                while ((inLine = in.readLine()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    out.write(inLine.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    out.write(lineBreak);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    if (outputFileName == null) { // flush stdout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            // Since we are done rename temporary file to desired output file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (createOutputFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (outputFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    // Some win32 platforms can't handle atomic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    // rename if source and target file paths are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    // identical. To make things simple we just unconditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    // delete the target file before calling renameTo()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    outputFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                tempFile.renameTo(outputFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch(Exception e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            error(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private void error(String msg){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        System.out.println(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    private void usage(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        System.out.println(getMsg("usage"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private BufferedReader getN2AInput(String inFile) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        InputStream forwardIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (inFile == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            forwardIn = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            File f = new File(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                throw new Exception(formatMsg("err.cannot.read", f.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 forwardIn = new FileInputStream(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
               throw new Exception(formatMsg("err.cannot.read", f.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        BufferedReader r = (encodingString != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            new BufferedReader(new InputStreamReader(forwardIn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                                                     encodingString)) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            new BufferedReader(new InputStreamReader(forwardIn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private BufferedWriter getN2AOutput(String outFile) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        Writer output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        BufferedWriter n2aOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (outFile == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            output = new OutputStreamWriter(System.out,"US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            File f = new File(outFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            File tempDir = f.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (tempDir == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                tempDir = new File(System.getProperty("user.dir"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            tempFile = File.createTempFile("_N2A",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                                           ".TMP",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                            tempDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            tempFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                output = new FileWriter(tempFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } catch (IOException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throw new Exception(formatMsg("err.cannot.write", tempFile.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        n2aOut = new BufferedWriter(new N2AFilter(output));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return n2aOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private BufferedReader getA2NInput(String inFile) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Reader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (inFile == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            in = new InputStreamReader(System.in, "US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            File f = new File(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (!f.canRead()){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                throw new Exception(formatMsg("err.cannot.read", f.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                 in = new FileReader(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
               throw new Exception(formatMsg("err.cannot.read", f.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        reader = new BufferedReader(new A2NFilter(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private Writer getA2NOutput(String outFile) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        OutputStreamWriter w = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        OutputStream output = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (outFile == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            output = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            File f = new File(outFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            File tempDir = f.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (tempDir == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                tempDir = new File(System.getProperty("user.dir"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            tempFile =  File.createTempFile("_N2A",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                            ".TMP",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                            tempDir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            tempFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                output = new FileOutputStream(tempFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            } catch (IOException e){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                throw new Exception(formatMsg("err.cannot.write", tempFile.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        w = (encodingString != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            new OutputStreamWriter(output, encodingString) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            new OutputStreamWriter(output);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return (w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static Charset lookupCharset(String csName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        if (Charset.isSupported(csName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
           try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                return Charset.forName(csName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
           } catch (UnsupportedCharsetException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                throw new Error(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    public static boolean canConvert(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return (encoder != null && encoder.canEncode(ch));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private static void initializeConverter() throws UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Charset cs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            cs = (encodingString == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                lookupCharset(defaultEncoding):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                lookupCharset(encodingString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            encoder =  (cs != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                cs.newEncoder() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } catch (IllegalCharsetNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new Error(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private static ResourceBundle rsrc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            rsrc = ResourceBundle.getBundle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                     "sun.tools.native2ascii.resources.MsgNative2ascii");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            throw new Error("Missing message file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    private String getMsg(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            return (rsrc.getString(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        } catch (MissingResourceException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throw new Error("Error in  message file format.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private String formatMsg(String key, String arg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        String msg = getMsg(key);
8560
f2abd715d39b 7021987: native2ascii "file cannot be read" error message is broken
alanb
parents: 5506
diff changeset
   366
        return MessageFormat.format(msg, arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Main program
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public static void main(String argv[]){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Main converter = new Main();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        System.exit(converter.convert(argv) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}