CommandList.java

package edu.hawaii.ics.yucheng;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * A class that contains DDL commands. It is a derived class of
 * Java.util.ArrayList. The initial items in the list are parsed from the
 * 'ddlfile' statements file.
 * 
 * @author     Cheng Jade
 * @assignment ICS 421 Assignment 1
 * @date       Feb 10, 2010
 * @bugs       None
 */
public class CommandList extends ArrayList<String> {

    /** The serialized version id. */
    private static final long serialVersionUID = 1L;

    public CommandList() {
    }
    /**
     * Initializes a new instance of the CommandList class.
     * 
     * @param path
     *            The path to a file that contains DDL commands.
     * 
     * @throws NullPointerException
     * @throws ProgramException
     *             Thrown if the DDL command file is invalid.
     */
    public CommandList(final String path) throws ProgramException {

        // Throw an exception if the specified path is null.
        if (null == path)
            throw new NullPointerException("path");

        // Open the file, initialize the scanner, and handle errors.
        final File file = new File(path);
        final Scanner scanner;
        try {
            scanner = new Scanner(file);
        } catch (final FileNotFoundException e) {
            throw new ProgramException("Cannot read DLL command file.", e);
        }

        // Read the file line after line; extract and add the DDL commands.
        // This will separate commands by checking for semicolons, and it will
        // allow comments that start with "--".
        try {
            String buffer = "";
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                final int comment = line.indexOf("--");
                if (comment >= 0)
                    line = line.substring(0, comment);
                buffer = buffer.trim() + " " + line.trim();
                int semicolon;
                while (-1 != (semicolon = buffer.indexOf(";"))) {
                    final String command = buffer.substring(0, semicolon);
                    this.addCommand(command);
                    buffer = buffer.substring(semicolon + 1);
                }
            }
            this.addCommand(buffer);

        } finally {
            // Always close the scanner.
            scanner.close();
        }
    }

    /**
     * Returns a readable version of the contents of the DDL command list.
     * 
     * @return A readable version of the contents of the DDL command list.
     */
    @Override
    public String toString() {
        // If the directory is empty, return a special note.
        if (this.size() == 0)
            return "The DDL command list is empty.\n";

        // Loop over each item, and add it to the string builder.
        final StringBuilder builder = new StringBuilder();
        builder.append("The DDL file contains:\n\n");
        final int size = this.size();
        for (int i = 0; i < size; i++) {
            builder.append("Statement ");
            builder.append(i + 1);
            builder.append(": \"");
            builder.append(this.get(i));
            builder.append("\"\n");
        }

        // Return the string created above.
        return builder.toString();
    }

    /**
     * Adds a DDL command to the CommandList. If the command has only whitespace
     * characters, it is ignored.
     * 
     * @param command
     *            The DDL command to add.
     */
    private void addCommand(String command) {
        assert null != command;
        command = command.trim();
        if (command.length() > 0)
            this.add(command);
    }

    /**
     * The entry point for a test for this class.
     * 
     * @param args
     *            The command line arguments.
     */
    public static void main(final String[] args) {
        assert null != args;

        // Print usage information, if wrong number of arguments were used.
        if (args.length != 1) {
            final String name = CommandList.class.getSimpleName();
            System.err.println("Usage: java " + name + " <path>");
            System.err.println("       <path> path to a DDL file");
            System.exit(1);
            return;
        }

        // Declare a CommandList object, and populate it with data from a file
        // that is specified as the only command line argument.
        try {
            assert null != args[0];
            System.out.println(new CommandList(args[0]));

        } catch (final ProgramException e) {
            System.err.println(e.getMessage());
            System.exit(1);
            return;
        }

        // Exit cleanly while debugging from Eclipse.
        System.exit(0);
    }
}
Valid HTML 4.01 Valid CSS