Fri, 25 Sep 2009 22:04:43 -0700
Merge
duke@1 | 1 | /* |
xdono@404 | 2 | * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
jjg@50 | 26 | package com.sun.tools.javac.file; |
duke@1 | 27 | |
jjg@57 | 28 | import java.io.File; |
duke@1 | 29 | import java.io.IOException; |
duke@1 | 30 | import java.io.InputStreamReader; |
duke@1 | 31 | import java.io.Reader; |
jjg@400 | 32 | import java.net.URI; |
jjg@400 | 33 | import java.net.URISyntaxException; |
duke@1 | 34 | import java.nio.charset.CharsetDecoder; |
duke@1 | 35 | import javax.lang.model.element.Modifier; |
duke@1 | 36 | import javax.lang.model.element.NestingKind; |
jjg@415 | 37 | import javax.tools.FileObject; |
duke@1 | 38 | import javax.tools.JavaFileObject; |
duke@1 | 39 | |
duke@1 | 40 | import static javax.tools.JavaFileObject.Kind.*; |
duke@1 | 41 | |
jjg@333 | 42 | /** |
jjg@333 | 43 | * <p><b>This is NOT part of any API supported by Sun Microsystems. |
jjg@333 | 44 | * If you write code that depends on this, you do so at your own risk. |
jjg@333 | 45 | * This code and its internal interfaces are subject to change or |
jjg@333 | 46 | * deletion without notice.</b> |
jjg@333 | 47 | */ |
duke@1 | 48 | public abstract class BaseFileObject implements JavaFileObject { |
jjg@57 | 49 | protected BaseFileObject(JavacFileManager fileManager) { |
jjg@57 | 50 | this.fileManager = fileManager; |
jjg@57 | 51 | } |
duke@1 | 52 | |
jjg@415 | 53 | /** Return a short name for the object, such as for use in raw diagnostics |
jjg@415 | 54 | */ |
jjg@415 | 55 | public abstract String getShortName(); |
duke@1 | 56 | |
duke@1 | 57 | @Override |
duke@1 | 58 | public String toString() { |
jjg@415 | 59 | return getClass().getSimpleName() + "[" + getName() + "]"; |
duke@1 | 60 | } |
duke@1 | 61 | |
duke@1 | 62 | public NestingKind getNestingKind() { return null; } |
duke@1 | 63 | |
duke@1 | 64 | public Modifier getAccessLevel() { return null; } |
duke@1 | 65 | |
duke@1 | 66 | public Reader openReader(boolean ignoreEncodingErrors) throws IOException { |
duke@1 | 67 | return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors)); |
duke@1 | 68 | } |
duke@1 | 69 | |
duke@1 | 70 | protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
duke@1 | 71 | throw new UnsupportedOperationException(); |
duke@1 | 72 | } |
duke@1 | 73 | |
jjg@57 | 74 | protected abstract String inferBinaryName(Iterable<? extends File> path); |
jjg@57 | 75 | |
jjg@415 | 76 | protected static JavaFileObject.Kind getKind(String filename) { |
jjg@415 | 77 | if (filename.endsWith(CLASS.extension)) |
jjg@415 | 78 | return CLASS; |
jjg@415 | 79 | else if (filename.endsWith(SOURCE.extension)) |
jjg@415 | 80 | return SOURCE; |
jjg@415 | 81 | else if (filename.endsWith(HTML.extension)) |
jjg@415 | 82 | return HTML; |
jjg@415 | 83 | else |
jjg@415 | 84 | return OTHER; |
jjg@415 | 85 | } |
jjg@415 | 86 | |
jjg@57 | 87 | protected static String removeExtension(String fileName) { |
jjg@57 | 88 | int lastDot = fileName.lastIndexOf("."); |
jjg@57 | 89 | return (lastDot == -1 ? fileName : fileName.substring(0, lastDot)); |
jjg@57 | 90 | } |
jjg@57 | 91 | |
jjg@400 | 92 | protected static URI createJarUri(File jarFile, String entryName) { |
jjg@400 | 93 | URI jarURI = jarFile.toURI().normalize(); |
jjg@400 | 94 | String separator = entryName.startsWith("/") ? "!" : "!/"; |
jjg@400 | 95 | try { |
jjg@400 | 96 | // The jar URI convention appears to be not to re-encode the jarURI |
jjg@400 | 97 | return new URI("jar:" + jarURI + separator + entryName); |
jjg@400 | 98 | } catch (URISyntaxException e) { |
jjg@400 | 99 | throw new CannotCreateUriError(jarURI + separator + entryName, e); |
jjg@400 | 100 | } |
jjg@400 | 101 | } |
jjg@400 | 102 | |
jjg@400 | 103 | /** Used when URLSyntaxException is thrown unexpectedly during |
jjg@400 | 104 | * implementations of (Base)FileObject.toURI(). */ |
jjg@400 | 105 | protected static class CannotCreateUriError extends Error { |
jjg@400 | 106 | private static final long serialVersionUID = 9101708840997613546L; |
jjg@400 | 107 | public CannotCreateUriError(String value, Throwable cause) { |
jjg@400 | 108 | super(value, cause); |
jjg@400 | 109 | } |
jjg@400 | 110 | } |
jjg@400 | 111 | |
jjg@415 | 112 | /** Return the last component of a presumed hierarchical URI. |
jjg@415 | 113 | * From the scheme specific part of the URI, it returns the substring |
jjg@415 | 114 | * after the last "/" if any, or everything if no "/" is found. |
jjg@415 | 115 | */ |
jjg@415 | 116 | public static String getSimpleName(FileObject fo) { |
jjg@415 | 117 | URI uri = fo.toUri(); |
jjg@415 | 118 | String s = uri.getSchemeSpecificPart(); |
jjg@415 | 119 | return s.substring(s.lastIndexOf("/") + 1); // safe when / not found |
jjg@415 | 120 | |
jjg@415 | 121 | } |
jjg@415 | 122 | |
jjg@57 | 123 | /** The file manager that created this JavaFileObject. */ |
jjg@57 | 124 | protected final JavacFileManager fileManager; |
duke@1 | 125 | } |