KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > builder > ClasspathDirectory


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.builder;
12
13 import java.io.IOException JavaDoc;
14
15 import org.eclipse.core.resources.*;
16 import org.eclipse.core.runtime.*;
17
18 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
19 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
20 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet;
21 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
22 import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
23 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
24 import org.eclipse.jdt.internal.core.util.Util;
25
26 public class ClasspathDirectory extends ClasspathLocation {
27
28 IContainer binaryFolder; // includes .class files for a single directory
29
boolean isOutputFolder;
30 SimpleLookupTable directoryCache;
31 String JavaDoc[] missingPackageHolder = new String JavaDoc[1];
32 AccessRuleSet accessRuleSet;
33
34 ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder, AccessRuleSet accessRuleSet) {
35     this.binaryFolder = binaryFolder;
36     this.isOutputFolder = isOutputFolder;
37     this.directoryCache = new SimpleLookupTable(5);
38     this.accessRuleSet = accessRuleSet;
39 }
40
41 public void cleanup() {
42     this.directoryCache = null;
43 }
44
45 String JavaDoc[] directoryList(String JavaDoc qualifiedPackageName) {
46     String JavaDoc[] dirList = (String JavaDoc[]) directoryCache.get(qualifiedPackageName);
47     if (dirList == missingPackageHolder) return null; // package exists in another classpath directory or jar
48
if (dirList != null) return dirList;
49
50     try {
51         IResource container = binaryFolder.findMember(qualifiedPackageName); // this is a case-sensitive check
52
if (container instanceof IContainer) {
53             IResource[] members = ((IContainer) container).members();
54             dirList = new String JavaDoc[members.length];
55             int index = 0;
56             for (int i = 0, l = members.length; i < l; i++) {
57                 IResource m = members[i];
58                 if (m.getType() == IResource.FILE && org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(m.getName()))
59                     // add exclusion pattern check here if we want to hide .class files
60
dirList[index++] = m.getName();
61             }
62             if (index < dirList.length)
63                 System.arraycopy(dirList, 0, dirList = new String JavaDoc[index], 0, index);
64             directoryCache.put(qualifiedPackageName, dirList);
65             return dirList;
66         }
67     } catch(CoreException ignored) {
68         // ignore
69
}
70     directoryCache.put(qualifiedPackageName, missingPackageHolder);
71     return null;
72 }
73
74 boolean doesFileExist(String JavaDoc fileName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedFullName) {
75     String JavaDoc[] dirList = directoryList(qualifiedPackageName);
76     if (dirList == null) return false; // most common case
77

78     for (int i = dirList.length; --i >= 0;)
79         if (fileName.equals(dirList[i]))
80             return true;
81     return false;
82 }
83
84 public boolean equals(Object JavaDoc o) {
85     if (this == o) return true;
86     if (!(o instanceof ClasspathDirectory)) return false;
87
88     ClasspathDirectory dir = (ClasspathDirectory) o;
89     if (this.accessRuleSet != dir.accessRuleSet)
90         if (this.accessRuleSet == null || !this.accessRuleSet.equals(dir.accessRuleSet))
91             return false;
92     return this.binaryFolder.equals(dir.binaryFolder);
93 }
94
95 public NameEnvironmentAnswer findClass(String JavaDoc binaryFileName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName) {
96     if (!doesFileExist(binaryFileName, qualifiedPackageName, qualifiedBinaryFileName)) return null; // most common case
97

98     ClassFileReader reader = null;
99     try {
100         reader = Util.newClassFileReader(this.binaryFolder.getFile(new Path(qualifiedBinaryFileName)));
101     } catch (CoreException e) {
102         return null;
103     } catch (ClassFormatException e) {
104         return null;
105     } catch (IOException JavaDoc e) {
106         return null;
107     }
108     if (reader != null) {
109         if (this.accessRuleSet == null)
110             return new NameEnvironmentAnswer(reader, null);
111         String JavaDoc fileNameWithoutExtension = qualifiedBinaryFileName.substring(0, qualifiedBinaryFileName.length() - SuffixConstants.SUFFIX_CLASS.length);
112         return new NameEnvironmentAnswer(reader, this.accessRuleSet.getViolatedRestriction(fileNameWithoutExtension.toCharArray()));
113     }
114     return null;
115 }
116
117 public IPath getProjectRelativePath() {
118     return binaryFolder.getProjectRelativePath();
119 }
120
121 protected boolean isExcluded(IResource resource) {
122     return false;
123 }
124
125 public boolean isOutputFolder() {
126     return isOutputFolder;
127 }
128
129 public boolean isPackage(String JavaDoc qualifiedPackageName) {
130     return directoryList(qualifiedPackageName) != null;
131 }
132
133 public void reset() {
134     this.directoryCache = new SimpleLookupTable(5);
135 }
136
137 public String JavaDoc toString() {
138     String JavaDoc start = "Binary classpath directory " + this.binaryFolder.getFullPath().toString(); //$NON-NLS-1$
139
if (this.accessRuleSet == null)
140         return start;
141     return start + " with " + this.accessRuleSet; //$NON-NLS-1$
142
}
143
144 public String JavaDoc debugPathString() {
145     return this.binaryFolder.getFullPath().toString();
146 }
147
148
149 }
150
Popular Tags