Class FormattedWriter
java.lang.Object
org.apache.sis.internal.xml.StreamWriterDelegate
org.apache.sis.internal.storage.xml.stream.FormattedWriter
- All Implemented Interfaces:
XMLStreamWriter
Adds indentation to a XML output.
Design note:
an alternative approach would have been to provide
startIdentation()
and endIndentation()
convenience methods in StaxStreamWriter
, and let subclasses perform their own formatting. It would
reduce the need to try to guess some formatting aspects (e.g. whether to format on a single line or not).
However, that approach does not integrate very well with JAXB; the Marshaller.JAXB_FORMATTED_OUTPUT
property seems to be ignored when marshalling a fragment using XMLStreamWriter
. Even if that property
was supported, there is no standard way as of JDK8 to tell to JAXB to begin the indentation at some level
(for taking in account the indentation of the elements containing the fragment to marshal with JAXB).- Since:
- 0.8
- Version:
- 0.8
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
Number of spaces to add for each new indentation level.private boolean
true
if the lastwriteStartElement(…)
method invocation has not yet been followed by awriteEndElement(…)
method invocation.private final String
The line separator to use.private int
The number of spaces to write before next XML start tags.private static final char[]
A predefined amount of spaces, used byindent()
for writing a greater amount of spaces in one call toXMLStreamWriter.writeCharacters(char[], int, int)
methods.Fields inherited from class org.apache.sis.internal.xml.StreamWriterDelegate
out
-
Constructor Summary
ConstructorsConstructorDescriptionFormattedWriter
(XMLStreamWriter out, int indentation) Creates a new XML writer with indentation. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
Appends indentation before an empty tag.private void
indent()
Writes a line separator, then the given amount of spaces.(package private) final void
Increases the indentation level and appends indentation before a start tag.void
writeEmptyElement
(String localName) Appends indentation, then forwards the call verbatim.void
writeEmptyElement
(String namespaceURI, String localName) Appends indentation, then forwards the call verbatim.void
writeEmptyElement
(String prefix, String localName, String namespaceURI) Appends indentation, then forwards the call verbatim.void
Appends a new line after the document.void
Appends indentation if the value is not inline, then forwards the call verbatim.void
writeStartElement
(String localName) Appends indentation, then forwards the call verbatim.void
writeStartElement
(String namespaceURI, String localName) Appends indentation, then forwards the call verbatim.void
writeStartElement
(String prefix, String localName, String namespaceURI) Appends indentation, then forwards the call verbatim.Methods inherited from class org.apache.sis.internal.xml.StreamWriterDelegate
close, flush, getNamespaceContext, getPrefix, getProperty, setDefaultNamespace, setNamespaceContext, setPrefix, writeAttribute, writeAttribute, writeAttribute, writeCData, writeCharacters, writeCharacters, writeComment, writeDefaultNamespace, writeDTD, writeEntityRef, writeNamespace, writeProcessingInstruction, writeProcessingInstruction, writeStartDocument, writeStartDocument, writeStartDocument
-
Field Details
-
SPACES
private static final char[] SPACESA predefined amount of spaces, used byindent()
for writing a greater amount of spaces in one call toXMLStreamWriter.writeCharacters(char[], int, int)
methods. -
lineSeparator
The line separator to use. -
indentation
private final int indentationNumber of spaces to add for each new indentation level. -
margin
private int marginThe number of spaces to write before next XML start tags. This value is incremented or decremented by theindentation
value every time a XML start element or end element is encountered. -
inline
private boolean inlinetrue
if the lastwriteStartElement(…)
method invocation has not yet been followed by awriteEndElement(…)
method invocation. In such case, the start and end tags can be written on the same line.
-
-
Constructor Details
-
FormattedWriter
FormattedWriter(XMLStreamWriter out, int indentation) throws XMLStreamException Creates a new XML writer with indentation.- Parameters:
out
- where to write the XML.- Throws:
XMLStreamException
-
-
Method Details
-
indent
Writes a line separator, then the given amount of spaces.- Throws:
XMLStreamException
-
emptyIndent
Appends indentation before an empty tag.- Throws:
XMLStreamException
-
startIndent
Increases the indentation level and appends indentation before a start tag.- Throws:
XMLStreamException
-
writeStartElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException Appends indentation, then forwards the call verbatim.- Specified by:
writeStartElement
in interfaceXMLStreamWriter
- Overrides:
writeStartElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEmptyElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElement
in interfaceXMLStreamWriter
- Overrides:
writeEmptyElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEndElement
Appends indentation if the value is not inline, then forwards the call verbatim.- Specified by:
writeEndElement
in interfaceXMLStreamWriter
- Overrides:
writeEndElement
in classStreamWriterDelegate
- Throws:
XMLStreamException
-
writeEndDocument
Appends a new line after the document.- Specified by:
writeEndDocument
in interfaceXMLStreamWriter
- Overrides:
writeEndDocument
in classStreamWriterDelegate
- Throws:
XMLStreamException
-