Click here to Skip to main content
15,887,980 members
Articles / Web Development / HTML

Java Class Viewer

Rate me:
Please Sign up or sign in to vote.
4.93/5 (41 votes)
21 Jun 2016MIT8 min read 178.5K   7.8K   96  
Watch the Java class file visually & interactively for the meaning of every byte
/*
 * JTreeZipFile.java    September 26, 2007, 9:36 AM
 *
 * Copyright 2007, FreeInternals.org. All rights reserved.
 * Use is subject to license terms.
 */
package org.freeinternals.javaclassviewer.ui;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;

/**
 * A tree file created from a {@code JarFile} or {@code ZipFile} containing the 
 * Java {@code class} file(s).
 *
 * @author Amos Shi
 * @since JDK 6.0
 */
public class JTreeZipFile extends JTree {

    private static final long serialVersionUID = 4876543219876500000L;
    private final ZipFile zipFile;
    private DefaultMutableTreeNode root = null;

    /**
     * Creates a tree from a {@code JarFile} or {@code ZipFile}.
     *
     * @param zipFile
     */
    public JTreeZipFile(final ZipFile zipFile) {
        if (zipFile == null) {
            throw new IllegalArgumentException("zipFile cannot be null.");
        }

        this.zipFile = zipFile;
        this.generateTreeNodes();
        this.setModel(new DefaultTreeModel(this.root));
    }

    /**
     * Get the {@code JarFile} or {@code ZipFile} of this tree.
     *
     * @return The {@code JarFile} or {@code ZipFile} in the tree
     */
    public ZipFile getZipFile() {
        return this.zipFile;
    }

    private void generateTreeNodes() {
        this.root = new DefaultMutableTreeNode(this.zipFile.getName());

        final Enumeration zipEntries = this.zipFile.entries();
        while (zipEntries.hasMoreElements()) {
            this.addTreeNode((ZipEntry) zipEntries.nextElement());
        }
    }

    private void addTreeNode(final ZipEntry ze) {
        boolean isFolder = false;

        String nodePath = ze.getName();
        if (nodePath == null || nodePath.isEmpty()) {
            return;
        }
        if (nodePath.endsWith("/")) //  This is for windows platform
        {
            isFolder = true;
            nodePath = nodePath.substring(0, nodePath.length() - 1);
            if (nodePath.isEmpty()) {
                return;
            }
        }

        final String[] nodePaths = nodePath.split("/");
        DefaultMutableTreeNode node = this.root;
        for (int i = 0; i < nodePaths.length; i++) {
            if ((i + 1) == nodePaths.length) {
                node = this.addTreeNode(node, nodePaths[i], isFolder, ze);
            } else {
                node = this.addTreeNode(node, nodePaths[i], isFolder, null);
            }
        }
    }

    private DefaultMutableTreeNode addTreeNode(final DefaultMutableTreeNode parent, final String childText, final boolean isFolder, final ZipEntry ze) {
        if (parent == null) {
            throw new IllegalArgumentException("parent node cannot be null.");
        }
        if (childText == null || childText.isEmpty()) {
            throw new IllegalArgumentException("child node text cannot be null or empty.");
        }

        DefaultMutableTreeNode child = null;

        if (parent.isLeaf()) {
            child = new DefaultMutableTreeNode(
                    new JTreeNodeZipFile(childText, ze));
            parent.add(child);
        } else {
            final int childCount = parent.getChildCount();
            TreeNode childTreeNode = null;
            for (int i = 0; i < childCount; i++) {
                childTreeNode = parent.getChildAt(i);

                if (childText.equals(childTreeNode.toString()) && childTreeNode instanceof DefaultMutableTreeNode) {
                    child = (DefaultMutableTreeNode) childTreeNode;
                }
            }

            if (child == null) {
                // Change the tree node icon here accroding to 'isFolder'.
                child = new DefaultMutableTreeNode(
                        new JTreeNodeZipFile(childText, ze));
                parent.add(child);
            }
        }

        return child;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
United States United States
Deliver useful software to the world.

Comments and Discussions