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