* 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.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import org.fife.rsta.ac.java.Util;
import org.fife.rsta.ac.java.classreader.ClassFile;
* Information about a jar of classes to add to the "build path."
* @see ClasspathLibraryInfo
public class JarLibraryInfo extends LibraryInfo {
public JarLibraryInfo(String jarFile) {
public JarLibraryInfo(File jarFile) {
public JarLibraryInfo(File jarFile, SourceLocation sourceLoc) {
setSourceLocation(sourceLoc);
* Compares this <code>LibraryInfo</code> to another one. Two instances of
* this class are only considered equal if they represent the same class
* file location. Source attachment is irrelevant.
* @return The sort order of these two library infos.
public int compareTo(Object o) {
if (o instanceof JarLibraryInfo) {
result = jarFile.compareTo(((JarLibraryInfo)o).jarFile);
public ClassFile createClassFile(String entryName) throws IOException {
JarFile jar = new JarFile(jarFile);
JarEntry entry = (JarEntry)jar.getEntry(entryName);
System.err.println("ERROR: Invalid entry: " + entryName);
DataInputStream in = new DataInputStream(
new BufferedInputStream(jar.getInputStream(entry)));
ClassFile cf = new ClassFile(in);
public TreeMap createPackageMap() throws IOException {
TreeMap packageMap = new TreeMap();
JarFile jar = new JarFile(jarFile);
Enumeration e = jar.entries();
while (e.hasMoreElements()) {
ZipEntry entry = (ZipEntry)e.nextElement();
String entryName = entry.getName();
if (entryName.endsWith(".class")) {
entryName = entryName.substring(0, entryName.length()-6);
String[] items = Util.splitOnChar(entryName, '/');
for (int i=0; i<items.length-1; i++) {
TreeMap submap = (TreeMap)m.get(items[i]);
String className = items[items.length-1];
m.put(className, null); // Lazily set value to ClassFile later
public long getLastModified() {
return jarFile.lastModified();
public String getLocationAsString() {
return jarFile.getAbsolutePath();
* Returns the jar file this instance is wrapping.
public File getJarFile() {
return jarFile.hashCode();
* Sets the jar file location.
* @param jarFile The jar file location. This cannot be <code>null</code>.
private void setJarFile(File jarFile) {
if (jarFile==null || !jarFile.exists()) {
String name = jarFile==null ? "null" : jarFile.getAbsolutePath();
throw new IllegalArgumentException("Jar does not exist: " + name);
* Returns a string representation of this jar information. Useful for
* @return A string representation of this object.
public String toString() {
return "[JarLibraryInfo: " +
"jar=" + jarFile.getAbsolutePath() +
"; source=" + getSourceLocation() +