1 /* 2 * Copyright (c) 2005, 2009, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.javac.file; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.io.InputStreamReader; 31 import java.io.Reader; 32 import java.net.URI; 33 import java.net.URISyntaxException; 34 import java.nio.charset.CharsetDecoder; 35 import javax.lang.model.element.Modifier; 36 import javax.lang.model.element.NestingKind; 37 import javax.tools.FileObject; 38 import javax.tools.JavaFileObject; 39 40 import static javax.tools.JavaFileObject.Kind.*; 41 42 import com.sun.tools.javac.util.BaseFileManager; 43 44 /** 45 * <p><b>This is NOT part of any supported API. 46 * If you write code that depends on this, you do so at your own risk. 47 * This code and its internal interfaces are subject to change or 48 * deletion without notice.</b> 49 */ 50 public abstract class BaseFileObject implements JavaFileObject { 51 protected BaseFileObject(JavacFileManager fileManager) { 52 this.fileManager = fileManager; 53 } 54 55 /** Return a short name for the object, such as for use in raw diagnostics 56 */ 57 public abstract String getShortName(); 58 59 @Override 60 public String toString() { 61 return getClass().getSimpleName() + "[" + getName() + "]"; 62 } 63 64 public NestingKind getNestingKind() { return null; } 65 66 public Modifier getAccessLevel() { return null; } 67 68 public Reader openReader(boolean ignoreEncodingErrors) throws IOException { 69 return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors)); 70 } 71 72 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { 73 throw new UnsupportedOperationException(); 74 } 75 76 protected abstract String inferBinaryName(Iterable<? extends File> path); 77 78 protected static JavaFileObject.Kind getKind(String filename) { 79 return BaseFileManager.getKind(filename); 80 } 81 82 protected static String removeExtension(String fileName) { 83 int lastDot = fileName.lastIndexOf("."); 84 return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); 85 } 86 87 protected static URI createJarUri(File jarFile, String entryName) { 88 URI jarURI = jarFile.toURI().normalize(); 89 String separator = entryName.startsWith("/") ? "!" : "!/"; 90 try { 91 // The jar URI convention appears to be not to re-encode the jarURI 92 return new URI("jar:" + jarURI + separator + entryName); 93 } catch (URISyntaxException e) { 94 throw new CannotCreateUriError(jarURI + separator + entryName, e); 95 } 96 } 97 98 /** Used when URLSyntaxException is thrown unexpectedly during 99 * implementations of (Base)FileObject.toURI(). */ 100 protected static class CannotCreateUriError extends Error { 101 private static final long serialVersionUID = 9101708840997613546L; 102 public CannotCreateUriError(String value, Throwable cause) { 103 super(value, cause); 104 } 105 } 106 107 /** Return the last component of a presumed hierarchical URI. 108 * From the scheme specific part of the URI, it returns the substring 109 * after the last "/" if any, or everything if no "/" is found. 110 */ 111 public static String getSimpleName(FileObject fo) { 112 URI uri = fo.toUri(); 113 String s = uri.getSchemeSpecificPart(); 114 return s.substring(s.lastIndexOf("/") + 1); // safe when / not found 115 116 } 117 118 // force subtypes to define equals 119 @Override 120 public abstract boolean equals(Object other); 121 122 // force subtypes to define hashCode 123 @Override 124 public abstract int hashCode(); 125 126 /** The file manager that created this JavaFileObject. */ 127 protected final JavacFileManager fileManager; 128 }