// joi/7/juno/WcCommand.java
// Ethan Bolker, December 2001
//
// Modified: 

import java.util.*;

/**
 * 
 *
 *
 * 
 */

public class WcCommand extends ShellCommand 
{
    public WcCommand() 
    {
	super( "                                       ", 
	       "                                       " );
    }

    /**
     *
     *
     *
     *
     *
     * @param args: the reminder of the command line.
     * @param sh: the current shell
     *
     * @exception JunoException for reporting errors
     */

    public void doIt( StringTokenizer args, Shell sh )
	 throws JunoException  
    {
	String filename = "";
	try {
	    filename = args.nextToken();
	    TextFile file = 
		(TextFile) (sh.getDot().retrieveJFile( filename ));
	    String contents = file.getContents();
	    int words = 
		new StringTokenizer(contents).countTokens();
	    int lines = 
		new StringTokenizer(contents, "\n").countTokens();
	    sh.getConsole().println( contents.length() + "\t" 
				     + words + "\t" + lines );
	}
	catch ( NoSuchElementException e ) {
	    throw new BadShellCommandException( this );
	}
	catch ( NullPointerException e ) {
	    throw new JunoException("JFile " + filename + 
				    " not found"); 
	}
	catch ( ClassCastException e ) {
	    throw new JunoException("JFile " + filename + 
				    " not a TextFile"); 
	}
    }
}

