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;
15 import net.sourceforge.phpdt.core.compiler.CharOperation;
16 import net.sourceforge.phpdt.internal.compiler.env.INameEnvironment;
17 import net.sourceforge.phpdt.internal.compiler.env.NameEnvironmentAnswer;
18 import net.sourceforge.phpdt.internal.compiler.util.SuffixConstants;
20 public class FileSystem implements INameEnvironment, SuffixConstants {
21 Classpath[] classpaths;
23 String[] knownFileNames;
26 NameEnvironmentAnswer findClass(char[] typeName,
27 String qualifiedPackageName, String qualifiedBinaryFileName);
29 boolean isPackage(String qualifiedPackageName);
32 * This method resets the environment. The resulting state is equivalent
33 * to a new name environment without creating a new object.
39 * classPathNames is a collection is Strings representing the full path of
40 * each class path initialFileNames is a collection is Strings, the trailing
41 * '.java' will be removed if its not already.
44 public FileSystem(String[] classpathNames, String[] initialFileNames,
46 this(classpathNames, initialFileNames, encoding, null);
49 public FileSystem(String[] classpathNames, String[] initialFileNames,
50 String encoding, int[] classpathDirectoryModes) {
51 int classpathSize = classpathNames.length;
52 this.classpaths = new Classpath[classpathSize];
53 String[] pathNames = new String[classpathSize];
54 int problemsOccured = 0;
55 for (int i = 0; i < classpathSize; i++) {
57 File file = new File(convertPathSeparators(classpathNames[i]));
58 if (file.isDirectory()) {
60 if (classpathDirectoryModes == null) {
61 this.classpaths[i] = new ClasspathDirectory(file,
64 this.classpaths[i] = new ClasspathDirectory(file,
65 encoding, classpathDirectoryModes[i]);
67 pathNames[i] = ((ClasspathDirectory) this.classpaths[i]).path;
70 //String lowercaseClasspathName = classpathNames[i].toLowerCase();
71 // if (lowercaseClasspathName.endsWith(SUFFIX_STRING_jar)
72 // || lowercaseClasspathName.endsWith(SUFFIX_STRING_zip)) {
73 // this.classpaths[i] = this.getClasspathJar(file); // will
74 // throw an IOException if file does not exist
75 // pathNames[i] = classpathNames[i].substring(0,
76 // classpathNames[i].lastIndexOf('.'));
79 // } catch (IOException e) {
80 // this.classpaths[i] = null;
82 if (this.classpaths[i] == null)
85 if (problemsOccured > 0) {
86 Classpath[] newPaths = new Classpath[classpathSize
88 String[] newNames = new String[classpathSize - problemsOccured];
89 for (int i = 0, current = 0; i < classpathSize; i++)
90 if (this.classpaths[i] != null) {
91 newPaths[current] = this.classpaths[i];
92 newNames[current++] = pathNames[i];
94 classpathSize = newPaths.length;
95 this.classpaths = newPaths;
99 this.knownFileNames = new String[initialFileNames.length];
100 for (int i = initialFileNames.length; --i >= 0;) {
101 String fileName = initialFileNames[i];
102 String matchingPathName = null;
103 if (fileName.lastIndexOf(".") != -1) //$NON-NLS-1$
104 fileName = fileName.substring(0, fileName.lastIndexOf('.')); // remove
108 fileName = convertPathSeparators(fileName);
109 for (int j = 0; j < classpathSize; j++)
110 if (fileName.startsWith(pathNames[j]))
111 matchingPathName = pathNames[j];
112 if (matchingPathName == null)
113 this.knownFileNames[i] = fileName; // leave as is...
115 this.knownFileNames[i] = fileName.substring(matchingPathName
120 public void cleanup() {
121 for (int i = 0, max = this.classpaths.length; i < max; i++)
122 this.classpaths[i].reset();
125 private String convertPathSeparators(String path) {
126 return File.separatorChar == '/' ? path.replace('\\', '/') : path
130 // private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[]
132 // for (int i = 0, length = this.knownFileNames.length; i < length; i++)
133 // if (qualifiedTypeName.equals(this.knownFileNames[i]))
134 // return null; // looking for a file which we know was provided at the
135 // beginning of the compilation
137 // String qualifiedBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class;
138 // String qualifiedPackageName =
139 // qualifiedTypeName.length() == typeName.length
140 // ? "" //$NON-NLS-1$
141 // : qualifiedBinaryFileName.substring(0, qualifiedTypeName.length() -
142 // typeName.length - 1);
143 // String qp2 = File.separatorChar == '/' ? qualifiedPackageName :
144 // qualifiedPackageName.replace('/', File.separatorChar);
145 // if (qualifiedPackageName == qp2) {
146 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
147 // NameEnvironmentAnswer answer = this.classpaths[i].findClass(typeName,
148 // qualifiedPackageName, qualifiedBinaryFileName);
149 // if (answer != null) return answer;
152 // String qb2 = qualifiedBinaryFileName.replace('/', File.separatorChar);
153 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
154 // Classpath p = this.classpaths[i];
155 // NameEnvironmentAnswer answer = (p instanceof ClasspathJar)
156 // ? p.findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName)
157 // : p.findClass(typeName, qp2, qb2);
158 // if (answer != null) return answer;
163 public NameEnvironmentAnswer findType(char[][] compoundName) {
164 // if (compoundName != null)
166 // new String(CharOperation.concatWith(compoundName, '/')),
167 // compoundName[compoundName.length - 1]);
171 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
172 // if (typeName != null)
174 // new String(CharOperation.concatWith(packageName, typeName, '/')),
179 // public ClasspathJar getClasspathJar(File file) throws IOException {
180 // return new ClasspathJar(new ZipFile(file), true);
182 public boolean isPackage(char[][] compoundName, char[] packageName) {
183 String qualifiedPackageName = new String(CharOperation.concatWith(
184 compoundName, packageName, '/'));
185 String qp2 = File.separatorChar == '/' ? qualifiedPackageName
186 : qualifiedPackageName.replace('/', File.separatorChar);
187 if (qualifiedPackageName == qp2) {
188 for (int i = 0, length = this.classpaths.length; i < length; i++)
189 if (this.classpaths[i].isPackage(qualifiedPackageName))
193 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
194 // Classpath p = this.classpaths[i];
195 // if ((p instanceof ClasspathJar) ? p.isPackage(qualifiedPackageName) :