Exploring the Hierarchy


Given a suite of objects to explore, we need a mechanism for using JXPath to experiment with the XPath syntax.

XPath is essentially a new query language, allowing you to specify queries for a hierarchical structure. It can be difficult to understand XPath when you are first starting out. To make this easier, we'll create an interactive Swing client to test our various XPath queries against Bob's family, starting with Bob. As shown in Figure 8-3, XPath queries can be entered in the text field at the top, and pressing return or clicking the Execute button will cause the results of the query to be displayed in the text area below.

Figure 8-3. JXPath Explorer interface.


Listing 8-5 shows the code for the interactive Swing client. The code uses Bob Smith as the default context for your XPath queriesall of the queries are made starting from the Bob Smith object. Note that the actual JXPath execution is performed in the executeButtonActionPerformed method. In addition, keep in mind that JXPath is returning the actual Person object, not just a String, but also that the Person objects define an implementation of the toString() method, as shown in Listing 8-3.

Listing 8-5. Interactive JXPath Swing Client
 package com.cascadetg.ch08; import java.awt.Color; public class JXPathExplorer extends javax.swing.JFrame {     public JXPathExplorer() { initComponents(); }     private void initComponents()     {         mainScrollPane = new javax.swing.JScrollPane();         resultsTextPane = new javax.swing.JTextPane();         errorLabel = new javax.swing.JLabel();         queryPanel = new javax.swing.JPanel();         queryField = new javax.swing.JTextField();         executeButton = new javax.swing.JButton();         appMenuBar = new javax.swing.JMenuBar();         fileMenu = new javax.swing.JMenu();         setTitle("JXPath Explorer");         setName("mainFrame");         addWindowListener(new java.awt.event.WindowAdapter()         {             public void windowClosing(                 java.awt.event.WindowEvent evt)             {                 exitForm(evt);             }         });         mainScrollPane.setViewportView(resultsTextPane);         getContentPane().add(             mainScrollPane,             java.awt.BorderLayout.CENTER);         errorLabel.setText("Ready.");         getContentPane().add(             errorLabel,             java.awt.BorderLayout.SOUTH);         queryPanel.setLayout(new java.awt.BorderLayout());         queryField.setFont(new java.awt.Font("DialogInput", 0, 12));         queryField.setText("Enter query here");         queryField             .addActionListener(new java.awt.event.ActionListener()         {             public void actionPerformed(                 java.awt.event.ActionEvent evt)             {                 queryFieldActionPerformed(evt);             }         });         queryPanel.add(queryField, java.awt.BorderLayout.CENTER);         executeButton.setText("Execute");         executeButton             .addActionListener(new java.awt.event.ActionListener()         {             public void actionPerformed(                 java.awt.event.ActionEvent evt)             {                 executeButtonActionPerformed(evt);             }         });         queryPanel.add(executeButton, java.awt.BorderLayout.EAST);         getContentPane().add(             queryPanel,             java.awt.BorderLayout.NORTH);         fileMenu.setText("File");         appMenuBar.add(fileMenu);         setJMenuBar(appMenuBar);         java.awt.Dimension screenSize =             java.awt.Toolkit.getDefaultToolkit().getScreenSize();         setBounds(             (screenSize.width - 400) / 2,             (screenSize.height - 300) / 2,             400,             300);     }     private void executeButtonActionPerformed(         java.awt.event.ActionEvent evt)     {         org.apache.commons.jxpath.JXPathContext context;         try         {             this.errorLabel.setText("Working...");             this.errorLabel.setForeground(Color.BLUE);             this.update(this.getGraphics());             Person bob = FamilyFactory.getPerson();             context =                 org.apache.commons.jxpath.JXPathContext.newContext(bob);             java.util.Iterator bar =                 context.iterate(this.queryField.getText());             StringBuffer output = new StringBuffer();             while (bar.hasNext())             {                 output.append(bar.next());                 output.append("\n");             }             this.resultsTextPane.setText(output.toString());             this.errorLabel.setText("Ok.");             this.errorLabel.setForeground(Color.BLACK);             this.queryField.selectAll();         } catch (Throwable e)         {             String message = e.getMessage();             if (e instanceof OutOfMemoryError)             {                 message = "Too many results - out of memory!";                 context = null;                 System.gc();             }             if (message.length() == 0)                 message = "Unknown error - check log for exception";             this.errorLabel.setText(message);             this.errorLabel.setForeground(Color.RED);             this.queryField.selectAll();             e.printStackTrace();         }     }     private void queryFieldActionPerformed(         java.awt.event.ActionEvent evt)     { executeButtonActionPerformed(evt); }     private void exitForm(java.awt.event.WindowEvent evt)     { System.exit(0); }     public static void main(String args[])     { new JXPathExplorer().show(); }     // Variables declaration     private javax.swing.JMenuBar appMenuBar;     private javax.swing.JLabel errorLabel;     private javax.swing.JButton executeButton;     private javax.swing.JMenu fileMenu;     private javax.swing.JScrollPane mainScrollPane;     private javax.swing.JTextField queryField;     private javax.swing.JPanel queryPanel;     private javax.swing.JTextPane resultsTextPane;     // End of variables declaration } 



    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net