Class PrettyPrintWriter
- java.lang.Object
-
- com.thoughtworks.xstream.io.AbstractWriter
-
- com.thoughtworks.xstream.io.xml.AbstractXmlWriter
-
- com.thoughtworks.xstream.io.xml.PrettyPrintWriter
-
- All Implemented Interfaces:
ExtendedHierarchicalStreamWriter
,HierarchicalStreamWriter
,XmlFriendlyWriter
- Direct Known Subclasses:
CompactWriter
public class PrettyPrintWriter extends AbstractXmlWriter
A simple writer that outputs XML in a pretty-printed indented stream.By default, the chars
& < > " ' \r
are escaped and replaced with a suitable XML entity. To alter this behavior, override thewriteText(com.thoughtworks.xstream.core.util.QuickWriter, String)
andwriteAttributeValue(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 parser will also read character entities of normally invalid characters).
-
-
Field Summary
Fields Modifier and Type Field Description private static char[]
AMP
private static char[]
APOS
private static char[]
CLOSE
private static char[]
CR
protected int
depth
private FastStack
elementStack
private static char[]
GT
private char[]
lineIndenter
private static char[]
LT
private int
mode
private java.lang.String
newLine
private static char[]
NULL
private static char[]
QUOT
private boolean
readyForNewLine
private boolean
tagInProgress
private boolean
tagIsEmpty
private QuickWriter
writer
static int
XML_1_0
static int
XML_1_1
static int
XML_QUIRKS
-
Constructor Summary
Constructors Modifier Constructor Description PrettyPrintWriter(java.io.Writer writer)
PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter)
PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine)
Deprecated.As of 1.3PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine, XmlFriendlyReplacer replacer)
Deprecated.As of 1.3PrettyPrintWriter(java.io.Writer writer, int mode)
PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter)
PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder)
private
PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder, java.lang.String newLine)
PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, char[], NameCoder)
insteadPrettyPrintWriter(java.io.Writer writer, int mode, NameCoder nameCoder)
PrettyPrintWriter(java.io.Writer writer, int mode, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, NameCoder)
insteadPrettyPrintWriter(java.io.Writer writer, int mode, java.lang.String lineIndenter)
PrettyPrintWriter(java.io.Writer writer, NameCoder nameCoder)
PrettyPrintWriter(java.io.Writer writer, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, NameCoder)
instead.PrettyPrintWriter(java.io.Writer writer, java.lang.String lineIndenter)
PrettyPrintWriter(java.io.Writer writer, java.lang.String lineIndenter, java.lang.String newLine)
Deprecated.As of 1.3
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAttribute(java.lang.String key, java.lang.String value)
void
close()
Close the writer, if necessary.void
endNode()
protected void
endOfLine()
private void
finishTag()
void
flush()
Flush the writer, if necessary.protected java.lang.String
getNewLine()
Retrieve the line terminator.void
setValue(java.lang.String text)
Write the value (text content) of the current node.void
startNode(java.lang.String name)
void
startNode(java.lang.String name, java.lang.Class clazz)
protected void
writeAttributeValue(QuickWriter writer, java.lang.String text)
protected void
writeText(QuickWriter writer, java.lang.String text)
private void
writeText(java.lang.String text, boolean isAttribute)
-
Methods inherited from class com.thoughtworks.xstream.io.xml.AbstractXmlWriter
escapeXmlName
-
Methods inherited from class com.thoughtworks.xstream.io.AbstractWriter
encodeAttribute, encodeNode, underlyingWriter
-
-
-
-
Field Detail
-
XML_QUIRKS
public static int XML_QUIRKS
-
XML_1_0
public static int XML_1_0
-
XML_1_1
public static int XML_1_1
-
writer
private final QuickWriter writer
-
elementStack
private final FastStack 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
private java.lang.String 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
-
-
Constructor Detail
-
PrettyPrintWriter
private PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder, java.lang.String newLine)
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine, XmlFriendlyReplacer replacer)
Deprecated.As of 1.3- Since:
- 1.2
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, NameCoder nameCoder)
- Since:
- 1.4
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, char[], NameCoder)
instead- Since:
- 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter, java.lang.String newLine)
Deprecated.As of 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, char[] lineIndenter)
- Since:
- 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, char[] lineIndenter)
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, java.lang.String lineIndenter, java.lang.String newLine)
Deprecated.As of 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, java.lang.String lineIndenter)
- Since:
- 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, java.lang.String lineIndenter)
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, NameCoder nameCoder)
- Since:
- 1.4
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, int, NameCoder)
instead- Since:
- 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, NameCoder nameCoder)
- Since:
- 1.4
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, XmlFriendlyReplacer replacer)
Deprecated.As of 1.4 usePrettyPrintWriter(Writer, NameCoder)
instead.
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer, int mode)
- Since:
- 1.3
-
PrettyPrintWriter
public PrettyPrintWriter(java.io.Writer writer)
-
-
Method Detail
-
startNode
public void startNode(java.lang.String name)
-
startNode
public void startNode(java.lang.String name, java.lang.Class clazz)
Description copied from class:AbstractWriter
- Specified by:
startNode
in interfaceExtendedHierarchicalStreamWriter
- Overrides:
startNode
in classAbstractWriter
-
setValue
public void setValue(java.lang.String text)
Description copied from interface:HierarchicalStreamWriter
Write the value (text content) of the current node.
-
addAttribute
public void addAttribute(java.lang.String key, java.lang.String value)
-
writeAttributeValue
protected void writeAttributeValue(QuickWriter writer, java.lang.String text)
-
writeText
protected void writeText(QuickWriter writer, java.lang.String text)
-
writeText
private void writeText(java.lang.String text, boolean isAttribute)
-
endNode
public void endNode()
-
finishTag
private void finishTag()
-
endOfLine
protected void endOfLine()
-
flush
public void flush()
Description copied from interface:HierarchicalStreamWriter
Flush the writer, if necessary.
-
close
public void close()
Description copied from interface:HierarchicalStreamWriter
Close the writer, if necessary.
-
getNewLine
protected java.lang.String 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
-
-