d99d1210e30a8d64fcd1613b8211567446c2e5dd
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / builder / ClasspathDirectory.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core.builder;
12
13 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
14 import net.sourceforge.phpdt.internal.core.util.SimpleLookupTable;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20
21 class ClasspathDirectory extends ClasspathLocation {
22
23         IContainer binaryFolder; // includes .class files for a single directory
24
25         boolean isOutputFolder;
26
27         String binaryLocation;
28
29         SimpleLookupTable directoryCache;
30
31         String[] missingPackageHolder = new String[1];
32
33         ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder) {
34                 this.binaryFolder = binaryFolder;
35                 this.isOutputFolder = isOutputFolder;
36                 // IPath location = binaryFolder.getLocation();
37                 // this.binaryLocation = location != null ?
38                 // location.addTrailingSeparator().toString() : ""; //$NON-NLS-1$
39                 this.binaryLocation = "";
40                 this.directoryCache = new SimpleLookupTable(5);
41         }
42
43         public void cleanup() {
44                 this.directoryCache = null;
45         }
46
47         String[] directoryList(String qualifiedPackageName) {
48                 String[] dirList = (String[]) directoryCache.get(qualifiedPackageName);
49                 if (dirList == missingPackageHolder)
50                         return null; // package exists in another classpath directory or
51                                                         // jar
52                 if (dirList != null)
53                         return dirList;
54
55                 try {
56                         IResource container = binaryFolder.findMember(qualifiedPackageName); // this
57                                                                                                                                                                         // is a
58                                                                                                                                                                         // case-sensitive
59                                                                                                                                                                         // check
60                         if (container instanceof IContainer) {
61                                 IResource[] members = ((IContainer) container).members();
62                                 dirList = new String[members.length];
63                                 int index = 0;
64                                 for (int i = 0, l = members.length; i < l; i++) {
65                                         IResource m = members[i];
66                                         // if (m.getType() == IResource.FILE &&
67                                         // ProjectPrefUtil.isClassFileName(m.getName()))
68                                         // // add exclusion pattern check here if we want to hide
69                                         // .class files
70                                         // dirList[index++] = m.getName();
71                                 }
72                                 if (index < dirList.length)
73                                         System.arraycopy(dirList, 0, dirList = new String[index],
74                                                         0, index);
75                                 directoryCache.put(qualifiedPackageName, dirList);
76                                 return dirList;
77                         }
78                 } catch (CoreException ignored) {
79                 }
80                 directoryCache.put(qualifiedPackageName, missingPackageHolder);
81                 return null;
82         }
83
84         boolean doesFileExist(String fileName, String qualifiedPackageName,
85                         String qualifiedFullName) {
86                 String[] dirList = directoryList(qualifiedPackageName);
87                 if (dirList == null)
88                         return false; // most common case
89
90                 for (int i = dirList.length; --i >= 0;)
91                         if (fileName.equals(dirList[i]))
92                                 return true;
93                 return false;
94         }
95
96         public boolean equals(Object o) {
97                 if (this == o)
98                         return true;
99                 if (!(o instanceof ClasspathDirectory))
100                         return false;
101
102                 return binaryFolder.equals(((ClasspathDirectory) o).binaryFolder);
103         }
104
105         public NameEnvironmentAnswer findClass(String binaryFileName,
106                         String qualifiedPackageName, String qualifiedBinaryFileName) {
107                 if (!doesFileExist(binaryFileName, qualifiedPackageName,
108                                 qualifiedBinaryFileName))
109                         return null; // most common case
110
111                 // try {
112                 // ClassFileReader reader = ClassFileReader.read(binaryLocation +
113                 // qualifiedBinaryFileName);
114                 // if (reader != null) return new NameEnvironmentAnswer(reader);
115                 // } catch (Exception e) {} // treat as if class file is missing
116                 return null;
117         }
118
119         public IPath getProjectRelativePath() {
120                 return binaryFolder.getProjectRelativePath();
121         }
122
123         public boolean isOutputFolder() {
124                 return isOutputFolder;
125         }
126
127         public boolean isPackage(String qualifiedPackageName) {
128                 return directoryList(qualifiedPackageName) != null;
129         }
130
131         public void reset() {
132                 this.directoryCache = new SimpleLookupTable(5);
133         }
134
135         public String toString() {
136                 return "Binary classpath directory " + binaryFolder.getFullPath().toString(); //$NON-NLS-1$
137         }
138 }