package com.github.juanmf.java2plant.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SaveFileHelper {

	private static final String J2PUML = "j2PUML";

	public static void save(StringBuilder pumlContent, String path) throws IOException {
		final File file;

		if (path == null) {
			file = new File(getPathName(path));
		} else {
			file = new File(getPathName(null));
		}

		BufferedWriter bw;
		bw = new BufferedWriter(new FileWriter(file));
		bw.write(pumlContent.toString());

		bw.flush();
		bw.close();
	}

	private static String getPathName(String rout) {
		final StringBuilder path;
		if (rout == null) {
			path = new StringBuilder(J2PUML);
		} else {
			//TODO: It must be checked what we got in the rout (path) to validate it...
			path = new StringBuilder(rout + J2PUML);
		}
		SimpleDateFormat instant = new SimpleDateFormat("ddMMyyyy_HM_mm", Locale.getDefault());
		Date now = new Date();

		path.append(instant.format(now)).append(".txt");

		return path.toString();
	}

}

@@ -267,25 +267,27 @@
 Helper %7B%0D%0A%0D%0A
-%09
+   
 private stat
@@ -318,19 +318,21 @@
  %22j2
-PUML
+puml
 %22;%0D%0A%0D%0A
-%09
+   
 publ
@@ -409,26 +409,30 @@
 xception %7B%0D%0A
-%09%09
+      
 final File f
@@ -438,137 +438,47 @@
 file
-;%0D%0A%0D%0A%09%09if (path == null) %7B%0D%0A%09%09%09file = new File(getPathName(path));%0D%0A%09%09%7D else %7B%0D%0A%09%09%09file = new File(getPathName(null));%0D%0A%09%09%7D%0D%0A%0D%0A%09%09
+ = new File(getPathName(path));%0D%0A      
 Buff
@@ -493,18 +493,22 @@
 er bw;%0D%0A
-%09%09
+      
 bw = new
@@ -547,18 +547,22 @@
 ile));%0D%0A
-%09%09
+      
 bw.write
@@ -590,18 +590,22 @@
 ());%0D%0A%0D%0A
-%09%09
+      
 bw.flush
@@ -609,18 +609,22 @@
 ush();%0D%0A
-%09%09
+      
 bw.close
@@ -632,15 +632,19 @@
 );%0D%0A
-%09
+   
 %7D%0D%0A%0D%0A
-%09
+   
 priv
@@ -689,18 +689,22 @@
 out) %7B%0D%0A
-%09%09
+      
 final St
@@ -722,18 +722,22 @@
  path;%0D%0A
-%09%09
+      
 if (rout
@@ -737,17 +737,17 @@
 f (rout 
-=
+!
 = null) 
@@ -753,63 +753,20 @@
  %7B%0D%0A
-%09%09%09path = new StringBuilder(J2PUML);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09
+         
 //
+ 
 TODO
@@ -832,68 +832,163 @@
 date
- it...%0D%0A%09%09%09path = new StringBuilder(rout + J2PUML);%0D%0A%09%09%7D%0D%0A%09%09
+%0D%0A         // it...%0D%0A         path = new StringBuilder(rout.concat(J2PUML));%0D%0A      %7D else %7B%0D%0A         path = new StringBuilder(J2PUML);%0D%0A      %7D%0D%0A%0D%0A      
 Simp
@@ -1072,18 +1072,22 @@
 lt());%0D%0A
-%09%09
+      
 Date now
@@ -1106,43 +1106,108 @@
 );%0D%0A
-%0D%0A%09%09path.append(instant.format(now)
+      StringBuilder fullName = new StringBuilder(instant.format(now));%0D%0A%0D%0A      path.append(fullName
 ).ap
@@ -1227,10 +1227,14 @@
 %0D%0A%0D%0A
-%09%09
+      
 retu
@@ -1258,9 +1258,11 @@
 );%0D%0A
-%09
+   
 %7D%0D%0A%0D

Save File Helper Refactor.