001    /*
002     * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.javac.file;
027    
028    import java.io.File;
029    import java.io.IOException;
030    import java.io.InputStreamReader;
031    import java.io.Reader;
032    import java.nio.charset.CharsetDecoder;
033    import javax.lang.model.element.Modifier;
034    import javax.lang.model.element.NestingKind;
035    import javax.tools.JavaFileObject;
036    
037    import static javax.tools.JavaFileObject.Kind.*;
038    
039    public abstract class BaseFileObject implements JavaFileObject {
040        protected BaseFileObject(JavacFileManager fileManager) {
041            this.fileManager = fileManager;
042        }
043    
044        public JavaFileObject.Kind getKind() {
045            String n = getName();
046            if (n.endsWith(CLASS.extension))
047                return CLASS;
048            else if (n.endsWith(SOURCE.extension))
049                return SOURCE;
050            else if (n.endsWith(HTML.extension))
051                return HTML;
052            else
053                return OTHER;
054        }
055    
056        @Override
057        public String toString() {
058            return getPath();
059        }
060    
061        /** @deprecated see bug 6410637 */
062        @Deprecated
063        public String getPath() {
064            return getName();
065        }
066    
067        /** @deprecated see bug 6410637 */
068        @Deprecated
069        abstract public String getName();
070    
071        public NestingKind getNestingKind() { return null; }
072    
073        public Modifier getAccessLevel()  { return null; }
074    
075        public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
076            return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors));
077        }
078    
079        protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
080            throw new UnsupportedOperationException();
081        }
082    
083        protected abstract String inferBinaryName(Iterable<? extends File> path);
084    
085        protected static String removeExtension(String fileName) {
086            int lastDot = fileName.lastIndexOf(".");
087            return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
088        }
089    
090        /** The file manager that created this JavaFileObject. */
091        protected final JavacFileManager fileManager;
092    
093    }