KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > ClasspathSourceDirectory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core.search.matching;
12
13 import org.eclipse.core.resources.IContainer;
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
19 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
20 import org.eclipse.jdt.internal.core.builder.ClasspathLocation;
21 import org.eclipse.jdt.internal.core.util.ResourceCompilationUnit;
22 import org.eclipse.jdt.internal.core.util.Util;
23
24 public class ClasspathSourceDirectory extends ClasspathLocation {
25
26     IContainer sourceFolder;
27     SimpleLookupTable directoryCache;
28     SimpleLookupTable missingPackageHolder = new SimpleLookupTable();
29     char[][] fullExclusionPatternChars;
30     char[][] fulInclusionPatternChars;
31
32 ClasspathSourceDirectory(IContainer sourceFolder, char[][] fullExclusionPatternChars, char[][] fulInclusionPatternChars) {
33     this.sourceFolder = sourceFolder;
34     this.directoryCache = new SimpleLookupTable(5);
35     this.fullExclusionPatternChars = fullExclusionPatternChars;
36     this.fulInclusionPatternChars = fulInclusionPatternChars;
37 }
38
39 public void cleanup() {
40     this.directoryCache = null;
41 }
42
43 SimpleLookupTable directoryTable(String JavaDoc qualifiedPackageName) {
44     SimpleLookupTable dirTable = (SimpleLookupTable) directoryCache.get(qualifiedPackageName);
45     if (dirTable == missingPackageHolder) return null; // package exists in another classpath directory or jar
46
if (dirTable != null) return dirTable;
47
48     try {
49         IResource container = sourceFolder.findMember(qualifiedPackageName); // this is a case-sensitive check
50
if (container instanceof IContainer) {
51             IResource[] members = ((IContainer) container).members();
52             dirTable = new SimpleLookupTable();
53             for (int i = 0, l = members.length; i < l; i++) {
54                 IResource m = members[i];
55                 String JavaDoc name;
56                 if (m.getType() == IResource.FILE) {
57                     int index = Util.indexOfJavaLikeExtension(name = m.getName());
58                     if (index >= 0) {
59                         String JavaDoc fullPath = m.getFullPath().toString();
60                         if (!org.eclipse.jdt.internal.compiler.util.Util.isExcluded(fullPath.toCharArray(), this.fulInclusionPatternChars, this.fullExclusionPatternChars, false/*not a folder path*/)) {
61                             dirTable.put(name.substring(0, index), m);
62                         }
63                     }
64                 }
65             }
66             directoryCache.put(qualifiedPackageName, dirTable);
67             return dirTable;
68         }
69     } catch(CoreException ignored) {
70         // treat as if missing
71
}
72     directoryCache.put(qualifiedPackageName, missingPackageHolder);
73     return null;
74 }
75
76 public boolean equals(Object JavaDoc o) {
77     if (this == o) return true;
78     if (!(o instanceof ClasspathSourceDirectory)) return false;
79
80     return sourceFolder.equals(((ClasspathSourceDirectory) o).sourceFolder);
81 }
82
83 public NameEnvironmentAnswer findClass(String JavaDoc sourceFileWithoutExtension, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedSourceFileWithoutExtension) {
84     SimpleLookupTable dirTable = directoryTable(qualifiedPackageName);
85     if (dirTable != null && dirTable.elementSize > 0) {
86         IFile file = (IFile) dirTable.get(sourceFileWithoutExtension);
87         if (file != null) {
88             return new NameEnvironmentAnswer(new ResourceCompilationUnit(file, file.getLocationURI()), null /* no access restriction */);
89         }
90     }
91     return null;
92 }
93
94 public IPath getProjectRelativePath() {
95     return sourceFolder.getProjectRelativePath();
96 }
97
98 public boolean isPackage(String JavaDoc qualifiedPackageName) {
99     return directoryTable(qualifiedPackageName) != null;
100 }
101
102 public void reset() {
103     this.directoryCache = new SimpleLookupTable(5);
104 }
105
106 public String JavaDoc toString() {
107     return "Source classpath directory " + sourceFolder.getFullPath().toString(); //$NON-NLS-1$
108
}
109
110 public String JavaDoc debugPathString() {
111     return this.sourceFolder.getFullPath().toString();
112 }
113
114 }
115
Popular Tags