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:
XMLStreamWriterstream = ... 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
outFields inherited from interface de.hsh.graja.util.marshal.Indentation
DEFAULT_INDENT, NORMAL_END_OF_LINE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidNote that data were written.protected voidNote that a document was ended.protected voidNote that an element was ended.protected voidNote that markup or indentation was written.protected voidNote that an element was started.protected voidPrepare to end an element, by writing a new line and indentation.protected voidPrepare to write markup, by writing a new line and indentation.protected voidPrepare to start an element, by allocating stack space.The characters used for one level of indentation.static StringThe characters that introduce a new line.voidSet the characters used for one level of indentation.voidsetNewLine(String newLine) Set the characters that introduce a new line.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 localName, String namespaceURI) voidvoidvoidwriteEntityRef(String name) protected voidwriteNewLine(int indentation) Write a line separator followed by indentation.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 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:IndentationSet the characters used for one level of indentation. The default isIndentation.DEFAULT_INDENT. "\t" is a popular alternative.- Specified by:
setIndentin interfaceIndentation
-
getIndent
Description copied from interface:IndentationThe characters used for one level of indentation.- Specified by:
getIndentin interfaceIndentation
-
setNewLine
Description copied from interface:IndentationSet the characters that introduce a new line. The default isIndentation.NORMAL_END_OF_LINE.getLineSeparator()() is a popular alternative.- Specified by:
setNewLinein interfaceIndentation
-
getLineSeparator
- Returns:
- System.getProperty("line.separator"); or
Indentation.NORMAL_END_OF_LINEif that fails.
-
getNewLine
Description copied from interface:IndentationThe characters that introduce a new line.- Specified by:
getNewLinein interfaceIndentation
-
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
-
writeDTD
- Specified by:
writeDTDin interfaceXMLStreamWriter- Overrides:
writeDTDin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter- Overrides:
writeProcessingInstructionin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeProcessingInstruction
- Specified by:
writeProcessingInstructionin interfaceXMLStreamWriter- Overrides:
writeProcessingInstructionin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeComment
- Specified by:
writeCommentin interfaceXMLStreamWriter- Overrides:
writeCommentin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException - Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin 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 localName, String namespaceURI) throws XMLStreamException - Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharactersin interfaceXMLStreamWriter- Overrides:
writeCharactersin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCharacters
- Specified by:
writeCharactersin interfaceXMLStreamWriter- Overrides:
writeCharactersin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeCData
- Specified by:
writeCDatain interfaceXMLStreamWriter- Overrides:
writeCDatain classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEntityRef
- Specified by:
writeEntityRefin interfaceXMLStreamWriter- Overrides:
writeEntityRefin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndElement
- Specified by:
writeEndElementin interfaceXMLStreamWriter- Overrides:
writeEndElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndDocument
- Specified by:
writeEndDocumentin interfaceXMLStreamWriter- Overrides:
writeEndDocumentin 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
-