// CSharpToJava.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2005-03-02 CSharpToJava.cs file created. // 2006-02-23 CSharpToJava.java file created. // 2006-02-23 Last modification. // This program helps to convert C# programs to Java 1.5 programs. // This is not a fully automatic converter. After a conversion, the // conversion result usually needs to be modified by hand. // This program is specifically tailored to convert the programs of // Kari Laitinen's books. // Note that this program is not heavily tested. I used the program // CSharpToJava.cs to convert C# programs to Java programs. Then // I created this by modifying the CSharpToJava.cs program. // According to tests with a few C# files, this program seems to // work. DO NOT USE THIS PROGRAM TO CONVERT CSharpToJava.cs TO A // JAVA PROGRAM. THE CONVERSION OPERATION DESTROYS ALL THE // STRINGS THAT NEED TO BE REPLACED. // The best way to execute this program is to run it in a // command prompt window where command line parameters can // be given. It may be difficult, and even impossible, to // supply the command line parameters if this program is // executed with a modern development tool such as JCreator // or Eclipse. import java.util.* ; import java.io.* ; // Classes for file handling. class CSharpToJava { static void store_text_lines_to_file( ArrayList given_array_of_text_lines, String given_file_name ) { try { PrintWriter output_file = new PrintWriter( new FileWriter( given_file_name ) ) ; for ( int line_index = 0 ; line_index < given_array_of_text_lines.size() ; line_index ++ ) { output_file.println( given_array_of_text_lines.get( line_index ) ) ; } output_file.close() ; } catch ( IOException caught_io_exception ) { System.out.print( "\n\n Cannot write to file \"" + given_file_name + "\"\n" ) ; } } static void replace_names_in_file( String original_file_name ) { ArrayList names_to_replace = new ArrayList() ; ArrayList replacement_names = new ArrayList() ; names_to_replace.add( "Console.Write" ) ; replacement_names.add( "System.out.print" ) ; names_to_replace.add( "using System.IO" ) ; replacement_names.add( "import java.io.*" ) ; names_to_replace.add( "using System" ) ; replacement_names.add( "import java.util.*" ) ; names_to_replace.add( "static void Main()" ) ; replacement_names.add( "public static void main( String[] not_in_use )" ) ; names_to_replace.add( "static void Main( string[] command_line_parameters )" ) ; replacement_names.add( "public static void main( String[] command_line_parameters )" ) ; names_to_replace.add( "Convert.ToInt32( Console.ReadLine()" ) ; replacement_names.add( "keyboard.nextInt(" ) ; names_to_replace.add( "Convert.ToDouble( Console.ReadLine()" ) ; replacement_names.add( "keyboard.nextDouble(" ) ; names_to_replace.add( "Convert.ToChar( Console.ReadLine()" ) ; replacement_names.add( "keyboard.nextLine().charAt( 0" ) ; names_to_replace.add( "Console.ReadLine()" ) ; replacement_names.add( "keyboard.nextLine()" ) ; names_to_replace.add( " string " ) ; replacement_names.add( " String " ) ; names_to_replace.add( " string[" ) ; replacement_names.add( " String[" ) ; names_to_replace.add( " virtual " ) ; replacement_names.add( " " ) ; names_to_replace.add( " override " ) ; replacement_names.add( " " ) ; names_to_replace.add( " bool " ) ; replacement_names.add( " boolean " ) ; names_to_replace.add( ".cs" ) ; replacement_names.add( ".java" ) ; names_to_replace.add( "string.Length" ) ; replacement_names.add( "string.length()" ) ; names_to_replace.add( ".Length" ) ; replacement_names.add( ".length" ) ; names_to_replace.add( "string[ character_index ]" ) ; replacement_names.add( "string.charAt( character_index )" ) ; names_to_replace.add( "IndexOf(" ) ; replacement_names.add( "indexOf(" ) ; names_to_replace.add( "ToUpper(" ) ; replacement_names.add( "toUpperCase(" ) ; names_to_replace.add( "ToCharArray(" ) ; replacement_names.add( "toCharArray(" ) ; names_to_replace.add( "Close(" ) ; replacement_names.add( "close(" ) ; names_to_replace.add( "ToLower(" ) ; replacement_names.add( "toLowerCase(" ) ; names_to_replace.add( "Convert.ToInt32(" ) ; replacement_names.add( "Integer.parseInt(" ) ; names_to_replace.add( "Equals(" ) ; replacement_names.add( "equals(" ) ; names_to_replace.add( "Add(" ) ; replacement_names.add( "add(" ) ; names_to_replace.add( "ToString(" ) ; replacement_names.add( "toString(" ) ; try { BufferedReader original_file = new BufferedReader( new FileReader( original_file_name ) ) ; ArrayList original_text_lines = new ArrayList() ; ArrayList modified_text_lines = new ArrayList() ; Calendar current_date = new GregorianCalendar() ; // The following statements add some Kari-Laitinen-style // comments at the beginning of the modified program file. modified_text_lines.add( "" ) ; modified_text_lines.add( String.format( "// %s Copyright (c) %d Kari Laitinen", original_file_name, current_date.get( Calendar.YEAR ) ) ) ; modified_text_lines.add( "" ) ; modified_text_lines.add( "// http://www.naturalprogramming.com" ) ; modified_text_lines.add( "" ) ; modified_text_lines.add( String.format( "// %tF File created.", current_date ) ) ; modified_text_lines.add( String.format( "// %tF Last modification.", current_date ) ) ; boolean opening_brace_of_main_method_found = false ; boolean start_of_main_method_found = false ; int line_counter = 0 ; boolean end_of_file_encountered = false ; while ( end_of_file_encountered == false ) { String text_line_from_file = original_file.readLine() ; if ( text_line_from_file == null ) { end_of_file_encountered = true ; } else { line_counter ++ ; original_text_lines.add( text_line_from_file ) ; for ( int name_index = 0 ; name_index < names_to_replace.size() ; name_index ++ ) { if ( text_line_from_file.indexOf( names_to_replace.get( name_index ) ) != -1 ) { text_line_from_file = text_line_from_file.replace( names_to_replace.get( name_index ), replacement_names.get( name_index ) ) ; System.out.print( "\n \"" + names_to_replace.get( name_index ) + "\" was replaced with \"" + replacement_names.get( name_index ) + "\" on line " + line_counter ) ; } } // The following three if constructs are used to find // out when to add the line "Scanner keyboard ... " // to the beginning of the main() method if ( opening_brace_of_main_method_found == true ) { modified_text_lines.add( " Scanner keyboard = new Scanner( System.in ) ;" ) ; modified_text_lines.add( "" ) ; opening_brace_of_main_method_found = false ; start_of_main_method_found = false ; } if ( start_of_main_method_found == true ) { if ( text_line_from_file.indexOf( "{" ) != -1 ) { opening_brace_of_main_method_found = true ; } } if ( text_line_from_file.indexOf( "void main(" ) != -1 ) { start_of_main_method_found = true ; } modified_text_lines.add( text_line_from_file ) ; } } original_file.close() ; String backup_file_name = original_file_name + ".bak" ; store_text_lines_to_file( original_text_lines, backup_file_name ) ; store_text_lines_to_file( modified_text_lines, original_file_name ) ; } catch ( FileNotFoundException caught_file_not_found_exception ) { System.out.print( "\n Cannot open file " + original_file_name ) ; } catch ( IOException caught_io_exception ) { System.out.print( "\n Error in reading " + original_file_name ) ; } } public static void main( String[] command_line_parameters ) { Scanner keyboard = new Scanner( System.in ) ; String file_name_given_by_user ; if ( command_line_parameters.length == 1 ) { file_name_given_by_user = command_line_parameters[ 0 ] ; } else { System.out.print( "\n This program helps you to convert a C# " + "\n program to a Java program. Please," + "\n type the name of the program file: " ) ; file_name_given_by_user = keyboard.nextLine() ; } replace_names_in_file( file_name_given_by_user ) ; } }