duke@1: /* ohair@554: * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package javax.tools; duke@1: duke@1: import java.io.*; duke@1: import java.net.URI; duke@1: import java.nio.CharBuffer; duke@1: import javax.lang.model.element.Modifier; duke@1: import javax.lang.model.element.NestingKind; duke@1: import javax.tools.JavaFileObject.Kind; duke@1: duke@1: /** duke@1: * Provides simple implementations for most methods in JavaFileObject. duke@1: * This class is designed to be subclassed and used as a basis for duke@1: * JavaFileObject implementations. Subclasses can override the duke@1: * implementation and specification of any method of this class as duke@1: * long as the general contract of JavaFileObject is obeyed. duke@1: * duke@1: * @author Peter von der Ahé duke@1: * @since 1.6 duke@1: */ duke@1: public class SimpleJavaFileObject implements JavaFileObject { duke@1: /** duke@1: * A URI for this file object. duke@1: */ duke@1: protected final URI uri; duke@1: duke@1: /** duke@1: * The kind of this file object. duke@1: */ duke@1: protected final Kind kind; duke@1: duke@1: /** duke@1: * Construct a SimpleJavaFileObject of the given kind and with the duke@1: * given URI. duke@1: * duke@1: * @param uri the URI for this file object duke@1: * @param kind the kind of this file object duke@1: */ duke@1: protected SimpleJavaFileObject(URI uri, Kind kind) { duke@1: // null checks duke@1: uri.getClass(); duke@1: kind.getClass(); duke@1: if (uri.getPath() == null) duke@1: throw new IllegalArgumentException("URI must have a path: " + uri); duke@1: this.uri = uri; duke@1: this.kind = kind; duke@1: } duke@1: duke@1: public URI toUri() { duke@1: return uri; duke@1: } duke@1: duke@1: public String getName() { duke@1: return toUri().getPath(); duke@1: } duke@1: duke@1: /** duke@1: * This implementation always throws {@linkplain duke@1: * UnsupportedOperationException}. Subclasses can change this duke@1: * behavior as long as the contract of {@link FileObject} is duke@1: * obeyed. duke@1: */ duke@1: public InputStream openInputStream() throws IOException { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: duke@1: /** duke@1: * This implementation always throws {@linkplain duke@1: * UnsupportedOperationException}. Subclasses can change this duke@1: * behavior as long as the contract of {@link FileObject} is duke@1: * obeyed. duke@1: */ duke@1: public OutputStream openOutputStream() throws IOException { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: duke@1: /** duke@1: * Wraps the result of {@linkplain #getCharContent} in a Reader. duke@1: * Subclasses can change this behavior as long as the contract of duke@1: * {@link FileObject} is obeyed. duke@1: * duke@1: * @param ignoreEncodingErrors {@inheritDoc} duke@1: * @return a Reader wrapping the result of getCharContent duke@1: * @throws IllegalStateException {@inheritDoc} duke@1: * @throws UnsupportedOperationException {@inheritDoc} duke@1: * @throws IOException {@inheritDoc} duke@1: */ duke@1: public Reader openReader(boolean ignoreEncodingErrors) throws IOException { duke@1: CharSequence charContent = getCharContent(ignoreEncodingErrors); duke@1: if (charContent == null) duke@1: throw new UnsupportedOperationException(); duke@1: if (charContent instanceof CharBuffer) { duke@1: CharBuffer buffer = (CharBuffer)charContent; duke@1: if (buffer.hasArray()) duke@1: return new CharArrayReader(buffer.array()); duke@1: } duke@1: return new StringReader(charContent.toString()); duke@1: } duke@1: duke@1: /** duke@1: * This implementation always throws {@linkplain duke@1: * UnsupportedOperationException}. Subclasses can change this duke@1: * behavior as long as the contract of {@link FileObject} is duke@1: * obeyed. duke@1: */ duke@1: public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: duke@1: /** duke@1: * Wraps the result of openOutputStream in a Writer. Subclasses duke@1: * can change this behavior as long as the contract of {@link duke@1: * FileObject} is obeyed. duke@1: * duke@1: * @return a Writer wrapping the result of openOutputStream duke@1: * @throws IllegalStateException {@inheritDoc} duke@1: * @throws UnsupportedOperationException {@inheritDoc} duke@1: * @throws IOException {@inheritDoc} duke@1: */ duke@1: public Writer openWriter() throws IOException { duke@1: return new OutputStreamWriter(openOutputStream()); duke@1: } duke@1: duke@1: /** duke@1: * This implementation returns {@code 0L}. Subclasses can change duke@1: * this behavior as long as the contract of {@link FileObject} is duke@1: * obeyed. duke@1: * duke@1: * @return {@code 0L} duke@1: */ duke@1: public long getLastModified() { duke@1: return 0L; duke@1: } duke@1: duke@1: /** duke@1: * This implementation does nothing. Subclasses can change this duke@1: * behavior as long as the contract of {@link FileObject} is duke@1: * obeyed. duke@1: * duke@1: * @return {@code false} duke@1: */ duke@1: public boolean delete() { duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * @return {@code this.kind} duke@1: */ duke@1: public Kind getKind() { duke@1: return kind; duke@1: } duke@1: duke@1: /** duke@1: * This implementation compares the path of its URI to the given duke@1: * simple name. This method returns true if the given kind is duke@1: * equal to the kind of this object, and if the path is equal to duke@1: * {@code simpleName + kind.extension} or if it ends with {@code duke@1: * "/" + simpleName + kind.extension}. duke@1: * duke@1: *

This method calls {@link #getKind} and {@link #toUri} and duke@1: * does not access the fields {@link #uri} and {@link #kind} duke@1: * directly. duke@1: * duke@1: *

Subclasses can change this behavior as long as the contract duke@1: * of {@link JavaFileObject} is obeyed. duke@1: */ duke@1: public boolean isNameCompatible(String simpleName, Kind kind) { duke@1: String baseName = simpleName + kind.extension; duke@1: return kind.equals(getKind()) duke@1: && (baseName.equals(toUri().getPath()) duke@1: || toUri().getPath().endsWith("/" + baseName)); duke@1: } duke@1: duke@1: /** duke@1: * This implementation returns {@code null}. Subclasses can duke@1: * change this behavior as long as the contract of duke@1: * {@link JavaFileObject} is obeyed. duke@1: */ duke@1: public NestingKind getNestingKind() { return null; } duke@1: duke@1: /** duke@1: * This implementation returns {@code null}. Subclasses can duke@1: * change this behavior as long as the contract of duke@1: * {@link JavaFileObject} is obeyed. duke@1: */ duke@1: public Modifier getAccessLevel() { return null; } duke@1: duke@1: @Override duke@1: public String toString() { duke@1: return getClass().getName() + "[" + toUri() + "]"; duke@1: } duke@1: }