Tue, 26 Aug 2008 14:52:59 -0700
6508981: cleanup file separator handling in JavacFileManager
Reviewed-by: mcimadamore
jjg@57 | 1 | /* |
jjg@57 | 2 | * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved. |
jjg@57 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@57 | 4 | * |
jjg@57 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@57 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@57 | 7 | * published by the Free Software Foundation. Sun designates this |
jjg@57 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@57 | 9 | * by Sun in the LICENSE file that accompanied this code. |
jjg@57 | 10 | * |
jjg@57 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@57 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@57 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@57 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@57 | 15 | * accompanied this code). |
jjg@57 | 16 | * |
jjg@57 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@57 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@57 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@57 | 20 | * |
jjg@57 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jjg@57 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jjg@57 | 23 | * have any questions. |
jjg@57 | 24 | */ |
jjg@57 | 25 | |
jjg@57 | 26 | package com.sun.tools.javac.file; |
jjg@57 | 27 | |
jjg@103 | 28 | import java.io.File; |
jjg@57 | 29 | import java.io.IOException; |
jjg@57 | 30 | import java.io.InputStream; |
jjg@57 | 31 | import java.io.OutputStream; |
jjg@57 | 32 | import java.io.Writer; |
jjg@57 | 33 | import java.net.URI; |
jjg@57 | 34 | import java.nio.ByteBuffer; |
jjg@57 | 35 | import java.nio.CharBuffer; |
jjg@57 | 36 | import java.nio.charset.CharsetDecoder; |
jjg@103 | 37 | import java.util.Enumeration; |
jjg@103 | 38 | import java.util.HashMap; |
jjg@103 | 39 | import java.util.Map; |
jjg@103 | 40 | import java.util.Set; |
jjg@103 | 41 | import java.util.zip.ZipEntry; |
jjg@103 | 42 | import java.util.zip.ZipFile; |
jjg@103 | 43 | |
jjg@103 | 44 | import javax.tools.JavaFileObject; |
jjg@103 | 45 | |
jjg@103 | 46 | import com.sun.tools.javac.file.JavacFileManager.Archive; |
jjg@103 | 47 | import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
jjg@103 | 48 | import com.sun.tools.javac.file.RelativePath.RelativeFile; |
jjg@103 | 49 | import com.sun.tools.javac.util.List; |
jjg@57 | 50 | |
jjg@57 | 51 | public class ZipArchive implements Archive { |
jjg@57 | 52 | |
jjg@57 | 53 | public ZipArchive(JavacFileManager fm, ZipFile zdir) throws IOException { |
jjg@103 | 54 | this(fm, zdir, true); |
jjg@103 | 55 | } |
jjg@103 | 56 | |
jjg@103 | 57 | protected ZipArchive(JavacFileManager fm, ZipFile zdir, boolean initMap) throws IOException { |
jjg@57 | 58 | this.fileManager = fm; |
jjg@57 | 59 | this.zdir = zdir; |
jjg@103 | 60 | this.map = new HashMap<RelativeDirectory,List<String>>(); |
jjg@103 | 61 | if (initMap) |
jjg@103 | 62 | initMap(); |
jjg@103 | 63 | } |
jjg@103 | 64 | |
jjg@103 | 65 | protected void initMap() throws IOException { |
jjg@57 | 66 | for (Enumeration<? extends ZipEntry> e = zdir.entries(); e.hasMoreElements(); ) { |
jjg@57 | 67 | ZipEntry entry; |
jjg@57 | 68 | try { |
jjg@57 | 69 | entry = e.nextElement(); |
jjg@57 | 70 | } catch (InternalError ex) { |
jjg@57 | 71 | IOException io = new IOException(); |
jjg@57 | 72 | io.initCause(ex); // convenience constructors added in Mustang :-( |
jjg@57 | 73 | throw io; |
jjg@57 | 74 | } |
jjg@57 | 75 | addZipEntry(entry); |
jjg@57 | 76 | } |
jjg@57 | 77 | } |
jjg@57 | 78 | |
jjg@57 | 79 | void addZipEntry(ZipEntry entry) { |
jjg@57 | 80 | String name = entry.getName(); |
jjg@57 | 81 | int i = name.lastIndexOf('/'); |
jjg@103 | 82 | RelativeDirectory dirname = new RelativeDirectory(name.substring(0, i+1)); |
jjg@57 | 83 | String basename = name.substring(i+1); |
jjg@57 | 84 | if (basename.length() == 0) |
jjg@57 | 85 | return; |
jjg@57 | 86 | List<String> list = map.get(dirname); |
jjg@57 | 87 | if (list == null) |
jjg@57 | 88 | list = List.nil(); |
jjg@57 | 89 | list = list.prepend(basename); |
jjg@57 | 90 | map.put(dirname, list); |
jjg@57 | 91 | } |
jjg@57 | 92 | |
jjg@103 | 93 | public boolean contains(RelativePath name) { |
jjg@103 | 94 | RelativeDirectory dirname = name.dirname(); |
jjg@103 | 95 | String basename = name.basename(); |
jjg@57 | 96 | if (basename.length() == 0) |
jjg@57 | 97 | return false; |
jjg@57 | 98 | List<String> list = map.get(dirname); |
jjg@57 | 99 | return (list != null && list.contains(basename)); |
jjg@57 | 100 | } |
jjg@57 | 101 | |
jjg@103 | 102 | public List<String> getFiles(RelativeDirectory subdirectory) { |
jjg@57 | 103 | return map.get(subdirectory); |
jjg@57 | 104 | } |
jjg@57 | 105 | |
jjg@103 | 106 | public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
jjg@103 | 107 | ZipEntry ze = new RelativeFile(subdirectory, file).getZipEntry(zdir); |
jjg@57 | 108 | return new ZipFileObject(this, file, ze); |
jjg@57 | 109 | } |
jjg@57 | 110 | |
jjg@103 | 111 | public Set<RelativeDirectory> getSubdirectories() { |
jjg@57 | 112 | return map.keySet(); |
jjg@57 | 113 | } |
jjg@57 | 114 | |
jjg@57 | 115 | public void close() throws IOException { |
jjg@57 | 116 | zdir.close(); |
jjg@57 | 117 | } |
jjg@57 | 118 | |
jjg@103 | 119 | public String toString() { |
jjg@103 | 120 | return "ZipArchive[" + zdir.getName() + "]"; |
jjg@103 | 121 | } |
jjg@103 | 122 | |
jjg@57 | 123 | protected JavacFileManager fileManager; |
jjg@103 | 124 | protected final Map<RelativeDirectory,List<String>> map; |
jjg@57 | 125 | protected final ZipFile zdir; |
jjg@57 | 126 | |
jjg@57 | 127 | /** |
jjg@57 | 128 | * A subclass of JavaFileObject representing zip entries. |
jjg@57 | 129 | */ |
jjg@57 | 130 | public static class ZipFileObject extends BaseFileObject { |
jjg@57 | 131 | |
jjg@57 | 132 | private String name; |
jjg@57 | 133 | ZipArchive zarch; |
jjg@57 | 134 | ZipEntry entry; |
jjg@57 | 135 | |
jjg@103 | 136 | protected ZipFileObject(ZipArchive zarch, String name, ZipEntry entry) { |
jjg@57 | 137 | super(zarch.fileManager); |
jjg@57 | 138 | this.zarch = zarch; |
jjg@57 | 139 | this.name = name; |
jjg@57 | 140 | this.entry = entry; |
jjg@57 | 141 | } |
jjg@57 | 142 | |
jjg@57 | 143 | public InputStream openInputStream() throws IOException { |
jjg@57 | 144 | return zarch.zdir.getInputStream(entry); |
jjg@57 | 145 | } |
jjg@57 | 146 | |
jjg@57 | 147 | public OutputStream openOutputStream() throws IOException { |
jjg@57 | 148 | throw new UnsupportedOperationException(); |
jjg@57 | 149 | } |
jjg@57 | 150 | |
jjg@57 | 151 | protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { |
jjg@57 | 152 | return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors); |
jjg@57 | 153 | } |
jjg@57 | 154 | |
jjg@57 | 155 | public Writer openWriter() throws IOException { |
jjg@57 | 156 | throw new UnsupportedOperationException(); |
jjg@57 | 157 | } |
jjg@57 | 158 | |
jjg@57 | 159 | @Deprecated |
jjg@57 | 160 | public String getName() { |
jjg@57 | 161 | return name; |
jjg@57 | 162 | } |
jjg@57 | 163 | |
jjg@57 | 164 | public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { |
jjg@57 | 165 | cn.getClass(); |
jjg@57 | 166 | // null check |
jjg@57 | 167 | if (k == Kind.OTHER && getKind() != k) { |
jjg@57 | 168 | return false; |
jjg@57 | 169 | } |
jjg@57 | 170 | return name.equals(cn + k.extension); |
jjg@57 | 171 | } |
jjg@57 | 172 | |
jjg@57 | 173 | @Deprecated |
jjg@57 | 174 | public String getPath() { |
jjg@57 | 175 | return zarch.zdir.getName() + "(" + entry + ")"; |
jjg@57 | 176 | } |
jjg@57 | 177 | |
jjg@57 | 178 | public long getLastModified() { |
jjg@57 | 179 | return entry.getTime(); |
jjg@57 | 180 | } |
jjg@57 | 181 | |
jjg@57 | 182 | public boolean delete() { |
jjg@57 | 183 | throw new UnsupportedOperationException(); |
jjg@57 | 184 | } |
jjg@57 | 185 | |
jjg@57 | 186 | public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
jjg@57 | 187 | CharBuffer cb = fileManager.getCachedContent(this); |
jjg@57 | 188 | if (cb == null) { |
jjg@57 | 189 | InputStream in = zarch.zdir.getInputStream(entry); |
jjg@57 | 190 | try { |
jjg@57 | 191 | ByteBuffer bb = fileManager.makeByteBuffer(in); |
jjg@57 | 192 | JavaFileObject prev = fileManager.log.useSource(this); |
jjg@57 | 193 | try { |
jjg@57 | 194 | cb = fileManager.decode(bb, ignoreEncodingErrors); |
jjg@57 | 195 | } finally { |
jjg@57 | 196 | fileManager.log.useSource(prev); |
jjg@57 | 197 | } |
jjg@57 | 198 | fileManager.recycleByteBuffer(bb); |
jjg@57 | 199 | if (!ignoreEncodingErrors) { |
jjg@57 | 200 | fileManager.cache(this, cb); |
jjg@57 | 201 | } |
jjg@57 | 202 | } finally { |
jjg@57 | 203 | in.close(); |
jjg@57 | 204 | } |
jjg@57 | 205 | } |
jjg@57 | 206 | return cb; |
jjg@57 | 207 | } |
jjg@57 | 208 | |
jjg@57 | 209 | @Override |
jjg@57 | 210 | public boolean equals(Object other) { |
jjg@57 | 211 | if (!(other instanceof ZipFileObject)) { |
jjg@57 | 212 | return false; |
jjg@57 | 213 | } |
jjg@57 | 214 | ZipFileObject o = (ZipFileObject) other; |
jjg@57 | 215 | return zarch.zdir.equals(o.zarch.zdir) || name.equals(o.name); |
jjg@57 | 216 | } |
jjg@57 | 217 | |
jjg@57 | 218 | @Override |
jjg@57 | 219 | public int hashCode() { |
jjg@57 | 220 | return zarch.zdir.hashCode() + name.hashCode(); |
jjg@57 | 221 | } |
jjg@57 | 222 | |
jjg@57 | 223 | public String getZipName() { |
jjg@57 | 224 | return zarch.zdir.getName(); |
jjg@57 | 225 | } |
jjg@57 | 226 | |
jjg@57 | 227 | public String getZipEntryName() { |
jjg@57 | 228 | return entry.getName(); |
jjg@57 | 229 | } |
jjg@57 | 230 | |
jjg@57 | 231 | public URI toUri() { |
jjg@57 | 232 | String zipName = new File(getZipName()).toURI().normalize().getPath(); |
jjg@57 | 233 | String entryName = getZipEntryName(); |
jjg@57 | 234 | return URI.create("jar:" + zipName + "!" + entryName); |
jjg@57 | 235 | } |
jjg@57 | 236 | |
jjg@57 | 237 | @Override |
jjg@57 | 238 | protected String inferBinaryName(Iterable<? extends File> path) { |
jjg@57 | 239 | String entryName = getZipEntryName(); |
jjg@57 | 240 | return removeExtension(entryName).replace('/', '.'); |
jjg@57 | 241 | } |
jjg@57 | 242 | } |
jjg@57 | 243 | |
jjg@57 | 244 | } |