KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Walter Harley - Patch for ensuring the parent folders are created
11  *******************************************************************************/

12
13 package org.eclipse.jdt.internal.compiler.apt.util;
14
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.FileReader JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.nio.charset.Charset JavaDoc;
27
28 import javax.lang.model.element.Modifier;
29 import javax.lang.model.element.NestingKind;
30 import javax.tools.SimpleJavaFileObject;
31
32 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
33 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
34 import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
35
36 /**
37  * Implementation of a Java file object that corresponds to a file on the file system
38  */

39 public class EclipseFileObject extends SimpleJavaFileObject {
40     private File JavaDoc f;
41     private Charset JavaDoc charset;
42     private boolean parentsExist; // parent directories exist
43

44     public EclipseFileObject(String JavaDoc className, URI JavaDoc uri, Kind kind, Charset JavaDoc charset) {
45         super(uri, kind);
46         this.f = new File JavaDoc(this.uri);
47         this.charset = charset;
48         this.parentsExist = false;
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.f);
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#getNestingKind()
85      */

86     public NestingKind getNestingKind() {
87         switch(kind) {
88             case SOURCE :
89                 return NestingKind.TOP_LEVEL;
90             case CLASS :
91                 ClassFileReader reader = null;
92                 try {
93                     reader = ClassFileReader.read(this.f);
94                 } catch (ClassFormatException e) {
95                     // ignore
96
} catch (IOException JavaDoc e) {
97                     // ignore
98
}
99                 if (reader == null) {
100                     return null;
101                 }
102                 if (reader.isAnonymous()) {
103                     return NestingKind.ANONYMOUS;
104                 }
105                 if (reader.isLocal()) {
106                     return NestingKind.LOCAL;
107                 }
108                 if (reader.isMember()) {
109                     return NestingKind.MEMBER;
110                 }
111                 return NestingKind.TOP_LEVEL;
112             default:
113                 return null;
114         }
115     }
116
117     /* (non-Javadoc)
118      * @see javax.tools.FileObject#delete()
119      */

120     public boolean delete() {
121         return this.f.delete();
122     }
123     
124     public boolean equals(Object JavaDoc o) {
125         if (!(o instanceof EclipseFileObject)) {
126             return false;
127         }
128         EclipseFileObject eclipseFileObject = (EclipseFileObject) o;
129         return eclipseFileObject.toUri().equals(this.uri);
130     }
131
132     /* (non-Javadoc)
133      * @see javax.tools.FileObject#getCharContent(boolean)
134      */

135     public CharSequence JavaDoc getCharContent(boolean ignoreEncodingErrors) throws IOException JavaDoc {
136         return Util.getCharContents(this, ignoreEncodingErrors, org.eclipse.jdt.internal.compiler.util.Util.getFileByteContent(this.f), this.charset.toString());
137     }
138
139     /* (non-Javadoc)
140      * @see javax.tools.FileObject#getLastModified()
141      */

142     public long getLastModified() {
143         return this.f.lastModified();
144     }
145
146     public String JavaDoc getName() {
147         return this.f.getPath();
148     }
149     
150     public int hashCode() {
151         return f.hashCode();
152     }
153
154     /* (non-Javadoc)
155      * @see javax.tools.FileObject#openInputStream()
156      */

157     public InputStream JavaDoc openInputStream() throws IOException JavaDoc {
158         // TODO (olivier) should be used buffered input stream
159
return new FileInputStream JavaDoc(this.f);
160     }
161
162     /* (non-Javadoc)
163      * @see javax.tools.FileObject#openOutputStream()
164      */

165     public OutputStream JavaDoc openOutputStream() throws IOException JavaDoc {
166         ensureParentDirectoriesExist();
167         return new FileOutputStream JavaDoc(this.f);
168     }
169
170     /* (non-Javadoc)
171      * @see javax.tools.FileObject#openReader(boolean)
172      */

173     public Reader JavaDoc openReader(boolean ignoreEncodingErrors) throws IOException JavaDoc {
174         return new FileReader JavaDoc(this.f);
175     }
176
177     /* (non-Javadoc)
178      * @see javax.tools.FileObject#openWriter()
179      */

180     public Writer JavaDoc openWriter() throws IOException JavaDoc {
181         ensureParentDirectoriesExist();
182         return new FileWriter JavaDoc(this.f);
183     }
184     
185     @Override JavaDoc
186     public String JavaDoc toString() {
187         return this.f.getAbsolutePath();
188     }
189     
190     private void ensureParentDirectoriesExist() throws IOException JavaDoc {
191         if (!this.parentsExist) {
192             File JavaDoc parent = f.getParentFile();
193             if (parent != null && !parent.exists()) {
194                 if (!parent.mkdirs()) {
195                     // could have been concurrently created
196
if (!parent.exists() || !parent.isDirectory())
197                         throw new IOException JavaDoc("Unable to create parent directories for " + f); //$NON-NLS-1$
198
}
199             }
200             this.parentsExist = true;
201         }
202     }
203
204
205 }
206
Popular Tags