KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > tools > SimpleJavaFileObject


1 /*
2  * @(#)SimpleJavaFileObject.java 1.13 06/06/25
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.tools;
9
10 import java.io.*;
11 import java.net.URI JavaDoc;
12 import java.net.URISyntaxException JavaDoc;
13 import java.nio.CharBuffer JavaDoc;
14 import javax.lang.model.element.Modifier;
15 import javax.lang.model.element.NestingKind;
16 import javax.tools.JavaFileObject.Kind;
17
18 /**
19  * Provides simple implementations for most methods in JavaFileObject.
20  * This class is designed to be subclassed and used as a basis for
21  * JavaFileObject implementations. Subclasses can override the
22  * implementation and specification of any method of this class as
23  * long as the general contract of JavaFileObject is obeyed.
24  *
25  * @author Peter von der Ahé
26  * @since 1.6
27  */

28 public class SimpleJavaFileObject implements JavaFileObject {
29     /**
30      * A URI for this file object.
31      */

32     protected final URI JavaDoc uri;
33
34     /**
35      * The kind of this file object.
36      */

37     protected final Kind kind;
38
39     /**
40      * Construct a SimpleJavaFileObject of the given kind and with the
41      * given URI.
42      *
43      * @param uri the URI for this file object
44      * @param kind the kind of this file object
45      */

46     protected SimpleJavaFileObject(URI JavaDoc uri, Kind kind) {
47         // null checks
48
uri.getClass();
49         kind.getClass();
50         if (uri.getPath() == null)
51             throw new IllegalArgumentException JavaDoc("URI must have a path: " + uri);
52         this.uri = uri;
53         this.kind = kind;
54     }
55
56     public URI JavaDoc toUri() {
57         return uri;
58     }
59
60     public String JavaDoc getName() {
61         return toUri().getPath();
62     }
63
64     /**
65      * This implementation always throws {@linkplain
66      * UnsupportedOperationException}. Subclasses can change this
67      * behavior as long as the contract of {@link FileObject} is
68      * obeyed.
69      */

70     public InputStream openInputStream() throws IOException {
71         throw new UnsupportedOperationException JavaDoc();
72     }
73
74     /**
75      * This implementation always throws {@linkplain
76      * UnsupportedOperationException}. Subclasses can change this
77      * behavior as long as the contract of {@link FileObject} is
78      * obeyed.
79      */

80     public OutputStream openOutputStream() throws IOException {
81         throw new UnsupportedOperationException JavaDoc();
82     }
83
84     /**
85      * Wraps the result of {@linkplain #getCharContent} in a Reader.
86      * Subclasses can change this behavior as long as the contract of
87      * {@link FileObject} is obeyed.
88      *
89      * @param ignoreEncodingErrors {@inheritDoc}
90      * @return a Reader wrapping the result of getCharContent
91      * @throws IllegalStateException {@inheritDoc}
92      * @throws UnsupportedOperationException {@inheritDoc}
93      * @throws IOException {@inheritDoc}
94      */

95     public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
96         CharSequence JavaDoc charContent = getCharContent(ignoreEncodingErrors);
97         if (charContent == null)
98             throw new UnsupportedOperationException JavaDoc();
99         if (charContent instanceof CharBuffer JavaDoc) {
100             CharBuffer JavaDoc buffer = (CharBuffer JavaDoc)charContent;
101             if (buffer.hasArray())
102                 return new CharArrayReader(buffer.array());
103         }
104         return new StringReader(charContent.toString());
105     }
106
107     /**
108      * This implementation always throws {@linkplain
109      * UnsupportedOperationException}. Subclasses can change this
110      * behavior as long as the contract of {@link FileObject} is
111      * obeyed.
112      */

113     public CharSequence JavaDoc getCharContent(boolean ignoreEncodingErrors) throws IOException {
114         throw new UnsupportedOperationException JavaDoc();
115     }
116
117     /**
118      * Wraps the result of openOutputStream in a Writer. Subclasses
119      * can change this behavior as long as the contract of {@link
120      * FileObject} is obeyed.
121      *
122      * @return a Writer wrapping the result of openOutputStream
123      * @throws IllegalStateException {@inheritDoc}
124      * @throws UnsupportedOperationException {@inheritDoc}
125      * @throws IOException {@inheritDoc}
126      */

127     public Writer openWriter() throws IOException {
128         return new OutputStreamWriter(openOutputStream());
129     }
130
131     /**
132      * This implementation returns {@code 0L}. Subclasses can change
133      * this behavior as long as the contract of {@link FileObject} is
134      * obeyed.
135      *
136      * @return {@code 0L}
137      */

138     public long getLastModified() {
139         return 0L;
140     }
141
142     /**
143      * This implementation does nothing. Subclasses can change this
144      * behavior as long as the contract of {@link FileObject} is
145      * obeyed.
146      *
147      * @return {@code false}
148      */

149     public boolean delete() {
150         return false;
151     }
152
153     /**
154      * @return {@code this.kind}
155      */

156     public Kind getKind() {
157         return kind;
158     }
159
160     /**
161      * This implementation compares the path of its URI to the given
162      * simple name. This method returns true if the given kind is
163      * equal to the kind of this object, and if the path is equal to
164      * {@code simpleName + kind.extension} or if it ends with {@code
165      * "/" + simpleName + kind.extension}.
166      *
167      * <p>This method calls {@link #getKind} and {@link #toUri} and
168      * does not access the fields {@link #uri} and {@link #kind}
169      * directly.
170      *
171      * <p>Subclasses can change this behavior as long as the contract
172      * of {@link JavaFileObject} is obeyed.
173      */

174     public boolean isNameCompatible(String JavaDoc simpleName, Kind kind) {
175         String JavaDoc baseName = simpleName + kind.extension;
176         return kind.equals(getKind())
177             && (baseName.equals(toUri().getPath())
178                 || toUri().getPath().endsWith("/" + baseName));
179     }
180
181     /**
182      * This implementation returns {@code null}. Subclasses can
183      * change this behavior as long as the contract of
184      * {@link JavaFileObject} is obeyed.
185      */

186     public NestingKind getNestingKind() { return null; }
187
188     /**
189      * This implementation returns {@code null}. Subclasses can
190      * change this behavior as long as the contract of
191      * {@link JavaFileObject} is obeyed.
192      */

193     public Modifier getAccessLevel() { return null; }
194
195     @Override JavaDoc
196     public String JavaDoc toString() {
197         return uri + " from " + getClass().getSimpleName();
198     }
199 }
200
Popular Tags