* Copyright (C) 2010 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
package org.fife.rsta.ac.java.buildpath;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.fife.rsta.ac.java.classreader.ClassFile;
import org.fife.rsta.ac.java.rjc.ast.CompilationUnit;
import org.fife.rsta.ac.java.rjc.lexer.Scanner;
import org.fife.rsta.ac.java.rjc.parser.ASTFactory;
* Represents source inside a zip or jar file. The source can be either in
* a "<code>src/</code>" subfolder, or at the root level of the archive. This
* class is useful for the JDK or other libraries that come with a
* <code>src.zip</code> file (<code>src.jar</code> on OS X).
public class ZipSourceLocation implements SourceLocation {
* @param archive The archive containing the source. This should be an
* absolute path to ensure correctness.
public ZipSourceLocation(String archive) {
* @param archive The archive containing the source.
public ZipSourceLocation(File archive) {
public CompilationUnit getCompilationUnit(ClassFile cf) throws IOException {
CompilationUnit cu = null;
ZipFile zipFile = new ZipFile(archive);
String entryName = cf.getClassName(true).replaceAll("\\.", "/");
//System.out.println("DEBUG: entry name: " + entryName);
ZipEntry entry = zipFile.getEntry(entryName);
// Seen in some src.jar files, for example OS X's src.jar
entry = zipFile.getEntry("src/" + entryName);
InputStream in = zipFile.getInputStream(entry);
Scanner s = new Scanner(new InputStreamReader(in));
cu = new ASTFactory().getCompilationUnit(entryName, s);
zipFile.close(); // Closes the input stream too
public String getLocationAsString() {
return archive.getAbsolutePath();