001    /*
002     * Copyright 1998-2003 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.doclets.internal.toolkit.util;
027    
028    import java.io.File;
029    
030    /**
031     * This class is used to represent a source path which can contain only
032     * directories no zip files. If a zip file is specified in the command line it
033     * will not get reflected in the SourcePath.
034     *
035     * This code is not part of an API.
036     * It is implementation that is subject to change.
037     * Do not use it as an API
038     *
039     * @author Atul M Dambalkar
040     */
041    public
042        class SourcePath {
043        private final char dirSeparator = File.pathSeparatorChar;
044    
045        /**
046         * The original class path string
047         */
048        private String pathstr;
049    
050        /**
051         * List of source path entries. Each entry is a directory.
052         */
053        private File[] sourcePath;
054    
055    
056        /**
057         * Build a source path from the specified path string on the command line.
058         */
059        public SourcePath(String pathstr) {
060            init(pathstr);
061        }
062    
063        /**
064         * Build a default source path from the path strings specified by
065         * the properties env.class.path.
066         */
067        public SourcePath() {
068            init(System.getProperty("env.class.path"));
069        }
070    
071        /**
072         * Initialize the SourcePath File array, which will contain only the
073         * directory names from the given path string.
074         *
075         * @param pathstr Path String.
076         */
077        private void init(String pathstr) {
078            if (pathstr == null ||  pathstr.length() == 0) {
079                pathstr = ".";
080            }
081    
082            int noOfFileSep = 0;
083            int index = 0;
084            this.pathstr = pathstr; // Save original class path string
085    
086            // Count the number of path separators
087            while ((index = pathstr.indexOf(dirSeparator, index)) != -1) {
088                noOfFileSep++;
089                index++;
090            }
091            // Build the source path
092            File[] tempPath = new File[noOfFileSep + 1];
093            int tempPathIndex = 0;
094            int len = pathstr.length();
095            int sepPos = 0;
096            for (index = 0; index < len; index = sepPos + 1) {
097                sepPos = pathstr.indexOf(dirSeparator, index);
098                if (sepPos < 0) {
099                    sepPos = len;
100                }
101                File file = new File(pathstr.substring(index, sepPos));
102                if (file.isDirectory()) {
103                    tempPath[tempPathIndex++] = file;
104                } // if it is really a file, ignore it.
105            }
106            sourcePath = new File[tempPathIndex];
107            System.arraycopy((Object)tempPath, 0, (Object)sourcePath,
108                             0, tempPathIndex);
109        }
110    
111        /**
112         * Find the specified directory in the source path.
113         *
114         * @param name Name of the directory to be searched for in the source path.
115         * @return File Return the directory if found else return null.
116         */
117        public File getDirectory(String name) {
118            for (int i = 0; i < sourcePath.length; i++) {
119                File directoryNeeded = new File(sourcePath[i], name);
120                if (directoryNeeded.isDirectory()) {
121                    return directoryNeeded;
122                }
123            }
124            return null;
125        }
126    
127        /**
128         * Return original source path string.
129         */
130        public String toString() {
131            return pathstr;
132        }
133    }