package db2ea;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by user on 2017/9/23.
 */
public class EAWriter {
    private String fileName;
    private FileWriter fileWriter;
    private EAItem pack;

    public static String Field_Separator = ",";
    public static String Field_Separator_Replace = "，";

    public static String Field_Header = "Name,Type,Stereotype,CSV_KEY,CSV_PARENT_KEY";

    public EAWriter(String fileName) {
        if (!StrUtils.isEmpty(fileName) && fileName.toLowerCase().endsWith(SqlParser.File_SQL_Ext)) {
            fileName = fileName.substring(0, fileName.length() - SqlParser.File_SQL_Ext.length());
        }
        this.fileName = String.format("%s%s", fileName == null ? "file" : fileName, SqlParser.File_EA_Ext);
    }

    public boolean isOpen() {
        return fileWriter != null;
    }

    public EAItem getPack() {
        return pack;
    }

    /**
     * open, write header, create root package which will be the first level elements' parent
     * @return
     */
    public EAItem open() {
        if (fileWriter != null) {
            close();
        }

        pack = null;
        try {
            fileWriter = new FileWriter(fileName);
            // headers
            fileWriter.write(Field_Header);
            fileWriter.write("\n");

            // package
            // pack = new EAItem("EA包", EAType.Package, EAStereotype.None, null);
            write(pack);
        } catch (IOException e) {
            fileWriter = null;
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

        return pack;
    }

    public void close() {
        if (fileWriter == null) {
            return;
        }

        pack = null;
        try {
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        } finally {
            fileWriter = null;
        }
        System.out.printf("EAWrite saves successfully: %s\n", fileName);
    }

    public void write(EAItem item) {
        if (item == null) {
            return;
        }

        if (fileWriter == null) {
            System.out.println("Please call open() firstly.");
            return;
        }

        try {
            fileWriter.write(item.toString());
            fileWriter.write("\n");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
@@ -24,20 +24,24 @@
 java.io.
-File
+Buffered
 Writer;%0A
@@ -55,27 +55,95 @@
 java.io.
-IOException
+FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.OutputStreamWriter
 ;%0A%0A/**%0A 
@@ -243,20 +243,24 @@
 private 
-File
+Buffered
 Writer f
@@ -1270,29 +1270,89 @@
 new 
-FileWriter(fileName);
+BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), %22UTF-8%22));%0A
 %0A   
@@ -1438,26 +1438,24 @@
 eWriter.
-write(%22%5Cn%22
+newLine(
 );%0A%0A    

Use buffedWriter