package jp.gr.java_conf.daisy.ajax_mutator.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import org.mozilla.javascript.ast.AstNode;

public class Util {
    /**
     * @return true if copy succeed.
     */
    public static boolean copyFile(String srcPath, String destPath) {
        boolean success = false;
        FileChannel srcChannel = null;
        FileChannel destChannel = null;

        try {
            File dest = new File(destPath);
            if (dest.exists())
                dest.createNewFile();
            srcChannel = new FileInputStream(srcPath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            srcChannel.transferTo(0, srcChannel.size(), destChannel);
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
            success = false;
        } finally {
            try {
                if (srcChannel != null)
                    srcChannel.close();
                if (destChannel != null)
                    destChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return success;
    }

    /**
     * read all string from specified file
     *
     * @return list of String, each element is a line in file. If some error
     *         happen during reading, returns null.
     */
    public static List<String> readFromFile(String pathToFile) {
        List<String> lines = new ArrayList<String>();
        boolean success = false;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(new File(pathToFile)));
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success ? lines : null;
    }

    /**
     * write content into a file with specified path. This method override
     * existing file. Even if file is not exist, this method does not create new
     * file.
     *
     * @return true if write is successfully finished.
     */
    public static boolean writeToFile(String pathToFile, String content) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(new File(pathToFile));
            writer.write(content);
        } catch (IOException e) {
            System.err.println("IOException" + e.getMessage());
            return false;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                System.err.println(
                        "Fail to close source file" + e.getMessage());
                return false;
            }
        }
        return true;
    }

    public static String oneLineStringOf(AstNode node) {
        if (node == null)
            return "";
        String[] str = node.toSource().split("\n");
        if (str.length == 1)
            return str[0];
        else {
            int numOfShownLine = 2;
            boolean use1 = str.length > 2 && str[0].length() < 10;
            boolean useLast2 = str.length + (use1 ? 1 : 0) > 2
                                    && str[str.length - 1].length() < 10;
            numOfShownLine += (use1 ? 1 : 0) + (useLast2 ? 1 : 0);

            return str[0] + (use1 ? str[1] : "") + " ..("
                + (str.length - numOfShownLine) + ").. "
                + (useLast2 ? str[str.length - 2] : "") + str[str.length - 1];
        }
    }

    public static String omitLineBreak(AstNode node) {
        return omitLineBreak(node.toSource());
    }

    public static String omitLineBreak(String string) {
        return string.replaceAll("(\r)?\n", "");
    }

    public static String join(String[] arrayOfString) {
        return join(arrayOfString, null);
    }

    public static String join(String[] arrayOfString, String separator) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < arrayOfString.length; i++) {
            builder.append(arrayOfString[i]);
            if (separator != null && i != arrayOfString.length - 1)
                builder.append(separator);
        }
        return builder.toString();
    }
}

@@ -62,190 +62,9 @@
 .io.
-BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.FileReader;%0Aimport java.io.FileWriter;%0Aimport java.io.IOException
+*
 ;%0Aim
@@ -127,16 +127,16 @@
 ayList;%0A
-
 import j
@@ -149,16 +149,42 @@
 il.List;
+%0Aimport java.util.Scanner;
 %0A%0Aimport
@@ -4099,32 +4099,1100 @@
 n%22, %22%22);%0A    %7D%0A%0A
+    /**%0A     * Convert line separator of given file to System's default line separator%0A     * obtained by System.lineSeparator().%0A     * @param file text file whose line separator can be converted.%0A     */%0A    public static void normalizeLineBreak(File file) %7B%0A        List%3CString%3E lines = new ArrayList%3CString%3E();%0A%0A        // read from file%0A        try %7B%0A            Scanner scanner = new Scanner(new FileInputStream(file));%0A            while (scanner.hasNext()) %7B%0A                lines.add(scanner.nextLine());%0A            %7D%0A            scanner.close();%0A        %7D catch (IOException e) %7B%0A            throw new IllegalStateException(%22Failed to read file %22 + file);%0A        %7D%0A%0A        try %7B%0A            BufferedWriter writer = new BufferedWriter(new FileWriter(file));%0A            for (String line: lines) %7B%0A                writer.write(line);%0A                writer.write(System.lineSeparator());%0A            %7D%0A            writer.close();%0A        %7D catch (IOException e) %7B%0A            throw new IllegalStateException(%22Failed to write file %22 + file);%0A        %7D%0A    %7D%0A%0A
     public stati

implement Util#normalizeLineBreak to reduce line separator issue among platforms