langtools/src/share/classes/com/sun/tools/javac/file/BaseFileObject.java
author jjg
Tue, 08 Sep 2009 11:12:13 -0700
changeset 3782 ae62279eeb46
parent 3380 a6c2bcab0fec
child 3995 73af8b6fb8bc
child 3890 b53fced26fa4
permissions -rw-r--r--
6419701: DefaultFileManager clean up: URI.create 6483788: DefaultFileManager.ZipFileObject.toUri() fails to escape space characters 6501502: JSR 199: FileObject.toUri should return file:///c:/ or file:/c:/ not file://c:/ 6877206: JavaFileObject.toUri returns bogus URI (win) 6877223: tests @ignored because of issues with File.toURI on Windows Reviewed-by: mcimadamore, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 731
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    26
package com.sun.tools.javac.file;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    28
import java.io.File;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.InputStreamReader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.Reader;
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    32
import java.net.URI;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    33
import java.net.URISyntaxException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.CharsetDecoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.element.Modifier;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.element.NestingKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import static javax.tools.JavaFileObject.Kind.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
3380
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    41
/**
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    42
 * <p><b>This is NOT part of any API supported by Sun Microsystems.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    43
 * If you write code that depends on this, you do so at your own risk.
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    44
 * This code and its internal interfaces are subject to change or
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    45
 * deletion without notice.</b>
a6c2bcab0fec 6865399: some javac files are missing Sun internal API comment
jjg
parents: 815
diff changeset
    46
*/
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public abstract class BaseFileObject implements JavaFileObject {
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    48
    protected BaseFileObject(JavacFileManager fileManager) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    49
        this.fileManager = fileManager;
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    50
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    public JavaFileObject.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        String n = getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        if (n.endsWith(CLASS.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            return CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        else if (n.endsWith(SOURCE.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            return SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        else if (n.endsWith(HTML.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            return HTML;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            return OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        return getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    public String getPath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        return getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    abstract public String getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    public NestingKind getNestingKind() { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    public Modifier getAccessLevel()  { return null; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return new InputStreamReader(openInputStream(), getDecoder(ignoreEncodingErrors));
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    91
    protected abstract String inferBinaryName(Iterable<? extends File> path);
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    92
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    93
    protected static String removeExtension(String fileName) {
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    94
        int lastDot = fileName.lastIndexOf(".");
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    95
        return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    96
    }
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
    97
3782
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    98
    protected static URI createJarUri(File jarFile, String entryName) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
    99
        URI jarURI = jarFile.toURI().normalize();
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   100
        String separator = entryName.startsWith("/") ? "!" : "!/";
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   101
        try {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   102
            // The jar URI convention appears to be not to re-encode the jarURI
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   103
            return new URI("jar:" + jarURI + separator + entryName);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   104
        } catch (URISyntaxException e) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   105
            throw new CannotCreateUriError(jarURI + separator + entryName, e);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   106
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   107
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   108
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   109
    /** Used when URLSyntaxException is thrown unexpectedly during
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   110
     *  implementations of (Base)FileObject.toURI(). */
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   111
    protected static class CannotCreateUriError extends Error {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   112
        private static final long serialVersionUID = 9101708840997613546L;
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   113
        public CannotCreateUriError(String value, Throwable cause) {
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   114
            super(value, cause);
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   115
        }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   116
    }
ae62279eeb46 6419701: DefaultFileManager clean up: URI.create
jjg
parents: 3380
diff changeset
   117
810
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
   118
    /** The file manager that created this JavaFileObject. */
e4b6a6d206e6 6714365: refactor JavacFileManager to move nested classes to top level
jjg
parents: 731
diff changeset
   119
    protected final JavacFileManager fileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
}