duke@1: /* jjh@1305: * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: ohrstrom@1224: package compileproperties; ohrstrom@1224: duke@1: import java.io.BufferedWriter; duke@1: import java.io.File; duke@1: import java.io.FileInputStream; duke@1: import java.io.FileNotFoundException; duke@1: import java.io.FileOutputStream; duke@1: import java.io.IOException; duke@1: import java.io.OutputStreamWriter; duke@1: import java.io.Writer; duke@1: import java.text.MessageFormat; duke@1: import java.util.ArrayList; duke@1: import java.util.Collections; duke@1: import java.util.Iterator; duke@1: import java.util.List; duke@1: import java.util.Properties; duke@1: duke@1: /** Translates a .properties file into a .java file containing the duke@1: * definition of a java.util.Properties subclass which can then be duke@1: * compiled with javac.
duke@1: *
duke@1: * Usage: java CompileProperties [path to .properties file] [path to .java file to be output] [super class]
duke@1: *
duke@1: * Infers the package by looking at the common suffix of the two
duke@1: * inputs, eliminating "classes" from it.
duke@1: *
duke@1: * @author Scott Violet
duke@1: * @author Kenneth Russell
duke@1: */
duke@1:
duke@1: public class CompileProperties {
duke@1:
duke@1: public static void main(String[] args) {
duke@1: CompileProperties cp = new CompileProperties();
duke@1: boolean ok = cp.run(args);
duke@1: if ( !ok ) {
duke@1: System.exit(1);
duke@1: }
duke@1: }
duke@1:
ohrstrom@1224: public static interface Log {
duke@1: void info(String msg);
duke@1: void verbose(String msg);
duke@1: void error(String msg, Exception e);
duke@1: }
duke@1:
duke@1: private String propfiles[];
duke@1: private String outfiles[] ;
duke@1: private String supers[] ;
duke@1: private int compileCount = 0;
duke@1: private boolean quiet = false;
ohrstrom@1224: public Log log;
duke@1:
duke@1: public void setLog(Log log) {
duke@1: this.log = log;
duke@1: }
duke@1:
duke@1: public boolean run(String[] args) {
duke@1: if (log == null) {
duke@1: log = new Log() {
duke@1: public void error(String msg, Exception e) {
duke@1: System.err.println("ERROR: CompileProperties: " + msg);
duke@1: if ( e != null ) {
duke@1: System.err.println("EXCEPTION: " + e.toString());
duke@1: e.printStackTrace();
duke@1: }
duke@1: }
duke@1: public void info(String msg) {
duke@1: System.out.println(msg);
duke@1: }
duke@1: public void verbose(String msg) {
duke@1: if (!quiet)
duke@1: System.out.println(msg);
duke@1: }
duke@1: };
duke@1: }
duke@1:
duke@1: boolean ok = true;
duke@1: /* Original usage */
duke@1: if (args.length == 2 && args[0].charAt(0) != '-' ) {
jjg@465: ok = createFile(args[0], args[1], "java.util.ListResourceBundle");
duke@1: } else if (args.length == 3) {
duke@1: ok = createFile(args[0], args[1], args[2]);
duke@1: } else if (args.length == 0) {
duke@1: usage(log);
duke@1: ok = false;
duke@1: } else {
duke@1: /* New batch usage */
duke@1: ok = parseOptions(args);
duke@1: if ( ok && compileCount == 0 ) {
duke@1: log.error("options parsed but no files to compile", null);
duke@1: ok = false;
duke@1: }
duke@1: /* Need at least one file. */
duke@1: if ( !ok ) {
duke@1: usage(log);
duke@1: } else {
duke@1: /* Process files */
duke@1: for ( int i = 0; i < compileCount && ok ; i++ ) {
duke@1: ok = createFile(propfiles[i], outfiles[i], supers[i]);
duke@1: }
duke@1: }
duke@1: }
duke@1: return ok;
duke@1: }
duke@1:
duke@1: private boolean parseOptions(String args[]) {
duke@1: boolean ok = true;
duke@1: if ( compileCount > 0 ) {
duke@1: String new_propfiles[] = new String[compileCount + args.length];
duke@1: String new_outfiles[] = new String[compileCount + args.length];
duke@1: String new_supers[] = new String[compileCount + args.length];
duke@1: System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount);
duke@1: System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount);
duke@1: System.arraycopy(supers, 0, new_supers, 0, compileCount);
duke@1: propfiles = new_propfiles;
duke@1: outfiles = new_outfiles;
duke@1: supers = new_supers;
duke@1: } else {
duke@1: propfiles = new String[args.length];
duke@1: outfiles = new String[args.length];
duke@1: supers = new String[args.length];
duke@1: }
duke@1:
duke@1: for ( int i = 0; i < args.length ; i++ ) {
duke@1: if ( "-compile".equals(args[i]) && i+3 < args.length ) {
duke@1: propfiles[compileCount] = args[++i];
duke@1: outfiles[compileCount] = args[++i];
duke@1: supers[compileCount] = args[++i];
duke@1: compileCount++;
duke@1: } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) {
duke@1: String filename = args[++i];
duke@1: FileInputStream finput = null;
duke@1: byte contents[] = null;
duke@1: try {
duke@1: finput = new FileInputStream(filename);
duke@1: int byteCount = finput.available();
duke@1: if ( byteCount <= 0 ) {
duke@1: log.error("The -optionsfile file is empty", null);
duke@1: ok = false;
duke@1: } else {
duke@1: contents = new byte[byteCount];
duke@1: int bytesRead = finput.read(contents);
duke@1: if ( byteCount != bytesRead ) {
duke@1: log.error("Cannot read all of -optionsfile file", null);
duke@1: ok = false;
duke@1: }
duke@1: }
duke@1: } catch ( IOException e ) {
duke@1: log.error("cannot open " + filename, e);
duke@1: ok = false;
duke@1: }
duke@1: if ( finput != null ) {
duke@1: try {
duke@1: finput.close();
duke@1: } catch ( IOException e ) {
duke@1: ok = false;
duke@1: log.error("cannot close " + filename, e);
duke@1: }
duke@1: }
duke@1: if ( ok = true && contents != null ) {
duke@1: String tokens[] = (new String(contents)).split("\\s+");
duke@1: if ( tokens.length > 0 ) {
duke@1: ok = parseOptions(tokens);
duke@1: }
duke@1: }
duke@1: if ( !ok ) {
duke@1: break;
duke@1: }
duke@1: } else if ( "-quiet".equals(args[i]) ) {
duke@1: quiet = true;
duke@1: } else {
duke@1: log.error("argument error", null);
duke@1: ok = false;
duke@1: }
duke@1: }
duke@1: return ok;
duke@1: }
duke@1:
duke@1: private boolean createFile(String propertiesPath, String outputPath,
duke@1: String superClass) {
duke@1: boolean ok = true;
duke@1: log.verbose("parsing: " + propertiesPath);
duke@1: Properties p = new Properties();
duke@1: try {
duke@1: p.load(new FileInputStream(propertiesPath));
duke@1: } catch ( FileNotFoundException e ) {
duke@1: ok = false;
duke@1: log.error("Cannot find file " + propertiesPath, e);
duke@1: } catch ( IOException e ) {
duke@1: ok = false;
duke@1: log.error("IO error on file " + propertiesPath, e);
duke@1: }
duke@1: if ( ok ) {
duke@1: String packageName = inferPackageName(propertiesPath, outputPath);
duke@1: log.verbose("inferred package name: " + packageName);
duke@1: List