1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.batch;
14 import java.io.IOException;
15 import java.util.zip.ZipFile;
17 import net.sourceforge.phpdt.core.compiler.CharOperation;
18 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
19 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
20 import net.sourceforge.phpdt.internal.compiler.util.SuffixConstants;
22 public class FileSystem implements INameEnvironment, SuffixConstants {
23 Classpath[] classpaths;
24 String[] knownFileNames;
27 NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName);
28 boolean isPackage(String qualifiedPackageName);
30 * This method resets the environment. The resulting state is equivalent to
31 * a new name environment without creating a new object.
36 classPathNames is a collection is Strings representing the full path of each class path
37 initialFileNames is a collection is Strings, the trailing '.java' will be removed if its not already.
40 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding) {
41 this(classpathNames, initialFileNames, encoding, null);
43 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding, int[] classpathDirectoryModes) {
44 int classpathSize = classpathNames.length;
45 this.classpaths = new Classpath[classpathSize];
46 String[] pathNames = new String[classpathSize];
47 int problemsOccured = 0;
48 for (int i = 0; i < classpathSize; i++) {
50 File file = new File(convertPathSeparators(classpathNames[i]));
51 if (file.isDirectory()) {
53 if (classpathDirectoryModes == null){
54 this.classpaths[i] = new ClasspathDirectory(file, encoding);
56 this.classpaths[i] = new ClasspathDirectory(file, encoding, classpathDirectoryModes[i]);
58 pathNames[i] = ((ClasspathDirectory) this.classpaths[i]).path;
61 String lowercaseClasspathName = classpathNames[i].toLowerCase();
62 // if (lowercaseClasspathName.endsWith(SUFFIX_STRING_jar)
63 // || lowercaseClasspathName.endsWith(SUFFIX_STRING_zip)) {
64 // this.classpaths[i] = this.getClasspathJar(file); // will throw an IOException if file does not exist
65 // pathNames[i] = classpathNames[i].substring(0, classpathNames[i].lastIndexOf('.'));
68 // } catch (IOException e) {
69 // this.classpaths[i] = null;
71 if (this.classpaths[i] == null)
74 if (problemsOccured > 0) {
75 Classpath[] newPaths = new Classpath[classpathSize - problemsOccured];
76 String[] newNames = new String[classpathSize - problemsOccured];
77 for (int i = 0, current = 0; i < classpathSize; i++)
78 if (this.classpaths[i] != null) {
79 newPaths[current] = this.classpaths[i];
80 newNames[current++] = pathNames[i];
82 classpathSize = newPaths.length;
83 this.classpaths = newPaths;
87 this.knownFileNames = new String[initialFileNames.length];
88 for (int i = initialFileNames.length; --i >= 0;) {
89 String fileName = initialFileNames[i];
90 String matchingPathName = null;
91 if (fileName.lastIndexOf(".") != -1) //$NON-NLS-1$
92 fileName = fileName.substring(0, fileName.lastIndexOf('.')); // remove trailing ".java"
94 fileName = convertPathSeparators(fileName);
95 for (int j = 0; j < classpathSize; j++)
96 if (fileName.startsWith(pathNames[j]))
97 matchingPathName = pathNames[j];
98 if (matchingPathName == null)
99 this.knownFileNames[i] = fileName; // leave as is...
101 this.knownFileNames[i] = fileName.substring(matchingPathName.length());
104 public void cleanup() {
105 for (int i = 0, max = this.classpaths.length; i < max; i++)
106 this.classpaths[i].reset();
108 private String convertPathSeparators(String path) {
109 return File.separatorChar == '/'
110 ? path.replace('\\', '/')
111 : path.replace('/', '\\');
113 //private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeName){
114 // for (int i = 0, length = this.knownFileNames.length; i < length; i++)
115 // if (qualifiedTypeName.equals(this.knownFileNames[i]))
116 // return null; // looking for a file which we know was provided at the beginning of the compilation
118 // String qualifiedBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class;
119 // String qualifiedPackageName =
120 // qualifiedTypeName.length() == typeName.length
121 // ? "" //$NON-NLS-1$
122 // : qualifiedBinaryFileName.substring(0, qualifiedTypeName.length() - typeName.length - 1);
123 // String qp2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
124 // if (qualifiedPackageName == qp2) {
125 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
126 // NameEnvironmentAnswer answer = this.classpaths[i].findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName);
127 // if (answer != null) return answer;
130 // String qb2 = qualifiedBinaryFileName.replace('/', File.separatorChar);
131 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
132 // Classpath p = this.classpaths[i];
133 // NameEnvironmentAnswer answer = (p instanceof ClasspathJar)
134 // ? p.findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName)
135 // : p.findClass(typeName, qp2, qb2);
136 // if (answer != null) return answer;
141 public NameEnvironmentAnswer findType(char[][] compoundName) {
142 // if (compoundName != null)
144 // new String(CharOperation.concatWith(compoundName, '/')),
145 // compoundName[compoundName.length - 1]);
148 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
149 // if (typeName != null)
151 // new String(CharOperation.concatWith(packageName, typeName, '/')),
155 //public ClasspathJar getClasspathJar(File file) throws IOException {
156 // return new ClasspathJar(new ZipFile(file), true);
158 public boolean isPackage(char[][] compoundName, char[] packageName) {
159 String qualifiedPackageName = new String(CharOperation.concatWith(compoundName, packageName, '/'));
160 String qp2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
161 if (qualifiedPackageName == qp2) {
162 for (int i = 0, length = this.classpaths.length; i < length; i++)
163 if (this.classpaths[i].isPackage(qualifiedPackageName))
167 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
168 // Classpath p = this.classpaths[i];
169 // if ((p instanceof ClasspathJar) ? p.isPackage(qualifiedPackageName) : p.isPackage(qp2))