Extension Interfaces and SAX (Page 1 of 4 )
In this conclusion to a three-part article, we look at extension interfaces, XML filters, and more as they relate to the Simple API for XML (SAX). 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). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
DeclHandler
A lesser used interface, DeclHandler is another of the extended SAX interfaces. This interface defines methods that receive notification of specific events within a DTD, such as element and attribute declarations. This is another item only good for very specific cases; again, XML editors and components that must know the exact lexical structure of documents and their DTDs come to mind. I’m not going to show you an example of using the DeclHandler ; at this point you know more than you’ll probably ever need to about handling callback methods. Instead, I’ll just give you a look at the interface, shown in Figure 4-6.
The DeclHandler interface is fairly self-explanatory. The first two methods handle the <!ELEMENT> and
<!ATTLIST> constructs. The third, externalEntityDecl() , reports entity declarations (through <!ENTITY> ) that refer to external resources. The final method, internalEntityDecl() , reports entities defined inline. That’s all there is to it.

Figure 4-6. The Dec1Handler interface isn't used often, but it's a real boon if you need to write code that deals directly with DTDs
Attributes2, Locator2, and EntityResolver2
SAX provides three other interesting interfaces in org.xml.sax.ext: Attributes2 , Locator2 , and EntityResolver2 . These all extend their respective core interfaces from org.xml.sax ( Attributes , Locator , and EntityResolver ), and class diagrams are shown for all three in Figure 4-7.

Figure 4-7. The Attributes2, EntityResolver2, and Locator2 interfaces
These interfaces provide additional information for use in parsing, ranging from whether an attribute was specified in a DTD to the encoding of an XML document (pulled from the XML declaration). You can find out if your parser supports and uses these extensions via the getFeature() method:
// Check for Attributes2 usag e
featureURI = "http://xml.org/sax/features/use-attributes2";
jTreeHandler.setUsingAttributes2(reader.getFeature(featureURI));
// Check for Locator2 usage
featureURI = "http://xml.org/sax/features/use-locator2";
jTreeHandler.setUsingLocator2(reader.getFeature(featureURI));
These and all other SAX-standard feature and property URIs are detailed in the Appendix.
Next: Extension Interfaces >>
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.
|
|