/*
 * Created on 11 mai 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.injac.indexing.util;

import java.io.File;

/**
 * Classe qui fournit les chemins vers le fichier courant
 * et le fichier parent de fichier courant. Elle fournit
 * aussi une methode pour effacer un repertoire non vide
 * @author marius
 */
public class FileTools {
	static public String getCurrent(){
	     File dir = new File (".");
	     try {
	     	String s = dir.getCanonicalPath().toString();
	     	return s;
	       }
	     catch(Exception e) {
	       e.printStackTrace();
	       return null;
	       }
	     }
	static public File getCurentFile(){
		File dir = new File (".");
	     try {
	     	File f = new File(dir.getCanonicalPath().toString());
	     	return f;
	       }
	     catch(Exception e) {
	       e.printStackTrace();
	       return null;
	       }
		}
	
	/**
	 * gets parent file
	 * @return String
	 */
	static public String getParent(){
		File dir = new File ("..");
	     try {
	     	String s = dir.getCanonicalPath().toString();
	     	return s;
	       }
	     catch(Exception e) {
	       e.printStackTrace();
	       return null;
	       }
		}
	/**
	 * gets parent file
	 * @return File
	 */
	static public File getParentFile(){
		File dir = new File ("..");
	     try {
	     	File f = new File(dir.getCanonicalPath().toString());
	     	return f;
	       }
	     catch(Exception e) {
	       e.printStackTrace();
	       return null;
	       }
		}
	/**
	 * efface un repertoire non vide 
	 * (deleteDirectory)
	 * @param file
	 */
	static public void eraseDir(File file){
			clearDir(file);
			file.delete();
	}
	
	public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
    
        // The directory is now empty so delete it
        return dir.delete();
    }
	
	/**
	 * clear a directory content 
	 * (clearDirectory)
	 * @param file
	 */
	static public void clearDir(File file){
		if(file.isDirectory()){
			File[] fii = file.listFiles();
			for(int i=0; i<fii.length; i++)
				eraseDir(fii[i]);
		}
	}

	
}
