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;
22 String[] knownFileNames;
25 NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName);
26 boolean isPackage(String qualifiedPackageName);
28 * This method resets the environment. The resulting state is equivalent to
29 * a new name environment without creating a new object.
34 classPathNames is a collection is Strings representing the full path of each class path
35 initialFileNames is a collection is Strings, the trailing '.java' will be removed if its not already.
38 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding) {
39 this(classpathNames, initialFileNames, encoding, null);
41 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding, int[] classpathDirectoryModes) {
42 int classpathSize = classpathNames.length;
43 this.classpaths = new Classpath[classpathSize];
44 String[] pathNames = new String[classpathSize];
45 int problemsOccured = 0;
46 for (int i = 0; i < classpathSize; i++) {
48 File file = new File(convertPathSeparators(classpathNames[i]));
49 if (file.isDirectory()) {
51 if (classpathDirectoryModes == null){
52 this.classpaths[i] = new ClasspathDirectory(file, encoding);
54 this.classpaths[i] = new ClasspathDirectory(file, encoding, classpathDirectoryModes[i]);
56 pathNames[i] = ((ClasspathDirectory) this.classpaths[i]).path;
59 String lowercaseClasspathName = classpathNames[i].toLowerCase();
60 // if (lowercaseClasspathName.endsWith(SUFFIX_STRING_jar)
61 // || lowercaseClasspathName.endsWith(SUFFIX_STRING_zip)) {
62 // this.classpaths[i] = this.getClasspathJar(file); // will throw an IOException if file does not exist
63 // pathNames[i] = classpathNames[i].substring(0, classpathNames[i].lastIndexOf('.'));
66 // } catch (IOException e) {
67 // this.classpaths[i] = null;
69 if (this.classpaths[i] == null)
72 if (problemsOccured > 0) {
73 Classpath[] newPaths = new Classpath[classpathSize - problemsOccured];
74 String[] newNames = new String[classpathSize - problemsOccured];
75 for (int i = 0, current = 0; i < classpathSize; i++)
76 if (this.classpaths[i] != null) {
77 newPaths[current] = this.classpaths[i];
78 newNames[current++] = pathNames[i];
80 classpathSize = newPaths.length;
81 this.classpaths = newPaths;
85 this.knownFileNames = new String[initialFileNames.length];
86 for (int i = initialFileNames.length; --i >= 0;) {
87 String fileName = initialFileNames[i];
88 String matchingPathName = null;
89 if (fileName.lastIndexOf(".") != -1) //$NON-NLS-1$
90 fileName = fileName.substring(0, fileName.lastIndexOf('.')); // remove trailing ".java"
92 fileName = convertPathSeparators(fileName);
93 for (int j = 0; j < classpathSize; j++)
94 if (fileName.startsWith(pathNames[j]))
95 matchingPathName = pathNames[j];
96 if (matchingPathName == null)
97 this.knownFileNames[i] = fileName; // leave as is...
99 this.knownFileNames[i] = fileName.substring(matchingPathName.length());
102 public void cleanup() {
103 for (int i = 0, max = this.classpaths.length; i < max; i++)
104 this.classpaths[i].reset();
106 private String convertPathSeparators(String path) {
107 return File.separatorChar == '/'
108 ? path.replace('\\', '/')
109 : path.replace('/', '\\');
111 //private NameEnvironmentAnswer findClass(String qualifiedTypeName, char[] typeName){
112 // for (int i = 0, length = this.knownFileNames.length; i < length; i++)
113 // if (qualifiedTypeName.equals(this.knownFileNames[i]))
114 // return null; // looking for a file which we know was provided at the beginning of the compilation
116 // String qualifiedBinaryFileName = qualifiedTypeName + SUFFIX_STRING_class;
117 // String qualifiedPackageName =
118 // qualifiedTypeName.length() == typeName.length
119 // ? "" //$NON-NLS-1$
120 // : qualifiedBinaryFileName.substring(0, qualifiedTypeName.length() - typeName.length - 1);
121 // String qp2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
122 // if (qualifiedPackageName == qp2) {
123 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
124 // NameEnvironmentAnswer answer = this.classpaths[i].findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName);
125 // if (answer != null) return answer;
128 // String qb2 = qualifiedBinaryFileName.replace('/', File.separatorChar);
129 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
130 // Classpath p = this.classpaths[i];
131 // NameEnvironmentAnswer answer = (p instanceof ClasspathJar)
132 // ? p.findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName)
133 // : p.findClass(typeName, qp2, qb2);
134 // if (answer != null) return answer;
139 public NameEnvironmentAnswer findType(char[][] compoundName) {
140 // if (compoundName != null)
142 // new String(CharOperation.concatWith(compoundName, '/')),
143 // compoundName[compoundName.length - 1]);
146 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
147 // if (typeName != null)
149 // new String(CharOperation.concatWith(packageName, typeName, '/')),
153 //public ClasspathJar getClasspathJar(File file) throws IOException {
154 // return new ClasspathJar(new ZipFile(file), true);
156 public boolean isPackage(char[][] compoundName, char[] packageName) {
157 String qualifiedPackageName = new String(CharOperation.concatWith(compoundName, packageName, '/'));
158 String qp2 = File.separatorChar == '/' ? qualifiedPackageName : qualifiedPackageName.replace('/', File.separatorChar);
159 if (qualifiedPackageName == qp2) {
160 for (int i = 0, length = this.classpaths.length; i < length; i++)
161 if (this.classpaths[i].isPackage(qualifiedPackageName))
165 // for (int i = 0, length = this.classpaths.length; i < length; i++) {
166 // Classpath p = this.classpaths[i];
167 // if ((p instanceof ClasspathJar) ? p.isPackage(qualifiedPackageName) : p.isPackage(qp2))