01: /*
02: This library is free software; you can redistribute it and/or
03: modify it under the terms of the GNU General Public
04: License as published by the Free Software Foundation; either
05: version 2 of the license, or (at your option) any later version.
06: */
07:
08: package org.gjt.jclasslib.browser.config.classpath;
09:
10: import javax.swing.tree.DefaultTreeModel;
11: import java.io.File;
12: import java.io.IOException;
13: import java.util.Enumeration;
14: import java.util.jar.JarEntry;
15: import java.util.jar.JarFile;
16:
17: /**
18: Classpath entry for an archive.
19:
20: @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
21: @version $Revision: 1.1 $ $Date: 2003/08/18 08:10:15 $
22: */
23: public class ClasspathArchiveEntry extends ClasspathEntry {
24:
25: public FindResult findClass(String className) {
26:
27: File file = getFile();
28: if (file == null) {
29: return null;
30: }
31: className = className.replace('.', '/') + ".class";
32: try {
33: JarFile jarFile = new JarFile(file);
34: JarEntry entry = jarFile.getJarEntry(className);
35: if (entry != null) {
36: FindResult findResult = new FindResult(this , file
37: .getPath()
38: + "!" + className);
39: return findResult;
40: }
41: } catch (IOException e) {
42: }
43:
44: return null;
45: }
46:
47: public void mergeClassesIntoTree(DefaultTreeModel model, boolean reset) {
48:
49: File archive = getFile();
50: if (archive == null) {
51: return;
52: }
53:
54: try {
55: JarFile jarFile = new JarFile(archive);
56: Enumeration enum = jarFile.entries();
57: while (enum.hasMoreElements()) {
58: JarEntry entry = (JarEntry)enum.nextElement();
59: if (!entry.isDirectory() && entry.getName().toLowerCase().endsWith(CLASSFILE_SUFFIX)) {
60: addEntry((stripClassSuffix(entry.getName())), model, reset);
61: }
62: }
63: } catch (IOException ex) {
64: }
65: }
66:
67: private void addEntry(String path, DefaultTreeModel model,
68: boolean reset) {
69:
70: String[] pathComponents = path.replace('\\', '/').split("/");
71: ClassTreeNode currentNode = (ClassTreeNode) model.getRoot();
72: for (int i = 0; i < pathComponents.length; i++) {
73: String pathComponent = pathComponents[i];
74: currentNode = addOrFindNode(pathComponent, currentNode,
75: i < pathComponents.length - 1, model, reset);
76: }
77: }
78:
79: }
|