public class XmlOutput
extends java.lang.Object
Use this class to write XML to any streaming source. While the class itself is unstructured and doesn't enforce any DTD specification, use of the class does ensure that the output is syntactically valid XML.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
XmlOutput.StringEscaper
Utility for replacing special characters
with escape sequences in strings.
|
(package private) static class |
XmlOutput.XMLAttrVector
List of attribute names and values.
|
Modifier and Type | Field and Description |
---|---|
private boolean |
alwaysQuoteCData |
private boolean |
compact |
private boolean |
glob |
private boolean |
ignorePcdata |
private int |
indent |
private java.lang.String |
indentString |
private boolean |
inTag
Whether we have started but not finished a start tag.
|
private java.io.PrintWriter |
out |
private java.util.Deque<java.lang.String> |
tagStack |
private int |
tagsWritten |
Constructor and Description |
---|
XmlOutput(java.io.PrintWriter out)
Constructs a new XmlOutput based on a
PrintWriter . |
XmlOutput(java.io.Writer out)
Constructs a new XmlOutput based on any
Writer . |
Modifier and Type | Method and Description |
---|---|
void |
attribute(java.lang.String name,
java.lang.String value)
Writes an attribute.
|
void |
beginBeginTag(java.lang.String tagName) |
void |
beginNode()
If we are currently inside the start tag, finishes it off.
|
void |
beginTag(java.lang.String tagName,
XmlOutput.XMLAttrVector attributes)
Starts writing a new tag to the stream.
|
void |
cdata(java.lang.String data)
Writes a CDATA section.
|
void |
cdata(java.lang.String data,
boolean quote)
Writes a CDATA section (as
cdata(String) ). |
void |
content(java.lang.String content)
Writes content.
|
private void |
displayIndent(java.io.PrintWriter out,
int indent)
Private helper function to display a degree of indentation
|
void |
emptyTag(java.lang.String tagName,
XmlOutput.XMLAttrVector attributes)
Writes an empty tag to the stream.
|
void |
endBeginTag(java.lang.String tagName) |
void |
endTag(java.lang.String tagName)
Completes a tag.
|
private static java.lang.String |
escapeForQuoting(java.lang.String val) |
boolean |
getCompact() |
boolean |
getIgnorePcdata() |
void |
header()
Write header.
|
void |
header(java.lang.String version)
Write header, take version as input.
|
int |
numTagsWritten()
Get the total number of tags written
|
void |
print(java.lang.String s)
Sends a string directly to the output stream, without escaping any
characters.
|
private static void |
printAtt(java.io.PrintWriter pw,
java.lang.String name,
java.lang.String val)
Print an XML attribute name and value for string val
|
void |
setAlwaysQuoteCData(boolean alwaysQuoteCData)
Sets whether to always quote cdata segments (even if they don't contain
special characters).
|
void |
setCompact(boolean compact)
Sets or unsets the compact mode.
|
void |
setGlob(boolean glob)
Sets whether to detect that tags are empty.
|
void |
setIgnorePcdata(boolean ignorePcdata)
Sets whether to ignore unquoted text, such as whitespace.
|
void |
setIndentString(java.lang.String indentString)
Sets the string to print for each level of indentation.
|
private static void |
stringEncodeXML(java.lang.String input,
java.io.PrintWriter out)
Encode a String for XML output, displaying it to a PrintWriter.
|
private static boolean |
stringHasXMLSpecials(java.lang.String input)
Returns whether a string contains any XML special characters.
|
void |
stringTag(java.lang.String name,
java.lang.String data)
Writes a String tag; a tag containing nothing but a CDATA section.
|
private final java.io.PrintWriter out
private final java.util.Deque<java.lang.String> tagStack
private int indent
private int tagsWritten
private boolean compact
private java.lang.String indentString
setIndentString(java.lang.String)
private boolean glob
setGlob(boolean)
private boolean inTag
glob
is true. The start tag is automatically closed
when we start a child node. If there are no child nodes, endTag(java.lang.String)
creates an empty tag.private boolean alwaysQuoteCData
setAlwaysQuoteCData(boolean)
private boolean ignorePcdata
setIgnorePcdata(boolean)
public XmlOutput(java.io.Writer out)
Writer
.out
- the writer to which this XmlOutput generates results.public XmlOutput(java.io.PrintWriter out)
PrintWriter
.out
- the writer to which this XmlOutput generates results.private void displayIndent(java.io.PrintWriter out, int indent)
out
- the PrintWriter to which to display output.indent
- the degree of indentation.public void setCompact(boolean compact)
compact
- true to turn on compact mode, or false to turn it off.public boolean getCompact()
public void setIndentString(java.lang.String indentString)
null
. Set this to the empty
string to achieve no indentation (note that
setCompact(boolean)
(true)
removes indentation and
newlines).public void setGlob(boolean glob)
public void setAlwaysQuoteCData(boolean alwaysQuoteCData)
public void setIgnorePcdata(boolean ignorePcdata)
public boolean getIgnorePcdata()
public void print(java.lang.String s)
public void beginTag(java.lang.String tagName, XmlOutput.XMLAttrVector attributes)
tagName
- the name of the tag to write.attributes
- an XMLAttrVector containing the attributes to include
in the tag.public void beginBeginTag(java.lang.String tagName)
public void endBeginTag(java.lang.String tagName)
public void attribute(java.lang.String name, java.lang.String value)
public void beginNode()
public void endTag(java.lang.String tagName)
tagName
- the name of the end tag to write.public void emptyTag(java.lang.String tagName, XmlOutput.XMLAttrVector attributes)
tagName
- the name of the empty tag.attributes
- an XMLAttrVector containing the attributes to
include in the tag.public void cdata(java.lang.String data)
&
"
'
<
>
]]>
is special.
]]>
will be enclosed in a CDATA section.
]]>
sequence will be left as-is but have all of its
special characters encoded as entities.
public void cdata(java.lang.String data, boolean quote)
cdata(String)
).data
- string to writequote
- if true, quote in a <![CDATA[
... ]]>
regardless of the content of
data
; if false, quote only if the content needs itpublic void stringTag(java.lang.String name, java.lang.String data)
public void content(java.lang.String content)
public void header()
public void header(java.lang.String version)
public int numTagsWritten()
private static void printAtt(java.io.PrintWriter pw, java.lang.String name, java.lang.String val)
private static void stringEncodeXML(java.lang.String input, java.io.PrintWriter out)
input
- a String to convert.out
- a PrintWriter to which to write the results.private static java.lang.String escapeForQuoting(java.lang.String val)
private static boolean stringHasXMLSpecials(java.lang.String input)
If this function returns true, the string will need to be encoded either using the stringEncodeXML function above or using a CDATA section. Note that MSXML has a nasty bug whereby whitespace characters outside of a CDATA section are lost when parsing. To avoid hitting this bug, this method treats many whitespace characters as "special".
input
- the String to scan for XML special characters.Copyright © 2012–2019 The Apache Software Foundation. All rights reserved.