jjg@57: /* xdono@404: * Copyright 2005-2009 Sun Microsystems, Inc. All Rights Reserved. jjg@57: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@57: * jjg@57: * This code is free software; you can redistribute it and/or modify it jjg@57: * under the terms of the GNU General Public License version 2 only, as jjg@57: * published by the Free Software Foundation. Sun designates this jjg@57: * particular file as subject to the "Classpath" exception as provided jjg@57: * by Sun in the LICENSE file that accompanied this code. jjg@57: * jjg@57: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@57: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@57: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@57: * version 2 for more details (a copy is included in the LICENSE file that jjg@57: * accompanied this code). jjg@57: * jjg@57: * You should have received a copy of the GNU General Public License version jjg@57: * 2 along with this work; if not, write to the Free Software Foundation, jjg@57: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@57: * jjg@57: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@57: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@57: * have any questions. jjg@57: */ jjg@57: jjg@57: package com.sun.tools.javac.file; jjg@57: jjg@57: import java.io.IOException; jjg@57: import java.util.Set; jjg@57: import javax.tools.JavaFileObject; jjg@57: jjg@57: import java.io.ByteArrayInputStream; jjg@57: import java.io.File; jjg@57: import java.io.InputStream; jjg@57: import java.io.OutputStream; jjg@57: import java.io.Writer; jjg@57: import java.net.URI; jjg@57: import java.nio.ByteBuffer; jjg@57: import java.nio.CharBuffer; jjg@57: import java.nio.charset.CharsetDecoder; jjg@57: jjg@103: import com.sun.tools.javac.file.JavacFileManager.Archive; jjg@103: import com.sun.tools.javac.file.RelativePath.RelativeDirectory; jjg@103: import com.sun.tools.javac.file.RelativePath.RelativeFile; jjg@103: import com.sun.tools.javac.util.List; jjg@103: jjg@333: /** jjg@333: *

This is NOT part of any API supported by Sun Microsystems. jjg@333: * If you write code that depends on this, you do so at your own risk. jjg@333: * This code and its internal interfaces are subject to change or jjg@333: * deletion without notice. jjg@333: */ jjg@57: public class ZipFileIndexArchive implements Archive { jjg@57: jjg@57: private final ZipFileIndex zfIndex; jjg@57: private JavacFileManager fileManager; jjg@57: jjg@57: public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException { jjg@57: super(); jjg@57: this.fileManager = fileManager; jjg@57: this.zfIndex = zdir; jjg@57: } jjg@57: jjg@103: public boolean contains(RelativePath name) { jjg@57: return zfIndex.contains(name); jjg@57: } jjg@57: jjg@103: public List getFiles(RelativeDirectory subdirectory) { jjg@103: return zfIndex.getFiles(subdirectory); jjg@57: } jjg@57: jjg@103: public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { jjg@103: RelativeFile fullZipFileName = new RelativeFile(subdirectory, file); jjg@57: ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName); jjg@400: JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile()); jjg@57: return ret; jjg@57: } jjg@57: jjg@103: public Set getSubdirectories() { jjg@57: return zfIndex.getAllDirectories(); jjg@57: } jjg@57: jjg@57: public void close() throws IOException { jjg@57: zfIndex.close(); jjg@57: } jjg@57: jjg@400: @Override jjg@103: public String toString() { jjg@103: return "ZipFileIndexArchive[" + zfIndex + "]"; jjg@103: } jjg@103: jjg@57: /** jjg@57: * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation. jjg@57: */ jjg@57: public static class ZipFileIndexFileObject extends BaseFileObject { jjg@57: jjg@57: /** The entry's name. jjg@57: */ jjg@57: private String name; jjg@57: jjg@57: /** The zipfile containing the entry. jjg@57: */ jjg@57: ZipFileIndex zfIndex; jjg@57: jjg@57: /** The underlying zip entry object. jjg@57: */ jjg@57: ZipFileIndex.Entry entry; jjg@57: jjg@57: /** The InputStream for this zip entry (file.) jjg@57: */ jjg@57: InputStream inputStream = null; jjg@57: jjg@57: /** The name of the zip file where this entry resides. jjg@57: */ jjg@400: File zipName; jjg@57: jjg@57: jjg@400: ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, File zipFileName) { jjg@57: super(fileManager); jjg@57: this.name = entry.getFileName(); jjg@57: this.zfIndex = zfIndex; jjg@57: this.entry = entry; jjg@57: this.zipName = zipFileName; jjg@57: } jjg@57: jjg@415: @Override jjg@415: public URI toUri() { jjg@415: return createJarUri(zipName, getPrefixedEntryName()); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getName() { jjg@415: return zipName + "(" + getPrefixedEntryName() + ")"; jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getShortName() { jjg@415: return zipName.getName() + "(" + entry.getName() + ")"; jjg@415: } jjg@415: jjg@415: @Override jjg@415: public JavaFileObject.Kind getKind() { jjg@415: return getKind(entry.getName()); jjg@415: } jjg@415: jjg@415: @Override jjg@57: public InputStream openInputStream() throws IOException { jjg@57: if (inputStream == null) { jjg@415: assert entry != null; // see constructor jjg@415: inputStream = new ByteArrayInputStream(zfIndex.read(entry)); jjg@57: } jjg@57: return inputStream; jjg@57: } jjg@57: jjg@400: @Override jjg@57: public OutputStream openOutputStream() throws IOException { jjg@57: throw new UnsupportedOperationException(); jjg@57: } jjg@57: jjg@400: @Override jjg@57: public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { jjg@57: CharBuffer cb = fileManager.getCachedContent(this); jjg@57: if (cb == null) { jjg@57: InputStream in = new ByteArrayInputStream(zfIndex.read(entry)); jjg@57: try { jjg@57: ByteBuffer bb = fileManager.makeByteBuffer(in); jjg@57: JavaFileObject prev = fileManager.log.useSource(this); jjg@57: try { jjg@57: cb = fileManager.decode(bb, ignoreEncodingErrors); jjg@57: } finally { jjg@57: fileManager.log.useSource(prev); jjg@57: } jjg@57: fileManager.recycleByteBuffer(bb); // save for next time jjg@57: if (!ignoreEncodingErrors) jjg@57: fileManager.cache(this, cb); jjg@57: } finally { jjg@57: in.close(); jjg@57: } jjg@57: } jjg@57: return cb; jjg@57: } jjg@57: jjg@57: @Override jjg@415: public Writer openWriter() throws IOException { jjg@415: throw new UnsupportedOperationException(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public long getLastModified() { jjg@415: return entry.getLastModified(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public boolean delete() { jjg@415: throw new UnsupportedOperationException(); jjg@415: } jjg@415: jjg@415: @Override jjg@415: protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { jjg@415: return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); jjg@415: } jjg@415: jjg@415: @Override jjg@57: protected String inferBinaryName(Iterable path) { jjg@415: String entryName = entry.getName(); jjg@57: if (zfIndex.symbolFilePrefix != null) { jjg@103: String prefix = zfIndex.symbolFilePrefix.path; jjg@57: if (entryName.startsWith(prefix)) jjg@57: entryName = entryName.substring(prefix.length()); jjg@57: } jjg@103: return removeExtension(entryName).replace('/', '.'); jjg@57: } jjg@415: jjg@415: @Override jjg@415: public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { jjg@415: cn.getClass(); // null check jjg@415: if (k == Kind.OTHER && getKind() != k) jjg@415: return false; jjg@415: return name.equals(cn + k.extension); jjg@415: } jjg@415: jjg@424: /** jjg@424: * Check if two file objects are equal. jjg@424: * Two ZipFileIndexFileObjects are equal if the absolute paths of the underlying jjg@424: * zip files are equal and if the paths within those zip files are equal. jjg@424: */ jjg@415: @Override jjg@415: public boolean equals(Object other) { jjg@424: if (this == other) jjg@424: return true; jjg@424: jjg@415: if (!(other instanceof ZipFileIndexFileObject)) jjg@415: return false; jjg@424: jjg@415: ZipFileIndexFileObject o = (ZipFileIndexFileObject) other; jjg@424: return zfIndex.getAbsoluteFile().equals(o.zfIndex.getAbsoluteFile()) jjg@424: && name.equals(o.name); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public int hashCode() { jjg@424: return zfIndex.getAbsoluteFile().hashCode() + name.hashCode(); jjg@415: } jjg@415: jjg@415: private String getPrefixedEntryName() { jjg@415: if (zfIndex.symbolFilePrefix != null) jjg@415: return zfIndex.symbolFilePrefix.path + entry.getName(); jjg@415: else jjg@415: return entry.getName(); jjg@415: } jjg@57: } jjg@57: jjg@57: }