http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Installation
Download
CVS Repository

Samples
API JavaDoc
XNI Manual
FAQs

Features
Properties

Release Info
Limitations
Report a Bug

Core Interfaces
 

The Xerces Native Interface core contains a series of interfaces and classes designed to communicate a document's "streaming" information set. This page documents the API available for receiving this information in the following sections:

A few examples are also included to illustrate the use of the streaming information set:

For information regarding the parser configuration framework, refer to the Parser Configuration documentation.

Note: The source code for the samples in this document are included in the downloaded packages for Xerces2.

Document Information
 

The document information is communicated using the XMLDocumentHandler interface. In addition, the XMLDocumentFragmentHandler interface is included to get information regarding document fragments. Programmers already familiar with the SAX programming interfaces should be immediately comfortable programming to the Xerces Native Interface. However, XNI does not depend on the SAX interfaces and classes.

Note: All of the interfaces and classes documented on this page are contained within the org.apache.xerces.xni package.
Interface XMLDocumentHandler
 

Communicates document structure and content information. This is the most important handler interface in the Xerces Native Interface.


Interface XMLDocumentFragmentHandler
 

Communicates information about a document fragment. This interface is provided for convenience in defining a document fragment but is not required to be used by a compliant XNI parser component or configuration.


Besides the handler interfaces there are several related interfaces and classes. All of these are described below.

Class XNIException
 

Represents a generic Xerces Native Interface exception.

Note: This exception extends java.lang.RuntimeException. Therefore, even though all of the handler interface methods can throw this type of exception, it is not explicitly required to be caught. Since XNI is intended to be an internal set of interfaces, it is expected that XNI implementations will provide a catch block for this exception at the top level so that XNI exceptions do not "leak" out to the application code.


Interface XMLLocator
 

This interface is used to communicate the document location to the various handler interfaces. The application can use the methods on this interface to query the public, system, and base system identifier as well as the line and column number.

A locator is passed as a parameter in the first method called by the XMLDocumentHandler, XMLDocumentFragmentHandler, and XMLDTDHandler interfaces.

Note: Parser components that emit document information are not required to provide a locator object. However, the Xerces2 reference implementation does provide a locator to registered handlers.


Class QName
 

The QName object is a structure of qualified name information.

Note: The fields of this object have public visibility but should be considered to be read-only to all methods that are passed this object. The caller that creates and passes the QName object "owns" the data. Therefore, callees should not retain a reference to the passed object and are required to copy the references contained in the object if the data is to be used beyond the scope of the method call.


Interface XMLAttributes
 

This interface represents the collection of attributes that is passed to the startElement and emptyElement methods of the XMLDocumentHandler and XMLDocumentFragmentHandler interfaces. This collection of attributes contains all of the information about the attributes of an element (except order) and is editable.

This interface is also capable of storing information about entities appearing in the attribute value. However, it should be noted that if entity information is set for an attribute, then the non-normalized value of the attribute must also be stored because the offsets and lengths of entities in the attribute have no meaning for the normalized value.


Class XMLString
 

The XMLString object is a structure for holding text information. This object allows the underlying implementation to pass text information by using its own internal character buffer without creating new String objects or copying the data.

Note: The fields of this object have public visibility but should be considered to be read-only to all methods that are passed this object. The caller that creates and passes the XMLString object "owns" the data. Therefore, callees should not retain a reference to the passed object and are required to copy the information contained in the object if the data is to be used beyond the scope of the method call. Also, callees should never modify the contents of the character array directly as that could adversely affect the operation of the caller.


Interface NamespaceContext
 

Namespace context information for document fragments. This object is passed to the first method of the XMLDocumentFragmentHandler interface.



DTD Information
 

The DTD information is communicated through two interfaces: XMLDTDHandler and XMLDTDContentModelHandler. The first handler interface passes the basic DTD information whereas the second handler interface breaks down each element declaration content model into separate callbacks.

Interface XMLDTDHandler
 

Communicates basic DTD information such as element and attribute declarations. The implementor of this interface can also be informed of characters within an ignored conditional section.


Interface XMLDTDContentModelHandler
 

Breaks down each element declaration's content model into a set of separate methods so that handlers don't have to reparse the content model string given in the XMLDTDHandler#elementDecl(String,String) method. This separation also helps those applications that want to know boundaries of entities when used as part of an element's content model.



Examples
 

The following examples demonstrate the basic use of the various XNI handler interfaces.

Pass-Through Document Handler Filter
 

The following example demonstrates a basic pass-through document handler filter. This filter receives document handler events and passes them through to the next document handler.

import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLDocumentHandler;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;

public class PassThroughFilter
    implements XMLDocumentHandler {
    
    // Data
    
    protected XMLDocumentHandler fDocumentHandler;
    
    // Public methods
   
    public void setDocumentHandler(XMLDocumentHandler handler) {
        fDocumentHandler = handler;
    }
    
    // XMLDocumentHandler methods
    
    public void startDocument(XMLLocator locator, String encoding)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.startDocument(locator, encoding);
        }
    }
    
    public void xmlDecl(String version, String encoding, 
                        String standalone) throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.xmlDecl(version, encoding, standalone);
        }
    }
    
    public void doctypeDecl(String rootElement, String publicId, 
                            String systemId) throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.doctypeDecl(rootElement, publicId, systemId);
        }
    }
    
    public void comment(XMLString text)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.comment(text);
        }
    }
    
    public void processingInstruction(String target, XMLString data)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.processingInstruction(target, data);
        }
    }
    
    public void startPrefixMapping(String prefix, String uri)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.startPrefixMapping(prefix, uri);
        }
    }
    
    public void endPrefixMapping(String prefix)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.endPrefixMapping(prefix);
        }
    }
    
    public void startElement(QName element, XMLAttributes attributes)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.startElement(element, attributes);
        }
    }
    
    public void emptyElement(QName element, XMLAttributes attributes)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.emptyElement(element, attributes);
        }
    }
    
    public void endElement(QName element)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.endElement(element);
        }
    }
    
    public void startEntity(String name, 
                            String publicId, String systemId, 
                            String baseSystemId, String encoding) 
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.startEntity(name, 
                                         publicId, systemId, 
                                         baseSystemId, encoding);
        }
    }
    
    public void textDecl(String version, String encoding)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.textDecl(version, encoding);
        }
    }
    
    public void endEntity(String name)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.endEntity(name);
        }
    }
    
    public void characters(XMLString text)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.characters(text);
        }
    }
    
    public void ignorableWhitespace(XMLString text)
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.ignorableWhitespace(text);
        }
    }
    
    public void startCDATA()
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.startCDATA();
        }
    }
    
    public void endCDATA()
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.endCDATA();
        }
    }
    
    public void endDocument()
        throws XNIException {
        if (fDocumentHandler != null) {
            fDocumentHandler.endDocument();
        }
    }
    
} // class PassThroughFilter

Upper-Case Filter
 

The following code extends the pass-through document handler filter to upper-case all of the element names.

import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XNIException;
 
public class UpperCaseFilter
    extends PassThroughFilter {
    
    // Data
    
    private final QName fQName = new QName();

    // XMLDocumentHandler methods
    
    public void startElement(QName element, XMLAttributes attributes)
        throws XNIException {
        super.startElement(toUpperCase(element), attributes);
    }
    
    public void emptyElement(QName element, XMLAttributes attributes)
        throws XNIException {
        super.emptyElement(toUpperCase(element), attributes);
    }
    
    public void endElement(QName element)
        throws XNIException {
        super.endElement(toUpperCase(element));
    }
    
    // Protected methods
    
    protected QName toUpperCase(QName qname) {
        String prefix = qname.prefix != null
                      ? qname.prefix.toUpperCase() : null;
        String localpart = qname.localpart != null
                         ? qname.localpart.toUpperCase() : null;
        String rawname = qname.rawname != null
                       ? qname.rawname.toUpperCase() : null;
        String uri = qname.uri;
        fQName.setValues(prefix, localpart, rawname, uri);
        return fQName;
    }

} // class UpperCaseFilter



Copyright © 1999-2001 The Apache Software Foundation. All Rights Reserved.