| 1 | /* |
| 2 | * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package javax.tools; |
| 27 | |
| 28 | import java.io.*; |
| 29 | import java.net.URI; |
| 30 | import java.net.URISyntaxException; |
| 31 | import java.nio.CharBuffer; |
| 32 | import javax.lang.model.element.Modifier; |
| 33 | import javax.lang.model.element.NestingKind; |
| 34 | import javax.tools.JavaFileObject.Kind; |
| 35 | |
| 36 | /** |
| 37 | * Provides simple implementations for most methods in JavaFileObject. |
| 38 | * This class is designed to be subclassed and used as a basis for |
| 39 | * JavaFileObject implementations. Subclasses can override the |
| 40 | * implementation and specification of any method of this class as |
| 41 | * long as the general contract of JavaFileObject is obeyed. |
| 42 | * |
| 43 | * @author Peter von der Ahé |
| 44 | * @since 1.6 |
| 45 | */ |
| 46 | public class SimpleJavaFileObject implements JavaFileObject { |
| 47 | /** |
| 48 | * A URI for this file object. |
| 49 | */ |
| 50 | protected final URI uri; |
| 51 | |
| 52 | /** |
| 53 | * The kind of this file object. |
| 54 | */ |
| 55 | protected final Kind kind; |
| 56 | |
| 57 | /** |
| 58 | * Construct a SimpleJavaFileObject of the given kind and with the |
| 59 | * given URI. |
| 60 | * |
| 61 | * @param uri the URI for this file object |
| 62 | * @param kind the kind of this file object |
| 63 | */ |
| 64 | protected SimpleJavaFileObject(URI uri, Kind kind) { |
| 65 | // null checks |
| 66 | uri.getClass(); |
| 67 | kind.getClass(); |
| 68 | if (uri.getPath() == null) |
| 69 | throw new IllegalArgumentException("URI must have a path: " + uri); |
| 70 | this.uri = uri; |
| 71 | this.kind = kind; |
| 72 | } |
| 73 | |
| 74 | public URI toUri() { |
| 75 | return uri; |
| 76 | } |
| 77 | |
| 78 | public String getName() { |
| 79 | return toUri().getPath(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * This implementation always throws {@linkplain |
| 84 | * UnsupportedOperationException}. Subclasses can change this |
| 85 | * behavior as long as the contract of {@link FileObject} is |
| 86 | * obeyed. |
| 87 | */ |
| 88 | public InputStream openInputStream() throws IOException { |
| 89 | throw new UnsupportedOperationException(); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This implementation always throws {@linkplain |
| 94 | * UnsupportedOperationException}. Subclasses can change this |
| 95 | * behavior as long as the contract of {@link FileObject} is |
| 96 | * obeyed. |
| 97 | */ |
| 98 | public OutputStream openOutputStream() throws IOException { |
| 99 | throw new UnsupportedOperationException(); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Wraps the result of {@linkplain #getCharContent} in a Reader. |
| 104 | * Subclasses can change this behavior as long as the contract of |
| 105 | * {@link FileObject} is obeyed. |
| 106 | * |
| 107 | * @param ignoreEncodingErrors {@inheritDoc} |
| 108 | * @return a Reader wrapping the result of getCharContent |
| 109 | * @throws IllegalStateException {@inheritDoc} |
| 110 | * @throws UnsupportedOperationException {@inheritDoc} |
| 111 | * @throws IOException {@inheritDoc} |
| 112 | */ |
| 113 | public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
| 114 | CharSequence charContent = getCharContent(ignoreEncodingErrors); |
| 115 | if (charContent == null) |
| 116 | throw new UnsupportedOperationException(); |
| 117 | if (charContent instanceof CharBuffer) { |
| 118 | CharBuffer buffer = (CharBuffer)charContent; |
| 119 | if (buffer.hasArray()) |
| 120 | return new CharArrayReader(buffer.array()); |
| 121 | } |
| 122 | return new StringReader(charContent.toString()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * This implementation always throws {@linkplain |
| 127 | * UnsupportedOperationException}. Subclasses can change this |
| 128 | * behavior as long as the contract of {@link FileObject} is |
| 129 | * obeyed. |
| 130 | */ |
| 131 | public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { |
| 132 | throw new UnsupportedOperationException(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Wraps the result of openOutputStream in a Writer. Subclasses |
| 137 | * can change this behavior as long as the contract of {@link |
| 138 | * FileObject} is obeyed. |
| 139 | * |
| 140 | * @return a Writer wrapping the result of openOutputStream |
| 141 | * @throws IllegalStateException {@inheritDoc} |
| 142 | * @throws UnsupportedOperationException {@inheritDoc} |
| 143 | * @throws IOException {@inheritDoc} |
| 144 | */ |
| 145 | public Writer openWriter() throws IOException { |
| 146 | return new OutputStreamWriter(openOutputStream()); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * This implementation returns {@code 0L}. Subclasses can change |
| 151 | * this behavior as long as the contract of {@link FileObject} is |
| 152 | * obeyed. |
| 153 | * |
| 154 | * @return {@code 0L} |
| 155 | */ |
| 156 | public long getLastModified() { |
| 157 | return 0L; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * This implementation does nothing. Subclasses can change this |
| 162 | * behavior as long as the contract of {@link FileObject} is |
| 163 | * obeyed. |
| 164 | * |
| 165 | * @return {@code false} |
| 166 | */ |
| 167 | public boolean delete() { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @return {@code this.kind} |
| 173 | */ |
| 174 | public Kind getKind() { |
| 175 | return kind; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * This implementation compares the path of its URI to the given |
| 180 | * simple name. This method returns true if the given kind is |
| 181 | * equal to the kind of this object, and if the path is equal to |
| 182 | * {@code simpleName + kind.extension} or if it ends with {@code |
| 183 | * "/" + simpleName + kind.extension}. |
| 184 | * |
| 185 | * <p>This method calls {@link #getKind} and {@link #toUri} and |
| 186 | * does not access the fields {@link #uri} and {@link #kind} |
| 187 | * directly. |
| 188 | * |
| 189 | * <p>Subclasses can change this behavior as long as the contract |
| 190 | * of {@link JavaFileObject} is obeyed. |
| 191 | */ |
| 192 | public boolean isNameCompatible(String simpleName, Kind kind) { |
| 193 | String baseName = simpleName + kind.extension; |
| 194 | return kind.equals(getKind()) |
| 195 | && (baseName.equals(toUri().getPath()) |
| 196 | || toUri().getPath().endsWith("/" + baseName)); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * This implementation returns {@code null}. Subclasses can |
| 201 | * change this behavior as long as the contract of |
| 202 | * {@link JavaFileObject} is obeyed. |
| 203 | */ |
| 204 | public NestingKind getNestingKind() { return null; } |
| 205 | |
| 206 | /** |
| 207 | * This implementation returns {@code null}. Subclasses can |
| 208 | * change this behavior as long as the contract of |
| 209 | * {@link JavaFileObject} is obeyed. |
| 210 | */ |
| 211 | public Modifier getAccessLevel() { return null; } |
| 212 | |
| 213 | @Override |
| 214 | public String toString() { |
| 215 | return getClass().getName() + "[" + toUri() + "]"; |
| 216 | } |
| 217 | } |