KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > batch > ClasspathJar


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.batch;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.zip.ZipEntry JavaDoc;
19 import java.util.zip.ZipFile JavaDoc;
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 JavaDoc file;
31 protected ZipFile JavaDoc zipFile;
32 protected boolean closeZipFileAtEnd;
33 protected Hashtable JavaDoc packageCache;
34 protected char[] normalizedPath;
35
36 public ClasspathJar(File JavaDoc file, boolean closeZipFileAtEnd,
37         AccessRuleSet accessRuleSet, String JavaDoc destinationPath) {
38     super(accessRuleSet, destinationPath);
39     this.file = file;
40     this.closeZipFileAtEnd = closeZipFileAtEnd;
41 }
42 public NameEnvironmentAnswer findClass(char[] typeName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName) {
43     return findClass(typeName, qualifiedPackageName, qualifiedBinaryFileName, false);
44 }
45 public NameEnvironmentAnswer findClass(char[] typeName, String JavaDoc qualifiedPackageName, String JavaDoc qualifiedBinaryFileName, boolean asBinaryOnly) {
46     if (!isPackage(qualifiedPackageName))
47         return null; // most common case
48

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         // treat as if class file is missing
55
} catch (IOException JavaDoc e) {
56         // treat as if class file is missing
57
}
58     return null;
59 }
60 public char[][][] findTypeNames(String JavaDoc qualifiedPackageName) {
61     if (!isPackage(qualifiedPackageName))
62         return null; // most common case
63

64     ArrayList JavaDoc answers = new ArrayList JavaDoc();
65     nextEntry : for (Enumeration JavaDoc e = this.zipFile.entries(); e.hasMoreElements(); ) {
66         String JavaDoc fileName = ((ZipEntry JavaDoc) e.nextElement()).getName();
67
68         // add the package name & all of its parent packages
69
int last = fileName.lastIndexOf('/');
70         while (last > 0) {
71             // extract the package name
72
String JavaDoc packageName = fileName.substring(0, last);
73             if (!qualifiedPackageName.equals(packageName))
74                 continue nextEntry;
75             int indexOfDot = fileName.lastIndexOf('.');
76             if (indexOfDot != -1) {
77                 String JavaDoc 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 JavaDoc {
95     this.zipFile = new ZipFile JavaDoc(this.file);
96 }
97 public boolean isPackage(String JavaDoc qualifiedPackageName) {
98     if (this.packageCache != null)
99         return this.packageCache.containsKey(qualifiedPackageName);
100
101     this.packageCache = new Hashtable JavaDoc(41);
102     this.packageCache.put(Util.EMPTY_STRING, Util.EMPTY_STRING);
103
104     nextEntry : for (Enumeration JavaDoc e = this.zipFile.entries(); e.hasMoreElements(); ) {
105         String JavaDoc fileName = ((ZipEntry JavaDoc) e.nextElement()).getName();
106
107         // add the package name & all of its parent packages
108
int last = fileName.lastIndexOf('/');
109         while (last > 0) {
110             // extract the package name
111
String JavaDoc 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 JavaDoc e) {
125             // ignore
126
}
127         this.zipFile = null;
128     }
129     this.packageCache = null;
130 }
131 public String JavaDoc toString() {
132     return "Classpath for jar file " + this.file.getPath(); //$NON-NLS-1$
133
}
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 JavaDoc getPath(){
145     return this.file.getPath();
146 }
147 }
148
Popular Tags