Package de.hsh.graja.util.marshal
Class IndentingXMLStreamWriter
java.lang.Object
de.hsh.graja.util.marshal.StreamWriterDelegate
de.hsh.graja.util.marshal.IndentingXMLStreamWriter
- All Implemented Interfaces:
Indentation
,XMLStreamWriter
A filter that indents an XML stream. To apply it, construct a filter that
contains another
XMLStreamWriter
, which you pass to the constructor.
Then call methods of the filter instead of the contained stream. For example:
XMLStreamWriter
stream = ... stream = newIndentingXMLStreamWriter
(stream); stream.writeStartDocument(); ...
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.
-
Field Summary
Fields inherited from class de.hsh.graja.util.marshal.StreamWriterDelegate
out
Fields inherited from interface de.hsh.graja.util.marshal.Indentation
DEFAULT_INDENT, NORMAL_END_OF_LINE
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Note that data were written.protected void
Note that a document was ended.protected void
Note that an element was ended.protected void
Note that markup or indentation was written.protected void
Note that an element was started.protected void
Prepare to end an element, by writing a new line and indentation.protected void
Prepare to write markup, by writing a new line and indentation.protected void
Prepare to start an element, by allocating stack space.The characters used for one level of indentation.static String
The characters that introduce a new line.void
Set the characters used for one level of indentation.void
setNewLine
(String newLine) Set the characters that introduce a new line.void
writeCData
(String data) void
writeCharacters
(char[] text, int start, int len) void
writeCharacters
(String text) void
writeComment
(String data) void
void
writeEmptyElement
(String localName) void
writeEmptyElement
(String namespaceURI, String localName) void
writeEmptyElement
(String prefix, String localName, String namespaceURI) void
void
void
writeEntityRef
(String name) protected void
writeNewLine
(int indentation) Write a line separator followed by indentation.void
writeProcessingInstruction
(String target) void
writeProcessingInstruction
(String target, String data) void
void
writeStartDocument
(String version) void
writeStartDocument
(String encoding, String version) void
writeStartElement
(String localName) void
writeStartElement
(String namespaceURI, String localName) void
writeStartElement
(String prefix, String localName, String namespaceURI) Methods inherited from class de.hsh.graja.util.marshal.StreamWriterDelegate
close, flush, getNamespaceContext, getPrefix, getProperty, setDefaultNamespace, setNamespaceContext, setPrefix, writeAttribute, writeAttribute, writeAttribute, writeDefaultNamespace, writeNamespace
-
Constructor Details
-
IndentingXMLStreamWriter
-
-
Method Details
-
setIndent
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
-
getIndent
Description copied from interface:Indentation
The characters used for one level of indentation.- Specified by:
getIndent
in interfaceIndentation
-
setNewLine
Description copied from interface:Indentation
Set the characters that introduce a new line. The default isIndentation.NORMAL_END_OF_LINE
.getLineSeparator()
() is a popular alternative.- Specified by:
setNewLine
in interfaceIndentation
-
getLineSeparator
- Returns:
- System.getProperty("line.separator"); or
Indentation.NORMAL_END_OF_LINE
if that fails.
-
getNewLine
Description copied from interface:Indentation
The characters that introduce a new line.- Specified by:
getNewLine
in interfaceIndentation
-
writeStartDocument
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
- Overrides:
writeStartDocument
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartDocument
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
- Overrides:
writeStartDocument
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartDocument
- Specified by:
writeStartDocument
in interfaceXMLStreamWriter
- Overrides:
writeStartDocument
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeDTD
- Specified by:
writeDTD
in interfaceXMLStreamWriter
- Overrides:
writeDTD
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstruction
in interfaceXMLStreamWriter
- Overrides:
writeProcessingInstruction
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstruction
in interfaceXMLStreamWriter
- Overrides:
writeProcessingInstruction
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeComment
- Specified by:
writeComment
in interfaceXMLStreamWriter
- Overrides:
writeComment
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException - Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartElement
- Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartElement
- Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException - Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharacters
in interfaceXMLStreamWriter
- Overrides:
writeCharacters
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharacters
in interfaceXMLStreamWriter
- Overrides:
writeCharacters
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeCData
- Specified by:
writeCData
in interfaceXMLStreamWriter
- Overrides:
writeCData
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEntityRef
- Specified by:
writeEntityRef
in interfaceXMLStreamWriter
- Overrides:
writeEntityRef
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEndElement
- Specified by:
writeEndElement
in interfaceXMLStreamWriter
- Overrides:
writeEndElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEndDocument
- Specified by:
writeEndDocument
in interfaceXMLStreamWriter
- Overrides:
writeEndDocument
in classStreamWriterDelegate
- 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. -
writeNewLine
Write a line separator followed by indentation.- Throws:
XMLStreamException
-