[an error occurred while processing this directive]

CircularListTester.java


package ioutil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *  This class provides a main() method for testing the CircularList class.
 */
public class CircularListTester
extends Tester {

    /** addCommands() adds testing commands to the command table.
     */
    public void addCommands() {
	super.addCommands();

	commandTable.add(new Command() {
	    {
		name = new String("lists");
		arguments = new String("");
		brief = full = new String("displays all list names");
	    }
	    public void execute(LineScanner ls) {
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    Enumeration enum = lists.keys();
		    while (enum.hasMoreElements()) {
			System.out.println("    " +
				(String)enum.nextElement());
		    }
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("create");
		arguments = new String("name");
		brief = new String("creates a named list");
		full = new String("creates a list named name");
	    }
	    public void execute(LineScanner ls) {
		String name;
		if (!ls.atString()) {
		    name = ls.readString();
		    ls.readEnd();
		    return;
		}
		name = ls.currentString();
		if (lists.containsKey(name)) {
		    ls.addError("Duplicate list name");
		}
		ls.advance();
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    lists.put(name, new CircularList());
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("isempty");
		arguments = new String("l");
		brief = new String("reports if a list is empty");
		full = new String("reports if l is empty");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    if (cl.isEmpty()) {
			System.out.println("The list is empty.");
		    } else {
			System.out.println("The list is not empty.");
		    }
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("getfirst");
		arguments = new String("l");
		brief = new String("displays the first string of a list");
		full = new String("displays the first string of l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    try {
			System.out.println(cl.getFirst());
		    } catch(IllegalArgumentException e) {
			e.printStackTrace();
		    }
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("getlast");
		arguments = new String("l");
		brief = new String("displays the last string of a list");
		full = new String("displays the last string of l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    try {
			System.out.println(cl.getLast());
		    } catch(IllegalArgumentException e) {
			e.printStackTrace();
		    }
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("addfirst");
		arguments = new String("l str");
		brief = new String("adds a string to the front of a list");
		full = new String("adds str to the front of l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		String str = ls.readString();
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    cl.addFirst(str);
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("addlast");
		arguments = new String("l str");
		brief = new String("adds a string to the rear of a list");
		full = new String("adds str to the rear of l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		String str = ls.readString();
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    cl.addLast(str);
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("removefirst");
		arguments = new String("l");
		brief = new String("removes the first string from a list");
		full = new String("removes the first string from l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    try {
			cl.removeFirst();
		    } catch(IllegalArgumentException e) {
			e.printStackTrace();
		    }
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("rotate");
		arguments = new String("l");
		brief = new String("rotates a list");
		full = new String("rotates the first string in l to the end");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    cl.rotate();
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("append");
		arguments = new String("l l1");
		brief = new String("appends one list to another");
		full = new String("appends l1 to l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		CircularList cl1 = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    cl.append(cl1);
		}
	    }
	});

	commandTable.add(new Command() {
	    {
		name = new String("tostring");
		arguments = new String("l");
		brief = new String("displays the strings in a list");
		full = new String("displays the strings in l");
	    }
	    public void execute(LineScanner ls) {
		CircularList cl = readList(ls);
		ls.readEnd();
		if (ls.errorCount() == 0) {
		    System.out.println(cl.toString());
		}
	    }
	});

    }  // public void addCommands()

    /** lists is a table of lists for this CircularListTester.
     */
    protected Hashtable lists = new Hashtable();

    /** readList(ls) reads the next argument of ls and returns the list
     *  that it names, provided the name is registered in lists.
     *  Otherwise, an error message is added to ls and null is returned.
     */
    protected CircularList readList(LineScanner ls) {
	if (!ls.atString()) {
	    ls.addError("List name expected");
	    return null;
	}
	String listName = ls.currentString();
	CircularList cl = (CircularList)lists.get(listName);
	if (cl == null) {
	    ls.addError("List name expected");
	    ls.advance();
	    return null;
	}
	ls.advance();
	return cl;
    }  //protected CircularList readList(LineScanner)

    /** If this program is executed with no command line argument then it
     *  executes commands entered from the terminal.  If this program is
     *  executed with a single command-line argument then that argument
     *  is interpreted as a file name.  The program attempts to open the
     *  named file and execute the commands in it.  If the open fails, or
     *  if there is more than one command-line argument, then the program
     *  terminates with an error message.
     */
    public static void main(String[] args)
    throws FileNotFoundException, IOException {
	Tester tester = new CircularListTester();
	tester.openStream(args);
	tester.addCommands();
	tester.doCommands();
    }  // public static main(String[])

}  // public class CircularListTester
[an error occurred while processing this directive]