// JavaToPython.java Copyright (c) 2006 Kari Laitinen // http://www.naturalprogramming.com // 2006-04-28 File created. // 2006-05-28 Last modification. // This program helps to convert Java programs to Python 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. // DO NOT USE THIS PROGRAM TO CONVERT THIS PROGRAM TO A // PYTHON 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 JavaToPython { 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( "System.out.print(" ) ; replacement_names.add( "print" ) ; names_to_replace.add( "System.out.printf(" ) ; replacement_names.add( "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( "public static void main( String[] not_in_use )"); replacement_names.add( "" ) ; names_to_replace.add( "public static void main( String[] command_line_parameters )" ) ; replacement_names.add( "" ) ; names_to_replace.add( "keyboard.nextInt(" ) ; replacement_names.add( "int( raw_input() " ) ; names_to_replace.add( "keyboard.nextDouble(" ) ; replacement_names.add( "float( raw_input() " ) ; names_to_replace.add( "keyboard.nextLine().charAt( 0" ) ; replacement_names.add( "raw_input(" ) ; names_to_replace.add( "keyboard.nextLine()" ) ; replacement_names.add( "raw_input()" ) ; names_to_replace.add( " String " ) ; replacement_names.add( " " ) ; names_to_replace.add( " public " ) ; replacement_names.add( " " ) ; names_to_replace.add( " protected " ) ; replacement_names.add( " " ) ; names_to_replace.add( " void " ) ; replacement_names.add( " def " ) ; // names_to_replace.add( " this" ) ; // replacement_names.add( " self" ) ; names_to_replace.add( " boolean " ) ; replacement_names.add( "" ) ; names_to_replace.add( " true" ) ; replacement_names.add( " True" ) ; names_to_replace.add( " false" ) ; replacement_names.add( " False" ) ; names_to_replace.add( " &&" ) ; replacement_names.add( " and" ) ; names_to_replace.add( " ||" ) ; replacement_names.add( " or" ) ; names_to_replace.add( " ++" ) ; replacement_names.add( " += 1" ) ; names_to_replace.add( " --" ) ; replacement_names.add( " -= 1" ) ; names_to_replace.add( ".java" ) ; replacement_names.add( ".py" ) ; // names_to_replace.add( "string.Length" ) ; // replacement_names.add( "string.length()" ) ; // names_to_replace.add( ".Length" ) ; // replacement_names.add( ".length" ) ; names_to_replace.add( ".charAt( character_index )" ) ; replacement_names.add( "[ character_index ]" ) ; names_to_replace.add( ".charAt(" ) ; replacement_names.add( "[" ) ; names_to_replace.add( "indexOf(" ) ; replacement_names.add( "find(" ) ; names_to_replace.add( ".substring(" ) ; replacement_names.add( "[" ) ; names_to_replace.add( "toUpperCase(" ) ; replacement_names.add( "upper(" ) ; // names_to_replace.add( "ToCharArray(" ) ; // replacement_names.add( "toCharArray(" ) ; // names_to_replace.add( "Close(" ) ; // replacement_names.add( "close(" ) ; names_to_replace.add( "toLowerCase(" ) ; replacement_names.add( "lower(" ) ; names_to_replace.add( "Integer.parseInt(" ) ; replacement_names.add( "int(" ) ; names_to_replace.add( "if (" ) ; replacement_names.add( "if " ) ; names_to_replace.add( "if (" ) ; replacement_names.add( "if " ) ; names_to_replace.add( "else if (" ) ; replacement_names.add( "elif " ) ; names_to_replace.add( "else if (" ) ; replacement_names.add( "elif " ) ; names_to_replace.add( "else" ) ; replacement_names.add( "else :" ) ; names_to_replace.add( "while (" ) ; replacement_names.add( "while " ) ; names_to_replace.add( "for (" ) ; replacement_names.add( "for " ) ; names_to_replace.add( " int " ) ; replacement_names.add( " " ) ; names_to_replace.add( "int[] " ) ; replacement_names.add( "" ) ; names_to_replace.add( " char " ) ; replacement_names.add( " " ) ; names_to_replace.add( "char[] " ) ; replacement_names.add( "" ) ; names_to_replace.add( " float " ) ; replacement_names.add( " " ) ; names_to_replace.add( " double " ) ; replacement_names.add( " " ) ; names_to_replace.add( "double[] " ) ; replacement_names.add( "" ) ; names_to_replace.add( " long " ) ; replacement_names.add( " " ) ; names_to_replace.add( " {" ) ; replacement_names.add( "" ) ; names_to_replace.add( " {" ) ; replacement_names.add( "" ) ; names_to_replace.add( " {" ) ; replacement_names.add( "" ) ; names_to_replace.add( " {" ) ; replacement_names.add( "" ) ; names_to_replace.add( "{" ) ; replacement_names.add( "" ) ; names_to_replace.add( " }" ) ; replacement_names.add( "" ) ; names_to_replace.add( " }" ) ; replacement_names.add( "" ) ; names_to_replace.add( " }" ) ; replacement_names.add( "" ) ; names_to_replace.add( " }" ) ; replacement_names.add( "" ) ; names_to_replace.add( "}" ) ; replacement_names.add( "" ) ; names_to_replace.add( " extends " ) ; replacement_names.add( " ( " ) ; // 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 ) ; // The following three if constructs are used to find // out when the main() method begins. if ( opening_brace_of_main_method_found == true ) { // This program works only when the main() method is // the last method in a program. // Because in Python programs the main "method" // is just statements at the end of a program, // we need to unindent the lines of a Java // main() method if ( text_line_from_file.startsWith( " " ) ) { // Two 3-space indentation steps will be removed. text_line_from_file = text_line_from_file.substring( 6 ) ; } } 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 ; } // The loop that replaces method names etc. 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 ) ; } } // Unnecessary space characters and semicolons will be removed // from the end of line. while( text_line_from_file.endsWith( " " ) || text_line_from_file.endsWith( ";" ) ) { text_line_from_file = text_line_from_file.substring( 0, text_line_from_file.length() - 1 ) ; } 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 Java " + "\n program to a Python 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 ) ; } }