001         package com.croftsoft.apps.src;
002    
003         import java.io.DataInputStream;
004         import java.io.File;
005         import java.io.FileInputStream;
006         import java.io.FileOutputStream;
007         import java.io.PrintStream;
008         import java.util.StringTokenizer;
009    
010         /*********************************************************************
011         * <P>
012         * This java application searches through all of the *.java files
013         * in the current directory and replaces the old package name with
014         * a new package name in the code.
015         * <P>
016         * java PackageNameChange package.name.old package.name.new
017         * <P>
018         * @version
019         *   1.0 1996-10-26
020         * @author
021         *   <A HREF="http://www.alumni.caltech.edu/~croft">David W. Croft</A>
022         *********************************************************************/
023    
024         public class  PackageNameChange {
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027    
028         public static void  main ( String [ ]  args ) {
029         //////////////////////////////////////////////////////////////////////
030           if ( args.length != 2 ) {
031             System.out.println ( "java PackageNameChange old new" );
032             return;
033           }
034           String  path = ".";
035           File  dir = new File ( path );
036           String [ ]  file_list = dir.list ( );
037           for ( int  i = 0; i < file_list.length; i++ ) {
038             File  file = new File ( dir, file_list [ i ] );
039             if ( !file.isFile ( ) ) continue;
040             StringTokenizer  tokens
041               = new StringTokenizer ( file_list [ i ], "." );
042             if ( tokens.countTokens ( ) != 2 ) continue;
043             String  filename = tokens.nextToken ( );
044             String  extension = tokens.nextToken ( );
045             if ( !extension.equals ( "java" ) ) continue;
046             System.out.println ( file_list [ i ] );
047             FileInputStream  fileInputStream;
048             DataInputStream  dataInputStream;
049             String  line;
050             try {
051               fileInputStream = new FileInputStream ( file );
052               dataInputStream = new DataInputStream ( fileInputStream );
053               line = dataInputStream.readLine ( );
054             } catch ( Exception  e ) { continue; }
055             tokens = new StringTokenizer ( line );
056             if ( tokens.countTokens ( ) != 2 ) continue;
057             String  tokenStr = tokens.nextToken ( );
058             if ( !tokenStr.equals ( "package" ) ) continue;
059             int  index = line.lastIndexOf ( args [ 0 ] );
060             System.out.println ( "" + index );
061             if ( index < 0 ) continue;
062             String new_line = line.substring ( 0, index )
063               + args [ 1 ] + line.substring ( index + args [ 0 ].length ( ) );
064             FileOutputStream  fileOutputStream;
065             PrintStream  printStream;
066             try {
067               fileOutputStream
068                 = new FileOutputStream ( file_list [ i ] + ".new" );
069               printStream = new PrintStream ( fileOutputStream );
070             } catch ( Exception e ) { continue; }
071             line = new_line;
072             try {
073               do {
074                 printStream.println ( line );
075                 line = dataInputStream.readLine ( );
076               } while ( line != null );
077             }
078             catch ( Exception e ) { }
079             printStream.close ( );
080           }
081         }
082    
083         //////////////////////////////////////////////////////////////////////
084         //////////////////////////////////////////////////////////////////////
085         }