Click here to Skip to main content
15,886,578 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.3K   7.8K   96  
Watch the Java class file visually & interactively for the meaning of every byte
/*
 * JPanelForTree.java    Nov 07, 2009, 08:41
 *
 * Copyright 2009, FreeInternals.org. All rights reserved.
 * Use is subject to license terms.
 */
package org.freeinternals.commonlib.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.xmltree.XMLTreeTable;
import org.freeinternals.commonlib.ui.xmltree.XMLTreeTableModel;
import org.xml.sax.InputSource;

/**
 *
 * @author Amos Shi
 */
public class JXMLViewer extends JPanel {

    private static final long serialVersionUID = 4876543219876500005L;
    public final JTabbedPane tabbedPane;

    public JXMLViewer(InputStream xml){
        this.tabbedPane = new JTabbedPane();

        try {
            XMLTreeTable treeTable = new XMLTreeTable(new XMLTreeTableModel(new InputSource(xml)));
            this.tabbedPane.addTab("XML View", new JScrollPaneTreeTable(treeTable));
        } catch (Exception ex) {
            //Logger.getLogger(JXMLViewer.class.getName()).log(Level.SEVERE, null, ex);

            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());

            // The title
            JLabel label = new JLabel("Exception occured when parsing the XML data.");
            label.setFont(new Font(Font.DIALOG, Font.BOLD, 16));
            label.setForeground(Color.red);
            panel.add(label, BorderLayout.NORTH);

            // The exception
            ByteArrayOutputStream output = new ByteArrayOutputStream(2096);
            ex.printStackTrace(new PrintStream(output));

            JTextArea textException = new JTextArea();
            textException.setLineWrap(true);
            textException.setEditable(false);
            textException.setText(output.toString());

            panel.add(new JScrollPane(textException), BorderLayout.CENTER);

            //
            this.tabbedPane.addTab("XML View", panel);
        }

        if (xml instanceof PosDataInputStream) {
            byte[] buf = ((PosDataInputStream) xml).getBuf();
            StringBuilder sb = new StringBuilder(buf.length + 1);
            for (byte b : buf) {
                sb.append((char) b);
            }

            JTextArea textPlainText = new JTextArea(sb.toString());
            textPlainText.setLineWrap(true);
            textPlainText.setEditable(false);
            tabbedPane.addTab("XML Plain Text", new JScrollPane(textPlainText));
        }

        this.setLayout(new BorderLayout());
        this.add(this.tabbedPane, BorderLayout.CENTER);
    }

    @SuppressWarnings("PackageVisibleInnerClass")
    class JScrollPaneTreeTable extends JScrollPane {

        private static final long serialVersionUID = 4876543219876500005L;

        private JScrollPaneTreeTable(Component component) {
            super(component);
        }

        @Override
        public void setColumnHeaderView(Component view) {
        }
    }
}

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