Top Description Fields Constructors Methods
org.dom4j.io

public Class OutputFormat

extends Object
implements Cloneable
Class Inheritance
All Implemented Interfaces
java.lang.Cloneable

OutputFormat represents the format configuration used by XMLWriterand its base classes to format the XML output
Author
James Strachan
Version
$Revision: 1.17 $

Field Summary

Modifier and TypeField and Description
private char
attributeQuoteChar

Quote character to use when writing attributes.

private boolean
doXHTML

Whether or not to use XHTML standard.

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
newLineAfterDeclaration

Whether or not to print new line after the XML declaration - default is true

private int
newLineAfterNTags

Controls when to output a line.separtor every so many tags in case of no lines and total text trimming.

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 final 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 Summary

AccessConstructor and Description
public
OutputFormat()

Creates an OutputFormat with no additional whitespace (indent or new lines) added.

public
OutputFormat(String
is the indent string to be used for indentation (usually a number of spaces).
indent
)

Creates an OutputFormat with the given indent added but no new lines added.

public
OutputFormat(String
is the indent string to be used for indentation (usually a number of spaces).
indent
,
boolean
whether new lines are added to layout the
newlines
)

Creates an OutputFormat with the given indent added with optional newlines between the Elements.

public
OutputFormat(String
is the indent string to be used for indentation (usually a number of spaces).
indent
,
boolean
whether new lines are added to layout the
newlines
,
String
is the text encoding to use for writing the XML
encoding
)

Creates an OutputFormat with the given indent added with optional newlines between the Elements and the given encoding format.

Method Summary

Modifier and TypeMethod and Description
public static OutputFormat

Returns:

DOCUMENT ME!
createCompactFormat
()

A static helper method to create the default compact format.

public static OutputFormat

Returns:

DOCUMENT ME!
createPrettyPrint
()

A static helper method to create the default pretty printing format.

public char
public String
public String
public String
public int
public boolean
public boolean

Returns:

true if a new line should be printed following XML declaration
isNewLineAfterDeclaration
()

DOCUMENT ME!

public boolean
public boolean
public boolean
public boolean

Returns:

true if the output of the XML declaration (<?xml version="1.0"?>) should be suppressed else false.
isSuppressDeclaration
()

DOCUMENT ME!

public boolean
public boolean

Returns:

DOCUMENT ME!
isXHTML
()

Whether or not to use the XHTML standard: like HTML but passes an XML parser with real, closed tags.

public int

Returns:

the index of first parameter that we didn't understand
parseOptions
(String[]
is the array of command line arguments
args
,
int
is the index in args to start parsing options
i
)

Parses command line arguments of the form -omitEncoding -indentSize 3 -newlines -trimText

public void
setAttributeQuoteCharacter(char
The character to use when quoting attribute values.
quoteChar
)

Sets the character used to quote attribute values.

public void
setEncoding(String
encoding format
encoding
)

DOCUMENT ME!

public void
setExpandEmptyElements(boolean
boolean indicating whether or not empty elements should be expanded.
expandEmptyElements
)

This will set whether empty elements are expanded from <tagName> to <tagName></tagName>.

public void
setIndent(String
String to use for indentation.
indent
)

This will set the indent String to use; this is usually a String of empty spaces.

public void
setIndent(boolean
if true, set indenting on; if false, set indenting off
doIndent
)

Set the indent on or off.

public void
setIndentSize(int
int number of spaces in indentation.
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).

public void
setLineSeparator(String
String line separator to use.
separator
)

This will set the new-line separator.

public void
setNewLineAfterDeclaration(boolean
boolean indicating whether or not to print new line following the XML declaration. The default is true.
newLineAfterDeclaration
)

This will set whether a new line is printed after the XML declaration (assuming it is not supressed.)

public void
setNewLineAfterNTags(int
DOCUMENT ME!
tagCount
)

Controls output of a line.separator every tagCount tags when isNewlines is false.

public void
setNewlines(boolean
true indicates new lines should be printed, else new lines are ignored (compacted).
newlines
)

DOCUMENT ME!

public void
setOmitEncoding(boolean
boolean indicating whether or not the XML declaration should indicate the document encoding.
omitEncoding
)

This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?

public void
setPadText(boolean
boolean if true, pad string-element boundaries
padText
)

Ensure that text immediately preceded by or followed by an element will be "padded" with a single space.

public void
setSuppressDeclaration(boolean
boolean indicating whether or not the XML declaration should be suppressed.
suppressDeclaration
)

This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?

public void
setTrimText(boolean
boolean true → trim the whitespace, false → use text verbatim
trimText
)

This will set whether the text is output verbatim (false) or with whitespace stripped as per org.dom4j.Element#getTextTrim().

public void
setXHTML(boolean
boolean true → conform to XHTML, false → conform to HTML, can have unclosed tags, etc.
xhtml
)

This will set whether or not to use the XHTML standard: like HTML but passes an XML parser with real, closed tags.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributeQuoteCharback to summary
private char attributeQuoteChar

Quote character to use when writing attributes.

doXHTMLback to summary
private boolean doXHTML

Whether or not to use XHTML standard.

encodingback to summary
private String encoding

The encoding format

expandEmptyElementsback to summary
private boolean expandEmptyElements

Whether or not to expand empty elements to <tagName></tagName> - default is false

indentback to summary
private String indent

The default indent is no spaces (as original document)

lineSeparatorback to summary
private String lineSeparator

New line separator

newLineAfterDeclarationback to summary
private boolean newLineAfterDeclaration

Whether or not to print new line after the XML declaration - default is true

newLineAfterNTagsback to summary
private int newLineAfterNTags

Controls when to output a line.separtor every so many tags in case of no lines and total text trimming.

newlinesback to summary
private boolean newlines

The default new line flag, set to do new lines only as in original document

omitEncodingback to summary
private boolean omitEncoding

Whether or not to output the encoding in the XML declaration - default is false

padTextback to summary
private boolean padText

pad string-element boundaries with whitespace

STANDARD_INDENTback to summary
protected static final String STANDARD_INDENT

standard value to indent by, if we are indenting

suppressDeclarationback to summary
private boolean suppressDeclaration

Whether or not to suppress the XML declaration - default is false

trimTextback to summary
private boolean trimText

should we preserve whitespace or not in text nodes?

Constructor Detail

OutputFormatback to summary
public OutputFormat()

Creates an OutputFormat with no additional whitespace (indent or new lines) added. The whitespace from the element text content is fully preserved.

OutputFormatback to summary
public OutputFormat(String indent)

Creates an OutputFormat with the given indent added but no new lines added. All whitespace from element text will be included.

Parameters
indent:String

is the indent string to be used for indentation (usually a number of spaces).

OutputFormatback to summary
public OutputFormat(String indent, boolean newlines)

Creates an OutputFormat with the given indent added with optional newlines between the Elements. All whitespace from element text will be included.

Parameters
indent:String

is the indent string to be used for indentation (usually a number of spaces).

newlines:boolean

whether new lines are added to layout the

OutputFormatback to summary
public 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.

Parameters
indent:String

is the indent string to be used for indentation (usually a number of spaces).

newlines:boolean

whether new lines are added to layout the

encoding:String

is the text encoding to use for writing the XML

Method Detail

createCompactFormatback to summary
public static OutputFormat createCompactFormat()

A static helper method to create the default compact format. This format does not have any indentation or newlines after an alement and all other whitespace trimmed

Returns:OutputFormat

DOCUMENT ME!

createPrettyPrintback to summary
public static OutputFormat createPrettyPrint()

A static helper method to create the default pretty printing format. This format consists of an indent of 2 spaces, newlines after each element and all other whitespace trimmed, and XMTML is false.

Returns:OutputFormat

DOCUMENT ME!

getAttributeQuoteCharacterback to summary
public char getAttributeQuoteCharacter()
getEncodingback to summary
public String getEncoding()
getIndentback to summary
public String getIndent()
getLineSeparatorback to summary
public String getLineSeparator()
getNewLineAfterNTagsback to summary
public int getNewLineAfterNTags()
isExpandEmptyElementsback to summary
public boolean isExpandEmptyElements()
isNewLineAfterDeclarationback to summary
public boolean isNewLineAfterDeclaration()

DOCUMENT ME!

Returns:boolean

true if a new line should be printed following XML declaration

isNewlinesback to summary
public boolean isNewlines()
isOmitEncodingback to summary
public boolean isOmitEncoding()
isPadTextback to summary
public boolean isPadText()
isSuppressDeclarationback to summary
public boolean isSuppressDeclaration()

DOCUMENT ME!

Returns:boolean

true if the output of the XML declaration (<?xml version="1.0"?>) should be suppressed else false.

isTrimTextback to summary
public boolean isTrimText()
isXHTMLback to summary
public boolean isXHTML()

Whether or not to use the XHTML standard: like HTML but passes an XML parser with real, closed tags. Also, XHTML CDATA sections will be output with the CDATA delimiters: ( " <![CDATA[ " and " ]]> " ) otherwise, the class HTMLWriter will output the CDATA text, but not the delimiters. Default is false

Returns:boolean

DOCUMENT ME!

parseOptionsback to summary
public int parseOptions(String[] args, int i)

Parses command line arguments of the form -omitEncoding -indentSize 3 -newlines -trimText

Parameters
args:String[]

is the array of command line arguments

i:int

is the index in args to start parsing options

Returns:int

the index of first parameter that we didn't understand

setAttributeQuoteCharacterback to summary
public void setAttributeQuoteCharacter(char quoteChar)

Sets the character used to quote attribute values. The specified character must be a valid XML attribute quote character, otherwise an IllegalArgumentException will be thrown.

Parameters
quoteChar:char

The character to use when quoting attribute values.

Exceptions
IllegalArgumentException:
If the specified character is not a valid XML attribute quote character.
setEncodingback to summary
public void setEncoding(String encoding)

DOCUMENT ME!

Parameters
encoding:String

encoding format

setExpandEmptyElementsback to summary
public void setExpandEmptyElements(boolean expandEmptyElements)

This will set whether empty elements are expanded from <tagName> to <tagName></tagName>.

Parameters
expandEmptyElements:boolean

boolean indicating whether or not empty elements should be expanded.

setIndentback to summary
public 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. Default: none (null)

Parameters
indent:String

String to use for indentation.

setIndentback to summary
public void setIndent(boolean doIndent)

Set the indent on or off. If setting on, will use the value of STANDARD_INDENT, which is usually two spaces.

Parameters
doIndent:boolean

if true, set indenting on; if false, set indenting off

setIndentSizeback to summary
public 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).

Parameters
indentSize:int

int number of spaces in indentation.

setLineSeparatorback to summary
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"))

Parameters
separator:String

String line separator to use.

See Also
setNewlines(boolean)
setNewLineAfterDeclarationback to summary
public void setNewLineAfterDeclaration(boolean newLineAfterDeclaration)

This will set whether a new line is printed after the XML declaration (assuming it is not supressed.)

Parameters
newLineAfterDeclaration:boolean

boolean indicating whether or not to print new line following the XML declaration. The default is true.

setNewLineAfterNTagsback to summary
public void setNewLineAfterNTags(int tagCount)

Controls output of a line.separator every tagCount tags when isNewlines is false. If tagCount equals zero, it means don't do anything special. If greater than zero, then a line.separator will be output after tagCount tags have been output. Used when you would like to squeeze the html as much as possible, but some browsers don't like really long lines. A tag count of 10 would produce a line.separator in the output after 10 close tags (including single tags).

Parameters
tagCount:int

DOCUMENT ME!

setNewlinesback to summary
public void setNewlines(boolean newlines)

DOCUMENT ME!

Parameters
newlines:boolean

true indicates new lines should be printed, else new lines are ignored (compacted).

See Also
setLineSeparator(String)
setOmitEncodingback to summary
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.

Parameters
omitEncoding:boolean

boolean indicating whether or not the XML declaration should indicate the document encoding.

setPadTextback to summary
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. The padding string will only be added if the text itself starts or ends with some whitespace characters. Default: false

Parameters
padText:boolean

boolean if true, pad string-element boundaries

setSuppressDeclarationback to summary
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.

Parameters
suppressDeclaration:boolean

boolean indicating whether or not the XML declaration should be suppressed.

setTrimTextback to summary
public void setTrimText(boolean trimText)

This will set whether the text is output verbatim (false) or with whitespace stripped as per org.dom4j.Element#getTextTrim(). Default: false

Parameters
trimText:boolean

boolean true → trim the whitespace, false → use text verbatim

setXHTMLback to summary
public void setXHTML(boolean xhtml)

This will set whether or not to use the XHTML standard: like HTML but passes an XML parser with real, closed tags. Also, XHTML CDATA sections will be output with the CDATA delimiters: ( " <[CDATA[ " and " ]]< ) otherwise, the class HTMLWriter will output the CDATA text, but not the delimiters. Default: false

Parameters
xhtml:boolean

boolean true → conform to XHTML, false → conform to HTML, can have unclosed tags, etc.