FileDocCategorySizeDatePackage
DirectoryTree.javaAPI DocphoneME MR2 API (J2ME)7133Wed May 02 17:59:48 BST 2007makedep

DirectoryTree

public class DirectoryTree extends Object
Encapsulates a notion of a directory tree. Designed to allow fast querying of full paths for unique filenames in the hierarchy.

Fields Summary
private Node
rootNode
The root of the read directoryTree
private Vector
subdirsToIgnore
Subdirs to ignore; Vector of Strings
private Hashtable
nameToNodeListTable
This maps file names to Lists of nodes.
private boolean
verbose
Output "."'s as directories are read. Defaults to false.
Constructors Summary
public DirectoryTree()

	subdirsToIgnore = new Vector();
	verbose = false;
    
public DirectoryTree(String baseDirectory)
Takes an absolute path to the root directory of this DirectoryTree. Throws IllegalArgumentException if the given string represents a plain file or nonexistent directory.

	this();
	readDirectory(baseDirectory);
    
Methods Summary
public voidaddSubdirToIgnore(java.lang.String subdir)

	subdirsToIgnore.add(subdir);
    
private voidbuildNameToNodeListTable(makedep.DirectoryTree$Node curNode)

	String fullName = curNode.getName();
	String parent = curNode.getParent();
	String separator = System.getProperty("file.separator");

        if (parent != null) {
          if (!fullName.startsWith(parent)) {
	    throw new RuntimeException(
	        "Internal error: parent of file name \"" + fullName +
		"\" does not match file name \"" + parent + "\""
	    );
          }

          int len = parent.length();
          if (!parent.endsWith(separator)) {
	    len += separator.length();
          }
	
          String fileName = fullName.substring(len);

          if (fileName == null) {
	    throw new RuntimeException(
	        "Internal error: file name was empty"
	    );
          }

          List nodeList = (List) nameToNodeListTable.get(fileName);
          if (nodeList == null) {
	    nodeList = new Vector();
	    nameToNodeListTable.put(fileName, nodeList);
          }
	
          nodeList.add(curNode);
        } else {
          if (curNode != rootNode) {
            throw new RuntimeException(
                "Internal error: parent of file + \"" + fullName + "\"" +
                " was null"
            );
          }
        }

	if (curNode.isDirectory()) {
          Iterator iter = curNode.getChildren();
          if (iter != null) {
            while (iter.hasNext()) {
              buildNameToNodeListTable((Node) iter.next());
            }
          }
	}
    
public java.util.ListfindFile(java.lang.String name)
Queries the DirectoryTree for a file or directory name. Takes only the name of the file or directory itself (i.e., no parent directory information should be in the passed name). Returns a List of DirectoryTreeNodes specifying the full paths of all of the files or directories of this name in the DirectoryTree. Returns null if the directory tree has not been read from disk yet or if the file was not found in the tree.

	if (rootNode == null) {
	    return null;
	}

	if (nameToNodeListTable == null) {
	    nameToNodeListTable = new Hashtable();
	    try {
		buildNameToNodeListTable(rootNode);
	    } catch (IOException e) {
		e.printStackTrace();
		return null;
	    }
	}

	return (List) nameToNodeListTable.get(name);
    
public booleangetVerbose()

	return verbose;
    
public voidreadDirectory(java.lang.String baseDirectory)
Takes an absolute path to the root directory of this DirectoryTree. Throws IllegalArgumentException if the given string represents a plain file or nonexistent directory.

	File root = new File(baseDirectory);
	if (!root.isDirectory()) {
	    throw new IllegalArgumentException("baseDirectory \"" +
					       baseDirectory +
					       "\" does not exist or " +
					       "is not a directory");
	}
	try {
	    root = root.getCanonicalFile();
	}
	catch (IOException e) {
	    throw new RuntimeException(e.toString());
	}
	rootNode = new Node(root);
	readDirectory(rootNode, root);
    
private voidreadDirectory(makedep.DirectoryTree$Node parentNode, java.io.File parentDir)
Reads all of the files in the given directory and adds them as children of the directory tree node. Requires that the passed node represents a directory.

	File[] children = parentDir.listFiles();
	if (children == null)
	    return;
	if (verbose) {
	    System.out.print(".");
	    System.out.flush();
	}
	for (int i = 0; i < children.length; i++) {
	    File child = children[i];
	    children[i] = null;
	    boolean isDir = child.isDirectory();
	    boolean mustSkip = false;
	    if (isDir) {
		for (Iterator iter = subdirsToIgnore.iterator();
		     iter.hasNext(); ) {
		    if (child.getName().equals((String) iter.next())) {
			mustSkip = true;
			break;
		    }
		}
	    }
	    if (!mustSkip) {
		Node childNode = new Node(child);
		parentNode.addChild(childNode);
		if (isDir) {
		    readDirectory(childNode, child);
		}
	    }
	}
    
public voidsetVerbose(boolean newValue)
Output "."'s to System.out as directories are read. Defaults to false.

	verbose = newValue;