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 intNumber of spaces to add for each new indentation level.private booleantrueif the lastwriteStartElement(…)method invocation has not yet been followed by awriteEndElement(…)method invocation.private final StringThe line separator to use.private intThe 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 voidAppends indentation before an empty tag.private voidindent()Writes a line separator, then the given amount of spaces.(package private) final voidIncreases the indentation level and appends indentation before a start tag.voidwriteEmptyElement(String localName) Appends indentation, then forwards the call verbatim.voidwriteEmptyElement(String namespaceURI, String localName) Appends indentation, then forwards the call verbatim.voidwriteEmptyElement(String prefix, String localName, String namespaceURI) Appends indentation, then forwards the call verbatim.voidAppends a new line after the document.voidAppends indentation if the value is not inline, then forwards the call verbatim.voidwriteStartElement(String localName) Appends indentation, then forwards the call verbatim.voidwriteStartElement(String namespaceURI, String localName) Appends indentation, then forwards the call verbatim.voidwriteStartElement(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 theindentationvalue every time a XML start element or end element is encountered. -
inline
private boolean inlinetrueif 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:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeStartElement
public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException Appends indentation, then forwards the call verbatim.- Specified by:
writeStartElementin interfaceXMLStreamWriter- Overrides:
writeStartElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEmptyElement
Appends indentation, then forwards the call verbatim.- Specified by:
writeEmptyElementin interfaceXMLStreamWriter- Overrides:
writeEmptyElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndElement
Appends indentation if the value is not inline, then forwards the call verbatim.- Specified by:
writeEndElementin interfaceXMLStreamWriter- Overrides:
writeEndElementin classStreamWriterDelegate- Throws:
XMLStreamException
-
writeEndDocument
Appends a new line after the document.- Specified by:
writeEndDocumentin interfaceXMLStreamWriter- Overrides:
writeEndDocumentin classStreamWriterDelegate- Throws:
XMLStreamException
-