1 /* |
|
2 * Copyright (c) 1998, 2003, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.doclets.internal.toolkit.util; |
|
27 |
|
28 import java.io.File; |
|
29 |
|
30 /** |
|
31 * This class is used to represent a source path which can contain only |
|
32 * directories no zip files. If a zip file is specified in the command line it |
|
33 * will not get reflected in the SourcePath. |
|
34 * |
|
35 * This code is not part of an API. |
|
36 * It is implementation that is subject to change. |
|
37 * Do not use it as an API |
|
38 * |
|
39 * @author Atul M Dambalkar |
|
40 */ |
|
41 public |
|
42 class SourcePath { |
|
43 private final char dirSeparator = File.pathSeparatorChar; |
|
44 |
|
45 /** |
|
46 * The original class path string |
|
47 */ |
|
48 private String pathstr; |
|
49 |
|
50 /** |
|
51 * List of source path entries. Each entry is a directory. |
|
52 */ |
|
53 private File[] sourcePath; |
|
54 |
|
55 |
|
56 /** |
|
57 * Build a source path from the specified path string on the command line. |
|
58 */ |
|
59 public SourcePath(String pathstr) { |
|
60 init(pathstr); |
|
61 } |
|
62 |
|
63 /** |
|
64 * Build a default source path from the path strings specified by |
|
65 * the properties env.class.path. |
|
66 */ |
|
67 public SourcePath() { |
|
68 init(System.getProperty("env.class.path")); |
|
69 } |
|
70 |
|
71 /** |
|
72 * Initialize the SourcePath File array, which will contain only the |
|
73 * directory names from the given path string. |
|
74 * |
|
75 * @param pathstr Path String. |
|
76 */ |
|
77 private void init(String pathstr) { |
|
78 if (pathstr == null || pathstr.length() == 0) { |
|
79 pathstr = "."; |
|
80 } |
|
81 |
|
82 int noOfFileSep = 0; |
|
83 int index = 0; |
|
84 this.pathstr = pathstr; // Save original class path string |
|
85 |
|
86 // Count the number of path separators |
|
87 while ((index = pathstr.indexOf(dirSeparator, index)) != -1) { |
|
88 noOfFileSep++; |
|
89 index++; |
|
90 } |
|
91 // Build the source path |
|
92 File[] tempPath = new File[noOfFileSep + 1]; |
|
93 int tempPathIndex = 0; |
|
94 int len = pathstr.length(); |
|
95 int sepPos = 0; |
|
96 for (index = 0; index < len; index = sepPos + 1) { |
|
97 sepPos = pathstr.indexOf(dirSeparator, index); |
|
98 if (sepPos < 0) { |
|
99 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 } |
|