//*********************************************************************************
// class tig.Files
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************
package tig;

import tig.GeneralConstants;

import java.io.*;
import javax.swing.tree.DefaultMutableTreeNode;

/******************************************************************************
Contains miscelaneous utility methods dealing with files and directories.
@author Thierry Graff
@history feb 26 2002 : creation from Misc and UtilTree.

@todo
*********************************************************************************/
public class Files implements GeneralConstants{

  //=================================================================================
  //                                      METHODS
  //=================================================================================

  //******************* getExtension(File f) ****************
  /**
  Returns the extension of a string (characters located after the last point).
  @param s String from which we want to find the extension.
  @return Extension of the String 's'.
  */
  public static String getExtension(File f) {
    return getExtension(f.getName());
  }//fin getExtension(File f)

  //******************* getExtension(String s) **************
  /**
  Returns the extension of a string (characters located after the last point).
  @param s String from which we want to find the extension.
  @return Extension of the String 's'.
  */
  public static String getExtension(String s){
    String ext = BLANK;
    int i = s.lastIndexOf('.');

    if (i > 0 &&  i < s.length() - 1) {
      ext = s.substring(i+1).toLowerCase();
    }
    return ext;
  }//fin getExtension(String s)

  //***************** dirToTree(theDir, rootNode) **********
  /** Converts a file hierarchy into a tree.
  
If 'theDir' is not a directory, 'rootNode' remains unchanged.
Each node of the tree contains the absolute path of the file it represents as UserObject.
All files contained in the directory are added to the tree, whatever their type.. @param theDir Directory to transform into a tree. @param rootNode Root node of the resulting tree. */ // Recursive method. public static void dirToTree(File theDir, DefaultMutableTreeNode rootNode){ if(!theDir.isDirectory()) return; File listFiles[] = theDir.listFiles(); for (int i = 0; i < listFiles.length; i ++) { DefaultMutableTreeNode curNode = new DefaultMutableTreeNode(listFiles[i].getName()); //System.out.println("i = " + i + "ext = " + getExtension(listFiles[i].getName())); rootNode.add(curNode); if (listFiles[i].isDirectory()) dirToTree(listFiles[i], curNode); } }// end dirToTree(theDir, rootNode) //******** dirToTree(theDir, rootNode, fileType) ***************** /** Converts a file hierarchy into a tree ; only files of a given type are put in the output tree. @param theDir Directory to transform into a tree. @param rootNode Root node of the resulting tree. @param fileType String used to specify which type of file should be included in the output tree. @todo handle a fileType like : ".htm, .html, .xml" (parsed using tig.Strings.parse()) @todo handle at least '*' */ //Recursive method. // === WARNING === does not work if a directory name contains a '.' public static void dirToTree(File theDir, DefaultMutableTreeNode rootNode, String fileType){ //System.out.println("dirToTree(theDir, rootNode, fileType) fileType = " + fileType); File listFiles[] = theDir.listFiles(); for (int i = 0; i < listFiles.length; i ++) { String curName = listFiles[i].getName(); String curExt = getExtension(curName); if(fileType.equalsIgnoreCase(curExt) || curExt==null){ //System.out.println("curName = " + curName + " ; curExt = " + curExt); DefaultMutableTreeNode curNode = new DefaultMutableTreeNode(curName); rootNode.add(curNode); if (curExt==null) dirToTree(listFiles[i], curNode, fileType); } } }// end dirToTree //******** fileCopy ***************** /** Copies file identified by 'source' to 'dest'.
Works with absolute or relative path names. @param source path of the source file. @param dest path of the destination file. @throws IOException if :
  • 'source' does not exist ;
  • 'dest' corresponds to a directory ;
  • other I/O problem occurs.
  • */ public static void fileCopy(String source, String dest) throws IOException{ FileInputStream fis = new FileInputStream(source); FileOutputStream fos = new FileOutputStream(dest); byte[] b = new byte[fis.available()]; fis.read(b); fos.write(b); fis.close(); fos.close(); }// end fileCopy //******** dirCopy ***************** /** Copies a directory identified by 'source' and all the files below it to directory identified by 'dest'.
    Works with absolute or relative path names. @param source path of the source directory. @param dest path of the destination directory. @throws IOException if :
  • 'source' does not exist ;
  • 'dest' corresponds to a directory ;
  • other I/O problem occurs.
  • */ // TO BE FINALIZED - remove NodeCarac /* public static void dirCopy(String source, String dest) throws IOException{ Properties p = new Properties(); p.load(new FileInputStream(new File(structureFile))); //p.list(System.out); String sourceDir = p.getProperty("XMLSources"); String destDir = p.getProperty("HTMLDest"); // create source and dest trees DefaultMutableTreeNode sourceRootNode, destRootNode; sourceRootNode = new DefaultMutableTreeNode(); Files.dirToTree(new File(sourceDir), sourceRootNode); sourceRootNode.setUserObject(BLANK); destRootNode = cloneDMTN(sourceRootNode); // fill dest tree DefaultMutableTreeNode curSourceNode, curDestNode; Enumeration se = sourceRootNode.depthFirstEnumeration(); Enumeration de = destRootNode.depthFirstEnumeration(); NodeCarac nc; for( ; se.hasMoreElements(); ){ curSourceNode = (DefaultMutableTreeNode)(se.nextElement()); curDestNode = (DefaultMutableTreeNode)(de.nextElement()); nc = new NodeCarac(getPath(curSourceNode), false); curDestNode.setUserObject(nc); } // print test // for(de = destRootNode.depthFirstEnumeration() ; de.hasMoreElements(); ){ // curDestNode = (DefaultMutableTreeNode)(de.nextElement()); // nc = (NodeCarac)curDestNode.getUserObject(); // System.out.println(nc.getPathToRoot()); // } // Replicate directory structure String strSource, strDest; // source and dest files File fSource, fDest; for(de = destRootNode.depthFirstEnumeration() ; de.hasMoreElements(); ){ curDestNode = (DefaultMutableTreeNode)(de.nextElement()); nc = (NodeCarac)curDestNode.getUserObject(); fSource = new File(sourceDir + FS + nc.getPathToRoot()); if(fSource.isDirectory()){ fDest = new File(destDir + FS + nc.getPathToRoot()); fDest.mkdirs(); } //System.out.println(fSource); //System.out.println(fDest); } // Generate the output files final String XML = "xml"; for(de = destRootNode.depthFirstEnumeration() ; de.hasMoreElements(); ){ curDestNode = (DefaultMutableTreeNode)(de.nextElement()); nc = (NodeCarac)curDestNode.getUserObject(); fSource = new File(sourceDir + FS + nc.getPathToRoot()); //System.out.println(fSource.getAbsolutePath()); if(!fSource.isDirectory()){ fDest = new File(destDir + FS + nc.getPathToRoot()); Files.fileCopy(fSource.getAbsolutePath(), fDest.getAbsolutePath()); }// end if(!fSource.isDirectory()) } }// end dirCopy */ //================================================================================= //================================================================================= // TESTS //================================================================================= //================================================================================= /* // **************** For tests only **************** public static void main(String[] args){ // no complete argument checking if(args[0].equalsIgnoreCase("testFileCopy")) testFileCopy(args[1], args[2]); else System.out.println("first argument must be 'testFileCopy'"); }// end main */ /* // **************** For tests only **************** //D:\Programs\java\jdk1.4\bin\java -classpath .;bin tig.Files testFileCopy in out public static void testFileCopy(String source, String dest){ try{ fileCopy(source, dest); } catch(Exception e){ e.printStackTrace(); } }// end testFileCopy */ }// end class Files

    Ads help maintain this website.