SAXTreeValidatorpublic class SAXTreeValidator extends JFrame SAXTreeValidator uses Swing to graphically
display an XML document, and performs validation. |
Fields Summary |
---|
private String | vendorParserClassDefault parser to use | private JTree | jTreeThe base tree to render | DefaultTreeModel | defaultTreeModelTree 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 void | buildTree(javax.swing.tree.DefaultTreeModel treeModel, javax.swing.tree.DefaultMutableTreeNode base, java.lang.String xmlURI)This handles building the Swing UI tree.
// 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 void | init(java.lang.String xmlURI) This will construct the tree using Swing.
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 void | main(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();
}
|
|