Top Description Fields Constructors Methods
com.sun.xml.txw2.output

public Class IndentingXMLFilter

extends XMLFilterImpl
implements LexicalHandler
Class Inheritance
All Implemented Interfaces
org.xml.sax.ext.LexicalHandler
Imports
org.xml.sax.Attributes, .ContentHandler, .SAXException, org.xml.sax.ext.LexicalHandler, org.xml.sax.helpers.XMLFilterImpl, java.util.Stack

XMLFilterImpl that does indentation to SAX events.
Author
Kohsuke Kawaguchi

Field Summary

Modifier and TypeField and Description
private int
private String
private LexicalHandler
private static final char[]
private static final Object
private static final Object
private static final Object
private Object
private Stack<Object>

Constructor Summary

AccessConstructor and Description
public
public
public

Method Summary

Modifier and TypeMethod and Description
public void
characters(char[]
The characters to write.
ch
,
int
The starting position in the array.
start
,
int
The number of characters to use.
length
)

Overrides org.xml.sax.helpers.XMLFilterImpl.characters.

Implements org.xml.sax.ContentHandler.characters.

Write a sequence of characters.

public void
comment(char[] ch, int start, int length)

Implements org.xml.sax.ext.LexicalHandler.comment.

Report an XML comment anywhere in the document.

private void
doIndent()

Print indentation for the current level.

public void
endCDATA()

Implements org.xml.sax.ext.LexicalHandler.endCDATA.

Report the end of a CDATA section.

public void
endDTD()

Implements org.xml.sax.ext.LexicalHandler.endDTD.

Report the end of DTD declarations.

public void
endElement(String
The element's Namespace URI.
uri
,
String
The element's local name.
localName
,
String
The element's qualified (prefixed) name.
qName
)

Overrides org.xml.sax.helpers.XMLFilterImpl.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Write an end tag.

public void
endEntity(String name)

Implements org.xml.sax.ext.LexicalHandler.endEntity.

Report the end of an entity.

public int

Returns:

The number of spaces in each indentation step, or 0 or less for no indentation.
getIndentStep
()
Deprecated Only return the length of the indent string.

Return the current indent step.

public LexicalHandler
public void
setIndentStep(int
The new indent step (0 or less for no indentation).
indentStep
)
Deprecated Should use the version that takes string.

Set the current indent step.

public void
public void
public void
startCDATA()

Implements org.xml.sax.ext.LexicalHandler.startCDATA.

Report the start of a CDATA section.

public void
startDTD(String name, String publicId, String systemId)

Implements org.xml.sax.ext.LexicalHandler.startDTD.

Report the start of DTD declarations, if any.

public void
startElement(String
The element's Namespace URI.
uri
,
String
The element's local name.
localName
,
String
The element's qualified (prefixed) name.
qName
,
Attributes
The element's attribute list.
atts
)

Overrides org.xml.sax.helpers.XMLFilterImpl.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Write a start tag.

public void
startEntity(String name)

Implements org.xml.sax.ext.LexicalHandler.startEntity.

Report the beginning of some internal and external XML entities.

private void
Inherited from org.xml.sax.helpers.XMLFilterImpl:
endDocumentendPrefixMappingerrorfatalErrorgetContentHandlergetDTDHandlergetEntityResolvergetErrorHandlergetFeaturegetParentgetPropertyignorableWhitespacenotationDeclparseparseprocessingInstructionresolveEntitysetContentHandlersetDocumentLocatorsetDTDHandlersetEntityResolversetErrorHandlersetFeaturesetParentsetPropertyskippedEntitystartDocumentstartPrefixMappingunparsedEntityDeclwarning

Field Detail

depthback to summary
private int depth
indentStepback to summary
private String indentStep
lexicalback to summary
private LexicalHandler lexical
NEWLINEback to summary
private static final char[] NEWLINE
SEEN_DATAback to summary
private static final Object SEEN_DATA
SEEN_ELEMENTback to summary
private static final Object SEEN_ELEMENT
SEEN_NOTHINGback to summary
private static final Object SEEN_NOTHING
stateback to summary
private Object state
stateStackback to summary
private Stack<Object> stateStack

Constructor Detail

IndentingXMLFilterback to summary
public IndentingXMLFilter()
IndentingXMLFilterback to summary
public IndentingXMLFilter(ContentHandler handler)
IndentingXMLFilterback to summary
public IndentingXMLFilter(ContentHandler handler, LexicalHandler lexical)

Method Detail

charactersback to summary
public void characters(char[] ch, int start, int length) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.characters.

Implements org.xml.sax.ContentHandler.characters.

Write a sequence of characters.

Parameters
ch:char[]

The characters to write.

start:int

The starting position in the array.

length:int

The number of characters to use.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the characters, or if a filter further down the chain raises an exception.
See Also
XMLWriter#characters(char[], int, int)
commentback to summary
public void comment(char[] ch, int start, int length) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.comment.

Doc from org.xml.sax.ext.LexicalHandler.comment.

Report an XML comment anywhere in the document.

This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read). Comments in the DTD must be properly nested inside start/endDTD and start/endEntity events (if used).

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
doIndentback to summary
private void doIndent() throws SAXException

Print indentation for the current level.

Exceptions
SAXException:
If there is an error writing the indentation characters, or if a filter further down the chain raises an exception.
endCDATAback to summary
public void endCDATA() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endCDATA.

Doc from org.xml.sax.ext.LexicalHandler.endCDATA.

Report the end of a CDATA section.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
endDTDback to summary
public void endDTD() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endDTD.

Doc from org.xml.sax.ext.LexicalHandler.endDTD.

Report the end of DTD declarations.

This method is intended to report the end of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
endElementback to summary
public void endElement(String uri, String localName, String qName) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.endElement.

Implements org.xml.sax.ContentHandler.endElement.

Write an end tag.

If the element has contained other elements, the tag will appear indented on a new line; otherwise, it will appear immediately following whatever came before.

The newline and indentation will be passed on down the filter chain through regular characters events.

Parameters
uri:String

The element's Namespace URI.

localName:String

The element's local name.

qName:String

The element's qualified (prefixed) name.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the end tag, or if a filter further down the chain raises an exception.
See Also
XMLWriter#endElement(String, String, String)
endEntityback to summary
public void endEntity(String name) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.endEntity.

Doc from org.xml.sax.ext.LexicalHandler.endEntity.

Report the end of an entity.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
getIndentStepback to summary
public int getIndentStep()

Deprecated

Only return the length of the indent string.

Return the current indent step.

Return the current indent step: each start tag will be indented by this number of spaces times the number of ancestors that the element has.

Returns:int

The number of spaces in each indentation step, or 0 or less for no indentation.

Annotations
@Deprecated
See Also
setIndentStep(int)
getLexicalHandlerback to summary
public LexicalHandler getLexicalHandler()
setIndentStepback to summary
public void setIndentStep(int indentStep)

Deprecated

Should use the version that takes string.

Set the current indent step.

Parameters
indentStep:int

The new indent step (0 or less for no indentation).

Annotations
@Deprecated
See Also
getIndentStep()
setIndentStepback to summary
public void setIndentStep(String s)
setLexicalHandlerback to summary
public void setLexicalHandler(LexicalHandler lexical)
startCDATAback to summary
public void startCDATA() throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startCDATA.

Doc from org.xml.sax.ext.LexicalHandler.startCDATA.

Report the start of a CDATA section.

The contents of the CDATA section will be reported through the regular characters event; this event is intended only to report the boundary.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
startDTDback to summary
public void startDTD(String name, String publicId, String systemId) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startDTD.

Doc from org.xml.sax.ext.LexicalHandler.startDTD.

Report the start of DTD declarations, if any.

This method is intended to report the beginning of the DOCTYPE declaration; if the document has no DOCTYPE declaration, this method will not be invoked.

All declarations reported through DTDHandler or DeclHandler events must appear between the startDTD and endDTD events. Declarations are assumed to belong to the internal DTD subset unless they appear between startEntity and endEntity events. Comments and processing instructions from the DTD should also be reported between the startDTD and endDTD events, in their original order of (logical) occurrence; they are not required to appear in their correct locations relative to DTDHandler or DeclHandler events, however.

Note that the start/endDTD events will appear within the start/endDocument events from ContentHandler and before the first startElement event.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
startElementback to summary
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException

Overrides org.xml.sax.helpers.XMLFilterImpl.startElement.

Implements org.xml.sax.ContentHandler.startElement.

Write a start tag.

Each tag will begin on a new line, and will be indented by the current indent step times the number of ancestors that the element has.

The newline and indentation will be passed on down the filter chain through regular characters events.

Parameters
uri:String

The element's Namespace URI.

localName:String

The element's local name.

qName:String

The element's qualified (prefixed) name.

atts:Attributes

The element's attribute list.

Annotations
@Override
Exceptions
SAXException:
If there is an error writing the start tag, or if a filter further down the chain raises an exception.
See Also
XMLWriter#startElement(String, String, String, Attributes)
startEntityback to summary
public void startEntity(String name) throws SAXException

Implements org.xml.sax.ext.LexicalHandler.startEntity.

Doc from org.xml.sax.ext.LexicalHandler.startEntity.

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 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.

Annotations
@Override
Exceptions
SAXException:
The application may raise an exception.
writeNewLineback to summary
private void writeNewLine() throws SAXException