KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > util > ClassPath


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.util;
26
27 import java.util.StringTokenizer JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.FilenameFilter JavaDoc;
33
34
35 /**
36  * ClassPath provides class file lookup according to a classpath
37  * specification.
38  */

39
40 public class ClassPath {
41   /* The entire class path specification */
42   private String JavaDoc theClassPathSpec;
43
44   /* Linked list of class path elements */
45   private ClassPathElement theClassPath;
46
47   /**
48    * Construct a class path from the input String argument.
49    * The path is expected to be in the form appropriate for the current
50    * execution environment.
51    */

52   public ClassPath(String JavaDoc path) {
53     theClassPathSpec = path;
54     parsePath();
55   }
56
57   /**
58    * locate a class file given a fully qualified class name
59    */

60   public ClassFileSource findClass(String JavaDoc className) {
61     return findClass(className, theClassPath);
62   }
63
64   /**
65    * locate a class file given a fully qualified class name
66    * starting at the specified class path element
67    */

68   static ClassFileSource findClass(String JavaDoc className, ClassPathElement path) {
69     for (ClassPathElement e = path; e != null; e = e.next()) {
70       ClassFileSource source = e.sourceOf(className);
71       if (source != null) {
72     source.setSourceElement(e);
73     return source;
74       }
75     }
76
77     return null;
78   }
79
80   /**
81    * Return a file name which might reasonably identify a file containing
82    * the specified class. The name is a "./" relative path.
83    */

84   public static String JavaDoc fileNameOf(String JavaDoc className, char separator) {
85     StringBuffer JavaDoc path = new StringBuffer JavaDoc();
86     StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(className, "./", false);//NOI18N
87
for (boolean first = true; parser.hasMoreElements(); first = false) {
88       if (!first)
89     path.append(separator);
90       path.append(parser.nextToken());
91     }
92     path.append(".class");//NOI18N
93
return path.toString();
94   }
95
96
97   /**
98    * Return a file name which might reasonably identify a file containing
99    * the specified class. The name is a "./" relative path.
100    */

101   public static String JavaDoc fileNameOf(String JavaDoc className) {
102     return fileNameOf(className, File.separatorChar);
103   }
104
105
106   /**
107    * Return a file name which might reasonably identify a file containing
108    * the specified class in a zip file.
109    */

110   public static String JavaDoc zipFileNameOf(String JavaDoc className) {
111     return fileNameOf(className, '/');
112   }
113
114
115   /**
116    * Return the vm class name which corresponds to the input file name.
117    * The file name is expected to be a "./" relative path.
118    * Returns null if the file name doesn't end in ".class"
119    */

120   public static String JavaDoc classNameOf(String JavaDoc fileName) {
121     int fnlen = fileName.length();
122     if (fnlen > 6 && fileName.regionMatches(true, fnlen - 6, ".class", 0, 6)) {//NOI18N
123
/* the file name ends with .class */
124       fileName = fileName.substring(0, fileName.length()-6);
125       StringBuffer JavaDoc className = new StringBuffer JavaDoc();
126       StringTokenizer JavaDoc parser = new StringTokenizer JavaDoc(fileName, "\\/", false);//NOI18N
127
for (boolean first = true; parser.hasMoreElements(); first = false) {
128     if (!first)
129       className.append('/');
130     className.append(parser.nextToken());
131       }
132       return className.toString();
133     }
134     return null;
135   }
136
137   /**
138    * Remove any class path elements which match directory
139    */

140   public boolean remove(File JavaDoc directory) {
141     boolean matched = false;
142     ClassPathElement firstElement = theClassPath;
143     ClassPathElement prevElement = null;
144     for (ClassPathElement cpe = firstElement; cpe != null; cpe = cpe.next()) {
145       if (cpe.matches(directory)) {
146     matched = true;
147     if (prevElement == null)
148       firstElement = cpe.next();
149     else
150       prevElement.setNext(cpe.next());
151       } else {
152     prevElement = cpe;
153       }
154     }
155     theClassPath = firstElement;
156     return matched;
157   }
158
159   /**
160    * Append a directory to the classpath.
161    */

162   public void append(File JavaDoc directory) {
163     append(ClassPathElement.create(directory.getPath()));
164   }
165
166   /**
167    * Append a class path element to the classpath.
168    */

169   public void append(ClassPathElement anElement) {
170     if (theClassPath == null)
171       theClassPath = anElement;
172     else
173       theClassPath.append(anElement);
174   }
175
176   /**
177    * Return an enumeration of all of the class files in the specified
178    * package in this class path.
179    * @param packageName specifies the VM format package name
180    * to which class files must belong.
181    * @returns an Enumeration of the VM format class names which
182    * can be found. Note that the Enumeration value is of type String
183    * and duplicate entries may be returned as the result of finding
184    * a class through more than one class path element. Note also
185    * that the class name returned might not correspond the the
186    * name of the class in the file.
187    */

188   public Enumeration JavaDoc classesInPackage(String JavaDoc packageName) {
189     return new ClassPackageEnumeration(this, packageName);
190   }
191
192   /* package local accessors */
193   ClassPathElement getPathElements() {
194     return theClassPath;
195   }
196
197   /* private accessors */
198
199   private void parsePath() {
200     StringTokenizer JavaDoc parser =
201       new StringTokenizer JavaDoc(theClassPathSpec,
202               java.io.File.pathSeparator,
203               false /* dont return delimiters */
204               );
205     
206     ClassPathElement lastElement = null;
207     while (parser.hasMoreElements()) {
208       ClassPathElement anElement = ClassPathElement.create(parser.nextToken());
209
210       if (lastElement == null)
211     theClassPath = anElement;
212       else
213     lastElement.append(anElement);
214
215       lastElement = anElement;
216     }
217   }
218
219 }
220
221 /**
222  * An enumeration class which returns the names of the classes which
223  * can be found in a class path
224  */

225
226 class ClassPackageEnumeration implements Enumeration JavaDoc {
227   /* The next class path element to look for matches in once
228      the current enumeration is complete */

229   private ClassPathElement nextClassPathElement;
230
231   /* The package name */
232   private String JavaDoc thePackageName;
233
234   /* The enumeration of matching class names in the current class path
235      element */

236   private Enumeration JavaDoc currentElementEnumeration;
237
238   /**
239    * Construct a ClassPackageEnumeration.
240    * @param classPath The class path in which to search for classes.
241    * @param packageName The VM name of the package in which to search.
242    */

243   ClassPackageEnumeration(ClassPath classPath, String JavaDoc packageName) {
244     nextClassPathElement = classPath.getPathElements();
245     thePackageName = packageName;
246   }
247
248   public boolean hasMoreElements() {
249     while ((currentElementEnumeration == null ||
250         !currentElementEnumeration.hasMoreElements()) &&
251        nextClassPathElement != null) {
252       currentElementEnumeration =
253     nextClassPathElement.classesInPackage(thePackageName);
254       nextClassPathElement = nextClassPathElement.next();
255     }
256
257     return (currentElementEnumeration != null &&
258         currentElementEnumeration.hasMoreElements());
259   }
260
261   public Object JavaDoc nextElement() {
262     if (hasMoreElements())
263       return currentElementEnumeration.nextElement();
264
265     throw new NoSuchElementException JavaDoc();
266   }
267 }
268
269
Popular Tags