// classes used import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Vector; // exceptions thrown import java.io.FileNotFoundException; import java.io.IOException; public class Transform { float[] args = null; public Transform( String filename ) { try { FileReader fRead = new FileReader( filename ); BufferedReader bRead = new BufferedReader( fRead ); Vector tempArray = new Vector( ); // temp storage for floats String tempString = null; // temp storage for line //////////////////////////////////////// // Read in each float until stream is // empty (tempString == null). while ( (tempString = bRead.readLine( )) != null ) tempArray.insertElementAt( tempString, tempArray.size( )); //////////////////////////////////////// // Create and initialize the float arg- // ument array. args = new float[ tempArray.size( ) ]; for ( int i = 0; i < tempArray.size( ); i++ ) args[i] = Float.valueOf( (String) tempArray.elementAt( i )). floatValue( ); //////////////////////////////////////// // Close the file. bRead.close( ); } catch (FileNotFoundException e) { System.err.println( "File " + filename + " not found." ); System.exit( 1 ); } catch (IOException e) { System.err.println( "Error in file stream." ); System.exit( 2 ); } catch (NumberFormatException e) { System.err.println( "Error in file format (non-float found)." ); System.exit( 3 ); } //////////////////////////////////////// // Wait for commands from the command // line. waitForCommands(); } public void waitForCommands() { boolean needHelp = false; // yes/no help command requested try { InputStreamReader iRead = new InputStreamReader( System.in ); BufferedReader rRead = new BufferedReader( iRead ); StreamTokenizer sRead = new StreamTokenizer( rRead ); DONE: while ( true ) { if (needHelp == false) System.out.print( "Command: " ); switch ( sRead.nextToken() ) { case StreamTokenizer.TT_WORD : //////////////////////////////////////// // First check to see if any of the com- // mands are driver dependent // // "display" display current arg // array // // "exit" exit the program // // "help " provide help for the // following command // // "save " save args to file if ( "display".equals( sRead.sval.trim( ) )) { System.out.print( "[ " ); for ( int i = 0; i < args.length; i++ ) System.out.print( "" + args[i] + " " ); System.out.println( " ]"); continue; } else if ( "exit".equals( sRead.sval.trim( ) )) { System.out.println( "Quitting . . . " ); System.exit( 0 ); } else if ( "help".equals( sRead.sval.trim( ) )) { needHelp = true; continue; } else if ( "save".equals( sRead.sval.trim( ) )) { switch( sRead.nextToken( ) ) { case StreamTokenizer.TT_WORD : save( sRead.sval ); break; default : System.out.println( "Usage: save " ); break; } continue; } //////////////////////////////////////// // Else assume it is a transformation // command so create the transformation // and perform it's operation. try { Class c = Class.forName( sRead.sval ); Transformation t = (Transformation) c.newInstance(); if (needHelp) { t.help( ); needHelp = false; } else { t.readArgs( sRead ); t.transform( args ); } } catch (ClassNotFoundException e) { //////////////////////////////////////// // Note that this exception does not force // program termination since it could be // caused by poor typing of commands. System.err.println( "No such transformation or " + "command."); } catch (InstantiationException e) { System.err.println( "Could not create class " + sRead.sval ); System.exit( 4 ); } catch (IllegalAccessException e) { System.err.println( "No default constructor found for " + sRead.sval ); System.exit( 5 ); } case StreamTokenizer.TT_EOL : break; case StreamTokenizer.TT_EOF : break DONE; default : System.err.println( "Unrecognized input" ); break; } } } catch (IOException e) { System.err.println( "Error in input stream. "); System.exit( 6 ); } } private void save( String filename ) { try { FileWriter fWrite = new FileWriter( filename ); BufferedWriter bWrite = new BufferedWriter( fWrite ); PrintWriter pWrite = new PrintWriter( bWrite ); for (int i = 0; i < args.length; i++ ) pWrite.println( args[i] ); pWrite.close( ); } catch (IOException e) { System.err.println( "Error in file stream" ); System.exit( 7 ); } } public static void main( String[] args ) { new Transform( args[0] ); } }