www.pudn.com > DomView.zip > DomViewPanel.java
/********************************************************************
Domview 1.0 displays contents of an XML DOM object
Copyright (C) 1999 The Bean Factory, LLC.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Author: Sun Koh, The Bean Factory, LLC.
*******************************************************************/
import java.awt.*; //AWT classes
import java.awt.event.*; //AWT event classes
import java.util.*; //Vectors, etc
import java.io.*; //Serializable, etc
import java.net.*; //Network classes
import javax.swing.*; //Swing classes
import javax.swing.event.*; //Swing events
import javax.swing.table.*; //JTable models
import javax.swing.tree.*; //JTree models
import javax.swing.border.*; //JComponent Borders
import org.w3c.dom.*;
/********************************************************************
DomViewPanel displays the content of DOM in a JTree.
When an item is slected from the tree, JTextArea displays interfaces
and accessor methods and its values.
@version : 1.0
@author : Sun Koh
********************************************************************/
public class DomViewPanel
extends JPanel
implements java.io.Serializable {
//
// Data Members
//
protected String parser;
protected DomView mainFrame;
protected Document doc;
protected JTree tree;
protected DomTreeModel model;
protected JTextArea taOutput;
protected String type;
public static final Class[] classType = {
Attr.class,
CDATASection.class,
Comment.class,
Document.class,
DocumentFragment.class,
DocumentType.class,
Element.class,
Entity.class,
EntityReference.class,
Notation.class,
ProcessingInstruction.class,
Text.class,
CharacterData.class,
Node.class
};
//
// Methods
//
/**
Default Constructor
@param d DomView main frame
*/
public DomViewPanel(DomView d, String parser){
this.parser=parser;
mainFrame = d;
//create a scroll panel for JTree;
tree = new JTree();
tree.putClientProperty("JTree.lineStyle", "Angled");
tree.setCellRenderer(new DomTreeCellRenderer());
tree.addTreeSelectionListener(new SelectionAction(this));
tree.setModel(null);
//openFile("d:/java/xml/birds1.xml");
JScrollPane spTree = new JScrollPane(tree);
JPanel pTree = new JPanel(new BorderLayout());
Dimension dm = new Dimension(200, 200);
pTree.setPreferredSize(dm);
pTree.add(spTree, BorderLayout.CENTER);
//create a scroll panel for query result
taOutput = new JTextArea();
JScrollPane spOutput = new JScrollPane(taOutput);
//create split panel
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
sp.setOneTouchExpandable(true);
sp.setLeftComponent(pTree);
sp.setRightComponent(spOutput);
setLayout(new BorderLayout());
add(sp, BorderLayout.CENTER);
}
/**
Get the information and value of an item when the item
is selected from the tree
@Param path a treepath
*/
public void getInfo(TreePath path){
java.util.List interfaces;
Hashtable typeValues = new Hashtable();
Hashtable nodeValues = new Hashtable();
Hashtable cDataValues = new Hashtable();
//given a path get a object
Object o = path.getLastPathComponent();
//get interfaces a node implements
interfaces = getInterfaces(o);
//find type of node
switch ( ((Node)o).getNodeType() ) {
case Node.ATTRIBUTE_NODE:
type="Attribute";
typeValues = getAttributeValues(o);
break;
case Node.CDATA_SECTION_NODE:
//since CDATASection implements Text interest
//and there is no method in CDATASection
//make the type as text.
type="Text";
typeValues = getTextValues(o);
cDataValues = getCDataValues(o);
break;
case Node.COMMENT_NODE:
type="Comment";
typeValues = getCommentValues(o);
cDataValues = getCDataValues(o);
break;
case Node.DOCUMENT_FRAGMENT_NODE:
type="DocumentFragment";
typeValues = getDocumentFragmentValues(o);
break;
case Node.DOCUMENT_NODE:
typeValues = getDocumentValues(o);
type="Document";
break;
case Node.DOCUMENT_TYPE_NODE:
type="DocumentType";
typeValues = getDocumentTypeValues(o);
break;
case Node.ELEMENT_NODE:
type="Element";
typeValues = getElementValues(o);
break;
case Node.ENTITY_NODE:
type="Entity";
typeValues = getEntityValues(o);
break;
case Node.ENTITY_REFERENCE_NODE:
type="EntityReference";
typeValues = getEntityReferenceValues(o);
break;
case Node.NOTATION_NODE:
type="Notation";
typeValues = getNotationValues(o);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
type="ProcessionInstruction";
typeValues = getProcessingInstructionValues(o);
break;
case Node.TEXT_NODE:
type="Text";
typeValues = getTextValues(o);
cDataValues = getCDataValues(o);
break;
}
//get values of accessor method in Node interface
nodeValues = getNodeValues(o);
//display the out in the JTextArea
displayResult(interfaces, typeValues, nodeValues, cDataValues);
}
/**
Get Node interface accessor methods and values
@Param o a Node object
@Param table a hashtable
*/
public Hashtable getNodeValues(Object o){
Hashtable table = new Hashtable();
//getting attributes
try {
table.put("getAttributes()", ((Node)o).getAttributes().toString() );
}
catch ( NullPointerException e ) {
table.put("getAttributes()", "null" );
}
//get a list of child
try {
if ( ((Node)o).hasChildNodes() ) {
NodeList nl = ((Node)o).getChildNodes();
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < nl.getLength(); i++ ) {
if ( nl.item(i).getNodeType() != Node.ELEMENT_NODE ) {
sb.append(nl.item(i).getNodeName()+", ");
}
else
sb.append("<"+nl.item(i).getNodeName()+">, ");
}
table.put("getChildNodes()", new String (sb) );
}
else table.put("getChildNodes()", "null");
}
catch ( NullPointerException e ) {
table.put("getChildNodes()", "null" );
}
//getting first child
try {
if ( ((Node)o).getFirstChild().getNodeType() != Node.ELEMENT_NODE ) {
table.put("getFirstChild()", (((Node)o).getFirstChild()).getNodeName() );
}
else
table.put("getFirstChild()", "<"+((Node)o).getFirstChild().getNodeName()+"> ... "+
""+((Node)o).getFirstChild().getNodeName()+">" );
}
catch ( NullPointerException e ) {
table.put("getFirstChild()", "null" );
}
//getting last child
try {
if ( ((Node)o).getLastChild().getNodeType() != Node.ELEMENT_NODE ) {
table.put("getLastChild()", (((Node)o).getLastChild()).getNodeName() );
}
else
table.put("getLastChild()", "<"+((Node)o).getLastChild().getNodeName()+"> ... "+
""+((Node)o).getLastChild().getNodeName()+">" );
}
catch ( NullPointerException e ) {
table.put("getLastChild()", "null" );
}
//getting next sibling
try {
if ( ((Node)o).getNextSibling().getNodeType() != Node.ELEMENT_NODE ) {
table.put("getNextSibling()", (((Node)o).getNextSibling()).getNodeName() );
}
else
table.put("getNextSibling()", "<"+((Node)o).getNextSibling().getNodeName()+"> ... "+
""+((Node)o).getNextSibling().getNodeName()+">" );
}
catch ( Exception e ) {
table.put("getNextSibling()", "null" );
}
//getting node name
try {
table.put("getNodeName()", ((Node)o).getNodeName() );
}
catch ( NullPointerException e ) {
table.put("getNodeName()", "null" );
}
//getting node type
try {
table.put("getNodeType()", new Short( ((Node)o).getNodeType()).toString()+
" (="+getNodeTypeName(o)+")" );
}
catch ( NullPointerException e ) {
table.put("getNodeType()", "null" );
}
//getting node value
try {
table.put("getNodeValue()", ((Node)o).getNodeValue() );
}
catch ( NullPointerException e ) {
table.put("getNodeValue()", "null" );
}
//getting owner document
try {
table.put("getOwnerDocument()", (((Node)o).getOwnerDocument()).toString() );
}
catch ( NullPointerException e ) {
table.put("getOwnerDocument()", "null" );
}
//getting parent node
try {
if ( ((Node)o).getParentNode().getNodeType() != Node.ELEMENT_NODE ) {
table.put("getParentNode()", (((Node)o).getParentNode()).getNodeName() );
}
else
table.put("getParentNode()", "<"+((Node)o).getParentNode().getNodeName()+"> ... "+
""+((Node)o).getParentNode().getNodeName()+">" );
}
catch ( NullPointerException e ) {
table.put("getParentNode()", "null" );
}
//getting previous sibling
try {
if ( ((Node)o).getPreviousSibling().getNodeType() != Node.ELEMENT_NODE ) {
table.put("getPreviousSibling()", (((Node)o).getPreviousSibling()).getNodeName() );
}
else
table.put("getPreviousSibling()", "<"+((Node)o).getPreviousSibling().getNodeName()+"> ... "+
""+((Node)o).getPreviousSibling().getNodeName()+">" );
}
catch ( NullPointerException e ) {
table.put("getPreviousSibling()", "null" );
}
//whether it has child
try {
table.put("hasChildNodes()", new Boolean(((Node)o).hasChildNodes()).toString());
}
catch ( NullPointerException e ) {
table.put("hasChildNodes()", "null" );
}
return table;
}
/**
Get Attribute interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getAttributeValues(Object o){
Hashtable table = new Hashtable();
table.put("getName()", ((Attr)o).getName() );
table.put("getSpecified()", new Boolean (((Attr)o).getName()).toString() );
table.put("getValue()", ((Attr)o).getValue() );
table = new Hashtable();
return table;
}
/**
Get CDATASection interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getCDATASectionValues(Object o){
Hashtable table = new Hashtable();
//There is not any method in the CDATASection interface
return table;
}
/**
Get Comment interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getCommentValues(Object o){
Hashtable table = new Hashtable();
//there is not any method in the Comment interface
return table;
}
/**
Get DocumentFragment interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getDocumentFragmentValues(Object o){
Hashtable table = new Hashtable();
//there is not any method
return table;
}
/**
Get Document interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getDocumentValues(Object o){
Hashtable table = new Hashtable();
table.put("getDocType()", ((Document)o).getDoctype().toString() );
NodeList nl = ((Node)o).getChildNodes();
StringBuffer sb = new StringBuffer();
if ( nl.item(0).getNodeType() != Node.ELEMENT_NODE ) {
sb.append(nl.item(0).getNodeName()+" ");
}
else sb.append("<"+nl.item(0).getNodeName()+"> ");
table.put("getDocumentElement()", new String(sb) );
table.put("getElementByTagName(String tagName)", "Not invoked");
return table;
}
/**
Get DocumentType interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getDocumentTypeValues(Object o){
Hashtable table = new Hashtable();
table.put("getEntities()", ((DocumentType)o).getEntities().toString() );
table.put("getName()", ((DocumentType)o).getName());
table.put("getNotations()", ((DocumentType)o).getNotations().toString() );
return table;
}
/**
Get Element interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getElementValues(Object o){
Hashtable table = new Hashtable();
table.put("getAttribute(String name)", "Not Invoked");
table.put("getAttributeNode(String name)", "Not Invoked");
table.put("getElementByTagName(String name)", "Not Invoked");
table.put("getTagName()", ((Element)o).getTagName() );
return table;
}
/**
Get Entity interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getEntityValues(Object o){
Hashtable table = new Hashtable();
table.put("getNotationName()", ((Entity)o).getNotationName() );
table.put("getPublicId()", ((Entity)o).getPublicId() );
table.put("getSystemId()", ((Entity)o).getSystemId() );
return table;
}
/**
Get EntityReference interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getEntityReferenceValues(Object o){
Hashtable table = new Hashtable();
//empty
return table;
}
/**
Get Notation interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getNotationValues(Object o){
Hashtable table = new Hashtable();
table.put("getPublicId()", ((Notation)o).getPublicId() );
table.put("getSystemId()", ((Notation)o).getSystemId() );
return table;
}
/**
Get ProcessingInstruction interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getProcessingInstructionValues(Object o){
Hashtable table = new Hashtable();
table.put("getData()", ((ProcessingInstruction)o).getData() );
table.put("getTarger()", ((ProcessingInstruction)o).getTarget() );
return table;
}
/**
Get Text interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getTextValues(Object o){
Hashtable table = new Hashtable();
table.put("splitText(int offset)", "Not Invoked");
return table;
}
/**
Get CharacterData interface accessor methods and values
@Param o a Node object
@Return table a hashtable
*/
public Hashtable getCDataValues(Object o){
Hashtable table = new Hashtable();
table.put("getData()", ((CharacterData)o).getData() );
table.put("getLength()", new Integer( ((CharacterData)o).getLength()).toString() );
return table;
}
/**
Get interfaces a Node implements
@Param o a Node object
@Return list a list of interface
*/
public java.util.List getInterfaces(Object o){
java.util.List list = new ArrayList();
for ( int i = 0; i 0 ) {
sb.append("Accessor method in "+type+":\n");
Enumeration keyList = typeValues.keys();
while ( keyList.hasMoreElements() ) {
String key = (String)keyList.nextElement();
//Object price = table.get( key );
sb.append(key+": "+typeValues.get(key)+"\n");
}
sb.append("\n");
}
//if character data is used
if ( cDataValues.size() > 0 ) {
sb.append("Accessor method in CharacterData:\n");
Enumeration keyList2 = cDataValues.keys();
while ( keyList2.hasMoreElements() ) {
String key = (String)keyList2.nextElement();
//Object price = table.get( key );
sb.append(key+": "+cDataValues.get(key)+"\n");
}
sb.append("\n");
}
//get the Node values
sb.append("Accessor method in Node:\n");
Enumeration keyList3 = nodeValues.keys();
while ( keyList3.hasMoreElements() ) {
String key = (String)keyList3.nextElement();
//Object price = table.get( key );
sb.append(key+": "+nodeValues.get(key)+"\n");
}
//display result to the text area
taOutput.setFont(new Font("Courier", Font.PLAIN, 12) );
taOutput.setText(new String(sb) );
}
/**
Get a DOM object given an XML file location
@Param location a file location
*/
public void openFile(String location){
if ( parser.equals("sun") ) {
doc = DomViewUtils.getSunDocument(location);
}
if ( parser.equals("openxml") ) {
doc = DomViewUtils.getOpenXmlDocument(location);
}
if (parser.equals("ibm") ) {
doc = DomViewUtils.getIBMDocument(location);
}
//make tree model and add to the JTree
model = new DomTreeModel(doc);
tree.setModel(model);
}
/**
Show dialogbox to get an XML file location
*/
public void showOpenFileDialog(){
OpenFileDialog od = new OpenFileDialog(mainFrame);
}
/**
Return preferrred size of this panel
@return a dimension of the panel
*/
public Dimension getPreferredSize(){
return new Dimension(600, 520);
}
/**
Return minimun size of this panel
@return a dimension of the panel
*/
public Dimension getMinimunSize(){
return getPreferredSize();
}
/**
Tree selection listener
*/
protected class SelectionAction implements TreeSelectionListener {
DomViewPanel panel;
public SelectionAction(DomViewPanel d){
panel = d;
}
/**
* Called whenever the value of the selection changes.
* @param e the event that characterizes the change.
*/
public void valueChanged(TreeSelectionEvent e) {
TreePath path = e.getPath();
panel.getInfo(path);
}
}
}//end of DomViewPanel class