1 11 package org.eclipse.jdt.internal.compiler.batch; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipFile ; 20 21 import org.eclipse.jdt.core.compiler.CharOperation; 22 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader; 23 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException; 24 import org.eclipse.jdt.internal.compiler.env.AccessRuleSet; 25 import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer; 26 import org.eclipse.jdt.internal.compiler.util.Util; 27 28 public class ClasspathJar extends ClasspathLocation { 29 30 protected File file; 31 protected ZipFile zipFile; 32 protected boolean closeZipFileAtEnd; 33 protected Hashtable packageCache; 34 protected char[] normalizedPath; 35 36 public ClasspathJar(File file, boolean closeZipFileAtEnd, 37 AccessRuleSet accessRuleSet, String destinationPath) { 38 super(accessRuleSet, destinationPath); 39 this.file = file; 40 this.closeZipFileAtEnd = closeZipFileAtEnd; 41 } 42 public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName) { 43 return findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, false); 44 } 45 public NameEnvironmentAnswer findClass(char[] typeName, String qualifiedPackageName, String qualifiedBinaryFileName, boolean asBinaryOnly) { 46 if (!isPackage(qualifiedPackageName)) 47 return null; 49 try { 50 ClassFileReader reader = ClassFileReader.read(this.zipFile, qualifiedBinaryFileName); 51 if (reader != null) 52 return new NameEnvironmentAnswer(reader, fetchAccessRestriction(qualifiedBinaryFileName)); 53 } catch(ClassFormatException e) { 54 } catch (IOException e) { 56 } 58 return null; 59 } 60 public char[][][] findTypeNames(String qualifiedPackageName) { 61 if (!isPackage(qualifiedPackageName)) 62 return null; 64 ArrayList answers = new ArrayList (); 65 nextEntry : for (Enumeration e = this.zipFile.entries(); e.hasMoreElements(); ) { 66 String fileName = ((ZipEntry ) e.nextElement()).getName(); 67 68 int last = fileName.lastIndexOf('/'); 70 while (last > 0) { 71 String packageName = fileName.substring(0, last); 73 if (!qualifiedPackageName.equals(packageName)) 74 continue nextEntry; 75 int indexOfDot = fileName.lastIndexOf('.'); 76 if (indexOfDot != -1) { 77 String typeName = fileName.substring(last + 1, indexOfDot); 78 char[] packageArray = packageName.toCharArray(); 79 answers.add( 80 CharOperation.arrayConcat( 81 CharOperation.splitOn('/', packageArray), 82 typeName.toCharArray())); 83 } 84 } 85 } 86 int size = answers.size(); 87 if (size != 0) { 88 char[][][] result = new char[size][][]; 89 answers.toArray(result); 90 return null; 91 } 92 return null; 93 } 94 public void initialize() throws IOException { 95 this.zipFile = new ZipFile (this.file); 96 } 97 public boolean isPackage(String qualifiedPackageName) { 98 if (this.packageCache != null) 99 return this.packageCache.containsKey(qualifiedPackageName); 100 101 this.packageCache = new Hashtable (41); 102 this.packageCache.put(Util.EMPTY_STRING, Util.EMPTY_STRING); 103 104 nextEntry : for (Enumeration e = this.zipFile.entries(); e.hasMoreElements(); ) { 105 String fileName = ((ZipEntry ) e.nextElement()).getName(); 106 107 int last = fileName.lastIndexOf('/'); 109 while (last > 0) { 110 String packageName = fileName.substring(0, last); 112 if (this.packageCache.containsKey(packageName)) 113 continue nextEntry; 114 this.packageCache.put(packageName, packageName); 115 last = packageName.lastIndexOf('/'); 116 } 117 } 118 return this.packageCache.containsKey(qualifiedPackageName); 119 } 120 public void reset() { 121 if (this.zipFile != null && this.closeZipFileAtEnd) { 122 try { 123 this.zipFile.close(); 124 } catch(IOException e) { 125 } 127 this.zipFile = null; 128 } 129 this.packageCache = null; 130 } 131 public String toString() { 132 return "Classpath for jar file " + this.file.getPath(); } 134 public char[] normalizedPath() { 135 if (this.normalizedPath == null) { 136 char[] rawName = this.file.getPath().toCharArray(); 137 if (File.separatorChar == '\\') { 138 CharOperation.replace(rawName, '\\', '/'); 139 } 140 this.normalizedPath = CharOperation.subarray(rawName, 0, CharOperation.lastIndexOf('.', rawName)); 141 } 142 return this.normalizedPath; 143 } 144 public String getPath(){ 145 return this.file.getPath(); 146 } 147 } 148 | Popular Tags |