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 class SourcePath {
042: private final char dirSeparator = File.pathSeparatorChar;
043:
044: /**
045: * The original class path string
046: */
047: private String pathstr;
048:
049: /**
050: * List of source path entries. Each entry is a directory.
051: */
052: private File[] sourcePath;
053:
054: /**
055: * Build a source path from the specified path string on the command line.
056: */
057: public SourcePath(String pathstr) {
058: init(pathstr);
059: }
060:
061: /**
062: * Build a default source path from the path strings specified by
063: * the properties env.class.path.
064: */
065: public SourcePath() {
066: init(System.getProperty("env.class.path"));
067: }
068:
069: /**
070: * Initialize the SourcePath File array, which will contain only the
071: * directory names from the given path string.
072: *
073: * @param pathstr Path String.
074: */
075: private void init(String pathstr) {
076: if (pathstr == null || pathstr.length() == 0) {
077: pathstr = ".";
078: }
079:
080: int noOfFileSep = 0;
081: int index = 0;
082: this .pathstr = pathstr; // Save original class path string
083:
084: // Count the number of path separators
085: while ((index = pathstr.indexOf(dirSeparator, index)) != -1) {
086: noOfFileSep++;
087: index++;
088: }
089: // Build the source path
090: File[] tempPath = new File[noOfFileSep + 1];
091: int tempPathIndex = 0;
092: int len = pathstr.length();
093: int sepPos = 0;
094: for (index = 0; index < len; index = sepPos + 1) {
095: sepPos = pathstr.indexOf(dirSeparator, index);
096: if (sepPos < 0) {
097: sepPos = len;
098: }
099: File file = new File(pathstr.substring(index, sepPos));
100: if (file.isDirectory()) {
101: tempPath[tempPathIndex++] = file;
102: } // if it is really a file, ignore it.
103: }
104: sourcePath = new File[tempPathIndex];
105: System.arraycopy((Object) tempPath, 0, (Object) sourcePath, 0,
106: tempPathIndex);
107: }
108:
109: /**
110: * Find the specified directory in the source path.
111: *
112: * @param name Name of the directory to be searched for in the source path.
113: * @return File Return the directory if found else return null.
114: */
115: public File getDirectory(String name) {
116: for (int i = 0; i < sourcePath.length; i++) {
117: File directoryNeeded = new File(sourcePath[i], name);
118: if (directoryNeeded.isDirectory()) {
119: return directoryNeeded;
120: }
121: }
122: return null;
123: }
124:
125: /**
126: * Return original source path string.
127: */
128: public String toString() {
129: return pathstr;
130: }
131: }
|