make/jdk/src/classes/build/tools/compileproperties/CompileProperties.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 47216 71c04702a3d5
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2002, 2013, 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
package build.tools.compileproperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.BufferedWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.MessageFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/** Translates a .properties file into a .java file containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *  definition of a java.util.Properties subclass which can then be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  compiled with javac. <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *  Usage: java -jar compileproperties.jar [path to .properties file] [path to .java file to be output] [super class]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *  Infers the package by looking at the common suffix of the two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *  inputs, eliminating "classes" from it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Kenneth Russell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class CompileProperties {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static final String FORMAT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            "{0}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            "import java.util.ListResourceBundle;\n\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            "public final class {1} extends {2} '{'\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            "    protected final Object[][] getContents() '{'\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            "        return new Object[][] '{'\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            "{3}" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            "        };\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            "    }\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            "}\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // This comes from Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final char[] hexDigit = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Note: different from that in Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String specialSaveChars = "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // This comes from Properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static char toHex(int nibble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return hexDigit[(nibble & 0xF)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void error(String msg, Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        System.err.println("ERROR: compileproperties: " + msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if ( e != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.err.println("EXCEPTION: " + e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static String propfiles[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static String outfiles[] ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static String supers[]   ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static int compileCount = 0;
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
    93
    private static boolean quiet = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private static boolean parseOptions(String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        boolean ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if ( compileCount > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String new_propfiles[] = new String[compileCount + args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            String new_outfiles[]  = new String[compileCount + args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            String new_supers[]    = new String[compileCount + args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            System.arraycopy(supers, 0, new_supers, 0, compileCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            propfiles = new_propfiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            outfiles  = new_outfiles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            supers    = new_supers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            propfiles = new String[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            outfiles  = new String[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            supers    = new String[args.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        for ( int i = 0; i < args.length ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if ( "-compile".equals(args[i]) && i+3 < args.length ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                propfiles[compileCount] = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                outfiles[compileCount]  = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                supers[compileCount]    = args[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                compileCount++;
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   118
            } else if ( args[i].charAt(0) == '@') {
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   119
                String filename = args[i].substring(1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                FileInputStream finput = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                byte contents[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    finput = new FileInputStream(filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    int byteCount = finput.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    if ( byteCount <= 0 ) {
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   126
                        error("The @file is empty", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        contents = new byte[byteCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        int bytesRead = finput.read(contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        if ( byteCount != bytesRead ) {
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   132
                            error("Cannot read all of @file", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    error("cannot open " + filename, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if ( finput != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        finput.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        error("cannot close " + filename, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   148
                if ( ok && contents != null ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    String tokens[] = (new String(contents)).split("\\s+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    if ( tokens.length > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ok = parseOptions(tokens);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if ( !ok ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                error("argument error", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        boolean ok = true;
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   167
        if (args.length >= 1 && args[0].equals("-quiet"))
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   168
        {
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   169
            quiet = true;
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   170
            String[] newargs = new String[args.length-1];
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   171
            System.arraycopy(args, 1, newargs, 0, newargs.length);
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   172
            args = newargs;
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   173
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        /* Original usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (args.length == 2 && args[0].charAt(0) != '-' ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            ok = createFile(args[0], args[1], "ListResourceBundle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } else if (args.length == 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            ok = createFile(args[0], args[1], args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } else if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            /* New batch usage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            ok = parseOptions(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if ( ok && compileCount == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                error("options parsed but no files to compile", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            /* Need at least one file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if ( !ok ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                /* Process files */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                for ( int i = 0; i < compileCount && ok ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    ok = createFile(propfiles[i], outfiles[i], supers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if ( !ok ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    private static void usage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        System.err.println("usage:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        System.err.println("    java -jar compileproperties.jar path_to_properties_file path_to_java_output_file [super_class]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        System.err.println("      -OR-");
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   208
        System.err.println("    java -jar compileproperties.jar {-compile path_to_properties_file path_to_java_output_file super_class} -or- @filename");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        System.err.println("Example:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        System.err.println("    java -jar compileproperties.jar -compile test.properties test.java ListResourceBundle");
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   212
        System.err.println("    java -jar compileproperties.jar @option_file");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.err.println("option_file contains: -compile test.properties test.java ListResourceBundle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private static boolean createFile(String propertiesPath, String outputPath,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            String superClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        boolean ok = true;
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   219
        if (!quiet) {
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   220
            System.out.println("parsing: " + propertiesPath);
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   221
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        Properties p = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            p.load(new FileInputStream(propertiesPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch ( FileNotFoundException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            error("Cannot find file " + propertiesPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            error("IO error on file " + propertiesPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if ( ok ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            String packageName = inferPackageName(propertiesPath, outputPath);
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   234
            if (!quiet) {
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   235
                System.out.println("inferred package name: " + packageName);
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   236
            }
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   237
            List<String> sortedKeys = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            for ( Object key : p.keySet() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                sortedKeys.add((String)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            Collections.sort(sortedKeys);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            StringBuffer data = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
10110
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   245
            for (String key : sortedKeys) {
75674d930b1f 7058708: Eliminate JDK build tools build warnings
jjg
parents: 5506
diff changeset
   246
                data.append("            { \"" + escape(key) + "\", \"" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        escape((String)p.get(key)) + "\" },\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Get class name from java filename, not the properties filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            //   (zh_TW properties might be used to create zh_HK files)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            File file = new File(outputPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            String name = file.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int dotIndex = name.lastIndexOf('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            String className;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (dotIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                className = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                className = name.substring(0, dotIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String packageString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            if (packageName != null && !packageName.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                packageString = "package " + packageName + ";\n\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            Writer writer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                writer = new BufferedWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                        new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                MessageFormat format = new MessageFormat(FORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                writer.write(format.format(new Object[] { packageString, className, superClass, data }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                error("IO error writing to file " + outputPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if ( writer != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    writer.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    error("IO error flush " + outputPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    writer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                } catch ( IOException e ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    error("IO error close " + outputPath, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
12317
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   291
            if (!quiet) {
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   292
                System.out.println("wrote: " + outputPath);
9670c1610c53 7074397: Build infrastructure changes (makefile re-write)
ohair
parents: 10110
diff changeset
   293
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        return ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private static String escape(String theString) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // This is taken from Properties.saveConvert with changes for Java strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int len = theString.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        StringBuffer outBuffer = new StringBuffer(len*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        for(int x=0; x<len; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            char aChar = theString.charAt(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            switch(aChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                case '\\':outBuffer.append('\\'); outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                case '\t':outBuffer.append('\\'); outBuffer.append('t');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                case '\n':outBuffer.append('\\'); outBuffer.append('n');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                case '\r':outBuffer.append('\\'); outBuffer.append('r');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                case '\f':outBuffer.append('\\'); outBuffer.append('f');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    if ((aChar < 0x0020) || (aChar > 0x007e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                        outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        outBuffer.append('u');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        outBuffer.append(toHex((aChar >> 12) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                        outBuffer.append(toHex((aChar >>  8) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        outBuffer.append(toHex((aChar >>  4) & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                        outBuffer.append(toHex( aChar        & 0xF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        if (specialSaveChars.indexOf(aChar) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                            outBuffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        outBuffer.append(aChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        return outBuffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static String inferPackageName(String inputPath, String outputPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // Normalize file names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        inputPath  = new File(inputPath).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        outputPath = new File(outputPath).getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        // Split into components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        String sep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (File.separatorChar == '\\') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            sep = "\\\\";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            sep = File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        String[] inputs  = inputPath.split(sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        String[] outputs = outputPath.split(sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // Match common names, eliminating first "classes" entry from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // each if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int inStart  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int inEnd    = inputs.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int outEnd   = outputs.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int i = inEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int j = outEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        while (i >= 0 && j >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            if (!inputs[i].equals(outputs[j]) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    (inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                ++j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            --i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            --j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        String result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            result = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (inputs[i].equals("classes") && outputs[j].equals("classes")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                ++i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
25859
3317bb8137f4 8054834: Modular Source Code
chegar
parents: 23010
diff changeset
   372
            if (i > 0 && inputs[i-1].equals("modules")) {
3317bb8137f4 8054834: Modular Source Code
chegar
parents: 23010
diff changeset
   373
                ++i;
3317bb8137f4 8054834: Modular Source Code
chegar
parents: 23010
diff changeset
   374
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            inStart = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            for (i = inStart; i <= inEnd; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                buf.append(inputs[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                if (i < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    buf.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            result = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}