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 SQL commands. It is a derived class of * Java.util.ArrayList. The initial items in the list are parsed from the * 'SQLfile' statements file. * * @author Cheng Jade * @assignment ICS 421 Project * @date Feb 10, 2010 * @bugs None */ public class SQLList extends ArrayList<String> { /** The serialized version id. */ private static final long serialVersionUID = 1L; public SQLList() { } /** * Initializes a new instance of the CommandList class. * * @param path * The path to a file that contains SQL commands. * * @throws NullPointerException * @throws ProgramException * Thrown if the SQL command file is invalid. */ public SQLList(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 SQL file.", e); } // Read the file line after line; extract and add the SQL 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 SQL command list. * * @return A readable version of the contents of the SQL command list. */ @Override public String toString() { // If the directory is empty, return a special note. if (this.size() == 0) return "The SQL command list is empty.\n"; // Loop over each item, and add it to the string builder. final StringBuilder builder = new StringBuilder(); builder.append("The SQL 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 SQL command to the CommandList. If the command has only whitespace * characters, it is ignored. * * @param command * The SQL 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 = SQLList.class.getSimpleName(); System.err.println("Usage: java " + name + " <path>"); System.err.println(" <path> path to a SQL 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 SQLList(args[0])); } catch (final ProgramException e) { System.err.println(e.getMessage()); System.exit(1); return; } // Exit cleanly while debugging from Eclipse. System.exit(0); } }