make/langtools/tools/compileproperties/CompileProperties.java
author herrick
Thu, 28 Mar 2019 13:49:38 -0400
branchJDK-8200758-branch
changeset 57292 7a683c461b80
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13631
dc1212c348f9 7191449: update copyright year to match last edit in jdk8 langtools repository
jjh
parents: 12085
diff changeset
     2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4700
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4700
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4700
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4700
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 4700
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
12085
ce2780cb121f 7150579: Moved ant code into a separate package, anttasks.
ohrstrom
parents: 10183
diff changeset
    26
package compileproperties;
ce2780cb121f 7150579: Moved ant code into a separate package, anttasks.
ohrstrom
parents: 10183
diff changeset
    27
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.FileOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStreamWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.text.MessageFormat;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.util.Properties;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
/** Translates a .properties file into a .java file containing the
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  definition of a java.util.Properties subclass which can then be
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  compiled with javac. <P>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  Usage: java CompileProperties [path to .properties file] [path to .java file to be output] [super class]
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  Infers the package by looking at the common suffix of the two
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  inputs, eliminating "classes" from it.
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * @author Scott Violet
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * @author Kenneth Russell
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
public class CompileProperties {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static void main(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        CompileProperties cp = new CompileProperties();
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        boolean ok = cp.run(args);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        if ( !ok ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            System.exit(1);
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
12085
ce2780cb121f 7150579: Moved ant code into a separate package, anttasks.
ohrstrom
parents: 10183
diff changeset
    66
    public static interface Log {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        void info(String msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        void verbose(String msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        void error(String msg, Exception e);
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private String propfiles[];
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    private String outfiles[] ;
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private String supers[]   ;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private int compileCount = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private boolean quiet = false;
12085
ce2780cb121f 7150579: Moved ant code into a separate package, anttasks.
ohrstrom
parents: 10183
diff changeset
    77
    public Log log;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public void setLog(Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        this.log = log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public boolean run(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        if (log == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            log = new Log() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                public void error(String msg, Exception e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    System.err.println("ERROR: CompileProperties: " + msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                    if ( e != null ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                        System.err.println("EXCEPTION: " + e.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                        e.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                public void info(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                    System.out.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                public void verbose(String msg) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                    if (!quiet)
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                        System.out.println(msg);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        boolean ok = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        /* Original usage */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        if (args.length == 2 && args[0].charAt(0) != '-' ) {
4700
d0ef13314ec4 6764569: [PATCH] Fix unused imports in list resource bundles
jjg
parents: 10
diff changeset
   106
            ok = createFile(args[0], args[1], "java.util.ListResourceBundle");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        } else if (args.length == 3) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            ok = createFile(args[0], args[1], args[2]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        } else if (args.length == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            usage(log);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            /* New batch usage */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            ok = parseOptions(args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            if ( ok && compileCount == 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
                log.error("options parsed but no files to compile", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            /* Need at least one file. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            if ( !ok ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                usage(log);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                /* Process files */
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                for ( int i = 0; i < compileCount && ok ; i++ ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                    ok = createFile(propfiles[i], outfiles[i], supers[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    private boolean parseOptions(String args[]) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        boolean ok = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        if ( compileCount > 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            String new_propfiles[] = new String[compileCount + args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            String new_outfiles[]  = new String[compileCount + args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            String new_supers[]    = new String[compileCount + args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
            System.arraycopy(supers, 0, new_supers, 0, compileCount);
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
            propfiles = new_propfiles;
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            outfiles  = new_outfiles;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            supers    = new_supers;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            propfiles = new String[args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            outfiles  = new String[args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            supers    = new String[args.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        for ( int i = 0; i < args.length ; i++ ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            if ( "-compile".equals(args[i]) && i+3 < args.length ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                propfiles[compileCount] = args[++i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                outfiles[compileCount]  = args[++i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                supers[compileCount]    = args[++i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                compileCount++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                String filename = args[++i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                FileInputStream finput = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                byte contents[] = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                    finput = new FileInputStream(filename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                    int byteCount = finput.available();
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                    if ( byteCount <= 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                        log.error("The -optionsfile file is empty", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                        ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                        contents = new byte[byteCount];
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                        int bytesRead = finput.read(contents);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                        if ( byteCount != bytesRead ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                            log.error("Cannot read all of -optionsfile file", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                            ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                    log.error("cannot open " + filename, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                    ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                if ( finput != null ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                        finput.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                    } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                        ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
                        log.error("cannot close " + filename, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                if ( ok = true && contents != null ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    String tokens[] = (new String(contents)).split("\\s+");
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                    if ( tokens.length > 0 ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                        ok = parseOptions(tokens);
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                if ( !ok ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            } else if ( "-quiet".equals(args[i]) ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                quiet = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                log.error("argument error", null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    private boolean createFile(String propertiesPath, String outputPath,
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            String superClass) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        boolean ok = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        log.verbose("parsing: " + propertiesPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        Properties p = new Properties();
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            p.load(new FileInputStream(propertiesPath));
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        } catch ( FileNotFoundException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            log.error("Cannot find file " + propertiesPath, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            log.error("IO error on file " + propertiesPath, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        if ( ok ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
            String packageName = inferPackageName(propertiesPath, outputPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            log.verbose("inferred package name: " + packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            List<String> sortedKeys = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            for ( Object key : p.keySet() ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                sortedKeys.add((String)key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
            Collections.sort(sortedKeys);
10183
63f2f2e5f642 7058174: Reduce langtools build warnings
jjg
parents: 7681
diff changeset
   227
            Iterator<String> keys = sortedKeys.iterator();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            StringBuffer data = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            while (keys.hasNext()) {
10183
63f2f2e5f642 7058174: Reduce langtools build warnings
jjg
parents: 7681
diff changeset
   232
                String key = keys.next();
63f2f2e5f642 7058174: Reduce langtools build warnings
jjg
parents: 7681
diff changeset
   233
                data.append("            { \"" + escape(key) + "\", \"" +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                        escape((String)p.get(key)) + "\" },\n");
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            // Get class name from java filename, not the properties filename.
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            //   (zh_TW properties might be used to create zh_HK files)
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            File file = new File(outputPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            String name = file.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
            int dotIndex = name.lastIndexOf('.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
            String className;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            if (dotIndex == -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                className = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                className = name.substring(0, dotIndex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            String packageString = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            if (packageName != null && !packageName.equals("")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
                packageString = "package " + packageName + ";\n\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            Writer writer = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                writer = new BufferedWriter(
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                        new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
                MessageFormat format = new MessageFormat(FORMAT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                writer.write(format.format(new Object[] { packageString, className, superClass, data }));
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                log.error("IO error writing to file " + outputPath, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            if ( writer != null ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                    writer.flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                    ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                    log.error("IO error flush " + outputPath, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                    writer.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                } catch ( IOException e ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                    ok = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    log.error("IO error close " + outputPath, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            log.verbose("wrote: " + outputPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        return ok;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    private static void usage(Log log) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        log.info("usage:");
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        log.info("    java CompileProperties path_to_properties_file path_to_java_output_file [super_class]");
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
        log.info("      -OR-");
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        log.info("    java CompileProperties {-compile path_to_properties_file path_to_java_output_file super_class} -or- -optionsfile filename");
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        log.info("");
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        log.info("Example:");
4700
d0ef13314ec4 6764569: [PATCH] Fix unused imports in list resource bundles
jjg
parents: 10
diff changeset
   290
        log.info("    java CompileProperties -compile test.properties test.java java.util.ListResourceBundle");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        log.info("    java CompileProperties -optionsfile option_file");
4700
d0ef13314ec4 6764569: [PATCH] Fix unused imports in list resource bundles
jjg
parents: 10
diff changeset
   292
        log.info("option_file contains: -compile test.properties test.java java.util.ListResourceBundle");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    private static String escape(String theString) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        // This is taken from Properties.saveConvert with changes for Java strings
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
        int len = theString.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        StringBuffer outBuffer = new StringBuffer(len*2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        for(int x=0; x<len; x++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            char aChar = theString.charAt(x);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            switch(aChar) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                case '\\':outBuffer.append('\\'); outBuffer.append('\\');
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                case '\t':outBuffer.append('\\'); outBuffer.append('t');
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                case '\n':outBuffer.append('\\'); outBuffer.append('n');
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                case '\r':outBuffer.append('\\'); outBuffer.append('r');
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                case '\f':outBuffer.append('\\'); outBuffer.append('f');
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                    if ((aChar < 0x0020) || (aChar > 0x007e)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                        outBuffer.append('\\');
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                        outBuffer.append('u');
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                        outBuffer.append(toHex((aChar >>  8) & 0xF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                        outBuffer.append(toHex((aChar >>  4) & 0xF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                        outBuffer.append(toHex( aChar        & 0xF));
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                        if (specialSaveChars.indexOf(aChar) != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                            outBuffer.append('\\');
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                        outBuffer.append(aChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        return outBuffer.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    private static String inferPackageName(String inputPath, String outputPath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        // Normalize file names
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        inputPath  = new File(inputPath).getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        outputPath = new File(outputPath).getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        // Split into components
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        String sep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        if (File.separatorChar == '\\') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            sep = "\\\\";
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            sep = File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        String[] inputs  = inputPath.split(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        String[] outputs = outputPath.split(sep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        // Match common names, eliminating first "classes" entry from
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
        // each if present
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        int inStart  = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
        int inEnd    = inputs.length - 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        int outEnd   = outputs.length - 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        int i = inEnd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
        int j = outEnd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        while (i >= 0 && j >= 0) {
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 13631
diff changeset
   353
            // If a dir contains a dot, it's not a valid package and most likely
83c19f00452c 8054834: Modular Source Code
chegar
parents: 13631
diff changeset
   354
            // a module name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            if (!inputs[i].equals(outputs[j]) ||
25874
83c19f00452c 8054834: Modular Source Code
chegar
parents: 13631
diff changeset
   356
                    (inputs[i].equals("gensrc") && outputs[j].equals("gensrc")) ||
83c19f00452c 8054834: Modular Source Code
chegar
parents: 13631
diff changeset
   357
                    (inputs[i].contains("."))) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                ++i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                ++j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            --i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            --j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
        String result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
        if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            result = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            inStart = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
            for (i = inStart; i <= inEnd; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                buf.append(inputs[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                if (i < inEnd) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                    buf.append('.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            result = buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    private static final String FORMAT =
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            "{0}" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            "public final class {1} extends {2} '{'\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            "    protected final Object[][] getContents() '{'\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
            "        return new Object[][] '{'\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            "{3}" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            "        };\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            "    }\n" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
            "}\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    // This comes from Properties
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
    private static char toHex(int nibble) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        return hexDigit[(nibble & 0xF)];
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
    // This comes from Properties
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
    private static final char[] hexDigit = {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    // Note: different from that in Properties
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    private static final String specialSaveChars = "\"";
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
}