001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFile;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IPath;
017: import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
018: import org.eclipse.jdt.internal.compiler.util.SimpleLookupTable;
019: import org.eclipse.jdt.internal.core.builder.ClasspathLocation;
020: import org.eclipse.jdt.internal.core.util.ResourceCompilationUnit;
021: import org.eclipse.jdt.internal.core.util.Util;
022:
023: public class ClasspathSourceDirectory extends ClasspathLocation {
024:
025: IContainer sourceFolder;
026: SimpleLookupTable directoryCache;
027: SimpleLookupTable missingPackageHolder = new SimpleLookupTable();
028: char[][] fullExclusionPatternChars;
029: char[][] fulInclusionPatternChars;
030:
031: ClasspathSourceDirectory(IContainer sourceFolder,
032: char[][] fullExclusionPatternChars,
033: char[][] fulInclusionPatternChars) {
034: this .sourceFolder = sourceFolder;
035: this .directoryCache = new SimpleLookupTable(5);
036: this .fullExclusionPatternChars = fullExclusionPatternChars;
037: this .fulInclusionPatternChars = fulInclusionPatternChars;
038: }
039:
040: public void cleanup() {
041: this .directoryCache = null;
042: }
043:
044: SimpleLookupTable directoryTable(String qualifiedPackageName) {
045: SimpleLookupTable dirTable = (SimpleLookupTable) directoryCache
046: .get(qualifiedPackageName);
047: if (dirTable == missingPackageHolder)
048: return null; // package exists in another classpath directory or jar
049: if (dirTable != null)
050: return dirTable;
051:
052: try {
053: IResource container = sourceFolder
054: .findMember(qualifiedPackageName); // this is a case-sensitive check
055: if (container instanceof IContainer) {
056: IResource[] members = ((IContainer) container)
057: .members();
058: dirTable = new SimpleLookupTable();
059: for (int i = 0, l = members.length; i < l; i++) {
060: IResource m = members[i];
061: String name;
062: if (m.getType() == IResource.FILE) {
063: int index = Util
064: .indexOfJavaLikeExtension(name = m
065: .getName());
066: if (index >= 0) {
067: String fullPath = m.getFullPath()
068: .toString();
069: if (!org.eclipse.jdt.internal.compiler.util.Util
070: .isExcluded(
071: fullPath.toCharArray(),
072: this .fulInclusionPatternChars,
073: this .fullExclusionPatternChars,
074: false/*not a folder path*/)) {
075: dirTable.put(name.substring(0, index),
076: m);
077: }
078: }
079: }
080: }
081: directoryCache.put(qualifiedPackageName, dirTable);
082: return dirTable;
083: }
084: } catch (CoreException ignored) {
085: // treat as if missing
086: }
087: directoryCache.put(qualifiedPackageName, missingPackageHolder);
088: return null;
089: }
090:
091: public boolean equals(Object o) {
092: if (this == o)
093: return true;
094: if (!(o instanceof ClasspathSourceDirectory))
095: return false;
096:
097: return sourceFolder
098: .equals(((ClasspathSourceDirectory) o).sourceFolder);
099: }
100:
101: public NameEnvironmentAnswer findClass(
102: String sourceFileWithoutExtension,
103: String qualifiedPackageName,
104: String qualifiedSourceFileWithoutExtension) {
105: SimpleLookupTable dirTable = directoryTable(qualifiedPackageName);
106: if (dirTable != null && dirTable.elementSize > 0) {
107: IFile file = (IFile) dirTable
108: .get(sourceFileWithoutExtension);
109: if (file != null) {
110: return new NameEnvironmentAnswer(
111: new ResourceCompilationUnit(file, file
112: .getLocationURI()), null /* no access restriction */);
113: }
114: }
115: return null;
116: }
117:
118: public IPath getProjectRelativePath() {
119: return sourceFolder.getProjectRelativePath();
120: }
121:
122: public boolean isPackage(String qualifiedPackageName) {
123: return directoryTable(qualifiedPackageName) != null;
124: }
125:
126: public void reset() {
127: this .directoryCache = new SimpleLookupTable(5);
128: }
129:
130: public String toString() {
131: return "Source classpath directory " + sourceFolder.getFullPath().toString(); //$NON-NLS-1$
132: }
133:
134: public String debugPathString() {
135: return this.sourceFolder.getFullPath().toString();
136: }
137:
138: }
|