Advanced SAX - Error Handling
(Page 3 of 4 )
Invoking the setFeature() and setProperty() methods can result in SAXNotSupportedException s and SAXNotRecognizedException s.
Both of these are also in the org.xml.sax package.
The first, SAXNotSupportedException , indicates that the parser “knows” about the feature or property but doesn’t support it. This is commonly used when a standard property or feature is not yet coded in (such as in alpha or beta versions of parsers). So invoking setFeature("http://xml.org/sax/features/namespaces") on a parser in development might result in a SAXNotSupportedException . The parser recognizes the feature (and probably plans to support it at some point), but doesn’t have the ability to perform the requested processing.
The second exception, SAXNotRecognizedException , commonly occurs when your code uses vendor-specific features and properties, and then you switch out your parser implementations. The new implementation won’t know anything about the other vendor’s features or properties, and will throw a SAXNotRecognizedException .
You should always explicitly catch these exceptions so you can report them, rather than treating them as just another generic SAXException . Otherwise, you end up losing valuable information about what happened in your code. This means you may have to write a bit of extra code, but thus is the price for good exception handling; here’s a slightly updated version of the buildTree() method (detailed originally in Chapter 3) that handles these problems gracefully:
public void buildTree(DefaultTreeModel treeModel ,
DefaultMutableTreeNode base, String xmlURI)
throws IOException, SAXException {
String featureURI = "";
XMLReader reader = null;
try {
// Create instances needed for parsing
reader = XMLReaderFactory.createXMLReader();
JTreeHandler jTreeHandler =
new JTreeHandler(treeModel, base);
// Register content handler
reader.setContentHandler(jTreeHandler);
// Register error handler
reader.setErrorHandler(jTreeHandler);
// Turn on validation
featureURI = http://xml.org/sax/features/validation;
reader.setFeature(featureURI, true);
// Turn on schema validation, as well
featureURI = http://apache.org/xml/features/ validation/schema;
reader.setFeature(featureURI, true);
// Parse
InputSource inputSource = new InputSource(xmlURI);
reader.parse(inputSource);
} catch (SAXNotRecognizedException e) {
System.err.println("The parser class " + reader.getClass().getName() +
" does not recognize the feature URI '" + featureURI + "'");
System.exit(-1);
} catch (SAXNotSupportedException e) {
System.err.println("The parser class " + reader.getClass().getName() +
" does not support the feature URI '" + featureURI + "'");
System.exit(-1);
}
}
Next: Resolving Entities >>
More Java Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the book Java and XML, Third Edition, written by Brett McLaughlin and Justin Edelson (O'Reilly, 2006; ISBN: 059610149X). Check it out today at your favorite bookstore. Buy this book now.
|
|