0N/A/*
797N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.BufferedWriter;
0N/Aimport java.io.File;
0N/Aimport java.io.FileInputStream;
0N/Aimport java.io.FileNotFoundException;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.OutputStreamWriter;
0N/Aimport java.io.Writer;
0N/Aimport java.text.MessageFormat;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.List;
0N/Aimport java.util.Properties;
0N/A
0N/A/** Translates a .properties file into a .java file containing the
0N/A * definition of a java.util.Properties subclass which can then be
0N/A * compiled with javac. <P>
0N/A *
0N/A * Usage: java CompileProperties [path to .properties file] [path to .java file to be output] [super class]
0N/A *
0N/A * Infers the package by looking at the common suffix of the two
0N/A * inputs, eliminating "classes" from it.
0N/A *
0N/A * @author Scott Violet
0N/A * @author Kenneth Russell
0N/A */
0N/A
0N/Apublic class CompileProperties {
0N/A
0N/A public static void main(String[] args) {
0N/A CompileProperties cp = new CompileProperties();
0N/A boolean ok = cp.run(args);
0N/A if ( !ok ) {
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A static interface Log {
0N/A void info(String msg);
0N/A void verbose(String msg);
0N/A void error(String msg, Exception e);
0N/A }
0N/A
0N/A private String propfiles[];
0N/A private String outfiles[] ;
0N/A private String supers[] ;
0N/A private int compileCount = 0;
0N/A private boolean quiet = false;
0N/A private Log log;
0N/A
0N/A public void setLog(Log log) {
0N/A this.log = log;
0N/A }
0N/A
0N/A public boolean run(String[] args) {
0N/A if (log == null) {
0N/A log = new Log() {
0N/A public void error(String msg, Exception e) {
0N/A System.err.println("ERROR: CompileProperties: " + msg);
0N/A if ( e != null ) {
0N/A System.err.println("EXCEPTION: " + e.toString());
0N/A e.printStackTrace();
0N/A }
0N/A }
0N/A public void info(String msg) {
0N/A System.out.println(msg);
0N/A }
0N/A public void verbose(String msg) {
0N/A if (!quiet)
0N/A System.out.println(msg);
0N/A }
0N/A };
0N/A }
0N/A
0N/A boolean ok = true;
0N/A /* Original usage */
0N/A if (args.length == 2 && args[0].charAt(0) != '-' ) {
464N/A ok = createFile(args[0], args[1], "java.util.ListResourceBundle");
0N/A } else if (args.length == 3) {
0N/A ok = createFile(args[0], args[1], args[2]);
0N/A } else if (args.length == 0) {
0N/A usage(log);
0N/A ok = false;
0N/A } else {
0N/A /* New batch usage */
0N/A ok = parseOptions(args);
0N/A if ( ok && compileCount == 0 ) {
0N/A log.error("options parsed but no files to compile", null);
0N/A ok = false;
0N/A }
0N/A /* Need at least one file. */
0N/A if ( !ok ) {
0N/A usage(log);
0N/A } else {
0N/A /* Process files */
0N/A for ( int i = 0; i < compileCount && ok ; i++ ) {
0N/A ok = createFile(propfiles[i], outfiles[i], supers[i]);
0N/A }
0N/A }
0N/A }
0N/A return ok;
0N/A }
0N/A
0N/A private boolean parseOptions(String args[]) {
0N/A boolean ok = true;
0N/A if ( compileCount > 0 ) {
0N/A String new_propfiles[] = new String[compileCount + args.length];
0N/A String new_outfiles[] = new String[compileCount + args.length];
0N/A String new_supers[] = new String[compileCount + args.length];
0N/A System.arraycopy(propfiles, 0, new_propfiles, 0, compileCount);
0N/A System.arraycopy(outfiles, 0, new_outfiles, 0, compileCount);
0N/A System.arraycopy(supers, 0, new_supers, 0, compileCount);
0N/A propfiles = new_propfiles;
0N/A outfiles = new_outfiles;
0N/A supers = new_supers;
0N/A } else {
0N/A propfiles = new String[args.length];
0N/A outfiles = new String[args.length];
0N/A supers = new String[args.length];
0N/A }
0N/A
0N/A for ( int i = 0; i < args.length ; i++ ) {
0N/A if ( "-compile".equals(args[i]) && i+3 < args.length ) {
0N/A propfiles[compileCount] = args[++i];
0N/A outfiles[compileCount] = args[++i];
0N/A supers[compileCount] = args[++i];
0N/A compileCount++;
0N/A } else if ( "-optionsfile".equals(args[i]) && i+1 < args.length ) {
0N/A String filename = args[++i];
0N/A FileInputStream finput = null;
0N/A byte contents[] = null;
0N/A try {
0N/A finput = new FileInputStream(filename);
0N/A int byteCount = finput.available();
0N/A if ( byteCount <= 0 ) {
0N/A log.error("The -optionsfile file is empty", null);
0N/A ok = false;
0N/A } else {
0N/A contents = new byte[byteCount];
0N/A int bytesRead = finput.read(contents);
0N/A if ( byteCount != bytesRead ) {
0N/A log.error("Cannot read all of -optionsfile file", null);
0N/A ok = false;
0N/A }
0N/A }
0N/A } catch ( IOException e ) {
0N/A log.error("cannot open " + filename, e);
0N/A ok = false;
0N/A }
0N/A if ( finput != null ) {
0N/A try {
0N/A finput.close();
0N/A } catch ( IOException e ) {
0N/A ok = false;
0N/A log.error("cannot close " + filename, e);
0N/A }
0N/A }
0N/A if ( ok = true && contents != null ) {
0N/A String tokens[] = (new String(contents)).split("\\s+");
0N/A if ( tokens.length > 0 ) {
0N/A ok = parseOptions(tokens);
0N/A }
0N/A }
0N/A if ( !ok ) {
0N/A break;
0N/A }
0N/A } else if ( "-quiet".equals(args[i]) ) {
0N/A quiet = true;
0N/A } else {
0N/A log.error("argument error", null);
0N/A ok = false;
0N/A }
0N/A }
0N/A return ok;
0N/A }
0N/A
0N/A private boolean createFile(String propertiesPath, String outputPath,
0N/A String superClass) {
0N/A boolean ok = true;
0N/A log.verbose("parsing: " + propertiesPath);
0N/A Properties p = new Properties();
0N/A try {
0N/A p.load(new FileInputStream(propertiesPath));
0N/A } catch ( FileNotFoundException e ) {
0N/A ok = false;
0N/A log.error("Cannot find file " + propertiesPath, e);
0N/A } catch ( IOException e ) {
0N/A ok = false;
0N/A log.error("IO error on file " + propertiesPath, e);
0N/A }
0N/A if ( ok ) {
0N/A String packageName = inferPackageName(propertiesPath, outputPath);
0N/A log.verbose("inferred package name: " + packageName);
0N/A List<String> sortedKeys = new ArrayList<String>();
0N/A for ( Object key : p.keySet() ) {
0N/A sortedKeys.add((String)key);
0N/A }
0N/A Collections.sort(sortedKeys);
0N/A Iterator keys = sortedKeys.iterator();
0N/A
0N/A StringBuffer data = new StringBuffer();
0N/A
0N/A while (keys.hasNext()) {
0N/A Object key = keys.next();
0N/A data.append(" { \"" + escape((String)key) + "\", \"" +
0N/A escape((String)p.get(key)) + "\" },\n");
0N/A }
0N/A
0N/A // Get class name from java filename, not the properties filename.
0N/A // (zh_TW properties might be used to create zh_HK files)
0N/A File file = new File(outputPath);
0N/A String name = file.getName();
0N/A int dotIndex = name.lastIndexOf('.');
0N/A String className;
0N/A if (dotIndex == -1) {
0N/A className = name;
0N/A } else {
0N/A className = name.substring(0, dotIndex);
0N/A }
0N/A
0N/A String packageString = "";
0N/A if (packageName != null && !packageName.equals("")) {
0N/A packageString = "package " + packageName + ";\n\n";
0N/A }
0N/A
0N/A Writer writer = null;
0N/A try {
0N/A writer = new BufferedWriter(
0N/A new OutputStreamWriter(new FileOutputStream(outputPath), "8859_1"));
0N/A MessageFormat format = new MessageFormat(FORMAT);
0N/A writer.write(format.format(new Object[] { packageString, className, superClass, data }));
0N/A } catch ( IOException e ) {
0N/A ok = false;
0N/A log.error("IO error writing to file " + outputPath, e);
0N/A }
0N/A if ( writer != null ) {
0N/A try {
0N/A writer.flush();
0N/A } catch ( IOException e ) {
0N/A ok = false;
0N/A log.error("IO error flush " + outputPath, e);
0N/A }
0N/A try {
0N/A writer.close();
0N/A } catch ( IOException e ) {
0N/A ok = false;
0N/A log.error("IO error close " + outputPath, e);
0N/A }
0N/A }
0N/A log.verbose("wrote: " + outputPath);
0N/A }
0N/A return ok;
0N/A }
0N/A
0N/A private static void usage(Log log) {
0N/A log.info("usage:");
0N/A log.info(" java CompileProperties path_to_properties_file path_to_java_output_file [super_class]");
0N/A log.info(" -OR-");
0N/A log.info(" java CompileProperties {-compile path_to_properties_file path_to_java_output_file super_class} -or- -optionsfile filename");
0N/A log.info("");
0N/A log.info("Example:");
464N/A log.info(" java CompileProperties -compile test.properties test.java java.util.ListResourceBundle");
0N/A log.info(" java CompileProperties -optionsfile option_file");
464N/A log.info("option_file contains: -compile test.properties test.java java.util.ListResourceBundle");
0N/A }
0N/A
0N/A private static String escape(String theString) {
0N/A // This is taken from Properties.saveConvert with changes for Java strings
0N/A int len = theString.length();
0N/A StringBuffer outBuffer = new StringBuffer(len*2);
0N/A
0N/A for(int x=0; x<len; x++) {
0N/A char aChar = theString.charAt(x);
0N/A switch(aChar) {
0N/A case '\\':outBuffer.append('\\'); outBuffer.append('\\');
0N/A break;
0N/A case '\t':outBuffer.append('\\'); outBuffer.append('t');
0N/A break;
0N/A case '\n':outBuffer.append('\\'); outBuffer.append('n');
0N/A break;
0N/A case '\r':outBuffer.append('\\'); outBuffer.append('r');
0N/A break;
0N/A case '\f':outBuffer.append('\\'); outBuffer.append('f');
0N/A break;
0N/A default:
0N/A if ((aChar < 0x0020) || (aChar > 0x007e)) {
0N/A outBuffer.append('\\');
0N/A outBuffer.append('u');
0N/A outBuffer.append(toHex((aChar >> 12) & 0xF));
0N/A outBuffer.append(toHex((aChar >> 8) & 0xF));
0N/A outBuffer.append(toHex((aChar >> 4) & 0xF));
0N/A outBuffer.append(toHex( aChar & 0xF));
0N/A } else {
0N/A if (specialSaveChars.indexOf(aChar) != -1) {
0N/A outBuffer.append('\\');
0N/A }
0N/A outBuffer.append(aChar);
0N/A }
0N/A }
0N/A }
0N/A return outBuffer.toString();
0N/A }
0N/A
0N/A private static String inferPackageName(String inputPath, String outputPath) {
0N/A // Normalize file names
0N/A inputPath = new File(inputPath).getPath();
0N/A outputPath = new File(outputPath).getPath();
0N/A // Split into components
0N/A String sep;
0N/A if (File.separatorChar == '\\') {
0N/A sep = "\\\\";
0N/A } else {
0N/A sep = File.separator;
0N/A }
0N/A String[] inputs = inputPath.split(sep);
0N/A String[] outputs = outputPath.split(sep);
0N/A // Match common names, eliminating first "classes" entry from
0N/A // each if present
0N/A int inStart = 0;
0N/A int inEnd = inputs.length - 2;
0N/A int outEnd = outputs.length - 2;
0N/A int i = inEnd;
0N/A int j = outEnd;
0N/A while (i >= 0 && j >= 0) {
0N/A if (!inputs[i].equals(outputs[j]) ||
0N/A (inputs[i].equals("gensrc") && inputs[j].equals("gensrc"))) {
0N/A ++i;
0N/A ++j;
0N/A break;
0N/A }
0N/A --i;
0N/A --j;
0N/A }
0N/A String result;
0N/A if (i < 0 || j < 0 || i >= inEnd || j >= outEnd) {
0N/A result = "";
0N/A } else {
0N/A if (inputs[i].equals("classes") && outputs[j].equals("classes")) {
0N/A ++i;
0N/A }
0N/A inStart = i;
0N/A StringBuffer buf = new StringBuffer();
0N/A for (i = inStart; i <= inEnd; i++) {
0N/A buf.append(inputs[i]);
0N/A if (i < inEnd) {
0N/A buf.append('.');
0N/A }
0N/A }
0N/A result = buf.toString();
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A private static final String FORMAT =
0N/A "{0}" +
0N/A "public final class {1} extends {2} '{'\n" +
0N/A " protected final Object[][] getContents() '{'\n" +
0N/A " return new Object[][] '{'\n" +
0N/A "{3}" +
0N/A " };\n" +
0N/A " }\n" +
0N/A "}\n";
0N/A
0N/A // This comes from Properties
0N/A private static char toHex(int nibble) {
0N/A return hexDigit[(nibble & 0xF)];
0N/A }
0N/A
0N/A // This comes from Properties
0N/A private static final char[] hexDigit = {
0N/A '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
0N/A };
0N/A
0N/A // Note: different from that in Properties
0N/A private static final String specialSaveChars = "\"";
0N/A}