KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > util > ArchiveFileObject


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.apt.util;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.io.Reader JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.net.URI JavaDoc;
20 import java.net.URISyntaxException JavaDoc;
21 import java.nio.charset.Charset JavaDoc;
22 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
24
25 import javax.lang.model.element.Modifier;
26 import javax.lang.model.element.NestingKind;
27 import javax.tools.JavaFileObject;
28
29 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
30 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
31 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
32
33 /**
34  * Implementation of a Java file object that corresponds to an entry in a zip/jar file
35  */

36 public class ArchiveFileObject implements JavaFileObject {
37     private ZipEntry JavaDoc zipEntry;
38     private ZipFile JavaDoc zipFile;
39     private String JavaDoc entryName;
40     private File JavaDoc file;
41     private Charset JavaDoc charset;
42     
43     public ArchiveFileObject(File JavaDoc file, ZipFile JavaDoc zipFile, String JavaDoc entryName, Charset JavaDoc charset) {
44         this.zipFile = zipFile;
45         this.zipEntry = zipFile.getEntry(entryName);
46         this.entryName = entryName;
47         this.file = file;
48         this.charset = charset;
49     }
50
51     /* (non-Javadoc)
52      * @see javax.tools.JavaFileObject#getAccessLevel()
53      */

54     public Modifier getAccessLevel() {
55         // cannot express multiple modifier
56
if (getKind() != Kind.CLASS) {
57             return null;
58         }
59         ClassFileReader reader = null;
60         try {
61             reader = ClassFileReader.read(this.zipFile, this.entryName);
62         } catch (ClassFormatException e) {
63             // ignore
64
} catch (IOException JavaDoc e) {
65             // ignore
66
}
67         if (reader == null) {
68             return null;
69         }
70         final int accessFlags = reader.accessFlags();
71         if ((accessFlags & ClassFileConstants.AccPublic) != 0) {
72             return Modifier.PUBLIC;
73         }
74         if ((accessFlags & ClassFileConstants.AccAbstract) != 0) {
75             return Modifier.ABSTRACT;
76         }
77         if ((accessFlags & ClassFileConstants.AccFinal) != 0) {
78             return Modifier.FINAL;
79         }
80         return null;
81     }
82
83     /* (non-Javadoc)
84      * @see javax.tools.JavaFileObject#getKind()
85      */

86     public Kind getKind() {
87         String JavaDoc name = this.entryName.toLowerCase();
88         if (name.endsWith(Kind.CLASS.extension)) {
89             return Kind.CLASS;
90         } else if (name.endsWith(Kind.SOURCE.extension)) {
91             return Kind.SOURCE;
92         } else if (name.endsWith(Kind.HTML.extension)) {
93             return Kind.HTML;
94         }
95         return Kind.OTHER;
96     }
97
98     /* (non-Javadoc)
99      * @see javax.tools.JavaFileObject#getNestingKind()
100      */

101     public NestingKind getNestingKind() {
102         switch(getKind()) {
103             case SOURCE :
104                 return NestingKind.TOP_LEVEL;
105             case CLASS :
106                 ClassFileReader reader = null;
107                 try {
108                     reader = ClassFileReader.read(this.zipFile, this.entryName);
109                 } catch (ClassFormatException e) {
110                     // ignore
111
} catch (IOException JavaDoc e) {
112                     // ignore
113
}
114                 if (reader == null) {
115                     return null;
116                 }
117                 if (reader.isAnonymous()) {
118                     return NestingKind.ANONYMOUS;
119                 }
120                 if (reader.isLocal()) {
121                     return NestingKind.LOCAL;
122                 }
123                 if (reader.isMember()) {
124                     return NestingKind.MEMBER;
125                 }
126                 return NestingKind.TOP_LEVEL;
127             default:
128                 return null;
129         }
130     }
131
132     /* (non-Javadoc)
133      * @see javax.tools.JavaFileObject#isNameCompatible(java.lang.String, javax.tools.JavaFileObject.Kind)
134      */

135     public boolean isNameCompatible(String JavaDoc simpleName, Kind kind) {
136         return this.zipEntry.getName().endsWith(simpleName + kind.extension);
137     }
138
139     /* (non-Javadoc)
140      * @see javax.tools.FileObject#delete()
141      */

142     public boolean delete() {
143         throw new UnsupportedOperationException JavaDoc();
144     }
145
146     public boolean equals(Object JavaDoc o) {
147         if (!(o instanceof ArchiveFileObject)) {
148             return false;
149         }
150         ArchiveFileObject archiveFileObject = (ArchiveFileObject) o;
151         return archiveFileObject.toUri().equals(this.toUri());
152     }
153
154     /* (non-Javadoc)
155      * @see javax.tools.FileObject#getCharContent(boolean)
156      */

157     public CharSequence JavaDoc getCharContent(boolean ignoreEncodingErrors) throws IOException JavaDoc {
158         if (getKind() == Kind.SOURCE) {
159             return Util.getCharContents(this, ignoreEncodingErrors, org.eclipse.jdt.internal.compiler.util.Util.getZipEntryByteContent(this.zipEntry, this.zipFile), this.charset.toString());
160         }
161         return null;
162     }
163
164     /* (non-Javadoc)
165      * @see javax.tools.FileObject#getLastModified()
166      */

167     public long getLastModified() {
168         return this.zipEntry.getTime(); // looks the closest from the last modification
169
}
170
171     /* (non-Javadoc)
172      * @see javax.tools.FileObject#getName()
173      */

174     public String JavaDoc getName() {
175         return this.zipEntry.getName();
176     }
177
178     /* (non-Javadoc)
179      * @see javax.tools.FileObject#openInputStream()
180      */

181     public InputStream JavaDoc openInputStream() throws IOException JavaDoc {
182         return this.zipFile.getInputStream(this.zipEntry);
183     }
184
185     /* (non-Javadoc)
186      * @see javax.tools.FileObject#openOutputStream()
187      */

188     public OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
189         throw new UnsupportedOperationException JavaDoc();
190     }
191
192     /* (non-Javadoc)
193      * @see javax.tools.FileObject#openReader(boolean)
194      */

195     public Reader JavaDoc openReader(boolean ignoreEncodingErrors) throws IOException JavaDoc {
196         throw new UnsupportedOperationException JavaDoc();
197     }
198
199     /* (non-Javadoc)
200      * @see javax.tools.FileObject#openWriter()
201      */

202     public Writer JavaDoc openWriter() throws IOException JavaDoc {
203         throw new UnsupportedOperationException JavaDoc();
204     }
205
206     /* (non-Javadoc)
207      * @see javax.tools.FileObject#toUri()
208      */

209     public URI JavaDoc toUri() {
210         try {
211             return new URI JavaDoc("jar:" + this.file.toURI().getPath() + "!" + this.zipEntry.getName()); //$NON-NLS-1$//$NON-NLS-2$
212
} catch (URISyntaxException JavaDoc e) {
213             return null;
214         }
215     }
216     
217
218     @Override JavaDoc
219     public String JavaDoc toString() {
220         return this.file.getAbsolutePath() + "[" + this.zipEntry.getName() + "]";//$NON-NLS-1$//$NON-NLS-2$
221
}
222 }
223
Popular Tags