Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / batch / FileSystem.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.batch;
12
13 import java.io.File;
14
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;
19
20 public class FileSystem implements INameEnvironment, SuffixConstants {
21         Classpath[] classpaths;
22         String[] knownFileNames;
23
24         interface Classpath {
25                 NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName);
26                 boolean isPackage(String qualifiedPackageName); 
27                 /**
28                  * This method resets the environment. The resulting state is equivalent to
29                  * a new name environment without creating a new object.
30                  */
31                 void reset();
32         }
33 /*
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.
36 */
37
38 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding) {
39         this(classpathNames, initialFileNames, encoding, null);
40 }
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++) {
47 //              try {
48                         File file = new File(convertPathSeparators(classpathNames[i]));
49                         if (file.isDirectory()) {
50                                 if (file.exists()) {
51                                         if (classpathDirectoryModes == null){
52                                                 this.classpaths[i] = new ClasspathDirectory(file, encoding);
53                                         } else {
54                                                 this.classpaths[i] = new ClasspathDirectory(file, encoding, classpathDirectoryModes[i]);
55                                         }
56                                         pathNames[i] = ((ClasspathDirectory) this.classpaths[i]).path;
57                                 }
58                         } else {
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('.'));
64 //                              }
65                         }
66 //              } catch (IOException e) {
67 //                      this.classpaths[i] = null;
68 //              }
69                 if (this.classpaths[i] == null)
70                         problemsOccured++;
71         }
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];
79                         }
80                 classpathSize = newPaths.length;
81                 this.classpaths = newPaths;
82                 pathNames = newNames;
83         }
84
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"
91
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...
98                 else
99                         this.knownFileNames[i] = fileName.substring(matchingPathName.length());
100         }
101 }
102 public void cleanup() {
103         for (int i = 0, max = this.classpaths.length; i < max; i++)
104                 this.classpaths[i].reset();
105 }
106 private String convertPathSeparators(String path) {
107         return File.separatorChar == '/'
108                 ? path.replace('\\', '/')
109                  : path.replace('/', '\\');
110 }
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
115 //
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;
126 //              }
127 //      } else {
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;
135 //              }
136 //      }
137 //      return null;
138 //}
139 public NameEnvironmentAnswer findType(char[][] compoundName) {
140 //      if (compoundName != null)
141 //              return findClass(
142 //                      new String(CharOperation.concatWith(compoundName, '/')),
143 //                      compoundName[compoundName.length - 1]);
144         return null;
145 }
146 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
147 //      if (typeName != null)
148 //              return findClass(
149 //                      new String(CharOperation.concatWith(packageName, typeName, '/')),
150 //                      typeName);
151         return null;
152 }
153 //public ClasspathJar getClasspathJar(File file) throws IOException {
154 //      return new ClasspathJar(new ZipFile(file), true);
155 //}
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))
162                                 return true;
163         } 
164 //      else {
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))
168 //                              return true;
169 //              }
170 //      }
171         return false;
172 }
173 }