1 // joi/7/juno/WcCommand.java 2 // Ethan Bolker, December 2001 3 // 4 // Modified: 5 6 import java.util.*; 7 8 /** 9 * 10 * 11 * 12 * 13 */ 14 15 public class WcCommand extends ShellCommand 16 { 17 public WcCommand() 18 { 19 super( " ", 20 " " ); 21 } 22 23 /** 24 * 25 * 26 * 27 * 28 * 29 * @param args: the reminder of the command line. 30 * @param sh: the current shell 31 * 32 * @exception JunoException for reporting errors 33 */ 34 35 public void doIt( StringTokenizer args, Shell sh ) 36 throws JunoException 37 { 38 String filename = ""; 39 try { 40 filename = args.nextToken(); 41 TextFile file = 42 (TextFile) (sh.getDot().retrieveJFile( filename )); 43 String contents = file.getContents(); 44 int words = 45 new StringTokenizer(contents).countTokens(); 46 int lines = 47 new StringTokenizer(contents, "\n").countTokens(); 48 sh.getConsole().println( contents.length() + "\t" 49 + words + "\t" + lines ); 50 } 51 catch ( NoSuchElementException e ) { 52 throw new BadShellCommandException( this ); 53 } 54 catch ( NullPointerException e ) { 55 throw new JunoException("JFile " + filename + 56 " not found"); 57 } 58 catch ( ClassCastException e ) { 59 throw new JunoException("JFile " + filename + 60 " not a TextFile"); 61 } 62 } 63 }