*** empty log message ***
[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 import java.io.IOException;
15 import java.util.zip.ZipFile;
16
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;
21
22 public class FileSystem implements INameEnvironment, SuffixConstants {
23         Classpath[] classpaths;
24         String[] knownFileNames;
25
26         interface Classpath {
27                 NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName);
28                 boolean isPackage(String qualifiedPackageName); 
29                 /**
30                  * This method resets the environment. The resulting state is equivalent to
31                  * a new name environment without creating a new object.
32                  */
33                 void reset();
34         }
35 /*
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.
38 */
39
40 public FileSystem(String[] classpathNames, String[] initialFileNames, String encoding) {
41         this(classpathNames, initialFileNames, encoding, null);
42 }
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++) {
49 //              try {
50                         File file = new File(convertPathSeparators(classpathNames[i]));
51                         if (file.isDirectory()) {
52                                 if (file.exists()) {
53                                         if (classpathDirectoryModes == null){
54                                                 this.classpaths[i] = new ClasspathDirectory(file, encoding);
55                                         } else {
56                                                 this.classpaths[i] = new ClasspathDirectory(file, encoding, classpathDirectoryModes[i]);
57                                         }
58                                         pathNames[i] = ((ClasspathDirectory) this.classpaths[i]).path;
59                                 }
60                         } else {
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('.'));
66 //                              }
67                         }
68 //              } catch (IOException e) {
69 //                      this.classpaths[i] = null;
70 //              }
71                 if (this.classpaths[i] == null)
72                         problemsOccured++;
73         }
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];
81                         }
82                 classpathSize = newPaths.length;
83                 this.classpaths = newPaths;
84                 pathNames = newNames;
85         }
86
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"
93
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...
100                 else
101                         this.knownFileNames[i] = fileName.substring(matchingPathName.length());
102         }
103 }
104 public void cleanup() {
105         for (int i = 0, max = this.classpaths.length; i < max; i++)
106                 this.classpaths[i].reset();
107 }
108 private String convertPathSeparators(String path) {
109         return File.separatorChar == '/'
110                 ? path.replace('\\', '/')
111                  : path.replace('/', '\\');
112 }
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
117 //
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;
128 //              }
129 //      } else {
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;
137 //              }
138 //      }
139 //      return null;
140 //}
141 public NameEnvironmentAnswer findType(char[][] compoundName) {
142 //      if (compoundName != null)
143 //              return findClass(
144 //                      new String(CharOperation.concatWith(compoundName, '/')),
145 //                      compoundName[compoundName.length - 1]);
146         return null;
147 }
148 public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
149 //      if (typeName != null)
150 //              return findClass(
151 //                      new String(CharOperation.concatWith(packageName, typeName, '/')),
152 //                      typeName);
153         return null;
154 }
155 //public ClasspathJar getClasspathJar(File file) throws IOException {
156 //      return new ClasspathJar(new ZipFile(file), true);
157 //}
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))
164                                 return true;
165         } 
166 //      else {
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))
170 //                              return true;
171 //              }
172 //      }
173         return false;
174 }
175 }