duke@1: /* xdono@404: * Copyright 2005-2009 Sun Microsystems, Inc. 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 duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun 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: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: jjg@50: package com.sun.tools.javac.file; duke@1: jjg@57: import java.io.File; duke@1: import java.io.IOException; duke@1: import java.io.InputStreamReader; duke@1: import java.io.Reader; jjg@400: import java.net.URI; jjg@400: import java.net.URISyntaxException; duke@1: import java.nio.charset.CharsetDecoder; duke@1: import javax.lang.model.element.Modifier; duke@1: import javax.lang.model.element.NestingKind; jjg@415: import javax.tools.FileObject; duke@1: import javax.tools.JavaFileObject; duke@1: duke@1: import static javax.tools.JavaFileObject.Kind.*; duke@1: jjg@450: import com.sun.tools.javac.util.BaseFileManager; jjg@450: jjg@333: /** jjg@333: *

This is NOT part of any API supported by Sun Microsystems. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. jjg@333: */ duke@1: public abstract class BaseFileObject implements JavaFileObject { jjg@57: protected BaseFileObject(JavacFileManager fileManager) { jjg@57: this.fileManager = fileManager; jjg@57: } duke@1: jjg@415: /** Return a short name for the object, such as for use in raw diagnostics jjg@415: */ jjg@415: public abstract String getShortName(); duke@1: duke@1: @Override duke@1: public String toString() { jjg@415: return getClass().getSimpleName() + "[" + getName() + "]"; duke@1: } duke@1: duke@1: public NestingKind getNestingKind() { return null; } duke@1: duke@1: public Modifier getAccessLevel() { return null; } duke@1: duke@1: public Reader openReader(boolean ignoreEncodingErrors) throws IOException { duke@1: return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors)); duke@1: } duke@1: duke@1: protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { duke@1: throw new UnsupportedOperationException(); duke@1: } duke@1: jjg@57: protected abstract String inferBinaryName(Iterable path); jjg@57: jjg@415: protected static JavaFileObject.Kind getKind(String filename) { jjg@450: return BaseFileManager.getKind(filename); jjg@415: } jjg@415: jjg@57: protected static String removeExtension(String fileName) { jjg@57: int lastDot = fileName.lastIndexOf("."); jjg@57: return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); jjg@57: } jjg@57: jjg@400: protected static URI createJarUri(File jarFile, String entryName) { jjg@400: URI jarURI = jarFile.toURI().normalize(); jjg@400: String separator = entryName.startsWith("/") ? "!" : "!/"; jjg@400: try { jjg@400: // The jar URI convention appears to be not to re-encode the jarURI jjg@400: return new URI("jar:" + jarURI + separator + entryName); jjg@400: } catch (URISyntaxException e) { jjg@400: throw new CannotCreateUriError(jarURI + separator + entryName, e); jjg@400: } jjg@400: } jjg@400: jjg@400: /** Used when URLSyntaxException is thrown unexpectedly during jjg@400: * implementations of (Base)FileObject.toURI(). */ jjg@400: protected static class CannotCreateUriError extends Error { jjg@400: private static final long serialVersionUID = 9101708840997613546L; jjg@400: public CannotCreateUriError(String value, Throwable cause) { jjg@400: super(value, cause); jjg@400: } jjg@400: } jjg@400: jjg@415: /** Return the last component of a presumed hierarchical URI. jjg@415: * From the scheme specific part of the URI, it returns the substring jjg@415: * after the last "/" if any, or everything if no "/" is found. jjg@415: */ jjg@415: public static String getSimpleName(FileObject fo) { jjg@415: URI uri = fo.toUri(); jjg@415: String s = uri.getSchemeSpecificPart(); jjg@415: return s.substring(s.lastIndexOf("/") + 1); // safe when / not found jjg@415: jjg@415: } jjg@415: jjg@424: // force subtypes to define equals jjg@424: @Override jjg@424: public abstract boolean equals(Object other); jjg@424: jjg@424: // force subtypes to define hashCode jjg@424: @Override jjg@424: public abstract int hashCode(); jjg@424: jjg@57: /** The file manager that created this JavaFileObject. */ jjg@57: protected final JavacFileManager fileManager; duke@1: }