Class SAXHandler
- All Implemented Interfaces:
ContentHandler,DTDHandler,EntityResolver,ErrorHandler,DeclHandler,LexicalHandler
SAXBuilder which listens for SAX events.
People overriding this class are cautioned to ensure that the implementation of the cleanUp() method resets to a virgin state. The cleanUp() method will be called when this SAXHandler is reset(), which may happen multiple times between parses. The cleanUp() method must ensure that there are no references remaining to any external instances.
Overriding of this class is permitted to allow for different handling of SAX
events. Once you have created a subclass of this, you also need to create a
custom implementation of SAXHandlerFactory to supply your instances
to SAXBuilder
If the XMLReader producing the SAX Events supports a document Locator, then
this instance will use the locator to supply the line and column data from
the SAX locator to the JDOMFactory. Note: the SAX
specification for the SAX Locator indicates that the line and column
represent the position of the end of the SAX Event. For
example, the line and column of the simple XML <root />
would be line 1, column 9.
- Author:
- Brett McLaughlin, Jason Hunter, Philip Nelson, Bradley S. Huffman, phil@triloggroup.com, Rolf Lear
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionThis will create a newSAXHandlerthat listens to SAX events and creates a JDOM Document.SAXHandler(JDOMFactory factory) This will create a newSAXHandlerthat listens to SAX events and creates a JDOM Document. -
Method Summary
Modifier and TypeMethodDescriptionvoidThis handles an attribute declaration in the internal subset.voidcharacters(char[] ch, int start, int length) This will report character data (within an element).voidcomment(char[] ch, int start, int length) This reports that a comments is parsed.voidelementDecl(String name, String model) Handle an element declaration in a DTD.voidendCDATA()Report a CDATA sectionvoidendDTD()This signifies that the reading of the DTD is complete.voidendElement(String namespaceURI, String localName, String qName) Indicates the end of an element (</[element name]>) is reached.voidvoidexternalEntityDecl(String name, String publicID, String systemID) This is called when the parser encounters an external entity declaration.protected voidThis will flush any characters from SAX character calls we've been buffering.protected voidflushCharacters(String data) Flush the given string into the document.Returns the being-parsed element.Returns the document.Provides access to theLocatorobject provided by the SAX parser.booleanReturns whether or not entities will be expanded during the build.Returns the factory used for constructing objects.booleanReturns whether or not the parser will elminate element content containing only whitespace.booleanReturns whether or not the parser will elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.voidignorableWhitespace(char[] ch, int start, int length) Capture ignorable whitespace as text.voidinternalEntityDecl(String name, String value) Handle an internal entity declaration in a DTD.voidnotationDecl(String name, String publicID, String systemID) Handle the declaration of a Notation in a DTDvoidprocessingInstruction(String target, String data) This will indicate that a processing instruction has been encountered.protected voidpushElement(Element element) Pushes an element onto the tree under construction.final voidreset()Restore this SAXHandler to a clean state ready for another parse round.protected voidOverride this method if you are a subclasser, and you want to clear the state of your SAXHandler instance in preparation for a new parse.voidsetDocumentLocator(Locator locator) Receives an object for locating the origin of SAX document events.voidsetExpandEntities(boolean expand) This sets whether or not to expand entities during the build.voidsetIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) Specifies whether or not the parser should eliminate text() nodes containing only whitespace when building the document.voidsetIgnoringElementContentWhitespace(boolean ignoringWhite) Specifies whether or not the parser should elminate whitespace in element content (sometimes known as "ignorable whitespace") when building the document.voidskippedEntity(String name) This indicates that an unresolvable entity reference has been encountered, normally because the external DTD subset has not been read.voidReport a CDATA sectionvoidvoidThis 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 JDOMDocumentobject.voidstartElement(String namespaceURI, String localName, String qName, Attributes atts) This reports the occurrence of an actual element.voidstartEntity(String name) voidstartPrefixMapping(String prefix, String uri) This will add the prefix mapping to the JDOMDocumentobject.voidunparsedEntityDecl(String name, String publicID, String systemID, String notationName) Handler for unparsed entity declarations in the DTDMethods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, resolveEntity, warningMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.xml.sax.ContentHandler
declaration
-
Constructor Details
-
SAXHandler
public SAXHandler()This will create a newSAXHandlerthat listens to SAX events and creates a JDOM Document. The objects will be constructed using the default factory. -
SAXHandler
This will create a newSAXHandlerthat listens to SAX events and creates a JDOM Document. The objects will be constructed using the provided factory.- Parameters:
factory-JDOMFactoryto be used for constructing objects
-
-
Method Details
-
resetSubCLass
protected void resetSubCLass()Override this method if you are a subclasser, and you want to clear the state of your SAXHandler instance in preparation for a new parse. -
reset
public final void reset()Restore this SAXHandler to a clean state ready for another parse round. All internal variables are cleared to an initialized state, and then the resetSubClass() method is called to clear any methods that a subclass may need to have reset. -
pushElement
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.- Parameters:
element- root element under which content will be built
-
getDocument
Returns the document. Should be called after parsing is complete.- Returns:
Document- Document that was built
-
getFactory
Returns the factory used for constructing objects.- Returns:
JDOMFactory- the factory used for constructing objects.- See Also:
-
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 asEntityRefobjects. The default is true.- Parameters:
expand-booleanindicating whether entity expansion should occur.
-
getExpandEntities
public boolean getExpandEntities()Returns whether or not entities will be expanded during the build.- Returns:
boolean- whether entity expansion will occur during build.- See Also:
-
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 isfalse.- Parameters:
ignoringWhite- Whether to ignore ignorable whitespace
-
setIgnoringBoundaryWhitespace
public void setIgnoringBoundaryWhitespace(boolean ignoringBoundaryWhite) Specifies whether or not the parser should eliminate text() nodes containing only whitespace when building the document. SeeSAXBuilder.setIgnoringBoundaryWhitespace(boolean).- Parameters:
ignoringBoundaryWhite- Whether to ignore only whitespace content
-
getIgnoringBoundaryWhitespace
public boolean getIgnoringBoundaryWhitespace()Returns whether or not the parser will elminate element content containing only whitespace.- Returns:
boolean- whether only whitespace content will be ignored during build.- See Also:
-
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.- Returns:
boolean- whether ignorable whitespace will be ignored during build.- See Also:
-
startDocument
public void startDocument()- Specified by:
startDocumentin interfaceContentHandler- Overrides:
startDocumentin classDefaultHandler
-
externalEntityDecl
This is called when the parser encounters an external entity declaration.- Specified by:
externalEntityDeclin interfaceDeclHandler- Parameters:
name- entity namepublicID- public idsystemID- system id- Throws:
SAXException- when things go wrong
-
attributeDecl
public void attributeDecl(String eName, String aName, String type, String valueDefault, String value) This handles an attribute declaration in the internal subset.- Specified by:
attributeDeclin interfaceDeclHandler- Parameters:
eName-Stringelement name of attributeaName-Stringattribute nametype-Stringattribute typevalueDefault-Stringdefault value of attributevalue-Stringvalue of attribute
-
elementDecl
Handle an element declaration in a DTD.- Specified by:
elementDeclin interfaceDeclHandler- Parameters:
name-Stringname of elementmodel-Stringmodel of the element in DTD syntax
-
internalEntityDecl
Handle an internal entity declaration in a DTD.- Specified by:
internalEntityDeclin interfaceDeclHandler- Parameters:
name-Stringname of entityvalue-Stringvalue of the entity
-
processingInstruction
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:
processingInstructionin interfaceContentHandler- Overrides:
processingInstructionin classDefaultHandler- Parameters:
target-Stringtarget of PIdata-Stringcontaining all data sent to the PI. This typically looks like one or more attribute value pairs.- Throws:
SAXException- when things go wrong
-
skippedEntity
This indicates that an unresolvable entity reference has been encountered, normally because the external DTD subset has not been read.- Specified by:
skippedEntityin interfaceContentHandler- Overrides:
skippedEntityin classDefaultHandler- Parameters:
name-Stringname of entity- Throws:
SAXException- when things go wrong
-
startPrefixMapping
This will add the prefix mapping to the JDOMDocumentobject.- Specified by:
startPrefixMappingin interfaceContentHandler- Overrides:
startPrefixMappingin classDefaultHandler- Parameters:
prefix-Stringnamespace prefix.uri-Stringnamespace URI.- Throws:
SAXException
-
startElement
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws 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 asxmlns:[namespace prefix]andxsi:schemaLocation.- Specified by:
startElementin interfaceContentHandler- Overrides:
startElementin classDefaultHandler- Parameters:
namespaceURI-Stringnamespace URI this element is associated with, or an emptyStringlocalName-Stringname of element (with no namespace prefix, if one is present)qName-StringXML 1.0 version of element name: [namespace prefix]:[localName]atts-Attributeslist for this element- Throws:
SAXException- when things go wrong
-
characters
This will report character data (within an element).- Specified by:
charactersin interfaceContentHandler- Overrides:
charactersin classDefaultHandler- Parameters:
ch-char[]character array with character datastart-intindex in array where data starts.length-intlength of data.- Throws:
SAXException
-
ignorableWhitespace
Capture ignorable whitespace as text. If setIgnoringElementContentWhitespace(true) has been called then this method does nothing.- Specified by:
ignorableWhitespacein interfaceContentHandler- Overrides:
ignorableWhitespacein classDefaultHandler- Parameters:
ch-[]- char array of ignorable whitespacestart-int- starting position within arraylength-int- length of whitespace after start- Throws:
SAXException- when things go wrong
-
flushCharacters
This will flush any characters from SAX character calls we've been buffering.- Throws:
SAXException- when things go wrong
-
flushCharacters
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.- Parameters:
data- string to flush- Throws:
SAXException- if the state of the handler does not allow this.
-
endElement
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:
endElementin interfaceContentHandler- Overrides:
endElementin classDefaultHandler- Parameters:
namespaceURI-StringURI of namespace this element is associated withlocalName-Stringname of element without prefixqName-Stringname of element in XML 1.0 form- Throws:
SAXException- when things go wrong
-
startDTD
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 JDOMDocumentobject.- Specified by:
startDTDin interfaceLexicalHandler- Parameters:
name-Stringname of element listed in DTDpublicID-Stringpublic ID of DTDsystemID-Stringsystem ID of DTD- Throws:
SAXException
-
endDTD
public void endDTD()This signifies that the reading of the DTD is complete.- Specified by:
endDTDin interfaceLexicalHandler
-
startEntity
- Specified by:
startEntityin interfaceLexicalHandler- Throws:
SAXException
-
endEntity
- Specified by:
endEntityin interfaceLexicalHandler- Throws:
SAXException
-
startCDATA
public void startCDATA()Report a CDATA section- Specified by:
startCDATAin interfaceLexicalHandler
-
endCDATA
Report a CDATA section- Specified by:
endCDATAin interfaceLexicalHandler- Throws:
SAXException
-
comment
This reports that a comments is parsed. If not in the DTD, this comment is added to the current JDOMElement, or theDocumentitself if at that level.- Specified by:
commentin interfaceLexicalHandler- Parameters:
ch-ch[]array of comment characters.start-intindex to start reading from.length-intlength of data.- Throws:
SAXException- if the state of the handler disallows this call
-
notationDecl
Handle the declaration of a Notation in a DTD- Specified by:
notationDeclin interfaceDTDHandler- Overrides:
notationDeclin classDefaultHandler- Parameters:
name- name of the notationpublicID- the public ID of the notationsystemID- the system ID of the notation- Throws:
SAXException
-
unparsedEntityDecl
Handler for unparsed entity declarations in the DTD- Specified by:
unparsedEntityDeclin interfaceDTDHandler- Overrides:
unparsedEntityDeclin classDefaultHandler- Parameters:
name-Stringof the unparsed entity declpublicID-Stringof the unparsed entity declsystemID-Stringof the unparsed entity declnotationName-Stringof the unparsed entity decl
-
getCurrentElement
Returns the being-parsed element.- Returns:
Element- element being built.- Throws:
SAXException- if the state of the handler disallows this call
-
setDocumentLocator
Receives an object for locating the origin of SAX document events. This method is invoked by the SAX parser.JDOMFactoryimplementations can use thegetDocumentLocator()method to get access to theLocatorduring parse.- Specified by:
setDocumentLocatorin interfaceContentHandler- Overrides:
setDocumentLocatorin classDefaultHandler- Parameters:
locator-Locatoran object that can return the location of any SAX document event.
-
getDocumentLocator
Provides access to theLocatorobject provided by the SAX parser.- Returns:
Locatoran object that can return the location of any SAX document event.
-