XML technologies / XML / XML in a Nutshell / SAX reference / The package org.xml.sax.ext

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

The package org.xml.sax.ext contains additional interface definitions extending the SAX core. Most major parsers support these features, but support is not universal.

 

The Attributes2 interface

SAX 2.0.2 introduces the subclass Attributes2 of Attributes, adding methods to determine whether an attribute was declared in the DTD and/or specified in the document instance rather than defaulted from the DTD. A parser supporting Attributes2 passes, instead of a plain object Attributes, an object Attributes2 to startElement(). The extra methods require a cast. Before casting, consider checking whether it will succeed by reading the feature "http://xml.org/sax/features/use-attributes2". If it is true, the parser passes objects Attributes2.

package org.xml.sax.ext;

public interface Attributes2 {

  public boolean isDeclared(int index);
  public boolean isDeclared(String qualifiedName);
  public boolean isSpecified(StringnamespaceURI, StringlocalName);
  public boolean isSpecified(int
 index);public boolean isSpecified(String qualifiedName);
  public boolean isSpecified(StringnamespaceURI, StringlocalName);

}

 

The DeclHandler interface

The interface DeclHandler is another callback interface exposing parser information, here about DTD declarations. To equip an object XMLReader with a DeclHandler, set the property "http://xml.org/sax/handlers/DeclHandler" and pass the handler object DeclHandler. Use the method setProperty() of the XMLReader:

try {
   parser.setProperty(
     "http://xml.org/sax/properties/DeclHandler",
      new YourDeclHandlerImplementationClass());
 }
 catch(SAXException e) {
   System.out.println("Dieser Parser kann keine Informationen über Deklarationen
                         liefern.");
 }

If declarations are unsupported, the parser throws a SAXNotRecognizedException. If it cannot set a DeclHandler right now, usually because it is parsing, it throws a SAXNotSupportedException. Without an exception, the DeclHandler is set and its methods are called while parsing the DTD:

package org.xml.sax.ext;

public interface DeclHandler {

  public void elementDecl(String name, String model) throws SAXException;
  public void attributeDecl(String
 elementName, String attributeName,
 String type, String defaultValue, String value) throws SAXException;
  public void internalEntityDecl(String name, String value)
 throws SAXException;
  public void externalEntityDecl(String name, String publicID,
String systemID) throws SAXException;

}

 

The EntityResolver2 interface

SAX 2.0.2 introduces a subclass EntityResolver2 of EntityResolver, adding methods for flexible resolution of entities. In particular, a program can supply an external DTD subset for a document without a document type declaration. It also resolves entities by root-element name and base URL, or PUBLIC or SYSTEM identifiers. Your code may always pass an object EntityResolver2 to setEntityResolver(). Parsers not supporting EntityResolver2 simply ignore the extra methods. The feature "http://xml.org/sax/features/use-entity-resolver2" tells you whether the parser uses the extra methods of EntityResolver2.

package org.xml.sax.ext;

public interface EntityResolver2 {

  public InputSource getExternalSubset(String name, String baseURI);
  public InputSource resolveEntity(
  String name, String publicID, String baseURI, String systemID);

}

 

The LexicalHandler interface

The interface LexicalHandler is another callback interface for parser information, here normally irrelevant details:

Without a LexicalHandler, the parser ignores comments and expands entity references and CDATA sections. The interface LexicalHandler lets you retain comments and distinguish text from ordinary sections, CDATA sections and entity references.

Configure an object XMLReader with a LexicalHandler using the handler class’s method setProperty(). Here the property name is "http://xml.org/sax/properties/LexicalHandler":

try {
   parser.setProperty(
   "http://xml.org/sax/properties/LexicalHandler",
    new YourLexicalHandlerClass()
   );
 }
 catch(SAXException ex) {
  System.out.println("Dieser Parser unterstützt keine lexikalischen Events.");
 }

If lexical events are unsupported, the parser throws a SAXNotRecognizedException. If it cannot set a LexicalHandler right now, usually because it is parsing, it throws a SAXNotSupportedException. Without an exception, the LexicalHandler is set and its methods are called while parsing. CDATA and parsed-entity contents still pass through the interface ContentHandler:

package org.xml.sax.ext;

public interface LexicalHandler {

  public void startDTD(String name,
String publicID,
String systemID) throws SAXException;
  public void endDTD() throws SAXException;
  public void startEntity(String name)
 throws SAXException;
  public void endEntity(String name)
 throws SAXException;
  public void startCDATA() throws SAXException;
  public void endCDATA() throws SAXException;
  public void comment(char[] text, int start, int length)
 throws SAXException;

}

 

The Locator2 interface

SAX 2.0.2 introduces a subclass Locator2 of Locator, with extra methods identifying the current entity’s encoding and XML version. A parser supporting Locator2 passes, instead of an object Locator, an object Locator2 to setLocator(). The extra methods require a cast. First check whether it will succeed by reading the feature "http://xml.org/sax/features/use-locator2". If it is true, the parser passes objects Locator2.

package org.xml.sax.ext;

public interface EntityResolver2 {

 public String getXMLVersion();
 public String getEncoding();

}

The method getXMLVersion() returns the version of the current entity. The property "http://xml.org/sax/properties/document-xml-version" returns the version of the current document. These may or may not be the same.

  

 

 

 

back to top