Package javanet.staxutils
Class IndentingXMLEventWriter
- java.lang.Object
-
- javanet.staxutils.helpers.EventWriterDelegate
-
- javanet.staxutils.IndentingXMLEventWriter
-
- All Implemented Interfaces:
Indentation
,XMLEventConsumer
,XMLEventWriter
public class IndentingXMLEventWriter extends EventWriterDelegate implements Indentation
A filter that indents an XML stream. To apply it, construct a filter that contains anotherXMLEventWriter
, which you pass to the constructor. Then call methods of the filter instead of the contained stream. For example:XMLEventWriter
stream = ... stream = newIndentingXMLEventWriter
(stream); stream.add(...);The filter inserts characters to format the document as an outline, with nested elements indented. Basically, it inserts a line break and whitespace before:
- each DTD, processing instruction or comment that's not preceded by data
- each starting tag that's not preceded by data
- each ending tag that's preceded by nested elements but not data
Indentation can be adjusted by setting the newLine and indent properties. But set them to whitespace only, for best results. Non-whitespace is apt to cause problems, for example when this class attempts to insert newLine before the root element.
- Author:
- Kohsuke Kawaguchi, John Kristian
-
-
Field Summary
-
Fields inherited from class javanet.staxutils.helpers.EventWriterDelegate
out
-
Fields inherited from interface javanet.staxutils.Indentation
DEFAULT_INDENT, NORMAL_END_OF_LINE
-
-
Constructor Summary
Constructors Constructor Description IndentingXMLEventWriter(XMLEventWriter out)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(XMLEvent event)
protected void
afterData()
Note that data were written.protected void
afterEndDocument()
Note that a document was ended.protected void
afterEndElement()
Note that an element was ended.protected void
afterMarkup()
Note that markup or indentation was written.protected void
afterStartElement()
Note that an element was started.protected void
beforeEndElement()
Prepare to end an element, by writing a new line and indentation.protected void
beforeMarkup()
Prepare to write markup, by writing a new line and indentation.protected void
beforeStartElement()
Prepare to start an element, by allocating stack space.String
getIndent()
The characters used for one level of indentation.static String
getLineSeparator()
String
getNewLine()
The characters that introduce a new line.void
setIndent(String indent)
Set the characters used for one level of indentation.void
setNewLine(String newLine)
Set the characters that introduce a new line.-
Methods inherited from class javanet.staxutils.helpers.EventWriterDelegate
add, close, flush, getNamespaceContext, getPrefix, setDefaultNamespace, setNamespaceContext, setPrefix
-
-
-
-
Constructor Detail
-
IndentingXMLEventWriter
public IndentingXMLEventWriter(XMLEventWriter out)
-
-
Method Detail
-
setIndent
public void setIndent(String indent)
Description copied from interface:Indentation
Set the characters used for one level of indentation. The default isIndentation.DEFAULT_INDENT
. "\t" is a popular alternative.- Specified by:
setIndent
in interfaceIndentation
-
setNewLine
public void setNewLine(String newLine)
Description copied from interface:Indentation
Set the characters that introduce a new line. The default isIndentation.NORMAL_END_OF_LINE
.IndentingXMLStreamWriter.getLineSeparator()
() is a popular alternative.- Specified by:
setNewLine
in interfaceIndentation
-
getIndent
public String getIndent()
Description copied from interface:Indentation
The characters used for one level of indentation.- Specified by:
getIndent
in interfaceIndentation
-
getNewLine
public String getNewLine()
Description copied from interface:Indentation
The characters that introduce a new line.- Specified by:
getNewLine
in interfaceIndentation
-
getLineSeparator
public static String getLineSeparator()
- Returns:
- System.getProperty("line.separator"); or
Indentation.NORMAL_END_OF_LINE
if that fails.
-
add
public void add(XMLEvent event) throws XMLStreamException
- Specified by:
add
in interfaceXMLEventConsumer
- Specified by:
add
in interfaceXMLEventWriter
- Overrides:
add
in classEventWriterDelegate
- Throws:
XMLStreamException
-
beforeMarkup
protected void beforeMarkup()
Prepare to write markup, by writing a new line and indentation.
-
afterMarkup
protected void afterMarkup()
Note that markup or indentation was written.
-
afterData
protected void afterData()
Note that data were written.
-
beforeStartElement
protected void beforeStartElement()
Prepare to start an element, by allocating stack space.
-
afterStartElement
protected void afterStartElement()
Note that an element was started.
-
beforeEndElement
protected void beforeEndElement()
Prepare to end an element, by writing a new line and indentation.
-
afterEndElement
protected void afterEndElement()
Note that an element was ended.
-
afterEndDocument
protected void afterEndDocument()
Note that a document was ended.
-
-