jjg@57: /* ohair@554: * Copyright (c) 2005, 2009, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this jjg@57: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. jjg@57: */ jjg@57: jjg@57: package com.sun.tools.javac.file; jjg@57: jjg@103: import java.io.File; jjg@57: import java.io.IOException; 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@103: import java.util.Enumeration; jjg@103: import java.util.HashMap; jjg@103: import java.util.Map; jjg@103: import java.util.Set; jjg@103: import java.util.zip.ZipEntry; jjg@103: import java.util.zip.ZipFile; jjg@103: jjg@103: import javax.tools.JavaFileObject; jjg@103: 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@424: import java.lang.ref.Reference; jjg@424: import java.lang.ref.SoftReference; jjg@57: jjg@333: /** jjg@581: *

This is NOT part of any supported API. 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 ZipArchive implements Archive { jjg@57: jjg@424: public ZipArchive(JavacFileManager fm, ZipFile zfile) throws IOException { jjg@424: this(fm, zfile, true); jjg@103: } jjg@103: jjg@424: protected ZipArchive(JavacFileManager fm, ZipFile zfile, boolean initMap) throws IOException { jjg@57: this.fileManager = fm; jjg@424: this.zfile = zfile; jjg@103: this.map = new HashMap>(); jjg@103: if (initMap) jjg@103: initMap(); jjg@103: } jjg@103: jjg@103: protected void initMap() throws IOException { jjg@424: for (Enumeration e = zfile.entries(); e.hasMoreElements(); ) { jjg@57: ZipEntry entry; jjg@57: try { jjg@57: entry = e.nextElement(); jjg@57: } catch (InternalError ex) { jjg@57: IOException io = new IOException(); jjg@57: io.initCause(ex); // convenience constructors added in Mustang :-( jjg@57: throw io; jjg@57: } jjg@57: addZipEntry(entry); jjg@57: } jjg@57: } jjg@57: jjg@57: void addZipEntry(ZipEntry entry) { jjg@57: String name = entry.getName(); jjg@57: int i = name.lastIndexOf('/'); jjg@103: RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1)); jjg@57: String basename = name.substring(i+1); jjg@57: if (basename.length() == 0) jjg@57: return; jjg@57: List list = map.get(dirname); jjg@57: if (list == null) jjg@57: list = List.nil(); jjg@57: list = list.prepend(basename); jjg@57: map.put(dirname, list); jjg@57: } jjg@57: jjg@103: public boolean contains(RelativePath name) { jjg@103: RelativeDirectory dirname = name.dirname(); jjg@103: String basename = name.basename(); jjg@57: if (basename.length() == 0) jjg@57: return false; jjg@57: List list = map.get(dirname); jjg@57: return (list != null && list.contains(basename)); jjg@57: } jjg@57: jjg@103: public List getFiles(RelativeDirectory subdirectory) { jjg@57: return map.get(subdirectory); jjg@57: } jjg@57: jjg@103: public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { jjg@424: ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zfile); jjg@57: return new ZipFileObject(this, file, ze); jjg@57: } jjg@57: jjg@103: public Set getSubdirectories() { jjg@57: return map.keySet(); jjg@57: } jjg@57: jjg@57: public void close() throws IOException { jjg@424: zfile.close(); jjg@57: } jjg@57: jjg@400: @Override jjg@103: public String toString() { jjg@424: return "ZipArchive[" + zfile.getName() + "]"; jjg@103: } jjg@103: jjg@424: private File getAbsoluteFile() { jjg@424: File absFile = (absFileRef == null ? null : absFileRef.get()); jjg@424: if (absFile == null) { jjg@424: absFile = new File(zfile.getName()).getAbsoluteFile(); jjg@424: absFileRef = new SoftReference(absFile); jjg@424: } jjg@424: return absFile; jjg@424: } jjg@424: jjg@424: /** jjg@424: * The file manager that created this archive. jjg@424: */ jjg@57: protected JavacFileManager fileManager; jjg@424: /** jjg@424: * The index for the contents of this archive. jjg@424: */ jjg@103: protected final Map> map; jjg@424: /** jjg@424: * The zip file for the archive. jjg@424: */ jjg@424: protected final ZipFile zfile; jjg@424: /** jjg@424: * A reference to the absolute filename for the zip file for the archive. jjg@424: */ jjg@424: protected Reference absFileRef; jjg@57: jjg@57: /** jjg@57: * A subclass of JavaFileObject representing zip entries. jjg@57: */ jjg@57: public static class ZipFileObject extends BaseFileObject { jjg@57: jjg@57: private String name; jjg@57: ZipArchive zarch; jjg@57: ZipEntry entry; jjg@57: jjg@103: protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) { jjg@57: super(zarch.fileManager); jjg@57: this.zarch = zarch; jjg@57: this.name = name; jjg@57: this.entry = entry; jjg@57: } jjg@57: jjg@415: public URI toUri() { jjg@424: File zipFile = new File(zarch.zfile.getName()); jjg@415: return createJarUri(zipFile, entry.getName()); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getName() { jjg@424: return zarch.zfile.getName() + "(" + entry.getName() + ")"; jjg@415: } jjg@415: jjg@415: @Override jjg@415: public String getShortName() { jjg@424: return new File(zarch.zfile.getName()).getName() + "(" + entry + ")"; 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@424: return zarch.zfile.getInputStream(entry); jjg@57: } jjg@57: jjg@415: @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@424: InputStream in = zarch.zfile.getInputStream(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); jjg@57: if (!ignoreEncodingErrors) { jjg@57: fileManager.cache(this, cb); jjg@57: } 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.getTime(); 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@415: protected String inferBinaryName(Iterable path) { jjg@415: String entryName = entry.getName(); jjg@415: return removeExtension(entryName).replace('/', '.'); jjg@415: } jjg@415: jjg@415: @Override jjg@415: public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { jjg@415: cn.getClass(); jjg@415: // null check jjg@415: if (k == Kind.OTHER && getKind() != k) { jjg@415: return false; jjg@415: } 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 ZipFileObjects 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@57: public boolean equals(Object other) { jjg@424: if (this == other) jjg@424: return true; jjg@424: jjg@424: if (!(other instanceof ZipFileObject)) jjg@57: return false; jjg@424: jjg@57: ZipFileObject o = (ZipFileObject) other; jjg@424: return zarch.getAbsoluteFile().equals(o.zarch.getAbsoluteFile()) jjg@424: && name.equals(o.name); jjg@57: } jjg@57: jjg@57: @Override jjg@57: public int hashCode() { jjg@424: return zarch.getAbsoluteFile().hashCode() + name.hashCode(); jjg@57: } jjg@57: } jjg@57: jjg@57: }