KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > gui > SearchPath


1 /*
2  * @(#)SearchPath.java 1.10 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 /*
8  * Copyright (c) 1997-1999 by Sun Microsystems, Inc. All Rights Reserved.
9  *
10  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
11  * modify and redistribute this software in source and binary code form,
12  * provided that i) this copyright notice and license appear on all copies of
13  * the software; and ii) Licensee does not utilize the software in a manner
14  * which is disparaging to Sun.
15  *
16  * This software is provided "AS IS," without a warranty of any kind. ALL
17  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
18  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
19  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
20  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
21  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
22  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
23  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
24  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
25  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGES.
27  *
28  * This software is not designed or intended for use in on-line control of
29  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
30  * the design, construction, operation or maintenance of any nuclear
31  * facility. Licensee represents and warrants that it will not use or
32  * redistribute the Software for such purposes.
33  */

34
35 package com.sun.tools.example.debug.gui;
36
37 import java.io.*;
38 import java.util.*;
39
40 public class SearchPath {
41
42     private String JavaDoc pathString;
43
44     private String JavaDoc[] pathArray;
45
46     public SearchPath(String JavaDoc searchPath) {
47     //### Should check searchpath for well-formedness.
48
StringTokenizer st = new StringTokenizer(searchPath, File.pathSeparator);
49     List dlist = new ArrayList();
50     while (st.hasMoreTokens()) {
51         dlist.add(st.nextToken());
52     }
53     pathString = searchPath;
54     pathArray = (String JavaDoc[])dlist.toArray(new String JavaDoc[dlist.size()]);
55     }
56
57     public boolean isEmpty() {
58     return (pathArray.length == 0);
59     }
60
61     public String JavaDoc asString() {
62     return pathString;
63     }
64     
65     public String JavaDoc[] asArray() {
66     return (String JavaDoc[])pathArray.clone();
67     }
68
69     public File resolve(String JavaDoc relativeFileName) {
70         for (int i = 0; i < pathArray.length; i++) {
71             File path = new File(pathArray[i], relativeFileName);
72             if (path.exists()) {
73         return path;
74             }
75         }
76         return null;
77     }
78
79     //### return List?
80

81     public String JavaDoc[] children(String JavaDoc relativeDirName, FilenameFilter filter) {
82     // If a file appears at the same relative path
83
// with respect to multiple entries on the classpath,
84
// the one corresponding to the earliest entry on the
85
// classpath is retained. This is the one that will be
86
// found if we later do a 'resolve'.
87
SortedSet s = new TreeSet(); // sorted, no duplicates
88
for (int i = 0; i < pathArray.length; i++) {
89             File path = new File(pathArray[i], relativeDirName);
90             if (path.exists()) {
91         String JavaDoc[] childArray = path.list(filter);
92         if (childArray != null) {
93             for (int j = 0; j < childArray.length; j++) {
94             if (!s.contains(childArray[j])) {
95                 s.add(childArray[j]);
96             }
97             }
98         }
99         }
100     }
101         return (String JavaDoc[])s.toArray(new String JavaDoc[s.size()]);
102     }
103
104 }
105
Popular Tags