Class PrettyPrintWriter
- All Implemented Interfaces:
ExtendedHierarchicalStreamWriter, HierarchicalStreamWriter, XmlFriendlyWriter
- Direct Known Subclasses:
CompactWriter
By default, the chars & < > " ' \r
are escaped
and replaced with a suitable XML entity. To alter this behavior, override
the writeText(com.thoughtworks.xstream.core.util.QuickWriter, String) and
writeAttributeValue(com.thoughtworks.xstream.core.util.QuickWriter, String) methods.
The XML specification requires XML parsers to drop CR characters completely. This implementation
will therefore use only a LF for line endings, never the platform encoding. You can overwrite the
getNewLine() method for a different behavior.
Note: Depending on the XML version some characters cannot be written. Especially a 0
character is never valid in XML, neither directly nor as entity nor within CDATA. However, this writer
works by default in a quirks mode, where it will write any character at least as character entity (even
a null character). You may switch into XML_1_1 mode (which supports most characters) or XML_1_0
that does only support a very limited number of control characters. See XML specification for version
1.0 or
1.1. If a character is
not supported, a StreamException is thrown. Select a proper parser implementation that
respects the version in the XML header (the Xpp3 and MX parsers will also read character entities of normally
invalid characters).
You may also switch to XML_1_0_REPLACEMENT or XML_1_1_REPLACEMENT mode, which will replace the invalid characters
with a U+FFFD replacement character.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char[]private static final char[]private static final char[]private static final char[]protected intprivate final FastStackprivate static final char[]private final char[]private static final char[]private final intprivate Stringprivate static final char[]private static final char[]private booleanprivate static final char[]private booleanprivate booleanprivate final QuickWriterstatic intXML 1.0 mode: Writes characters according XML 1.0 specification, throwsStreamExceptionfor invalid characters.static intXML 1.0 mode: Writes characters according XML 1.0 specification, writes character U+FFFFD as replacement for invalid ones.static intXML 1.1 mode: Writes characters according XML 1.1 specification, throwsStreamExceptionfor invalid characters.static intXML 1.1 mode: Writes characters according XML 1.1 specification, writes character U+FFFFD as replacement for invalid ones.static intQuirks mode: Writes any character into data stream incl. -
Constructor Summary
ConstructorsModifierConstructorDescriptionPrettyPrintWriter(Writer writer) PrettyPrintWriter(Writer writer, char[] lineIndenter) PrettyPrintWriter(Writer writer, char[] lineIndenter, String newLine) Deprecated.As of 1.3PrettyPrintWriter(Writer writer, char[] lineIndenter, String newLine, XmlFriendlyReplacer replacer) Deprecated.As of 1.3PrettyPrintWriter(Writer writer, int mode) PrettyPrintWriter(Writer writer, int mode, char[] lineIndenter) PrettyPrintWriter(Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder) privatePrettyPrintWriter(Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder, String newLine) PrettyPrintWriter(Writer writer, int mode, char[] lineIndenter, XmlFriendlyReplacer replacer) Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, char[], NameCoder)insteadPrettyPrintWriter(Writer writer, int mode, NameCoder nameCoder) PrettyPrintWriter(Writer writer, int mode, XmlFriendlyReplacer replacer) Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, NameCoder)insteadPrettyPrintWriter(Writer writer, int mode, String lineIndenter) PrettyPrintWriter(Writer writer, NameCoder nameCoder) PrettyPrintWriter(Writer writer, XmlFriendlyReplacer replacer) Deprecated.As of 1.4 usePrettyPrintWriter(Writer, NameCoder)instead.PrettyPrintWriter(Writer writer, String lineIndenter) PrettyPrintWriter(Writer writer, String lineIndenter, String newLine) Deprecated.As of 1.3 -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttribute(String key, String value) voidclose()Close the writer, if necessary.voidendNode()protected voidprivate voidvoidflush()Flush the writer, if necessary.protected StringRetrieve the line terminator.voidWrite the value (text content) of the current node.voidvoidprotected voidwriteAttributeValue(QuickWriter writer, String text) protected voidwriteText(QuickWriter writer, String text) private voidMethods inherited from class AbstractXmlWriter
escapeXmlNameMethods inherited from class AbstractWriter
encodeAttribute, encodeNode, underlyingWriter
-
Field Details
-
XML_QUIRKS
public static int XML_QUIRKSQuirks mode: Writes any character into data stream incl. U+0000. -
XML_1_0
public static int XML_1_0XML 1.0 mode: Writes characters according XML 1.0 specification, throwsStreamExceptionfor invalid characters. -
XML_1_1
public static int XML_1_1XML 1.1 mode: Writes characters according XML 1.1 specification, throwsStreamExceptionfor invalid characters. -
XML_1_0_REPLACEMENT
public static int XML_1_0_REPLACEMENTXML 1.0 mode: Writes characters according XML 1.0 specification, writes character U+FFFFD as replacement for invalid ones.- Since:
- 1.4.21
-
XML_1_1_REPLACEMENT
public static int XML_1_1_REPLACEMENTXML 1.1 mode: Writes characters according XML 1.1 specification, writes character U+FFFFD as replacement for invalid ones.- Since:
- 1.4.21
-
writer
-
elementStack
-
lineIndenter
private final char[] lineIndenter -
mode
private final int mode -
tagInProgress
private boolean tagInProgress -
depth
protected int depth -
readyForNewLine
private boolean readyForNewLine -
tagIsEmpty
private boolean tagIsEmpty -
newLine
-
NULL
private static final char[] NULL -
AMP
private static final char[] AMP -
LT
private static final char[] LT -
GT
private static final char[] GT -
CR
private static final char[] CR -
QUOT
private static final char[] QUOT -
APOS
private static final char[] APOS -
CLOSE
private static final char[] CLOSE -
REPLACEMENT
private static final char[] REPLACEMENT
-
-
Constructor Details
-
PrettyPrintWriter
-
PrettyPrintWriter
public PrettyPrintWriter(Writer writer, char[] lineIndenter, String newLine, XmlFriendlyReplacer replacer) Deprecated.As of 1.3- Since:
- 1.2
-
PrettyPrintWriter
-
PrettyPrintWriter
public PrettyPrintWriter(Writer writer, int mode, char[] lineIndenter, XmlFriendlyReplacer replacer) Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, char[], NameCoder)instead- Since:
- 1.3
-
PrettyPrintWriter
-
PrettyPrintWriter
- Since:
- 1.3
-
PrettyPrintWriter
-
PrettyPrintWriter
-
PrettyPrintWriter
-
PrettyPrintWriter
-
PrettyPrintWriter
-
PrettyPrintWriter
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, NameCoder)instead- Since:
- 1.3
-
PrettyPrintWriter
-
PrettyPrintWriter
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, NameCoder)instead. -
PrettyPrintWriter
- Since:
- 1.3
-
PrettyPrintWriter
-
-
Method Details
-
startNode
-
startNode
Description copied from class:AbstractWriter- Specified by:
startNodein interfaceExtendedHierarchicalStreamWriter- Overrides:
startNodein classAbstractWriter
-
setValue
Description copied from interface:HierarchicalStreamWriterWrite the value (text content) of the current node. -
addAttribute
-
writeAttributeValue
-
writeText
-
writeText
-
endNode
public void endNode() -
finishTag
private void finishTag() -
endOfLine
protected void endOfLine() -
flush
public void flush()Description copied from interface:HierarchicalStreamWriterFlush the writer, if necessary. -
close
public void close()Description copied from interface:HierarchicalStreamWriterClose the writer, if necessary. -
getNewLine
Retrieve the line terminator. This method returns always a line feed, since according the XML specification any parser must ignore a carriage return. Overload this method, if you need different behavior.- Returns:
- the line terminator
- Since:
- 1.3
-