package javaformatter;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;

public class Main {

    public static void main(final String... args) throws IOException {
        if (args.length != 1) {
            System.err.println("Usage java -jar formatter.jar <directory-with-java-sources>");
            System.exit(1610121904);
        }
        Path inputDirectory = Paths.get(args[0]);
        new SourceCodeFiles(inputDirectory, "java").forEach(process());
    }

    private static Consumer<SourceCodeFile> process() {
        return (source) -> {
            System.out.println("\n\n---------------------------------------------------");
            System.out.println(source.getPath().getFileName());
            System.out.println("---------------------------------------------------\n\n");

            SourceCodeFormatter formatter = new SourceCodeFormatter(source);
            try {
                formatter.format();
            } catch (IOException e) {
                System.exit(1610122032);
            }
            formatter.withSource(System.out::println);
            System.out.println("\n\n---------------------------------------------------\n\n");
        };
    }


}

@@ -17,16 +17,57 @@
 atter;%0A%0A
+import org.apache.commons.io.FileUtils;%0A%0A
 import j
@@ -651,16 +651,24 @@
  (source
+CodeFile
 ) -%3E %7B%0A 
@@ -683,273 +683,149 @@
    S
-ystem.out.println(%22%5Cn%5Cn---------------------------------------------------%22);%0A            System.out.println(source.getPath().getFileName());%0A            System.out.println(%22---------------------------------------------------%5Cn%5Cn%22);%0A%0A            SourceCodeFormatter
+ourceCodeFormatter formatter = new SourceCodeFormatter(sourceCodeFile);%0A            try %7B%0A                formatter.format();%0A               
  for
@@ -834,44 +834,44 @@
 tter
- = new 
+.with
 Source
-CodeF
+(f
 ormatte
-r(s
+dS
 ource
-);%0A
+ -%3E %7B%0A   
     
@@ -870,32 +870,37 @@
 %0A               
+     
 try %7B%0A          
@@ -905,36 +905,113 @@
         
-formatter.format();%0A
+        FileUtils.writeStringToFile(sourceCodeFile.getPath().toFile(), formattedSource);%0A        
         
@@ -1048,32 +1048,40 @@
                 
+        
 System.exit(1610
@@ -1085,12 +1085,12 @@
 6101
-2203
+4204
 2);%0A
@@ -1093,32 +1093,40 @@
 2);%0A            
+        
 %7D%0A            fo
@@ -1127,145 +1127,108 @@
     
-formatter.withSource(System.out::println);%0A            System.out.println(%22%5Cn%5Cn---------------------------------------------------%5Cn%5Cn%22);
+    %7D);%0A            %7D catch (IOException e) %7B%0A                System.exit(1610122032);%0A            %7D
 %0A   

write files back after formatting them