langtools/src/share/classes/com/sun/tools/javac/file/JavacFileManager.java
author jjg
Tue, 17 Jun 2008 10:44:32 -0700
changeset 809 2106a64c0a38
parent 731 1dd22bdb9ca5
child 810 e4b6a6d206e6
permissions -rw-r--r--
6625520: javac handles missing entries on classpath badly Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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: 657
diff changeset
    26
package com.sun.tools.javac.file;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    28
import java.io.ByteArrayInputStream;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.FileInputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.FileNotFoundException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.FileOutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.InputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import java.io.OutputStreamWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import java.io.Writer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import java.lang.ref.SoftReference;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import java.net.MalformedURLException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
import java.net.URI;
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
import java.net.URISyntaxException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import java.net.URL;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
import java.net.URLClassLoader;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import java.nio.ByteBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
import java.nio.CharBuffer;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
import java.nio.channels.FileChannel;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
import java.nio.charset.Charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
import java.nio.charset.CharsetDecoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
import java.nio.charset.CoderResult;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
import java.nio.charset.CodingErrorAction;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
import java.nio.charset.IllegalCharsetNameException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
import java.nio.charset.UnsupportedCharsetException;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
import java.util.ArrayList;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import java.util.Collections;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import java.util.EnumSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import java.util.Enumeration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import java.util.Iterator;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
import java.util.Set;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    64
import java.util.concurrent.ConcurrentHashMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
import java.util.zip.ZipEntry;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
import java.util.zip.ZipFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
import javax.tools.FileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
import javax.tools.JavaFileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
import javax.tools.JavaFileObject;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    72
import javax.tools.StandardJavaFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
import com.sun.tools.javac.code.Source;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    75
import com.sun.tools.javac.main.JavacOption;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    76
import com.sun.tools.javac.main.OptionName;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    77
import com.sun.tools.javac.main.RecognizedOptions;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    78
import com.sun.tools.javac.util.Context;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    80
import com.sun.tools.javac.util.List;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    81
import com.sun.tools.javac.util.ListBuffer;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    82
import com.sun.tools.javac.util.Log;
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
    83
import com.sun.tools.javac.util.Options;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
import static com.sun.tools.javac.main.OptionName.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
import static javax.tools.StandardLocation.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
 * This class provides access to the source, class and other files
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
 * used by the compiler and related tools.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
public class JavacFileManager implements StandardJavaFileManager {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    private static final String[] symbolFileLocation = { "lib", "ct.sym" };
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    private static final String symbolFilePrefix = "META-INF/sym/rt.jar/";
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    boolean useZipFileIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    private static int symbolFilePrefixLength = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    static {
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            symbolFilePrefixLength = symbolFilePrefix.getBytes("UTF-8").length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        } catch (java.io.UnsupportedEncodingException uee) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            // Can't happen...UTF-8 is always supported.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private static boolean CHECK_ZIP_TIMESTAMP = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    private static Map<File, Boolean> isDirectory = new ConcurrentHashMap<File, Boolean>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public static char[] toArray(CharBuffer buffer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        if (buffer.hasArray())
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            return ((CharBuffer)buffer.compact().flip()).array();
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return buffer.toString().toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * The log to be used for error reporting.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    protected Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    /** Encapsulates knowledge of paths
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    private Paths paths;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private Options options;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
    private final File uninited = new File("U N I N I T E D");
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    private final Set<JavaFileObject.Kind> sourceOrClass =
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        EnumSet.of(JavaFileObject.Kind.SOURCE, JavaFileObject.Kind.CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** The standard output directory, primarily used for classes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     *  Initialized by the "-d" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *  If classOutDir = null, files are written into same directory as the sources
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     *  they were generated from.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    private File classOutDir = uninited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    /** The output directory, used when generating sources while processing annotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     *  Initialized by the "-s" option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    private File sourceOutDir = uninited;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    protected boolean mmappedIO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    protected boolean ignoreSymbolFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * User provided charset (through javax.tools).
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    protected Charset charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Register a Context.Factory to create a JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    public static void preRegister(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        context.put(JavaFileManager.class, new Context.Factory<JavaFileManager>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            public JavaFileManager make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                return new JavacFileManager(context, true, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * Create a JavacFileManager using a given context, optionally registering
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     * it as the JavaFileManager for that context.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public JavacFileManager(Context context, boolean register, Charset charset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        if (register)
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            context.put(JavaFileManager.class, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        byteBufferCache = new ByteBufferCache();
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        this.charset = charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * Set the context for JavacFileManager.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public void setContext(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        if (paths == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            paths = Paths.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            // Reuse the Paths object as it stores the locations that
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            // have been set with setLocation, etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            paths.setContext(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        options = Options.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        useZipFileIndex = System.getProperty("useJavaUtilZip") == null;// TODO: options.get("useJavaUtilZip") == null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        CHECK_ZIP_TIMESTAMP = System.getProperty("checkZipIndexTimestamp") != null;// TODO: options.get("checkZipIndexTimestamp") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        mmappedIO = options.get("mmappedIO") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        ignoreSymbolFile = options.get("ignore.symbol.file") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    public JavaFileObject getFileForInput(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        return getRegularFile(new File(name));
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public JavaFileObject getRegularFile(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        return new RegularFileObject(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    public JavaFileObject getFileForOutput(String classname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                                           JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                                           JavaFileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        return getJavaFileForOutput(CLASS_OUTPUT, classname, kind, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromStrings(Iterable<String> names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        ListBuffer<File> files = new ListBuffer<File>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        for (String name : names)
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            files.append(new File(nullCheck(name)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        return getJavaFileObjectsFromFiles(files.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    public Iterable<? extends JavaFileObject> getJavaFileObjects(String... names) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        return getJavaFileObjectsFromStrings(Arrays.asList(nullCheck(names)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    protected JavaFileObject.Kind getKind(String extension) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        if (extension.equals(JavaFileObject.Kind.CLASS.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            return JavaFileObject.Kind.CLASS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        else if (extension.equals(JavaFileObject.Kind.SOURCE.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            return JavaFileObject.Kind.SOURCE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        else if (extension.equals(JavaFileObject.Kind.HTML.extension))
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            return JavaFileObject.Kind.HTML;
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
            return JavaFileObject.Kind.OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    private static boolean isValidName(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        // Arguably, isValidName should reject keywords (such as in SourceVersion.isName() ),
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        // but the set of keywords depends on the source level, and we don't want
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        // impls of JavaFileManager to have to be dependent on the source level.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        // Therefore we simply check that the argument is a sequence of identifiers
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        // separated by ".".
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        for (String s : name.split("\\.", -1)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            if (!SourceVersion.isIdentifier(s))
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    private static void validateClassName(String className) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        if (!isValidName(className))
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            throw new IllegalArgumentException("Invalid class name: " + className);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    private static void validatePackageName(String packageName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        if (packageName.length() > 0 && !isValidName(packageName))
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            throw new IllegalArgumentException("Invalid packageName name: " + packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    public static void testName(String name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                                boolean isValidPackageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                                boolean isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
            validatePackageName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            if (!isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                throw new AssertionError("Invalid package name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            printAscii("Valid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            if (isValidPackageName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                throw new AssertionError("Valid package name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            printAscii("Invalid package name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            validateClassName(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            if (!isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                throw new AssertionError("Invalid class name accepted: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            printAscii("Valid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        } catch (IllegalArgumentException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            if (isValidClassName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                throw new AssertionError("Valid class name rejected: " + name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            printAscii("Invalid class name: \"%s\"", name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    private static void printAscii(String format, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        String message;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            final String ascii = "US-ASCII";
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            message = new String(String.format(null, format, args).getBytes(ascii), ascii);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        } catch (java.io.UnsupportedEncodingException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            throw new AssertionError(ex);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        System.out.println(message);
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    /** Return external representation of name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     *  converting '.' to File.separatorChar.
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    private static String externalizeFileName(CharSequence name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        return name.toString().replace('.', File.separatorChar);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    private static String externalizeFileName(CharSequence n, JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        return externalizeFileName(n) + kind.extension;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    private static String baseName(String fileName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        return fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     * Insert all files in subdirectory `subdirectory' of `directory' which end
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     * in one of the extensions in `extensions' into packageSym.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    private void listDirectory(File directory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                               String subdirectory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                               Set<JavaFileObject.Kind> fileKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                               boolean recurse,
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                               ListBuffer<JavaFileObject> l) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        Archive archive = archives.get(directory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        boolean isFile = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
        if (CHECK_ZIP_TIMESTAMP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
            Boolean isf = isDirectory.get(directory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            if (isf == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                isFile = directory.isFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                isDirectory.put(directory, isFile);
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
            else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                isFile = directory.isFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            isFile = directory.isFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
        if (archive != null || isFile) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
            if (archive == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                    archive = openArchive(directory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                    log.error("error.reading.file",
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                       directory, ex.getLocalizedMessage());
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            if (subdirectory.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                if (!useZipFileIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                    subdirectory = subdirectory.replace('\\', '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                    if (!subdirectory.endsWith("/")) subdirectory = subdirectory + "/";
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    if (File.separatorChar == '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                        subdirectory = subdirectory.replace('\\', '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                    else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                        subdirectory = subdirectory.replace('/', '\\');
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                    if (!subdirectory.endsWith(File.separator)) subdirectory = subdirectory + File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            List<String> files = archive.getFiles(subdirectory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            if (files != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                for (String file; !files.isEmpty(); files = files.tail) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                    file = files.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                    if (isValidFile(file, fileKinds)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                        l.append(archive.getFileObject(subdirectory, file));
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
            if (recurse) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                for (String s: archive.getSubdirectories()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                    if (s.startsWith(subdirectory) && !s.equals(subdirectory)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                        // Because the archive map is a flat list of directories,
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                        // the enclosing loop will pick up all child subdirectories.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                        // Therefore, there is no need to recurse deeper.
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                        listDirectory(directory, s, fileKinds, false, l);
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
            File d = subdirectory.length() != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                ? new File(directory, subdirectory)
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                : directory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            if (!caseMapCheck(d, subdirectory))
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            File[] files = d.listFiles();
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            if (files == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            for (File f: files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                String fname = f.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                if (f.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                    if (recurse && SourceVersion.isIdentifier(fname)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                        listDirectory(directory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                                      subdirectory + File.separator + fname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                                      fileKinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                                      recurse,
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                                      l);
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                    if (isValidFile(fname, fileKinds)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                        JavaFileObject fe =
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                        new RegularFileObject(fname, new File(d, fname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                        l.append(fe);
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
    private boolean isValidFile(String s, Set<JavaFileObject.Kind> fileKinds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        int lastDot = s.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        String extn = (lastDot == -1 ? s : s.substring(lastDot));
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        JavaFileObject.Kind kind = getKind(extn);
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        return fileKinds.contains(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    private static final boolean fileSystemIsCaseSensitive =
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        File.separatorChar == '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
    /** Hack to make Windows case sensitive. Test whether given path
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
     *  ends in a string of characters with the same case as given name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
     *  Ignore file separators in both path and name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
    private boolean caseMapCheck(File f, String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        if (fileSystemIsCaseSensitive) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        // Note that getCanonicalPath() returns the case-sensitive
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        // spelled file name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        String path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            path = f.getCanonicalPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
        } catch (IOException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        char[] pcs = path.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
        char[] ncs = name.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        int i = pcs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        int j = ncs.length - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        while (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            while (i >= 0 && pcs[i] == File.separatorChar) i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            while (j >= 0 && ncs[j] == File.separatorChar) j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            if (i >= 0 && j >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                if (pcs[i] != ncs[j]) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
                i--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
                j--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        return j < 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
     * An archive provides a flat directory structure of a ZipFile by
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
     * mapping directory names to lists of files (basenames).
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
    public interface Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
        void close() throws IOException;
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
        boolean contains(String name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        JavaFileObject getFileObject(String subdirectory, String file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        List<String> getFiles(String subdirectory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        Set<String> getSubdirectories();
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    public class ZipArchive implements Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        protected final Map<String,List<String>> map;
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        protected final ZipFile zdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        public ZipArchive(ZipFile zdir) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            this.zdir = zdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
            this.map = new HashMap<String,List<String>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                ZipEntry entry;
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    entry = e.nextElement();
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
                } catch (InternalError ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
                    IOException io = new IOException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
                    io.initCause(ex); // convenience constructors added in Mustang :-(
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
                    throw io;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                addZipEntry(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
        void addZipEntry(ZipEntry entry) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            String name = entry.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            int i = name.lastIndexOf('/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            String dirname = name.substring(0, i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            String basename = name.substring(i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            if (basename.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            List<String> list = map.get(dirname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
            if (list == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                list = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            list = list.prepend(basename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            map.put(dirname, list);
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        public boolean contains(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            int i = name.lastIndexOf('/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            String dirname = name.substring(0, i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            String basename = name.substring(i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            if (basename.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            List<String> list = map.get(dirname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            return (list != null && list.contains(basename));
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        public List<String> getFiles(String subdirectory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
            return map.get(subdirectory);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        public JavaFileObject getFileObject(String subdirectory, String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            ZipEntry ze = zdir.getEntry(subdirectory + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            return new ZipFileObject(file, zdir, ze);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        public Set<String> getSubdirectories() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            return map.keySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        public void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            zdir.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
    public class SymbolArchive extends ZipArchive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        final File origFile;
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        public SymbolArchive(File orig, ZipFile zdir) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
            super(zdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            this.origFile = orig;
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        void addZipEntry(ZipEntry entry) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
            // called from super constructor, may not refer to origFile.
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            String name = entry.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
            if (!name.startsWith(symbolFilePrefix))
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            name = name.substring(symbolFilePrefix.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
            int i = name.lastIndexOf('/');
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            String dirname = name.substring(0, i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
            String basename = name.substring(i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            if (basename.length() == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
            List<String> list = map.get(dirname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            if (list == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
                list = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
            list = list.prepend(basename);
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
            map.put(dirname, list);
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
        public JavaFileObject getFileObject(String subdirectory, String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
            return super.getFileObject(symbolFilePrefix + subdirectory, file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    public class MissingArchive implements Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        final File zipFileName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        public MissingArchive(File name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
            zipFileName = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        public boolean contains(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
              return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
        public JavaFileObject getFileObject(String subdirectory, String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
        public List<String> getFiles(String subdirectory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        public Set<String> getSubdirectories() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            return Collections.emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
    /** A directory of zip files already opened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
    Map<File, Archive> archives = new HashMap<File,Archive>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    /** Open a new zip file directory.
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    protected Archive openArchive(File zipFileName) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        Archive archive = archives.get(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        if (archive == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            File origZipFileName = zipFileName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            if (!ignoreSymbolFile && paths.isBootClassPathRtJar(zipFileName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
                File file = zipFileName.getParentFile().getParentFile(); // ${java.home}
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
                if (new File(file.getName()).equals(new File("jre")))
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
                    file = file.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                // file == ${jdk.home}
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                for (String name : symbolFileLocation)
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                    file = new File(file, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
                // file == ${jdk.home}/lib/ct.sym
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
                if (file.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                    zipFileName = file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
                ZipFile zdir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
                boolean usePreindexedCache = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
                String preindexCacheLocation = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
                if (!useZipFileIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                    zdir = new ZipFile(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
                    usePreindexedCache = options.get("usezipindex") != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
                    preindexCacheLocation = options.get("java.io.tmpdir");
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
                    String optCacheLoc = options.get("cachezipindexdir");
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
                    if (optCacheLoc != null && optCacheLoc.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
                        if (optCacheLoc.startsWith("\"")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
                            if (optCacheLoc.endsWith("\"")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
                                optCacheLoc = optCacheLoc.substring(1, optCacheLoc.length() - 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
                           else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
                                optCacheLoc = optCacheLoc.substring(1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
                        File cacheDir = new File(optCacheLoc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
                        if (cacheDir.exists() && cacheDir.canWrite()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
                            preindexCacheLocation = optCacheLoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
                            if (!preindexCacheLocation.endsWith("/") &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                                !preindexCacheLocation.endsWith(File.separator)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
                                preindexCacheLocation += File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
                            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                if (origZipFileName == zipFileName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
                    if (!useZipFileIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
                        archive = new ZipArchive(zdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                        archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, 0,
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                                usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null));
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
                    if (!useZipFileIndex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
                        archive = new SymbolArchive(origZipFileName, zdir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                    else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                        archive = new ZipFileIndexArchive(this, ZipFileIndex.getZipFileIndex(zipFileName, symbolFilePrefixLength,
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
                                usePreindexedCache, preindexCacheLocation, options.get("writezipindexfiles") != null));
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            } catch (FileNotFoundException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
                archive = new MissingArchive(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
            } catch (IOException ex) {
809
2106a64c0a38 6625520: javac handles missing entries on classpath badly
jjg
parents: 731
diff changeset
   668
                if (zipFileName.exists())
2106a64c0a38 6625520: javac handles missing entries on classpath badly
jjg
parents: 731
diff changeset
   669
                    log.error("error.reading.file", zipFileName, ex.getLocalizedMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
                archive = new MissingArchive(zipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
            archives.put(origZipFileName, archive);
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        return archive;
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
    /** Flush any output resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    public void flush() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        contentCache.clear();
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
     * Close the JavaFileManager, releasing resources.
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
    public void close() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
        for (Iterator<Archive> i = archives.values().iterator(); i.hasNext(); ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
            Archive a = i.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
            i.remove();
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
                a.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
    private Map<JavaFileObject, SoftReference<CharBuffer>> contentCache = new HashMap<JavaFileObject, SoftReference<CharBuffer>>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
    private String defaultEncodingName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
    private String getDefaultEncodingName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        if (defaultEncodingName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            defaultEncodingName =
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
                new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        return defaultEncodingName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    protected String getEncodingName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        String encName = options.get(OptionName.ENCODING);
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        if (encName == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            return getDefaultEncodingName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            return encName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
    protected Source getSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        String sourceName = options.get(OptionName.SOURCE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
        Source source = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
        if (sourceName != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            source = Source.lookup(sourceName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        return (source != null ? source : Source.DEFAULT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
     * Make a byte buffer from an input stream.
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
    private ByteBuffer makeByteBuffer(InputStream in)
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        int limit = in.available();
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        if (mmappedIO && in instanceof FileInputStream) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
            // Experimental memory mapped I/O
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
            FileInputStream fin = (FileInputStream)in;
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
            return fin.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        if (limit < 1024) limit = 1024;
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        ByteBuffer result = byteBufferCache.get(limit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        int position = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        while (in.available() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
            if (position >= limit)
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
                // expand buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
                result = ByteBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
                    allocate(limit <<= 1).
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
                    put((ByteBuffer)result.flip());
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            int count = in.read(result.array(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
                position,
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                limit - position);
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            if (count < 0) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
            result.position(position += count);
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
        return (ByteBuffer)result.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
     * A single-element cache of direct byte buffers.
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    private static class ByteBufferCache {
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
        private ByteBuffer cached;
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        ByteBuffer get(int capacity) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            if (capacity < 20480) capacity = 20480;
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            ByteBuffer result =
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
                (cached != null && cached.capacity() >= capacity)
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
                ? (ByteBuffer)cached.clear()
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                : ByteBuffer.allocate(capacity + capacity>>1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            cached = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
        void put(ByteBuffer x) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
            cached = x;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
    private final ByteBufferCache byteBufferCache;
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
    private CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
        Charset charset = (this.charset == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
            ? Charset.forName(encodingName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
            : this.charset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
        CharsetDecoder decoder = charset.newDecoder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        CodingErrorAction action;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
        if (ignoreEncodingErrors)
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
            action = CodingErrorAction.REPLACE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
            action = CodingErrorAction.REPORT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        return decoder
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
            .onMalformedInput(action)
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
            .onUnmappableCharacter(action);
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
     * Decode a ByteBuffer into a CharBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
    private CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        String encodingName = getEncodingName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        CharsetDecoder decoder;
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
            decoder = getDecoder(encodingName, ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
        } catch (IllegalCharsetNameException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            log.error("unsupported.encoding", encodingName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
            return (CharBuffer)CharBuffer.allocate(1).flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
        } catch (UnsupportedCharsetException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            log.error("unsupported.encoding", encodingName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            return (CharBuffer)CharBuffer.allocate(1).flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
        // slightly overestimate the buffer size to avoid reallocation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
        float factor =
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            decoder.averageCharsPerByte() * 0.8f +
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
            decoder.maxCharsPerByte() * 0.2f;
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
        CharBuffer dest = CharBuffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
            allocate(10 + (int)(inbuf.remaining()*factor));
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
            CoderResult result = decoder.decode(inbuf, dest, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
            dest.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
            if (result.isUnderflow()) { // done reading
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
                // make sure there is at least one extra character
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
                if (dest.limit() == dest.capacity()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
                    dest.flip();
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
                return dest;
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
            } else if (result.isOverflow()) { // buffer too small; expand
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
                int newCapacity =
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
                    10 + dest.capacity() +
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
                dest = CharBuffer.allocate(newCapacity).put(dest);
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
            } else if (result.isMalformed() || result.isUnmappable()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
                // bad character in input
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
                // report coding error (warn only pre 1.5)
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
                if (!getSource().allowEncodingErrors()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
                    log.error(new SimpleDiagnosticPosition(dest.limit()),
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
                              "illegal.char.for.encoding",
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
                              charset == null ? encodingName : charset.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
                    log.warning(new SimpleDiagnosticPosition(dest.limit()),
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
                                "illegal.char.for.encoding",
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
                                charset == null ? encodingName : charset.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
                // skip past the coding error
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
                inbuf.position(inbuf.position() + result.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
                // undo the flip() to prepare the output buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
                // for more translation
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
                dest.position(dest.limit());
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
                dest.limit(dest.capacity());
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
                dest.put((char)0xfffd); // backward compatible
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
                throw new AssertionError(result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        // unreached
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
    public ClassLoader getClassLoader(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
        ListBuffer<URL> lb = new ListBuffer<URL>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
        for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                lb.append(f.toURI().toURL());
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
            } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                throw new AssertionError(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
        return new URLClassLoader(lb.toArray(new URL[lb.size()]),
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            getClass().getClassLoader());
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
    public Iterable<JavaFileObject> list(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                                         String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                                         Set<JavaFileObject.Kind> kinds,
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                                         boolean recurse)
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
        nullCheck(kinds);
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        String subdirectory = externalizeFileName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
        ListBuffer<JavaFileObject> results = new ListBuffer<JavaFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
        for (File directory : path)
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            listDirectory(directory, subdirectory, kinds, recurse, results);
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
        return results.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
    public String inferBinaryName(Location location, JavaFileObject file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
        file.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
        location.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
        // Need to match the path semantics of list(location, ...)
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
        if (path == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
            //System.err.println("Path for " + location + " is null");
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
        //System.err.println("Path for " + location + " is " + path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
        if (file instanceof RegularFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            RegularFileObject r = (RegularFileObject) file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
            String rPath = r.getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
            //System.err.println("RegularFileObject " + file + " " +r.getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
            for (File dir: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
                //System.err.println("dir: " + dir);
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                String dPath = dir.getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                if (!dPath.endsWith(File.separator))
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                    dPath += File.separator;
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
                if (rPath.regionMatches(true, 0, dPath, 0, dPath.length())
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
                    && new File(rPath.substring(0, dPath.length())).equals(new File(dPath))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
                    String relativeName = rPath.substring(dPath.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
                    return removeExtension(relativeName).replace(File.separatorChar, '.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
        } else if (file instanceof ZipFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
            ZipFileObject z = (ZipFileObject) file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
            String entryName = z.getZipEntryName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
            if (entryName.startsWith(symbolFilePrefix))
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                entryName = entryName.substring(symbolFilePrefix.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
            return removeExtension(entryName).replace('/', '.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        } else if (file instanceof ZipFileIndexFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
            ZipFileIndexFileObject z = (ZipFileIndexFileObject) file;
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
            String entryName = z.getZipEntryName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
            if (entryName.startsWith(symbolFilePrefix))
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                entryName = entryName.substring(symbolFilePrefix.length());
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
            return removeExtension(entryName).replace(File.separatorChar, '.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
            throw new IllegalArgumentException(file.getClass().getName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
        // System.err.println("inferBinaryName failed for " + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
        private static String removeExtension(String fileName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
            int lastDot = fileName.lastIndexOf(".");
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
            return (lastDot == -1 ? fileName : fileName.substring(0, lastDot));
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
    public boolean isSameFile(FileObject a, FileObject b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
        nullCheck(a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
        nullCheck(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
        if (!(a instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
            throw new IllegalArgumentException("Not supported: " + a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
        if (!(b instanceof BaseFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
            throw new IllegalArgumentException("Not supported: " + b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
        return a.equals(b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
    public boolean handleOption(String current, Iterator<String> remaining) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
        for (JavacOption o: javacFileManagerOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
            if (o.matches(current))  {
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
                if (o.hasArg()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
                    if (remaining.hasNext()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
                        if (!o.process(options, current, remaining.next()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                    if (!o.process(options, current))
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
                        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                // operand missing, or process returned false
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
                throw new IllegalArgumentException(current);
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
        private static JavacOption[] javacFileManagerOptions =
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
            RecognizedOptions.getJavacFileManagerOptions(
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
            new RecognizedOptions.GrumpyHelper());
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
    public int isSupportedOption(String option) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
        for (JavacOption o : javacFileManagerOptions) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            if (o.matches(option))
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                return o.hasArg() ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
        return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
    public boolean hasLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
        return getLocation(location) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
    public JavaFileObject getJavaFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                                              String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                                              JavaFileObject.Kind kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
        if (!sourceOrClass.contains(kind))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
            throw new IllegalArgumentException("Invalid kind " + kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
        return getFileForInput(location, externalizeFileName(className, kind));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
    public FileObject getFileForInput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                                      String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
                                      String relativeName)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
            throw new IllegalArgumentException("Invalid relative name: " + relativeName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        String name = packageName.length() == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
            ? relativeName
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
            : new File(externalizeFileName(packageName), relativeName).getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
        return getFileForInput(location, name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
    private JavaFileObject getFileForInput(Location location, String name) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
        Iterable<? extends File> path = getLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
        if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
        for (File dir: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            if (dir.isDirectory()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                File f = new File(dir, name.replace('/', File.separatorChar));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                if (f.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                    return new RegularFileObject(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                Archive a = openArchive(dir);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                if (a.contains(name)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                    int i = name.lastIndexOf('/');
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                    String dirname = name.substring(0, i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                    String basename = name.substring(i+1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
                    return a.getFileObject(dirname, basename);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
    public JavaFileObject getJavaFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                                               String className,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
                                               JavaFileObject.Kind kind,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
                                               FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        // validateClassName(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
        nullCheck(className);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
        nullCheck(kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
        if (!sourceOrClass.contains(kind))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            throw new IllegalArgumentException("Invalid kind " + kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
        return getFileForOutput(location, externalizeFileName(className, kind), sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
    public FileObject getFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                                       String packageName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                                       String relativeName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                                       FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
        // validatePackageName(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
        nullCheck(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
        if (!isRelativeUri(URI.create(relativeName))) // FIXME 6419701
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            throw new IllegalArgumentException("relativeName is invalid");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
        String name = packageName.length() == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
            ? relativeName
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
            : new File(externalizeFileName(packageName), relativeName).getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
        return getFileForOutput(location, name, sibling);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
    private JavaFileObject getFileForOutput(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
                                            String fileName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
                                            FileObject sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
        File dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
            if (getClassOutDir() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                dir = getClassOutDir();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
                File siblingDir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                if (sibling != null && sibling instanceof RegularFileObject) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                    siblingDir = ((RegularFileObject)sibling).f.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1093
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
                return new RegularFileObject(new File(siblingDir, baseName(fileName)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
            dir = (getSourceOutDir() != null ? getSourceOutDir() : getClassOutDir());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
            Iterable<? extends File> path = paths.getPathForLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
            dir = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
            for (File f: path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                dir = f;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
        File file = (dir == null ? new File(fileName) : new File(dir, fileName));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
        return new RegularFileObject(file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromFiles(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
        Iterable<? extends File> files)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
        ArrayList<RegularFileObject> result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
        if (files instanceof Collection)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
            result = new ArrayList<RegularFileObject>(((Collection)files).size());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1119
            result = new ArrayList<RegularFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
        for (File f: files)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
            result.add(new RegularFileObject(nullCheck(f)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
    public Iterable<? extends JavaFileObject> getJavaFileObjects(File... files) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
        return getJavaFileObjectsFromFiles(Arrays.asList(nullCheck(files)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
    public void setLocation(Location location,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                            Iterable<? extends File> path)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
        throws IOException
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
        paths.lazy();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
        final File dir = location.isOutputLocation() ? getOutputDirectory(path) : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
        if (location == CLASS_OUTPUT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
            classOutDir = getOutputLocation(dir, D);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
        else if (location == SOURCE_OUTPUT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
            sourceOutDir = getOutputLocation(dir, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
            paths.setPathForLocation(location, path);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
        private File getOutputDirectory(Iterable<? extends File> path) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
            if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
            Iterator<? extends File> pathIter = path.iterator();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
            if (!pathIter.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                throw new IllegalArgumentException("empty path for directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
            File dir = pathIter.next();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
            if (pathIter.hasNext())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
                throw new IllegalArgumentException("path too long for directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            if (!dir.exists())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
                throw new FileNotFoundException(dir + ": does not exist");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            else if (!dir.isDirectory())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
                throw new IOException(dir + ": not a directory");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
            return dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
    private File getOutputLocation(File dir, OptionName defaultOptionName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        if (dir != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            return dir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        String arg = options.get(defaultOptionName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
        if (arg == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
        return new File(arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
    public Iterable<? extends File> getLocation(Location location) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
        nullCheck(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
        paths.lazy();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
        if (location == CLASS_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
            return (getClassOutDir() == null ? null : List.of(getClassOutDir()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
        } else if (location == SOURCE_OUTPUT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
            return (getSourceOutDir() == null ? null : List.of(getSourceOutDir()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
            return paths.getPathForLocation(location);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
06bc494ca11e Initial load
duke
parents:
diff changeset
  1182
    private File getClassOutDir() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
        if (classOutDir == uninited)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
            classOutDir = getOutputLocation(null, D);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1185
        return classOutDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
    private File getSourceOutDir() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
        if (sourceOutDir == uninited)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1190
            sourceOutDir = getOutputLocation(null, S);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
        return sourceOutDir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
     * Enforces the specification of a "relative" URI as used in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
     * {@linkplain #getFileForInput(Location,String,URI)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
     * getFileForInput}.  This method must follow the rules defined in
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
     * that method, do not make any changes without consulting the
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
     * specification.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
    protected static boolean isRelativeUri(URI uri) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
        if (uri.isAbsolute())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
        String path = uri.normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
        if (path.length() == 0 /* isEmpty() is mustang API */)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
        char first = path.charAt(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
        return first != '.' && first != '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
  1209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
     * Converts a relative file name to a relative URI.  This is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
     * different from File.toURI as this method does not canonicalize
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
     * the file before creating the URI.  Furthermore, no schema is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
     * used.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
     * @param file a relative file name
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
     * @return a relative URI
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
     * @throws IllegalArgumentException if the file name is not
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
     * relative according to the definition given in {@link
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
     * javax.tools.JavaFileManager#getFileForInput}
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
    public static String getRelativeName(File file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
        if (!file.isAbsolute()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            String result = file.getPath().replace(File.separatorChar, '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            if (JavacFileManager.isRelativeUri(URI.create(result))) // FIXME 6419701
06bc494ca11e Initial load
duke
parents:
diff changeset
  1226
                return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
        throw new IllegalArgumentException("Invalid relative path: " + file);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
    @SuppressWarnings("deprecation") // bug 6410637
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
  1232
    public static String getJavacFileName(FileObject file) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1233
        if (file instanceof BaseFileObject)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
            return ((BaseFileObject)file).getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
        URI uri = file.toUri();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1236
        String scheme = uri.getScheme();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1237
        if (scheme == null || scheme.equals("file") || scheme.equals("jar"))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1238
            return uri.getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
            return uri.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1241
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1242
06bc494ca11e Initial load
duke
parents:
diff changeset
  1243
    @SuppressWarnings("deprecation") // bug 6410637
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 657
diff changeset
  1244
    public static String getJavacBaseFileName(FileObject file) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1245
        if (file instanceof BaseFileObject)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1246
            return ((BaseFileObject)file).getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1247
        URI uri = file.toUri();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1248
        String scheme = uri.getScheme();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1249
        if (scheme == null || scheme.equals("file") || scheme.equals("jar")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1250
            String path = uri.getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1251
            if (path == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1252
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1253
            if (scheme != null && scheme.equals("jar"))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1254
                path = path.substring(path.lastIndexOf('!') + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1255
            return path.substring(path.lastIndexOf('/') + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1257
            return uri.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1259
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1260
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
    private static <T> T nullCheck(T o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1262
        o.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1263
        return o;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1265
06bc494ca11e Initial load
duke
parents:
diff changeset
  1266
    private static <T> Iterable<T> nullCheck(Iterable<T> it) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1267
        for (T t : it)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1268
            t.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
        return it;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1271
06bc494ca11e Initial load
duke
parents:
diff changeset
  1272
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
     * A subclass of JavaFileObject representing regular files.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
    private class RegularFileObject extends BaseFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
        /** Have the parent directories been created?
06bc494ca11e Initial load
duke
parents:
diff changeset
  1277
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1278
        private boolean hasParents=false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1279
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
        /** The file's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1282
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
        /** The underlying file.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1286
        final File f;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
06bc494ca11e Initial load
duke
parents:
diff changeset
  1288
        public RegularFileObject(File f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1289
            this(f.getName(), f);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1291
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
        public RegularFileObject(String name, File f) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1293
            if (f.isDirectory())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1294
                throw new IllegalArgumentException("directories not supported");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
            this.f = f;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1298
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
        public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            return new FileInputStream(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1302
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
        protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1304
            return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
        public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
            ensureParentDirectoriesExist();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1309
            return new FileOutputStream(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1311
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
        public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
            ensureParentDirectoriesExist();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
            return new OutputStreamWriter(new FileOutputStream(f), getEncodingName());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1315
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
06bc494ca11e Initial load
duke
parents:
diff changeset
  1317
        private void ensureParentDirectoriesExist() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
            if (!hasParents) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
                File parent = f.getParentFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
                if (parent != null && !parent.exists()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1321
                    if (!parent.mkdirs()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1322
                        // if the mkdirs failed, it may be because another process concurrently
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
                        // created the directory, so check if the directory got created
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
                        // anyway before throwing an exception
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
                        if (!parent.exists() || !parent.isDirectory())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
                            throw new IOException("could not create parent directories");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1327
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1329
                hasParents = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
        public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
        public boolean isNameCompatible(String cn, JavaFileObject.Kind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
            cn.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
            if (kind == Kind.OTHER && getKind() != kind)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
            String n = cn + kind.extension;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
            if (name.equals(n))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
            if (name.equalsIgnoreCase(n)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
                    // allow for Windows
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
                    return (f.getCanonicalFile().getName().equals(n));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
                } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
        public String getPath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
            return f.getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
        public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
            return f.lastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
            return f.delete();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1369
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
        public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
            SoftReference<CharBuffer> r = contentCache.get(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
            CharBuffer cb = (r == null ? null : r.get());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
            if (cb == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
                InputStream in = new FileInputStream(f);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1376
                    ByteBuffer bb = makeByteBuffer(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1377
                    JavaFileObject prev = log.useSource(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1378
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
                        cb = decode(bb, ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
                    byteBufferCache.put(bb); // save for next time
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
                    if (!ignoreEncodingErrors)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                        contentCache.put(this, new SoftReference<CharBuffer>(cb));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1386
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1387
                    in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1388
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1389
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
            return cb;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
            if (!(other instanceof RegularFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
            RegularFileObject o = (RegularFileObject) other;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
                return f.equals(o.f)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
                    || f.getCanonicalFile().equals(o.f.getCanonicalFile());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
            } catch (IOException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1403
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
06bc494ca11e Initial load
duke
parents:
diff changeset
  1406
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
            return f.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
        public URI toUri() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                // Do no use File.toURI to avoid file system access
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
                String path = f.getAbsolutePath().replace(File.separatorChar, '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
                return new URI("file://" + path).normalize();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
            } catch (URISyntaxException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
                return f.toURI();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
06bc494ca11e Initial load
duke
parents:
diff changeset
  1421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1424
     * A subclass of JavaFileObject representing zip entries.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
    public class ZipFileObject extends BaseFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
        /** The entry's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
        /** The zipfile containing the entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
        ZipFile zdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1435
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
        /** The underlying zip entry object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1437
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
        ZipEntry entry;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
        public ZipFileObject(String name, ZipFile zdir, ZipEntry entry) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
            this.name = name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
            this.zdir = zdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1443
            this.entry = entry;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1444
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
        public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
            return zdir.getInputStream(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
06bc494ca11e Initial load
duke
parents:
diff changeset
  1450
        public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1452
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
        protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
            return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
06bc494ca11e Initial load
duke
parents:
diff changeset
  1458
        public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1460
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1464
        public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1466
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1467
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
        public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
            cn.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
            if (k == Kind.OTHER && getKind() != k)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
            return name.equals(cn + k.extension);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1476
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
        public String getPath() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
            return zdir.getName() + "(" + entry + ")";
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
        public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
            return entry.getTime();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
        public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
            SoftReference<CharBuffer> r = contentCache.get(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
            CharBuffer cb = (r == null ? null : r.get());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
            if (cb == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
                InputStream in = zdir.getInputStream(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                    ByteBuffer bb = makeByteBuffer(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
                    JavaFileObject prev = log.useSource(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                        cb = decode(bb, ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                    byteBufferCache.put(bb); // save for next time
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                    if (!ignoreEncodingErrors)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                        contentCache.put(this, new SoftReference<CharBuffer>(cb));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
                    in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
            return cb;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1514
            if (!(other instanceof ZipFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
            ZipFileObject o = (ZipFileObject) other;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
            return zdir.equals(o.zdir) || name.equals(o.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
            return zdir.hashCode() + name.hashCode();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
        public String getZipName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
            return zdir.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
        public String getZipEntryName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
            return entry.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
06bc494ca11e Initial load
duke
parents:
diff changeset
  1533
        public URI toUri() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
            String zipName = new File(getZipName()).toURI().normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
            String entryName = getZipEntryName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
            return URI.create("jar:" + zipName + "!" + entryName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
     * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.zip.ZipFileIndex implementation.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
    public class ZipFileIndexFileObject extends BaseFileObject {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
            /** The entry's name.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
        private String name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
        /** The zipfile containing the entry.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
        ZipFileIndex zfIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
        /** The underlying zip entry object.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
        ZipFileIndexEntry entry;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
        /** The InputStream for this zip entry (file.)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
        InputStream inputStream = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
        /** The name of the zip file where this entry resides.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
        String zipName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
        JavacFileManager defFileManager = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1567
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
        public ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndexEntry entry, String zipFileName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            this.name = entry.getFileName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
            this.zfIndex = zfIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
            this.entry = entry;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
            this.zipName = zipFileName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
            defFileManager = fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
        public InputStream openInputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1578
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
            if (inputStream == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
                inputStream = new ByteArrayInputStream(read());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
            return inputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
        protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
            return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
        public OutputStream openOutputStream() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
        public Writer openWriter() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
        public String getName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1601
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
        public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
            cn.getClass(); // null check
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
            if (k == Kind.OTHER && getKind() != k)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1607
            return name.equals(cn + k.extension);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
        /** @deprecated see bug 6410637 */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
        @Deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
        public String getPath() {
657
99cc2b9325f2 6705935: javac reports path name of entry in ZipFileIndex incorectly
jjg
parents: 10
diff changeset
  1613
            return zipName + "(" + entry.getName() + ")";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
06bc494ca11e Initial load
duke
parents:
diff changeset
  1616
        public long getLastModified() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
            return entry.getLastModified();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
        public boolean delete() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
            throw new UnsupportedOperationException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
        public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
            if (!(other instanceof ZipFileIndexFileObject))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
            ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
            return entry.equals(o.entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
        @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
        public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
            return zipName.hashCode() + (name.hashCode() << 10);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
        public String getZipName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
            return zipName;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
        public String getZipEntryName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
            return entry.getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
        public URI toUri() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            String zipName = new File(getZipName()).toURI().normalize().getPath();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
            String entryName = getZipEntryName();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
            if (File.separatorChar != '/') {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
                entryName = entryName.replace(File.separatorChar, '/');
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
            return URI.create("jar:" + zipName + "!" + entryName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
        private byte[] read() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
            if (entry == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
                entry = zfIndex.getZipIndexEntry(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
                if (entry == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1658
                  throw new FileNotFoundException();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
            return zfIndex.read(entry);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
        public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
            SoftReference<CharBuffer> r = defFileManager.contentCache.get(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
            CharBuffer cb = (r == null ? null : r.get());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
            if (cb == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
                InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
                try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
                    ByteBuffer bb = makeByteBuffer(in);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
                    JavaFileObject prev = log.useSource(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1671
                    try {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
                        cb = decode(bb, ignoreEncodingErrors);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
                    } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
                        log.useSource(prev);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
                    byteBufferCache.put(bb); // save for next time
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
                    if (!ignoreEncodingErrors)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
                        defFileManager.contentCache.put(this, new SoftReference<CharBuffer>(cb));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1679
                } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
                    in.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1683
            return cb;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
    public class ZipFileIndexArchive implements Archive {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
        private final ZipFileIndex zfIndex;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
        private JavacFileManager fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
        public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
            this.fileManager = fileManager;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
            this.zfIndex = zdir;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
        public boolean contains(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
            return zfIndex.contains(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1698
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
06bc494ca11e Initial load
duke
parents:
diff changeset
  1700
        public com.sun.tools.javac.util.List<String> getFiles(String subdirectory) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
              return zfIndex.getFiles(((subdirectory.endsWith("/") || subdirectory.endsWith("\\"))? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
        public JavaFileObject getFileObject(String subdirectory, String file) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
            String fullZipFileName = subdirectory + file;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
            ZipFileIndexEntry entry = zfIndex.getZipIndexEntry(fullZipFileName);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
            JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
            return ret;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
        public Set<String> getSubdirectories() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
            return zfIndex.getAllDirectories();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
        public void close() throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
            zfIndex.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
}