Package org.dellroad.stuff.xml
Class IndentXMLStreamWriter
java.lang.Object
org.dellroad.stuff.xml.StreamWriterDelegate
org.dellroad.stuff.xml.IndentXMLStreamWriter
- All Implemented Interfaces:
XMLStreamWriter
Wrapper for an underlying
XMLStreamWriter
that "pretty-prints" the XML by replacing the whitespace between XML tags
so that the result is properly indented.
This class will also fixup a missing/incomplete initial XML declaration.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
Initial value for the default character encoding.static final int
Default number of spaces corresponding to one indent level.static final String
Initial value for the default XML version. -
Constructor Summary
ConstructorDescriptionDefault constructor.IndentXMLStreamWriter
(XMLStreamWriter writer) Convenience constructor.IndentXMLStreamWriter
(XMLStreamWriter writer, int indent) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
setAddMissingXmlDeclaration
(boolean addMissingXmlDeclaration) Set whether to add an XML declaration, if missing.void
setDefaultEncoding
(String defaultEncoding) Set the character encoding for the XML declaration in case it's not already specified.void
setDefaultVersion
(String defaultVersion) Set the version for the XML declaration in case it's not already specified.void
setIndentAfterXmlDeclaration
(boolean indentAfterXmlDeclaration) Set whether to "indent" (actually, just output a newline) after the XML declaration if necessary.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 namespaceURI, String localName) void
void
void
writeEntityRef
(String name) 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 namespaceURI, String localName) Methods inherited from class org.dellroad.stuff.xml.StreamWriterDelegate
close, flush, getNamespaceContext, getParent, getPrefix, getProperty, setDefaultNamespace, setNamespaceContext, setParent, setPrefix, writeAttribute, writeAttribute, writeAttribute, writeDefaultNamespace, writeNamespace
-
Field Details
-
DEFAULT_VERSION
Initial value for the default XML version.- See Also:
-
DEFAULT_ENCODING
Initial value for the default character encoding.- See Also:
-
DEFAULT_INDENT
public static final int DEFAULT_INDENTDefault number of spaces corresponding to one indent level.- See Also:
-
-
Constructor Details
-
IndentXMLStreamWriter
public IndentXMLStreamWriter()Default constructor. Sets the indent toDEFAULT_INDENT
. The parent must be configured viasetParent()
. -
IndentXMLStreamWriter
Convenience constructor. Equivalent to:IndentXMLStreamWriter
(writer,
DEFAULT_INDENT
)
;- Parameters:
writer
- underlying writer
-
IndentXMLStreamWriter
Primary constructor.- Parameters:
writer
- underlying writerindent
- number of spaces corresponding to one indent level, or negative for no inter-tag whitespace at all
-
-
Method Details
-
setAddMissingXmlDeclaration
public void setAddMissingXmlDeclaration(boolean addMissingXmlDeclaration) Set whether to add an XML declaration, if missing.Default is true.
- Parameters:
addMissingXmlDeclaration
- true to add XML declaration
-
setIndentAfterXmlDeclaration
public void setIndentAfterXmlDeclaration(boolean indentAfterXmlDeclaration) Set whether to "indent" (actually, just output a newline) after the XML declaration if necessary. In some cases, such as transforming into a DOM, this behavior must be disabled to avoid hierarchy exceptions due to characters not being allowed before the document element.Default is true.
- Parameters:
indentAfterXmlDeclaration
- true to indent after XML declaration
-
setDefaultVersion
Set the version for the XML declaration in case it's not already specified.Default is
DEFAULT_VERSION
("1.0").- Parameters:
defaultVersion
- XML version
-
setDefaultEncoding
Set the character encoding for the XML declaration in case it's not already specified.Default is
DEFAULT_ENCODING
("UTF-8").- Parameters:
defaultEncoding
- character encoding name
-
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
-
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 namespaceURI, String localName) throws XMLStreamException - Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String namespaceURI, String localName) throws XMLStreamException - Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeComment
- Specified by:
writeComment
in interfaceXMLStreamWriter
- Overrides:
writeComment
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
-
writeCData
- Specified by:
writeCData
in interfaceXMLStreamWriter
- Overrides:
writeCData
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeDTD
- Specified by:
writeDTD
in interfaceXMLStreamWriter
- Overrides:
writeDTD
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEntityRef
- Specified by:
writeEntityRef
in interfaceXMLStreamWriter
- Overrides:
writeEntityRef
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
-
writeEndDocument
- Specified by:
writeEndDocument
in interfaceXMLStreamWriter
- Overrides:
writeEndDocument
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEndElement
- Specified by:
writeEndElement
in interfaceXMLStreamWriter
- Overrides:
writeEndElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-