<commit_before>//============================================================================
// Name        : ProActive Files Split-Merge Framework
// Author      : <NAME>, ActiveEon team
// Version     : 0.1
// Copyright   : Copyright ActiveEon 2008-2009, <NAME> (All Rights Reserved)
// Description : Framework for building distribution layers for native applications
//================================================================================

package org.ow2.proactive.scheduler.ext.filessplitmerge.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.ow2.proactive.scheduler.ext.filessplitmerge.logging.LoggerManager;


public class FilesTools {

    /**
     * We assume that, in a xxx.conf file, attributes are written on a single
     * line: attrName = value OR attrName value
     * 
     * This method will read the xxx.conf file line by line and look for the
     * attributeName at the beginning of a line in order to get the value.
     * 
     * @param configFile
     *            the gold configuration file
     * @param attributeName
     * @param stopToFirstBlank
     *            If true, the value of the attribute is between "=" and the
     *            first blank. Otherwise, the value is between "=" and the end
     *            of the line.
     * @return the value of attributeName, or null if the attribute is not
     *         defined in the configuration file
     * @throws IOException
     */
    public synchronized String getValueForAttribute(File configFile, String attributeName,
            boolean stopToFirstBlank) throws IOException {
        FileReader fr = new FileReader(configFile);
        BufferedReader gcReader = new BufferedReader(fr);
        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (line.startsWith(attributeName))
            // we found the atttribute
            {
                line = line.substring(attributeName.length());
                line = line.trim();
                // the line contains now eather the value eather '= '+value
                if (line.startsWith("="))
                    line = line.substring(1);
                line = line.trim();

                if (stopToFirstBlank) {
                    if (line.indexOf(" ") != -1) {
                        line = line.substring(0, line.indexOf(" "));
                        line.trim();
                    }
                }// stop to first blank

                // we have our value in the line variable.
                gcReader.close();
                return line;
            }

        }
        return null;
    }

    public synchronized void saveValueForAttribute(File configFile, String attributeName,
            String atttributeValue) throws IOException {
        boolean attributeFound = false;
        String attributeLine = attributeName + " = " + atttributeValue;

        String fileContentUpdated = "";
        FileReader fr = new FileReader(configFile);
        BufferedReader gcReader = new BufferedReader(fr);

        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (line.startsWith(attributeName))
            // we found the atttribute
            {
                line = attributeLine;
                attributeFound = true;
            }
            fileContentUpdated = fileContentUpdated + line + "\n";
        }

        if (!attributeFound) {
            fileContentUpdated = fileContentUpdated + attributeLine + "\n";
        }

        gcReader.close();
        configFile.delete();
        configFile.createNewFile();
        BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(configFile));
        writer.write(fileContentUpdated);
        writer.close();
    }

    /**
     * remove the line with the assignment of the value of an attribute this
     * removes
     * 
     * @param attributesFile
     * @param attributeName
     * @throws IOException
     */
    public synchronized void removeAttribute(File attributesFile, String attributeName) throws IOException {
        String fileContentUpdated = "";
        FileReader fr = new FileReader(attributesFile);
        BufferedReader gcReader = new BufferedReader(fr);

        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (!line.startsWith(attributeName))
            // we write all the lines that do not start with the attr name
            {
                fileContentUpdated = fileContentUpdated + line + "\n";
            }
        }

        gcReader.close();
        attributesFile.delete();
        attributesFile.createNewFile();
        BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(attributesFile));
        writer.write(fileContentUpdated);
        writer.close();
    }

    public boolean copyfile(String srFile, String dtFile) {
        try {
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
            InputStream in = new FileInputStream(f1);

            OutputStream out = new FileOutputStream(f2);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
            return false;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }

        return true;
    }

    public boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

    public static File createDir(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();

            // Cannot set read write attributes, need java5 compliance
            dir.setWritable(true, false);
            dir.setReadable(true, false);
            // System.out.println("Directory "+path+" hhave been created." );
            return dir;
        }

        if (dir.isDirectory()) {
            LoggerManager.getInstane().warning(
                    "Directory " + path + " has not been created as it already existed.");
            return dir;
        }

        LoggerManager<commit_msg>forgot 2 lines in last commit ..... <commit_after>//============================================================================
// Name        : ProActive Files Split-Merge Framework
// Author      : <NAME>anu, ActiveEon team
// Version     : 0.1
// Copyright   : Copyright ActiveEon 2008-2009, <NAME> (All Rights Reserved)
// Description : Framework for building distribution layers for native applications
//================================================================================

package org.ow2.proactive.scheduler.ext.filessplitmerge.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.ow2.proactive.scheduler.ext.filessplitmerge.logging.LoggerManager;


public class FilesTools {

    /**
     * We assume that, in a xxx.conf file, attributes are written on a single
     * line: attrName = value OR attrName value
     * 
     * This method will read the xxx.conf file line by line and look for the
     * attributeName at the beginning of a line in order to get the value.
     * 
     * @param configFile
     *            the gold configuration file
     * @param attributeName
     * @param stopToFirstBlank
     *            If true, the value of the attribute is between "=" and the
     *            first blank. Otherwise, the value is between "=" and the end
     *            of the line.
     * @return the value of attributeName, or null if the attribute is not
     *         defined in the configuration file
     * @throws IOException
     */
    public synchronized String getValueForAttribute(File configFile, String attributeName,
            boolean stopToFirstBlank) throws IOException {
        FileReader fr = new FileReader(configFile);
        BufferedReader gcReader = new BufferedReader(fr);
        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (line.startsWith(attributeName))
            // we found the atttribute
            {
                line = line.substring(attributeName.length());
                line = line.trim();
                // the line contains now eather the value eather '= '+value
                if (line.startsWith("="))
                    line = line.substring(1);
                line = line.trim();

                if (stopToFirstBlank) {
                    if (line.indexOf(" ") != -1) {
                        line = line.substring(0, line.indexOf(" "));
                        line.trim();
                    }
                }// stop to first blank

                // we have our value in the line variable.
                gcReader.close();
                return line;
            }

        }
        return null;
    }

    public synchronized void saveValueForAttribute(File configFile, String attributeName,
            String atttributeValue) throws IOException {
        boolean attributeFound = false;
        String attributeLine = attributeName + " = " + atttributeValue;

        String fileContentUpdated = "";
        FileReader fr = new FileReader(configFile);
        BufferedReader gcReader = new BufferedReader(fr);

        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (line.startsWith(attributeName))
            // we found the atttribute
            {
                line = attributeLine;
                attributeFound = true;
            }
            fileContentUpdated = fileContentUpdated + line + "\n";
        }

        if (!attributeFound) {
            fileContentUpdated = fileContentUpdated + attributeLine + "\n";
        }

        gcReader.close();
        configFile.delete();
        configFile.createNewFile();
        BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(configFile));
        writer.write(fileContentUpdated);
        writer.close();
    }

    /**
     * remove the line with the assignment of the value of an attribute this
     * removes
     * 
     * @param attributesFile
     * @param attributeName
     * @throws IOException
     */
    public synchronized void removeAttribute(File attributesFile, String attributeName) throws IOException {
        String fileContentUpdated = "";
        FileReader fr = new FileReader(attributesFile);
        BufferedReader gcReader = new BufferedReader(fr);

        String line;
        while ((line = gcReader.readLine()) != null) {
            // look for the attributeName at the begining at the line
            line = line.trim();

            if (!line.startsWith(attributeName))
            // we write all the lines that do not start with the attr name
            {
                fileContentUpdated = fileContentUpdated + line + "\n";
            }
        }

        gcReader.close();
        attributesFile.delete();
        attributesFile.createNewFile();
        BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(attributesFile));
        writer.write(fileContentUpdated);
        writer.close();
    }

    public boolean copyfile(String srFile, String dtFile) {
        try {
            File f1 = new File(srFile);
            File f2 = new File(dtFile);
            InputStream in = new FileInputStream(f1);

            OutputStream out = new FileOutputStream(f2);

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
            return false;
        } catch (IOException e) {
            System.out.println(e.getMessage());
            return false;
        }

        return true;
    }

    public boolean deleteDirectory(File path) {
        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
        return (path.delete());
    }

    public static File createDir(String path) {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdir();

            // Cannot set read write attributes, need java5 compliance
            //dir.setWritable(true, false);
            //dir.setReadable(true, false);
            // System.out.println("Directory "+path+" hhave been created." );
            return dir;
        }

        if (dir.isDirectory()) {
            LoggerManager.getInstane().warning(
                    "Directory " + path + " has not been created as it already existed.");
            return dir;
        }

        LoggerManager
{'fraction_non_alphanumeric': 0.11396189681546377, 'fraction_numerical': 0.0038242247253511335, 'mean_word_length': 1.8634282301413498, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6206905', 'n_tokens_mistral': 3627, 'n_tokens_neox': 3460, 'n_words': 1065}