Methods Summary |
---|
private void | buildTree(JDOMNode node, javax.swing.tree.DefaultMutableTreeNode treeNode)Recursively build the JTree from nodes of the JDOM Document
// If this is a whitespace node or unhandled node, ignore it
if ((node == null) || (node.toString().trim().equals(""))) {
return;
}
DefaultMutableTreeNode newTreeNode = new DefaultMutableTreeNode(node);
// Walk over the children of the node
Iterator i = node.iterator();
while (i.hasNext()) {
// Create JDOMNodes on the children and add to the tree
JDOMNode newNode = createNode(i.next());
buildTree(newNode, newTreeNode);
}
// After all the children have been added, connect to the tree
treeNode.add(newTreeNode);
|
private void | buildXPath(JDOMNode node)Put the xpath path to the selected node in the status bar
statusText.setText(new XPathDisplayNode(node).getXPath());
|
private JDOMNode | createNode(java.lang.Object node)Factory method to create nodes according to the type of
the given object.
if (node instanceof Element) {
lastElement = (Element)node;
return new ElementNode((Element)node);
}
if (node instanceof Attribute) {
return new AttributeNode((Attribute)node);
}
if (node instanceof String) {
return new TextNode((String)node).setParent(lastElement);
}
// All other nodes are not implemented
return null;
|
private void | initConnections()This initializes connections between swing components, models
and event handlers.
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
// Setup the JTree and a pane to display it in
jdomTree = new JTree();
jdomTree.setName("JDOM Tree");
jdomTree.addTreeSelectionListener(eventHandler);
selectionModel = (DefaultTreeSelectionModel)jdomTree.getSelectionModel();
getContentPane().add(new JScrollPane(jdomTree), BorderLayout.CENTER);
// Setup a text box for use in a status bar
statusText = new JTextField("Click on an element to view xpath");
JPanel statusBarPane= new JPanel();
statusBarPane.setLayout(new BorderLayout());
statusBarPane.add(statusText, BorderLayout.CENTER );
getContentPane().add(statusBarPane, BorderLayout.SOUTH);
|
private void | initialize()Initialize the class, load and display the xml document.
setTitle("Simple XPath Viewer");
// Setup the UI
initConnections();
// Load the JDOM Document
Document doc = loadDocument(filename);
// Create the initial JDOMNode from the Factory method
JDOMNode root = createNode(doc.getRootElement());
// Create the root node of the JTree and build it from the JDOM Document
DefaultMutableTreeNode treeNode =
new DefaultMutableTreeNode("Document: " + filename);
buildTree(root, treeNode);
// Add the node to the tree's model
((DefaultTreeModel)jdomTree.getModel()).setRoot(treeNode);
|
private Document | loadDocument(java.lang.String filename)Load the given xml file for display.
SAXBuilder builder = new SAXBuilder();
builder.setIgnoringElementContentWhitespace(true);
return builder.build(new File(filename));
|
public static void | main(java.lang.String[] args)Static entry point
try {
if (args.length != 1) {
System.out.println("Usage: java javaxml2.SimpleXPathViewer " +
"[XML Document filename]");
return;
}
/* Create the frame */
SimpleXPathViewer viewer= new SimpleXPathViewer(args[0]);
/* Add a windowListener for the windowClosedEvent */
viewer.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent e) {
System.exit(0);
};
});
viewer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
|