86f7d355e4c37e04ab8abef3ab057301d3ed7a81
[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;
15 import net.sourceforge.phpdt.internal.core.util.SimpleLookupTable;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21
22 class ClasspathDirectory extends ClasspathLocation {
23
24 IContainer binaryFolder; // includes .class files for a single directory
25 boolean isOutputFolder;
26 String binaryLocation;
27 SimpleLookupTable directoryCache;
28 String[] missingPackageHolder = new String[1];
29
30 ClasspathDirectory(IContainer binaryFolder, boolean isOutputFolder) {
31         this.binaryFolder = binaryFolder;
32         this.isOutputFolder = isOutputFolder;
33         IPath location = binaryFolder.getLocation();
34         this.binaryLocation = location != null ? location.addTrailingSeparator().toString() : ""; //$NON-NLS-1$
35
36         this.directoryCache = new SimpleLookupTable(5);
37 }
38
39 public void cleanup() {
40         this.directoryCache = null;
41 }
42
43 String[] directoryList(String qualifiedPackageName) {
44         String[] dirList = (String[]) directoryCache.get(qualifiedPackageName);
45         if (dirList == missingPackageHolder) return null; // package exists in another classpath directory or jar
46         if (dirList != null) return dirList;
47
48         try {
49                 IResource container = binaryFolder.findMember(qualifiedPackageName); // this is a case-sensitive check
50                 if (container instanceof IContainer) {
51                         IResource[] members = ((IContainer) container).members();
52                         dirList = new String[members.length];
53                         int index = 0;
54                         for (int i = 0, l = members.length; i < l; i++) {
55                                 IResource m = members[i];
56 //                              if (m.getType() == IResource.FILE && Util.isClassFileName(m.getName()))
57 //                                      // add exclusion pattern check here if we want to hide .class files
58 //                                      dirList[index++] = m.getName();
59                         }
60                         if (index < dirList.length)
61                                 System.arraycopy(dirList, 0, dirList = new String[index], 0, index);
62                         directoryCache.put(qualifiedPackageName, dirList);
63                         return dirList;
64                 }
65         } catch(CoreException ignored) {
66         }
67         directoryCache.put(qualifiedPackageName, missingPackageHolder);
68         return null;
69 }
70
71 boolean doesFileExist(String fileName, String qualifiedPackageName, String qualifiedFullName) {
72         String[] dirList = directoryList(qualifiedPackageName);
73         if (dirList == null) return false; // most common case
74
75         for (int i = dirList.length; --i >= 0;)
76                 if (fileName.equals(dirList[i]))
77                         return true;
78         return false;
79 }
80
81 public boolean equals(Object o) {
82         if (this == o) return true;
83         if (!(o instanceof ClasspathDirectory)) return false;
84
85         return binaryFolder.equals(((ClasspathDirectory) o).binaryFolder);
86
87
88 public NameEnvironmentAnswer findClass(String binaryFileName, String qualifiedPackageName, String qualifiedBinaryFileName) {
89         if (!doesFileExist(binaryFileName, qualifiedPackageName, qualifiedBinaryFileName)) return null; // most common case
90
91 //      try {
92 //              ClassFileReader reader = ClassFileReader.read(binaryLocation + qualifiedBinaryFileName);
93 //              if (reader != null) return new NameEnvironmentAnswer(reader);
94 //      } catch (Exception e) {} // treat as if class file is missing
95         return null;
96 }
97
98 public IPath getProjectRelativePath() {
99         return binaryFolder.getProjectRelativePath();
100 }
101
102 public boolean isOutputFolder() {
103         return isOutputFolder;
104 }
105
106 public boolean isPackage(String qualifiedPackageName) {
107         return directoryList(qualifiedPackageName) != null;
108 }
109
110 public void reset() {
111         this.directoryCache = new SimpleLookupTable(5);
112 }
113
114 public String toString() {
115         return "Binary classpath directory " + binaryFolder.getFullPath().toString(); //$NON-NLS-1$
116 }
117 }