FileDocCategorySizeDatePackage
SAXTreeValidator.javaAPI DocExample19247Thu Aug 16 16:27:20 BST 2001javaxml2

SAXTreeValidator

public class SAXTreeValidator extends JFrame
SAXTreeValidator uses Swing to graphically display an XML document, and performs validation.

Fields Summary
private String
vendorParserClass
Default parser to use
private JTree
jTree
The base tree to render
DefaultTreeModel
defaultTreeModel
Tree model to use
Constructors Summary
public SAXTreeValidator()

This initializes the needed Swing settings.


                 
      
        // Handle Swing setup
        super("SAX Tree Validator");
        setSize(600, 450);
    
Methods Summary
public voidbuildTree(javax.swing.tree.DefaultTreeModel treeModel, javax.swing.tree.DefaultMutableTreeNode base, java.lang.String xmlURI)

This handles building the Swing UI tree.

param
treeModel Swing component to build upon.
param
base tree node to build on.
param
xmlURI URI to build XML document from.
throws
IOException - when reading the XML URI fails.
throws
SAXException - when errors in parsing occur.


        // Create instances needed for parsing
        XMLReader reader = 
            XMLReaderFactory.createXMLReader(vendorParserClass);
        ContentHandler jTreeContentHandler = 
            new JValidatorContentHandler(treeModel, base);
        ErrorHandler jTreeErrorHandler = new JValidatorErrorHandler();

        // Register content handler
        reader.setContentHandler(jTreeContentHandler);

        // Register error handler
        reader.setErrorHandler(jTreeErrorHandler);

        // Turn on validation
        reader.setFeature("http://xml.org/sax/features/validation", true);
        reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);

        // Parse
        InputSource inputSource = 
            new InputSource(xmlURI);
        reader.parse(inputSource);
    
public voidinit(java.lang.String xmlURI)

This will construct the tree using Swing.

param
filename String path to XML document.

        DefaultMutableTreeNode base = 
            new DefaultMutableTreeNode("XML Document: " + 
                xmlURI);
        
        // Build the tree model
        defaultTreeModel = new DefaultTreeModel(base);
        jTree = new JTree(defaultTreeModel);

        // Construct the tree hierarchy
        buildTree(defaultTreeModel, base, xmlURI);

        // Display the results
        getContentPane().add(new JScrollPane(jTree), 
            BorderLayout.CENTER);
    
public static voidmain(java.lang.String[] args)

Static entry point for running the viewer.

        try {
            if (args.length != 1) {
                System.out.println(
                    "Usage: java javaxml2.SAXTreeViewer " +
                    "[XML Document URI]");
                System.exit(0);
            }
            SAXTreeValidator viewer = new SAXTreeValidator();
            viewer.init(args[0]);
            viewer.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }