package org.batgen.generators;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class GenUtil {
    public static final String PROTECTED_CODE = "PROTECTED CODE";
    public static final String TAB = "    ";

    /**
     * @return a path derived from the passed in package.
     */
    public static String packageToPath( String pkg ) {
        return "src/main/java/" + pkg.replace( ".", "/" );
    }

    /**
     * Writes the content of the string to a specified file.
     * 
     * @param fileName
     *            Fully qualified filename with path
     * @param content
     */
    public static void writeToFile( String fileName, String content ) {
        File file = new File( fileName );

        file = new File( file.getParent() );
        if ( !file.exists() )
            file.mkdirs();
        try {
            PrintWriter pw = new PrintWriter( fileName );
            pw.write( content );
            pw.close();

        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        }
    }

    /**
     * Used for finding the protected lines in the original file.
     *
     * @param fileName
     *            filename and path of the file to be checked for protected
     *            lines
     * @return returns a list of strings
     */
    public static List<String> getProtectedLines( String fileName ) {
        List<String> protectedLines = new ArrayList<String>();

        File exportFile = new File( fileName );
        BufferedReader br = null;

        try {

            br = new BufferedReader( new FileReader( exportFile ) );
            String line = "";

            // find start of protection
            while ( !line.contains( PROTECTED_CODE ) ) {

                line = br.readLine();
                if ( line == null )
                    break;
            }

            // read next line
            line = br.readLine();
            while ( line != null ) {

                line = "\n" + line;
                protectedLines.add( line );
                line = br.readLine();
            }

            br.close();

        } catch ( FileNotFoundException e ) {
            // Ignored if file doesn't exist.

        } catch ( IOException e ) {
            e.printStackTrace();
        }

        return protectedLines;
    }

}

@@ -134,32 +134,59 @@
 .io.FileReader;%0A
+import java.io.FileWriter;%0A
 import java.io.I
@@ -189,32 +189,32 @@
 io.IOException;%0A
-
 import java.io.P
@@ -401,16 +401,27 @@
 ing TAB 
+           
 = %22    %22
@@ -1149,32 +1149,40 @@
 se();%0A%0A        %7D
+%0A       
  catch ( FileNot
@@ -1244,32 +1244,724 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Appends to the end of file without overwriting what's already in the%0A     * file.%0A     * %0A     * @param filePath%0A     *            Fully qualified filename with path%0A     * @param content%0A     */%0A    public static void appendToFile( String fileName, String content ) %7B%0A%0A        File file = new File( fileName );%0A        file = new File( file.getParent() );%0A%0A        if ( !file.exists() ) %7B%0A            file.mkdirs();%0A        %7D%0A%0A        FileWriter fw = null;%0A%0A        try %7B%0A%0A            fw = new FileWriter( fileName, true );%0A            fw.write( content );%0A            fw.close();%0A%0A        %7D%0A        catch ( IOException e ) %7B%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A
     /**%0A     * U
@@ -2024,16 +2024,17 @@
 .%0A     *
+ 
 %0A     * 
@@ -3005,32 +3005,40 @@
 se();%0A%0A        %7D
+%0A       
  catch ( FileNot
@@ -3105,16 +3105,16 @@
 exist.%0A%0A
-
         
@@ -3114,16 +3114,24 @@
        %7D
+%0A       
  catch (

Update GenUtil.java