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
FieldsModifier and TypeFieldDescriptionstatic final StringInitial value for the default character encoding.static final intDefault number of spaces corresponding to one indent level.static final StringInitial value for the default XML version. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.IndentXMLStreamWriter(XMLStreamWriter writer) Convenience constructor.IndentXMLStreamWriter(XMLStreamWriter writer, int indent) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidsetAddMissingXmlDeclaration(boolean addMissingXmlDeclaration) Set whether to add an XML declaration, if missing.voidsetDefaultEncoding(String defaultEncoding) Set the character encoding for the XML declaration in case it's not already specified.voidsetDefaultVersion(String defaultVersion) Set the version for the XML declaration in case it's not already specified.voidsetIndentAfterXmlDeclaration(boolean indentAfterXmlDeclaration) Set whether to "indent" (actually, just output a newline) after the XML declaration if necessary.voidwriteCData(String data) voidwriteCharacters(char[] text, int start, int len) voidwriteCharacters(String text) voidwriteComment(String data) voidvoidwriteEmptyElement(String localName) voidwriteEmptyElement(String namespaceURI, String localName) voidwriteEmptyElement(String prefix, String namespaceURI, String localName) voidvoidvoidwriteEntityRef(String name) voidwriteProcessingInstruction(String target) voidwriteProcessingInstruction(String target, String data) voidvoidwriteStartDocument(String version) voidwriteStartDocument(String encoding, String version) voidwriteStartElement(String localName) voidwriteStartElement(String namespaceURI, String localName) voidwriteStartElement(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:
writeStartDocumentin interfaceXMLStreamWriter- Overrides:
writeStartDocumentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartDocument
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter- Overrides:
writeStartDocumentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartDocument
- Specified by:
writeStartDocumentin interfaceXMLStreamWriter- Overrides:
writeStartDocumentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartElement
- Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartElement
- Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartElement
public void writeStartElement(String prefix, String namespaceURI, String localName) throws XMLStreamException - Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String namespaceURI, String localName) throws XMLStreamException - Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeComment
- Specified by:
writeCommentin interfaceXMLStreamWriter- Overrides:
writeCommentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter- Overrides:
writeProcessingInstructionin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter- Overrides:
writeProcessingInstructionin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCData
- Specified by:
writeCDatain interfaceXMLStreamWriter- Overrides:
writeCDatain classStreamWriterDelegate- Throws:
XMLStreamException
-
writeDTD
- Specified by:
writeDTDin interfaceXMLStreamWriter- Overrides:
writeDTDin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEntityRef
- Specified by:
writeEntityRefin interfaceXMLStreamWriter- Overrides:
writeEntityRefin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharactersin interfaceXMLStreamWriter- Overrides:
writeCharactersin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharactersin interfaceXMLStreamWriter- Overrides:
writeCharactersin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndDocument
- Specified by:
writeEndDocumentin interfaceXMLStreamWriter- Overrides:
writeEndDocumentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndElement
- Specified by:
writeEndElementin interfaceXMLStreamWriter- Overrides:
writeEndElementin classStreamWriterDelegate- Throws:
XMLStreamException
-