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