Class SourceFormatter
- All Implemented Interfaces:
CharStreamSource
Any indentation present in the original source text is removed.
Use one of the following methods to obtain the output:
The output text is functionally equivalent to the original source and should be rendered identically unless specified below.
The following points describe the process in general terms. Any aspect of the algorithm not specifically mentioned here is subject to change without notice in future versions.
- Every element that is not an inline-level element appears on a new line with an indent corresponding to its depth in the document element hierarchy.
- The indent is formed by writing n repetitions of the string specified in the
IndentStringproperty, where n is the depth of the indentation. - The content of an indented element starts on a new line and is indented at a depth one greater than that of the element, with the end tag appearing on a new line at the same depth as the start tag. If the content contains only text and inline-level elements, it may continue on the same line as the start tag. Additionally, if the output content contains no new lines, the end tag may also continue on the same line.
- The content of preformatted elements such as
PREandTEXTAREAare not indented, nor is the white space modified in any way. - Only normal and document type declaration elements are indented. All others are treated as inline-level elements.
- White space and indentation inside HTML comments, CDATA sections, or any server tag is preserved, but with the indentation of new lines starting at a depth one greater than that of the surrounding text.
- White space and indentation inside
SCRIPTelements is preserved, but with the indentation of new lines starting at a depth one greater than that of theSCRIPTelement. - If the
TidyTagsproperty is set totrue, every tag in the document is replaced with the output from itsTag.tidy()method. If this property is set tofalse, the tag from the original text is used, including all white space, but with any new lines indented at a depth one greater than that of the element. - If the
CollapseWhiteSpaceproperty is set totrue, every string of one or more white space characters located outside of a tag is replaced with a single space in the output. White space located adjacent to a non-inline-level element tag (except server tags) may be removed. - If the
IndentAllElementsproperty is set totrue, every element appears indented on a new line, including inline-level elements. This generates output that is a good representation of the actual document element hierarchy, but is very likely to introduce white space that compromises the functional equivalency of the document. - The
NewLineproperty specifies the character sequence to use for each newline in the output document. - If the source document contains server tags, the functional equivalency of the output document may be compromised.
Formatting an entire Source object performs a full sequential parse automatically.
-
Constructor Summary
ConstructorsConstructorDescriptionSourceFormatter(Segment segment) Constructs a newSourceFormatterbased on the specifiedSegment. -
Method Summary
Modifier and TypeMethodDescriptionvoidappendTo(Appendable appendable) Appends the output to the specifiedAppendableobject.booleanIndicates whether white space in the text between the tags is to be collapsed.longReturns the estimated maximum number of characters in the output, or-1if no estimate is available.booleanIndicates whether all elements are to be indented, including inline-level elements and those with preformatted contents.Returns the string to be used for indentation.Returns the string to be used to represent a newline in the output.booleanIndicates whether the original text of each tag is to be replaced with the output from itsTag.tidy()method.setCollapseWhiteSpace(boolean collapseWhiteSpace) Sets whether white space in the text between the tags is to be collapsed.setIndentAllElements(boolean indentAllElements) Sets whether all elements are to be indented, including inline-level elements and those with preformatted contents.setIndentString(String indentString) Sets the string to be used for indentation.setNewLine(String newLine) Sets the string to be used to represent a newline in the output.setTidyTags(boolean tidyTags) Sets whether the original text of each tag is to be replaced with the output from itsTag.tidy()method.toString()Returns the output as a string.voidWrites the output to the specifiedWriter.
-
Constructor Details
-
SourceFormatter
-
-
Method Details
-
writeTo
Description copied from interface:CharStreamSourceWrites the output to the specifiedWriter.- Specified by:
writeToin interfaceCharStreamSource- Parameters:
writer- the destinationjava.io.Writerfor the output.- Throws:
IOException- if an I/O exception occurs.
-
appendTo
Description copied from interface:CharStreamSourceAppends the output to the specifiedAppendableobject.- Specified by:
appendToin interfaceCharStreamSource- Parameters:
appendable- the destinationjava.lang.Appendableobject for the output.- Throws:
IOException- if an I/O exception occurs.
-
getEstimatedMaximumOutputLength
public long getEstimatedMaximumOutputLength()Description copied from interface:CharStreamSourceReturns the estimated maximum number of characters in the output, or-1if no estimate is available.The returned value should be used as a guide for efficiency purposes only, for example to set an initial
StringBuildercapacity. There is no guarantee that the length of the output is indeed less than this value, as classes implementing this method often use assumptions based on typical usage to calculate the estimate.Although implementations of this method should never return a value less than -1, users of this method must not assume that this will always be the case. Standard practice is to interpret any negative value as meaning that no estimate is available.
- Specified by:
getEstimatedMaximumOutputLengthin interfaceCharStreamSource- Returns:
- the estimated maximum number of characters in the output, or
-1if no estimate is available.
-
toString
Description copied from interface:CharStreamSourceReturns the output as a string.- Specified by:
toStringin interfaceCharStreamSource- Overrides:
toStringin classObject- Returns:
- the output as a string.
-
setIndentString
Sets the string to be used for indentation.The default value is a string containing a single tab character (U+0009).
The most commonly used indent strings are
"\t"(single tab)," "(single space)," "(2 spaces), and" "(4 spaces).- Parameters:
indentString- the string to be used for indentation, must not benull.- Returns:
- this
SourceFormatterinstance, allowing multiple property setting methods to be chained in a single statement. - See Also:
-
getIndentString
Returns the string to be used for indentation.See the
setIndentString(String)method for a full description of this property.- Returns:
- the string to be used for indentation.
-
setTidyTags
Sets whether the original text of each tag is to be replaced with the output from itsTag.tidy()method.The default value is
false.If this property is set to
false, the tag from the original text is used, including all white space, but with any new lines indented at a depth one greater than that of the element.- Parameters:
tidyTags- specifies whether the original text of each tag is to be replaced with the output from itsTag.tidy()method.- Returns:
- this
SourceFormatterinstance, allowing multiple property setting methods to be chained in a single statement. - See Also:
-
getTidyTags
public boolean getTidyTags()Indicates whether the original text of each tag is to be replaced with the output from itsTag.tidy()method.See the
setTidyTags(boolean)method for a full description of this property.- Returns:
trueif the original text of each tag is to be replaced with the output from itsTag.tidy()method, otherwisefalse.
-
setCollapseWhiteSpace
Sets whether white space in the text between the tags is to be collapsed.The default value is
false.If this property is set to
true, every string of one or more white space characters located outside of a tag is replaced with a single space in the output. White space located adjacent to a non-inline-level element tag (except server tags) may be removed.- Parameters:
collapseWhiteSpace- specifies whether white space in the text between the tags is to be collapsed.- Returns:
- this
SourceFormatterinstance, allowing multiple property setting methods to be chained in a single statement. - See Also:
-
getCollapseWhiteSpace
public boolean getCollapseWhiteSpace()Indicates whether white space in the text between the tags is to be collapsed.See the
setCollapseWhiteSpace(boolean collapseWhiteSpace)method for a full description of this property.- Returns:
trueif white space in the text between the tags is to be collapsed, otherwisefalse.
-
setIndentAllElements
Sets whether all elements are to be indented, including inline-level elements and those with preformatted contents.The default value is
false.If this property is set to
true, every element appears indented on a new line, including inline-level elements.This generates output that is a good representation of the actual document element hierarchy, but is very likely to introduce white space that compromises the functional equivalency of the document.
- Parameters:
indentAllElements- specifies whether all elements are to be indented.- Returns:
- this
SourceFormatterinstance, allowing multiple property setting methods to be chained in a single statement. - See Also:
-
getIndentAllElements
public boolean getIndentAllElements()Indicates whether all elements are to be indented, including inline-level elements and those with preformatted contents.See the
setIndentAllElements(boolean)method for a full description of this property.- Returns:
trueif all elements are to be indented, otherwisefalse.
-
setNewLine
Sets the string to be used to represent a newline in the output.The default is to use the same new line string as is used in the source document, which is determined via the
Source.getNewLine()method. If the source document does not contain any new lines, a "best guess" is made by either taking the new line string of a previously parsed document, or using the value from the staticConfig.NewLineproperty.Specifying a
nullargument resets the property to its default value, which is to use the same new line string as is used in the source document.- Parameters:
newLine- the string to be used to represent a newline in the output, may benull.- Returns:
- this
SourceFormatterinstance, allowing multiple property setting methods to be chained in a single statement. - See Also:
-
getNewLine
Returns the string to be used to represent a newline in the output.See the
setNewLine(String)method for a full description of this property.- Returns:
- the string to be used to represent a newline in the output.
-