Format Tool

package org.mcraig.cs445.refentry;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Properties;
import org.xml.sax.InputSource;

/*
 * Utility for formatting RefEntry documents. 
 * @author <a href="mailto:mark@mcraig.org">Mark Craig</a>
 */
public class Format {
    /** Filename of XSLT Properties File. */
    private final static String XSLTProps = "src/conf/xslt.props";

    /** USAGE message. */
    private final static String usage =
    "Usage:\n" +
    "\tjava format [-infile <inputFile>][-outfile <outputFile>]\n" +
    "\tWhere <inputFile> is an XML RefEntry and <outputFile> is\n" +
    "\tan XHTML representation of the RefEntry.\n" +
    "\tIf <inputFile> is not provided, standard input is used.\n" +
    "\tIf <outputFile> is not provided, standard output is used.\n";
    /** Print a usage message to System.err and exit. */
    private static void usage() {
        System.err.println(usage);
        System.exit(1);
    }

    /** Filename of XML RefEntry source. */
    private static String infile;
    /** Filename of XHTML output. */
    private static String outfile;

    /**
     * Format a single RefEntry document.
     * @param args Expects arguments as shown:<p>
     * <tt>java format
     * [-infile <i>inputFile</i>][-outfile <i>outputFile</i>]</tt>
     * <p>If the <i>inputFile</i> is not provided,
     * input is taken from System.in.
     * <p>If the <i>outputFile</i> is not provided,
     * input is sent to System.out.
     */
    public static void main(String[] args) {
        handleInput(args);

        Properties xslt = new Properties();
        try {
            xslt.load(new FileInputStream(XSLTProps));

            File inf;
            if (infile.equals("")) {
                inf = File.createTempFile("format", "");
                BufferedReader br = 
                    new BufferedReader(new InputStreamReader(System.in));
                PrintStream ps =
                    new PrintStream(
                        new BufferedOutputStream(
                            new FileOutputStream(inf)));
                String str;
                while ((str = br.readLine()) != null) ps.println(str);
                br.close();
                ps.close();
            } else {
                inf = new File(infile);
            }
            
            InputSource input = new InputSource(new FileReader(inf));
            if (!RefEntryValidator.isValid(input)) {
                System.err.println("Input file is not valid.");
                if (infile.equals("")) inf.delete();
                System.exit(1);
            }

            File outf = File.createTempFile("format", "");
            RefEntry re = new RefEntry(inf, outf, xslt);
            RefEntryTransformer tr = 
                new RefEntryTransformer("CLI format");
            String outstr = new String(tr.transform(re, ""));

            if (outfile.equals("")) {
                System.out.print(outstr);
            } else {
                PrintWriter pw =
                    new PrintWriter(
                        new BufferedWriter(
                            new FileWriter(outfile)));
                pw.write(outstr);
                pw.close();
            }

            if (infile.equals("")) inf.delete();
            outf.delete();
        } catch (Exception e) {
            System.err.println(e.toString());
            System.exit(1);
        }
    }

    /** Handle command line input. */
    private static void handleInput(String[] args) {
        if (args.length == 0) {
            infile  = "";
            outfile = "";
        } else if (args.length == 2) {
            if (args[0].equals("-infile")) {
                infile  = args[1];
                outfile = "";
            } else if (args[0].equals("-outfile")) {
                infile  = "";
                outfile = args[1];
            } else {
                usage();
            }
        } else if (args.length == 4) {
            if (args[0].equals("-infile") && args[2].equals("-outfile")) {
                infile  = args[1];
                outfile = args[3];
            } else {
                usage();
            }
        } else {
            usage();
        }

        if (infile.length() != 0) {
            File test = new File(infile);
            if (!test.canRead()) {
                System.err.println("ERROR: Cannot read " + infile);
                usage();
            } 
            if (!test.isFile()) {
                System.err.println("ERROR: " + infile + " is not a file.");
                usage();
            }
        }

        if (outfile.length() != 0) {
            File test = new File(outfile);
            if (test.isDirectory() && test.canWrite()) {
                outfile += "OUTFILE.HTM";
            } else if (test.isFile()) {
                System.err.println("ERROR: " + outfile + " already exists.");
                usage();
            }
        }
    }
}