src/share/tools/MakeDeps/DirectoryTree.java

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/tools/MakeDeps/DirectoryTree.java	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 + * Copyright 1999-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +/** Encapsulates a notion of a directory tree. Designed to allow fast
    1.29 +    querying of full paths for unique filenames in the hierarchy. */
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.util.*;
    1.33 +
    1.34 +public class DirectoryTree {
    1.35 +
    1.36 +    /** The root of the read directoryTree */
    1.37 +    private Node rootNode;
    1.38 +
    1.39 +    /** Subdirs to ignore; Vector of Strings */
    1.40 +    private Vector subdirsToIgnore;
    1.41 +
    1.42 +    /** This maps file names to Lists of nodes. */
    1.43 +    private Hashtable nameToNodeListTable;
    1.44 +
    1.45 +    /** Output "."'s as directories are read. Defaults to false. */
    1.46 +    private boolean verbose;
    1.47 +
    1.48 +    public DirectoryTree() {
    1.49 +        subdirsToIgnore = new Vector();
    1.50 +        verbose = false;
    1.51 +    }
    1.52 +
    1.53 +    /** Takes an absolute path to the root directory of this
    1.54 +        DirectoryTree. Throws IllegalArgumentException if the given
    1.55 +        string represents a plain file or nonexistent directory. */
    1.56 +
    1.57 +    public DirectoryTree(String baseDirectory) {
    1.58 +        this();
    1.59 +        readDirectory(baseDirectory);
    1.60 +    }
    1.61 +
    1.62 +    public void addSubdirToIgnore(String subdir) {
    1.63 +        subdirsToIgnore.add(subdir);
    1.64 +    }
    1.65 +
    1.66 +    /** Output "."'s to System.out as directories are read. Defaults
    1.67 +        to false. */
    1.68 +    public void setVerbose(boolean newValue) {
    1.69 +        verbose = newValue;
    1.70 +    }
    1.71 +
    1.72 +    public boolean getVerbose() {
    1.73 +        return verbose;
    1.74 +    }
    1.75 +
    1.76 +    public String getRootNodeName() {
    1.77 +        return rootNode.getName();
    1.78 +    }
    1.79 +
    1.80 +    /** Takes an absolute path to the root directory of this
    1.81 +        DirectoryTree. Throws IllegalArgumentException if the given
    1.82 +        string represents a plain file or nonexistent directory. */
    1.83 +
    1.84 +    public void readDirectory(String baseDirectory)
    1.85 +        throws IllegalArgumentException {
    1.86 +        File root = new File(baseDirectory);
    1.87 +        if (!root.isDirectory()) {
    1.88 +            throw new IllegalArgumentException("baseDirectory \"" +
    1.89 +                                               baseDirectory +
    1.90 +                                               "\" does not exist or " +
    1.91 +                                               "is not a directory");
    1.92 +        }
    1.93 +        try {
    1.94 +            root = root.getCanonicalFile();
    1.95 +        }
    1.96 +        catch (IOException e) {
    1.97 +            throw new RuntimeException(e.toString());
    1.98 +        }
    1.99 +        rootNode = new Node(root);
   1.100 +        readDirectory(rootNode, root);
   1.101 +    }
   1.102 +
   1.103 +    /** Queries the DirectoryTree for a file or directory name. Takes
   1.104 +        only the name of the file or directory itself (i.e., no parent
   1.105 +        directory information should be in the passed name). Returns a
   1.106 +        List of DirectoryTreeNodes specifying the full paths of all of
   1.107 +        the files or directories of this name in the DirectoryTree.
   1.108 +        Returns null if the directory tree has not been read from disk
   1.109 +        yet or if the file was not found in the tree. */
   1.110 +    public List findFile(String name) {
   1.111 +        if (rootNode == null) {
   1.112 +            return null;
   1.113 +        }
   1.114 +
   1.115 +        if (nameToNodeListTable == null) {
   1.116 +            nameToNodeListTable = new Hashtable();
   1.117 +            try {
   1.118 +                buildNameToNodeListTable(rootNode);
   1.119 +            } catch (IOException e) {
   1.120 +                e.printStackTrace();
   1.121 +                return null;
   1.122 +            }
   1.123 +        }
   1.124 +
   1.125 +        return (List) nameToNodeListTable.get(name);
   1.126 +    }
   1.127 +
   1.128 +    private void buildNameToNodeListTable(Node curNode)
   1.129 +      throws IOException {
   1.130 +        String fullName = curNode.getName();
   1.131 +        String parent = curNode.getParent();
   1.132 +        String separator = System.getProperty("file.separator");
   1.133 +
   1.134 +        if (parent != null) {
   1.135 +          if (!fullName.startsWith(parent)) {
   1.136 +            throw new RuntimeException(
   1.137 +                "Internal error: parent of file name \"" + fullName +
   1.138 +                "\" does not match file name \"" + parent + "\""
   1.139 +            );
   1.140 +          }
   1.141 +
   1.142 +          int len = parent.length();
   1.143 +          if (!parent.endsWith(separator)) {
   1.144 +            len += separator.length();
   1.145 +          }
   1.146 +
   1.147 +          String fileName = fullName.substring(len);
   1.148 +
   1.149 +          if (fileName == null) {
   1.150 +            throw new RuntimeException(
   1.151 +                "Internal error: file name was empty"
   1.152 +            );
   1.153 +          }
   1.154 +
   1.155 +          List nodeList = (List) nameToNodeListTable.get(fileName);
   1.156 +          if (nodeList == null) {
   1.157 +            nodeList = new Vector();
   1.158 +            nameToNodeListTable.put(fileName, nodeList);
   1.159 +          }
   1.160 +
   1.161 +          nodeList.add(curNode);
   1.162 +        } else {
   1.163 +          if (curNode != rootNode) {
   1.164 +            throw new RuntimeException(
   1.165 +                "Internal error: parent of file + \"" + fullName + "\"" +
   1.166 +                " was null"
   1.167 +            );
   1.168 +          }
   1.169 +        }
   1.170 +
   1.171 +        if (curNode.isDirectory()) {
   1.172 +          Iterator iter = curNode.getChildren();
   1.173 +          if (iter != null) {
   1.174 +            while (iter.hasNext()) {
   1.175 +              buildNameToNodeListTable((Node) iter.next());
   1.176 +            }
   1.177 +          }
   1.178 +        }
   1.179 +    }
   1.180 +
   1.181 +    /** Reads all of the files in the given directory and adds them as
   1.182 +        children of the directory tree node. Requires that the passed
   1.183 +        node represents a directory. */
   1.184 +
   1.185 +    private void readDirectory(Node parentNode, File parentDir) {
   1.186 +        File[] children = parentDir.listFiles();
   1.187 +        if (children == null)
   1.188 +            return;
   1.189 +        if (verbose) {
   1.190 +            System.out.print(".");
   1.191 +            System.out.flush();
   1.192 +        }
   1.193 +        for (int i = 0; i < children.length; i++) {
   1.194 +            File child = children[i];
   1.195 +            children[i] = null;
   1.196 +            boolean isDir = child.isDirectory();
   1.197 +            boolean mustSkip = false;
   1.198 +            if (isDir) {
   1.199 +                for (Iterator iter = subdirsToIgnore.iterator();
   1.200 +                     iter.hasNext(); ) {
   1.201 +                    if (child.getName().equals((String) iter.next())) {
   1.202 +                        mustSkip = true;
   1.203 +                        break;
   1.204 +                    }
   1.205 +                }
   1.206 +            }
   1.207 +            if (!mustSkip) {
   1.208 +                Node childNode = new Node(child);
   1.209 +                parentNode.addChild(childNode);
   1.210 +                if (isDir) {
   1.211 +                    readDirectory(childNode, child);
   1.212 +                }
   1.213 +            }
   1.214 +        }
   1.215 +    }
   1.216 +
   1.217 +    private class Node implements DirectoryTreeNode {
   1.218 +        private File file;
   1.219 +        private Vector children;
   1.220 +
   1.221 +        /** file must be a canonical file */
   1.222 +        Node(File file) {
   1.223 +            this.file = file;
   1.224 +            children = new Vector();
   1.225 +        }
   1.226 +
   1.227 +        public boolean isFile() {
   1.228 +            return file.isFile();
   1.229 +        }
   1.230 +
   1.231 +        public boolean isDirectory() {
   1.232 +            return file.isDirectory();
   1.233 +        }
   1.234 +
   1.235 +        public String getName() {
   1.236 +            return file.getPath();
   1.237 +        }
   1.238 +
   1.239 +        public String getParent() {
   1.240 +            return file.getParent();
   1.241 +        }
   1.242 +
   1.243 +        public void addChild(Node n) {
   1.244 +            children.add(n);
   1.245 +        }
   1.246 +
   1.247 +        public Iterator getChildren() throws IllegalArgumentException {
   1.248 +            return children.iterator();
   1.249 +        }
   1.250 +
   1.251 +        public int getNumChildren() throws IllegalArgumentException {
   1.252 +            return children.size();
   1.253 +        }
   1.254 +
   1.255 +        public DirectoryTreeNode getChild(int i)
   1.256 +            throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.257 +            return (DirectoryTreeNode) children.get(i);
   1.258 +        }
   1.259 +    }
   1.260 +}

mercurial