public class OutputFormat extends Object implements Cloneable
OutputFormat
represents the format configuration
used by XMLWriter
and its base classes to format the XML output
Modifier and Type | Field and Description |
---|---|
private String |
encoding
The encoding format
|
private boolean |
expandEmptyElements
Whether or not to expand empty elements to <tagName></tagName> - default is
false |
private String |
indent
The default indent is no spaces (as original document)
|
private String |
lineSeparator
New line separator
|
private boolean |
newlines
The default new line flag, set to do new lines only as in original document
|
private boolean |
omitEncoding
Whether or not to output the encoding in the XML declaration - default is
false |
private boolean |
padText
pad string-element boundaries with whitespace
|
protected static String |
STANDARD_INDENT
standard value to indent by, if we are indenting
|
private boolean |
suppressDeclaration
Whether or not to suppress the XML declaration - default is
false |
private boolean |
trimText
should we preserve whitespace or not in text nodes?
|
Constructor and Description |
---|
OutputFormat()
Creates an
OutputFormat with
no additional whitespace (indent or new lines) added. |
OutputFormat(String indent)
Creates an
OutputFormat with the given indent added but
no new lines added. |
OutputFormat(String indent,
boolean newlines)
Creates an
OutputFormat with the given indent added
with optional newlines between the Elements. |
OutputFormat(String indent,
boolean newlines,
String encoding)
Creates an
OutputFormat with the given indent added
with optional newlines between the Elements
and the given encoding format. |
Modifier and Type | Method and Description |
---|---|
static OutputFormat |
createCompactFormat()
A static helper method to create the default compact format.
|
static OutputFormat |
createPrettyPrint()
A static helper method to create the default pretty printing format.
|
String |
getEncoding() |
String |
getIndent() |
String |
getLineSeparator() |
boolean |
isExpandEmptyElements() |
boolean |
isNewlines() |
boolean |
isOmitEncoding() |
boolean |
isPadText() |
boolean |
isSuppressDeclaration() |
boolean |
isTrimText() |
int |
parseOptions(String[] args,
int i)
Parses command line arguments of the form
-omitEncoding -indentSize 3 -newlines -trimText |
void |
setEncoding(String encoding) |
void |
setExpandEmptyElements(boolean expandEmptyElements)
This will set whether empty elements are expanded from
<tagName> to
<tagName></tagName> . |
void |
setIndent(boolean doIndent)
Set the indent on or off.
|
void |
setIndent(String indent)
This will set the indent
String to use; this
is usually a String of empty spaces. |
void |
setIndentSize(int indentSize)
This will set the indent
String 's size; an indentSize
of 4 would result in the indention being equivalent to the String
" " (four space characters). |
void |
setLineSeparator(String separator)
This will set the new-line separator.
|
void |
setNewlines(boolean newlines) |
void |
setOmitEncoding(boolean omitEncoding)
This will set whether the XML declaration
(
<? |
void |
setPadText(boolean padText)
Ensure that text immediately preceded by or followed by an
element will be "padded" with a single space.
|
void |
setSuppressDeclaration(boolean suppressDeclaration)
This will set whether the XML declaration
(
<? |
void |
setTrimText(boolean trimText)
This will set whether the text is output verbatim (false)
or with whitespace stripped as per
. |
protected static final String STANDARD_INDENT
private boolean suppressDeclaration
false
private boolean omitEncoding
false
private boolean expandEmptyElements
false
private boolean newlines
private String lineSeparator
private boolean trimText
private boolean padText
public OutputFormat()
OutputFormat
with
no additional whitespace (indent or new lines) added.
The whitespace from the element text content is fully preserved.public OutputFormat(String indent)
OutputFormat
with the given indent added but
no new lines added. All whitespace from element text will be included.indent
- is the indent string to be used for indentation
(usually a number of spaces).public OutputFormat(String indent, boolean newlines)
OutputFormat
with the given indent added
with optional newlines between the Elements.
All whitespace from element text will be included.indent
- is the indent string to be used for indentation
(usually a number of spaces).newlines
- whether new lines are added to layout thepublic OutputFormat(String indent, boolean newlines, String encoding)
OutputFormat
with the given indent added
with optional newlines between the Elements
and the given encoding format.indent
- is the indent string to be used for indentation
(usually a number of spaces).newlines
- whether new lines are added to layout theencoding
- is the text encoding to use for writing the XMLpublic String getLineSeparator()
public void setLineSeparator(String separator)
This will set the new-line separator. The default is
\n
. Note that if the "newlines" property is
false, this value is irrelevant. To make it output the system
default line ending string, call
setLineSeparator(System.getProperty("line.separator"))
separator
- String
line separator to use.setNewlines(boolean)
public boolean isNewlines()
public void setNewlines(boolean newlines)
newlines
- true
indicates new lines should be
printed, else new lines are ignored (compacted).setLineSeparator(String)
public String getEncoding()
public void setEncoding(String encoding)
encoding
- encoding formatpublic boolean isOmitEncoding()
public void setOmitEncoding(boolean omitEncoding)
This will set whether the XML declaration
(<?xml version="1.0" encoding="UTF-8"?>
)
includes the encoding of the document.
It is common to suppress this in protocols such as WML and SOAP.
omitEncoding
- boolean
indicating whether or not
the XML declaration should indicate the document encoding.public void setSuppressDeclaration(boolean suppressDeclaration)
This will set whether the XML declaration
(<?xml version="1.0" encoding="UTF-8"?>
)
is included or not.
It is common to suppress this in protocols such as WML and SOAP.
suppressDeclaration
- boolean
indicating whether or not
the XML declaration should be suppressed.public boolean isSuppressDeclaration()
<?xml version="1.0"?>
) should be suppressed else false.public boolean isExpandEmptyElements()
public void setExpandEmptyElements(boolean expandEmptyElements)
This will set whether empty elements are expanded from
<tagName>
to
<tagName></tagName>
.
expandEmptyElements
- boolean
indicating whether or not
empty elements should be expanded.public boolean isTrimText()
public void setTrimText(boolean trimText)
This will set whether the text is output verbatim (false)
or with whitespace stripped as per
.Element.getTextTrim()
Default: false
trimText
- boolean
true=>trim the whitespace, false=>use text verbatimpublic boolean isPadText()
public void setPadText(boolean padText)
Ensure that text immediately preceded by or followed by an
element will be "padded" with a single space. This is used to
allow make browser-friendly HTML, avoiding trimText's
transformation of, e.g.,
The quick <b>brown</b> fox
into
The quick<b>brown</b>fox
(the latter
will run the three separate words together into a single word).
This setting is not too useful if you haven't also called
setTrimText(boolean)
.
Default: false
padText
- boolean
if true, pad string-element boundariespublic void setIndent(String indent)
This will set the indent String
to use; this
is usually a String
of empty spaces. If you pass
null, or the empty string (""), then no indentation will
happen.
indent
- String
to use for indentation.public void setIndent(boolean doIndent)
doIndent
- if true, set indenting on; if false, set indenting offpublic void setIndentSize(int indentSize)
This will set the indent String
's size; an indentSize
of 4 would result in the indention being equivalent to the String
" " (four space characters).
indentSize
- int
number of spaces in indentation.public int parseOptions(String[] args, int i)
-omitEncoding -indentSize 3 -newlines -trimText
args
- is the array of command line argumentsi
- is the index in args to start parsing optionspublic static OutputFormat createPrettyPrint()
public static OutputFormat createCompactFormat()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses