Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.jdom.input
Class SAXHandler  view SAXHandler download SAXHandler.java

java.lang.Object
  extended byorg.xml.sax.helpers.DefaultHandler
      extended byorg.jdom.input.SAXHandler
All Implemented Interfaces:
org.xml.sax.ContentHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler, org.xml.sax.ext.LexicalHandler

public class SAXHandler
extends org.xml.sax.helpers.DefaultHandler
implements org.xml.sax.ext.LexicalHandler, org.xml.sax.ext.DeclHandler, org.xml.sax.DTDHandler

A support class for SAXBuilder.

Version:
$Revision: 1.68 $, $Date: 2004/08/31 06:14:05 $

Field Summary
private  boolean atRoot
          Indicator of where in the document we are
private static java.util.Map attrNameToTypeMap
          Hash table to map SAX attribute type names to JDOM attribute types.
private  org.jdom.Element currentElement
          Element object being built
private static java.lang.String CVS_ID
           
private  java.util.List declaredNamespaces
          Temporary holder for namespaces that have been declared with startPrefixMapping, but are not yet available on the element
private  org.jdom.Document document
          Document object being built
private  int entityDepth
          How many nested entities we're currently within
private  boolean expand
          Indicator of whether we should expand entities
private  java.util.Map externalEntities
          The external entities defined in this document
private  org.jdom.input.JDOMFactory factory
          The JDOMFactory used for JDOM object creation
private  boolean ignoringWhite
          Whether to ignore ignorable whitespace
private  boolean inCDATA
          Indicator of whether we are in a CDATA
private  boolean inDTD
          Indicator of whether we are in the DocType.
private  boolean inInternalSubset
          Indicator of whether we are in the internal subset
private  java.lang.StringBuffer internalSubset
          Temporary holder for the internal subset
private  org.xml.sax.Locator locator
          The SAX Locator object provided by the parser
private  boolean previousCDATA
          Indicator of whether we previously were in a CDATA
private  boolean suppress
          Indicator of whether we are actively suppressing (non-expanding) a current entity
private  TextBuffer textBuffer
          Temporary holder for Text and CDATA
 
Constructor Summary
SAXHandler()
          This will create a new SAXHandler that listens to SAX events and creates a JDOM Document.
SAXHandler(org.jdom.input.JDOMFactory factory)
          This will create a new SAXHandler that listens to SAX events and creates a JDOM Document.
 
Method Summary
private  void appendExternalId(java.lang.String publicID, java.lang.String systemID)
          Appends an external ID to the internal subset buffer.
 void attributeDecl(java.lang.String eName, java.lang.String aName, java.lang.String type, java.lang.String valueDefault, java.lang.String value)
          This handles an attribute declaration in the internal subset.
 void characters(char[] ch, int start, int length)
          This will report character data (within an element).
 void comment(char[] ch, int start, int length)
          This reports that a comments is parsed.
 void elementDecl(java.lang.String name, java.lang.String model)
          Handle an element declaration in a DTD.
 void endCDATA()
          Report a CDATA section
 void endDTD()
          This signifies that the reading of the DTD is complete.
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
          Indicates the end of an element (</[element name]>) is reached.
 void endEntity(java.lang.String name)
          Report the end of an entity.
 void externalEntityDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
          This is called when the parser encounters an external entity declaration.
protected  void flushCharacters()
          This will flush any characters from SAX character calls we've been buffering.
protected  void flushCharacters(java.lang.String data)
          Flush the given string into the document.
private static int getAttributeType(java.lang.String typeName)
          Returns the the JDOM Attribute type value from the SAX 2.0 attribute type string provided by the parser.
 org.jdom.Element getCurrentElement()
          Returns the being-parsed element.
 org.jdom.Document getDocument()
          Returns the document.
 org.xml.sax.Locator getDocumentLocator()
          Provides access to the org.xml.sax.Locator object provided by the SAX parser.
 boolean getExpandEntities()
          Returns whether or not entities will be expanded during the build.
 org.jdom.input.JDOMFactory getFactory()
          Returns the factory used for constructing objects.
 boolean getIgnoringElementContentWhitespace()
          Returns whether or not the parser will elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.
 void ignorableWhitespace(char[] ch, int start, int length)
          Capture ignorable whitespace as text.
 void internalEntityDecl(java.lang.String name, java.lang.String value)
          Handle an internal entity declaration in a DTD.
 void notationDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
          Handle the declaration of a Notation in a DTD
 void processingInstruction(java.lang.String target, java.lang.String data)
          This will indicate that a processing instruction has been encountered.
protected  void pushElement(org.jdom.Element element)
          Pushes an element onto the tree under construction.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Receives an object for locating the origin of SAX document events.
 void setExpandEntities(boolean expand)
          This sets whether or not to expand entities during the build.
 void setIgnoringElementContentWhitespace(boolean ignoringWhite)
          Specifies whether or not the parser should elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.
 void skippedEntity(java.lang.String name)
          This indicates that an unresolvable entity reference has been encountered, normally because the external DTD subset has not been read.
 void startCDATA()
          Report a CDATA section
 void startDocument()
          Receive notification of the beginning of the document.
 void startDTD(java.lang.String name, java.lang.String publicID, java.lang.String systemID)
          This will signify that a DTD is being parsed, and can be used to ensure that comments and other lexical structures in the DTD are not added to the JDOM Document object.
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
          This reports the occurrence of an actual element.
 void startEntity(java.lang.String name)
          Report the beginning of some internal and external XML entities.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
          This will add the prefix mapping to the JDOM Document object.
private  void transferNamespaces(org.jdom.Element element)
          This will take the supplied org.jdom.Element and transfer its namespaces to the global namespace storage.
 void unparsedEntityDecl(java.lang.String name, java.lang.String publicID, java.lang.String systemID, java.lang.String notationName)
          Handler for unparsed entity declarations in the DTD
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, resolveEntity, warning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CVS_ID

private static final java.lang.String CVS_ID
See Also:
Constant Field Values

attrNameToTypeMap

private static final java.util.Map attrNameToTypeMap
Hash table to map SAX attribute type names to JDOM attribute types.


document

private org.jdom.Document document
Document object being built


currentElement

private org.jdom.Element currentElement
Element object being built


atRoot

private boolean atRoot
Indicator of where in the document we are


inDTD

private boolean inDTD
Indicator of whether we are in the DocType. Note that the DTD consists of both the internal subset (inside the tag) and the external subset (in a separate .dtd file).


inInternalSubset

private boolean inInternalSubset
Indicator of whether we are in the internal subset


previousCDATA

private boolean previousCDATA
Indicator of whether we previously were in a CDATA


inCDATA

private boolean inCDATA
Indicator of whether we are in a CDATA


expand

private boolean expand
Indicator of whether we should expand entities


suppress

private boolean suppress
Indicator of whether we are actively suppressing (non-expanding) a current entity


entityDepth

private int entityDepth
How many nested entities we're currently within


declaredNamespaces

private java.util.List declaredNamespaces
Temporary holder for namespaces that have been declared with startPrefixMapping, but are not yet available on the element


internalSubset

private java.lang.StringBuffer internalSubset
Temporary holder for the internal subset


textBuffer

private TextBuffer textBuffer
Temporary holder for Text and CDATA


externalEntities

private java.util.Map externalEntities
The external entities defined in this document


factory

private org.jdom.input.JDOMFactory factory
The JDOMFactory used for JDOM object creation


ignoringWhite

private boolean ignoringWhite
Whether to ignore ignorable whitespace


locator

private org.xml.sax.Locator locator
The SAX Locator object provided by the parser

Constructor Detail

SAXHandler

public SAXHandler()
This will create a new SAXHandler that listens to SAX events and creates a JDOM Document. The objects will be constructed using the default factory.


SAXHandler

public SAXHandler(org.jdom.input.JDOMFactory factory)
This will create a new SAXHandler that listens to SAX events and creates a JDOM Document. The objects will be constructed using the provided factory.

Method Detail

pushElement

protected void pushElement(org.jdom.Element element)
Pushes an element onto the tree under construction. Allows subclasses to put content under a dummy root element which is useful for building content that would otherwise be a non-well formed document.


getDocument

public org.jdom.Document getDocument()
Returns the document. Should be called after parsing is complete.


getFactory

public org.jdom.input.JDOMFactory getFactory()
Returns the factory used for constructing objects.


setExpandEntities

public void setExpandEntities(boolean expand)
This sets whether or not to expand entities during the build. A true means to expand entities as normal content. A false means to leave entities unexpanded as EntityRef objects. The default is true.


getExpandEntities

public boolean getExpandEntities()
Returns whether or not entities will be expanded during the build.


setIgnoringElementContentWhitespace

public void setIgnoringElementContentWhitespace(boolean ignoringWhite)
Specifies whether or not the parser should elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document. Only whitespace which is contained within element content that has an element only content model will be eliminated (see XML Rec 3.2.1). For this setting to take effect requires that validation be turned on. The default value of this setting is false.


getIgnoringElementContentWhitespace

public boolean getIgnoringElementContentWhitespace()
Returns whether or not the parser will elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.


startDocument

public void startDocument()
Description copied from class: org.xml.sax.helpers.DefaultHandler
Receive notification of the beginning of the document.

By default, do nothing. Application writers may override this method in a subclass to take specific actions at the beginning of a document (such as allocating the root node of a tree or creating an output file).

Specified by:
startDocument in interface org.xml.sax.ContentHandler

externalEntityDecl

public void externalEntityDecl(java.lang.String name,
                               java.lang.String publicID,
                               java.lang.String systemID)
                        throws org.xml.sax.SAXException
This is called when the parser encounters an external entity declaration.

Specified by:
externalEntityDecl in interface org.xml.sax.ext.DeclHandler

attributeDecl

public void attributeDecl(java.lang.String eName,
                          java.lang.String aName,
                          java.lang.String type,
                          java.lang.String valueDefault,
                          java.lang.String value)
                   throws org.xml.sax.SAXException
This handles an attribute declaration in the internal subset.

Specified by:
attributeDecl in interface org.xml.sax.ext.DeclHandler

elementDecl

public void elementDecl(java.lang.String name,
                        java.lang.String model)
                 throws org.xml.sax.SAXException
Handle an element declaration in a DTD.

Specified by:
elementDecl in interface org.xml.sax.ext.DeclHandler

internalEntityDecl

public void internalEntityDecl(java.lang.String name,
                               java.lang.String value)
                        throws org.xml.sax.SAXException
Handle an internal entity declaration in a DTD.

Specified by:
internalEntityDecl in interface org.xml.sax.ext.DeclHandler

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
This will indicate that a processing instruction has been encountered. (The XML declaration is not a processing instruction and will not be reported.)

Specified by:
processingInstruction in interface org.xml.sax.ContentHandler

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
This indicates that an unresolvable entity reference has been encountered, normally because the external DTD subset has not been read.

Specified by:
skippedEntity in interface org.xml.sax.ContentHandler

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
This will add the prefix mapping to the JDOM Document object.

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
This reports the occurrence of an actual element. It will include the element's attributes, with the exception of XML vocabulary specific attributes, such as xmlns:[namespace prefix] and xsi:schemaLocation.

Specified by:
startElement in interface org.xml.sax.ContentHandler

transferNamespaces

private void transferNamespaces(org.jdom.Element element)
This will take the supplied org.jdom.Element and transfer its namespaces to the global namespace storage.


characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
This will report character data (within an element).

Specified by:
characters in interface org.xml.sax.ContentHandler

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Capture ignorable whitespace as text. If setIgnoringElementContentWhitespace(true) has been called then this method does nothing.

Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler

flushCharacters

protected void flushCharacters()
                        throws org.xml.sax.SAXException
This will flush any characters from SAX character calls we've been buffering.


flushCharacters

protected void flushCharacters(java.lang.String data)
                        throws org.xml.sax.SAXException
Flush the given string into the document. This is a protected method so subclassers can control text handling without knowledge of the internals of this class.


endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Indicates the end of an element (</[element name]>) is reached. Note that the parser does not distinguish between empty elements and non-empty elements, so this will occur uniformly.

Specified by:
endElement in interface org.xml.sax.ContentHandler

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicID,
                     java.lang.String systemID)
              throws org.xml.sax.SAXException
This will signify that a DTD is being parsed, and can be used to ensure that comments and other lexical structures in the DTD are not added to the JDOM Document object.

Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
This signifies that the reading of the DTD is complete.

Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException
Description copied from interface: org.xml.sax.ext.LexicalHandler
Report the beginning of some internal and external XML entities.

The reporting of parameter entities (including the external DTD subset) is optional, and SAX2 drivers that report LexicalHandler events may not implement it; you can use the http://xml.org/sax/features/lexical-handler/parameter-entities feature to query or control the reporting of parameter entities.

General entities are reported with their regular names, parameter entities have '%' prepended to their names, and the external DTD subset has the pseudo-entity name "[dtd]".

When a SAX2 driver is providing these events, all other events must be properly nested within start/end entity events. There is no additional requirement that events from DeclHandler or DTDHandler be properly ordered.

Note that skipped entities will be reported through the skippedEntity 55 event, which is part of the ContentHandler interface.

Because of the streaming event model that SAX uses, some entity boundaries cannot be reported under any circumstances:

  • general entities within attribute values
  • parameter entities within declarations

These will be silently expanded, with no indication of where the original entity boundaries were.

Note also that the boundaries of character references (which are not really entities anyway) are not reported.

All start/endEntity events must be properly nested.

Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException
Description copied from interface: org.xml.sax.ext.LexicalHandler
Report the end of an entity.

Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Report a CDATA section

Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Report a CDATA section

Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws org.xml.sax.SAXException
This reports that a comments is parsed. If not in the DTD, this comment is added to the current JDOM Element, or the Document itself if at that level.

Specified by:
comment in interface org.xml.sax.ext.LexicalHandler

notationDecl

public void notationDecl(java.lang.String name,
                         java.lang.String publicID,
                         java.lang.String systemID)
                  throws org.xml.sax.SAXException
Handle the declaration of a Notation in a DTD

Specified by:
notationDecl in interface org.xml.sax.DTDHandler

unparsedEntityDecl

public void unparsedEntityDecl(java.lang.String name,
                               java.lang.String publicID,
                               java.lang.String systemID,
                               java.lang.String notationName)
                        throws org.xml.sax.SAXException
Handler for unparsed entity declarations in the DTD

Specified by:
unparsedEntityDecl in interface org.xml.sax.DTDHandler

appendExternalId

private void appendExternalId(java.lang.String publicID,
                              java.lang.String systemID)
Appends an external ID to the internal subset buffer. Either publicID or systemID may be null, but not both.


getCurrentElement

public org.jdom.Element getCurrentElement()
                                   throws org.xml.sax.SAXException
Returns the being-parsed element.


getAttributeType

private static int getAttributeType(java.lang.String typeName)
Returns the the JDOM Attribute type value from the SAX 2.0 attribute type string provided by the parser.


setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Receives an object for locating the origin of SAX document events. This method is invoked by the SAX parser.

org.jdom.JDOMFactory implementations can use the getDocumentLocator() 55 method to get access to the org.xml.sax.Locator during parse.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

getDocumentLocator

public org.xml.sax.Locator getDocumentLocator()
Provides access to the org.xml.sax.Locator object provided by the SAX parser.