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