27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.util.Set; |
29 import java.util.Set; |
30 import javax.tools.JavaFileObject; |
30 import javax.tools.JavaFileObject; |
31 |
31 |
32 import com.sun.tools.javac.file.JavacFileManager.Archive; |
|
33 import com.sun.tools.javac.util.List; |
|
34 import java.io.ByteArrayInputStream; |
32 import java.io.ByteArrayInputStream; |
35 import java.io.File; |
33 import java.io.File; |
36 import java.io.FileNotFoundException; |
|
37 import java.io.InputStream; |
34 import java.io.InputStream; |
38 import java.io.OutputStream; |
35 import java.io.OutputStream; |
39 import java.io.Writer; |
36 import java.io.Writer; |
40 import java.net.URI; |
37 import java.net.URI; |
41 import java.nio.ByteBuffer; |
38 import java.nio.ByteBuffer; |
42 import java.nio.CharBuffer; |
39 import java.nio.CharBuffer; |
43 import java.nio.charset.CharsetDecoder; |
40 import java.nio.charset.CharsetDecoder; |
44 |
41 |
|
42 import com.sun.tools.javac.file.JavacFileManager.Archive; |
|
43 import com.sun.tools.javac.file.RelativePath.RelativeDirectory; |
|
44 import com.sun.tools.javac.file.RelativePath.RelativeFile; |
|
45 import com.sun.tools.javac.util.List; |
|
46 |
45 public class ZipFileIndexArchive implements Archive { |
47 public class ZipFileIndexArchive implements Archive { |
46 |
48 |
47 private final ZipFileIndex zfIndex; |
49 private final ZipFileIndex zfIndex; |
48 private JavacFileManager fileManager; |
50 private JavacFileManager fileManager; |
49 |
51 |
51 super(); |
53 super(); |
52 this.fileManager = fileManager; |
54 this.fileManager = fileManager; |
53 this.zfIndex = zdir; |
55 this.zfIndex = zdir; |
54 } |
56 } |
55 |
57 |
56 public boolean contains(String name) { |
58 public boolean contains(RelativePath name) { |
57 return zfIndex.contains(name); |
59 return zfIndex.contains(name); |
58 } |
60 } |
59 |
61 |
60 public List<String> getFiles(String subdirectory) { |
62 public List<String> getFiles(RelativeDirectory subdirectory) { |
61 return zfIndex.getFiles((subdirectory.endsWith("/") || subdirectory.endsWith("\\")) ? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory); |
63 return zfIndex.getFiles(subdirectory); |
62 } |
64 } |
63 |
65 |
64 public JavaFileObject getFileObject(String subdirectory, String file) { |
66 public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) { |
65 String fullZipFileName = subdirectory + file; |
67 RelativeFile fullZipFileName = new RelativeFile(subdirectory, file); |
66 ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName); |
68 ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName); |
67 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath()); |
69 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath()); |
68 return ret; |
70 return ret; |
69 } |
71 } |
70 |
72 |
71 public Set<String> getSubdirectories() { |
73 public Set<RelativeDirectory> getSubdirectories() { |
72 return zfIndex.getAllDirectories(); |
74 return zfIndex.getAllDirectories(); |
73 } |
75 } |
74 |
76 |
75 public void close() throws IOException { |
77 public void close() throws IOException { |
76 zfIndex.close(); |
78 zfIndex.close(); |
|
79 } |
|
80 |
|
81 public String toString() { |
|
82 return "ZipFileIndexArchive[" + zfIndex + "]"; |
77 } |
83 } |
78 |
84 |
79 /** |
85 /** |
80 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation. |
86 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation. |
81 */ |
87 */ |
179 } |
185 } |
180 |
186 |
181 public URI toUri() { |
187 public URI toUri() { |
182 String zipName = new File(getZipName()).toURI().normalize().getPath(); |
188 String zipName = new File(getZipName()).toURI().normalize().getPath(); |
183 String entryName = getZipEntryName(); |
189 String entryName = getZipEntryName(); |
184 if (File.separatorChar != '/') { |
|
185 entryName = entryName.replace(File.separatorChar, '/'); |
|
186 } |
|
187 return URI.create("jar:" + zipName + "!" + entryName); |
190 return URI.create("jar:" + zipName + "!" + entryName); |
188 } |
191 } |
189 |
192 |
190 private byte[] read() throws IOException { |
193 private byte[] read() throws IOException { |
191 if (entry == null) { |
194 assert entry != null; // see constructor |
192 entry = zfIndex.getZipIndexEntry(name); |
|
193 if (entry == null) |
|
194 throw new FileNotFoundException(); |
|
195 } |
|
196 return zfIndex.read(entry); |
195 return zfIndex.read(entry); |
197 } |
196 } |
198 |
197 |
199 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
198 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { |
200 CharBuffer cb = fileManager.getCachedContent(this); |
199 CharBuffer cb = fileManager.getCachedContent(this); |
220 |
219 |
221 @Override |
220 @Override |
222 protected String inferBinaryName(Iterable<? extends File> path) { |
221 protected String inferBinaryName(Iterable<? extends File> path) { |
223 String entryName = getZipEntryName(); |
222 String entryName = getZipEntryName(); |
224 if (zfIndex.symbolFilePrefix != null) { |
223 if (zfIndex.symbolFilePrefix != null) { |
225 String prefix = zfIndex.symbolFilePrefix; |
224 String prefix = zfIndex.symbolFilePrefix.path; |
226 if (entryName.startsWith(prefix)) |
225 if (entryName.startsWith(prefix)) |
227 entryName = entryName.substring(prefix.length()); |
226 entryName = entryName.substring(prefix.length()); |
228 } |
227 } |
229 return removeExtension(entryName).replace(File.separatorChar, '.'); |
228 return removeExtension(entryName).replace('/', '.'); |
230 } |
229 } |
231 } |
230 } |
232 |
231 |
233 } |
232 } |