Class Text
The Text class handles all forms of textual content in XML documents, including regular text nodes, CDATA sections, and whitespace-only content. It maintains both the decoded text content and the original raw content with entities preserved, enabling lossless round-trip processing.
Text Handling:
- Entity Preservation - Maintains original entity encoding
- CDATA Support - Handles CDATA sections
- Whitespace Preservation - Preserves significant whitespace
- Content Normalization - Optional whitespace normalization
Content Types:
- Regular Text - Standard text content with entity escaping
- CDATA Sections - Unescaped text within CDATA blocks
- Whitespace - Formatting whitespace between elements
Usage Examples:
// Create regular text content
Text text = new Text("Hello & welcome!");
// Create using factory methods
Text factoryText = Text.of("Hello World");
Text cdataText = Text.cdata("<script>alert('test');</script>");
Text explicitCdata = Text.of("content", true);
// Create and modify with fluent API
Text fluentText = Text.of("Regular text").asCData();
Text preservedText = Text.of(" spaces ").preserveWhitespace(false);
// Check content properties
if (text.isWhitespaceOnly()) {
// Handle formatting whitespace
}
// Access both decoded and raw content
String content = text.content(); // "Hello & welcome!"
String raw = text.getRawContent(); // "Hello & welcome!" (if preserved)
Entity Handling:
Text nodes automatically handle XML entity encoding and decoding:
&↔&<↔<>↔>"↔"'↔'
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Node
Node.NodeType -
Field Summary
Fields inherited from class Node
modified, parent, precedingWhitespace -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccept(DomTripVisitor visitor) Accepts a visitor for depth-first tree traversal.asCData()Converts this text node to a CDATA section.booleancdata()cdata(boolean cData) static TextCreates a CDATA text node with the specified content.clone()Deprecated.content()contentPreservingWhitespace(String newContent) Sets new content while preserving the existing whitespace pattern.copy()Creates a deep copy of this node.booleanChecks if the content has leading whitespace.booleanChecks if the content has trailing whitespace.booleanisEmpty()Returns true if this text node is emptybooleanReturns true if this text node contains only whitespaceGets the leading whitespace from the text content.voidNormalizes whitespace in the content (collapses multiple spaces to single space)static TextCreates a text node with the specified content.static TextCreates a text node with the specified content and CDATA flag.parent(ContainerNode parent) Sets the parent container node of this node.precedingWhitespace(String whitespace) Sets the whitespace that precedes this node.booleanpreserveWhitespace(boolean preserveWhitespace) rawContent(String rawContent) toString()voidtoXml(StringBuilder sb) Appends this text node's XML representation to the provided StringBuilder.Gets the trailing whitespace from the text content.voidtrim()Trims whitespace from the content while preserving internal structureGets the text content with leading and trailing whitespace removed.type()Returns the type of this XML node.static StringunescapeTextContent(String text) Unescapes XML entities in text content, including numeric character references.Methods inherited from class Node
clearModified, depth, document, isDescendantOf, isModified, markModified, nextSibling, nextSiblingElement, parent, parentElement, precedingWhitespace, previousSibling, previousSiblingElement, siblingIndex, toXml
-
Constructor Details
-
Text
-
Text
-
Text
-
-
Method Details
-
type
Description copied from class:NodeReturns the type of this XML node.The node type determines the node's behavior and capabilities. This method must be implemented by all concrete node classes.
- Specified by:
typein classNode- Returns:
- the
Node.NodeTypeof this node
-
content
-
parent
Sets the parent container node of this node.This method is typically called automatically when adding nodes to containers. Manual use should be done carefully to maintain tree consistency.
-
precedingWhitespace
Sets the whitespace that precedes this node.This method allows control over the whitespace formatting before this node when serializing to XML.
- Overrides:
precedingWhitespacein classNode- Parameters:
whitespace- the whitespace string to set, null is treated as empty string- Returns:
- this text node for method chaining
- See Also:
-
content
-
rawContent
-
rawContent
-
cdata
public boolean cdata() -
cdata
-
preserveWhitespace
public boolean preserveWhitespace() -
preserveWhitespace
-
accept
Accepts a visitor for depth-first tree traversal.This method implements the visitor pattern, allowing structured traversal of the XML tree with enter/exit lifecycle callbacks. Each node type dispatches to the appropriate visitor method.
-
isWhitespaceOnly
public boolean isWhitespaceOnly()Returns true if this text node contains only whitespace -
isEmpty
public boolean isEmpty()Returns true if this text node is empty -
trimmedContent
Gets the text content with leading and trailing whitespace removed.This is a convenience method that returns the trimmed content without modifying the original content. The original content with whitespace is preserved for lossless round-trip processing.
- Returns:
- the content with leading and trailing whitespace removed
- See Also:
-
leadingWhitespace
Gets the leading whitespace from the text content.Extracts and returns any whitespace characters (spaces, tabs, newlines) that appear at the beginning of the content. This is useful for understanding the whitespace structure without modifying the original content.
- Returns:
- the leading whitespace, or empty string if none
- See Also:
-
trailingWhitespace
Gets the trailing whitespace from the text content.Extracts and returns any whitespace characters (spaces, tabs, newlines) that appear at the end of the content. This is useful for understanding the whitespace structure without modifying the original content.
- Returns:
- the trailing whitespace, or empty string if none
- See Also:
-
contentPreservingWhitespace
Sets new content while preserving the existing whitespace pattern.This method replaces the actual content but maintains the same leading and trailing whitespace as the original content. This is useful when you want to update the meaningful content without affecting the formatting.
Example:
// Original: " Hello World " text.contentPreservingWhitespace("Goodbye"); // Result: " Goodbye "- Parameters:
newContent- the new content to set (will be placed between existing whitespace)- See Also:
-
hasLeadingWhitespace
public boolean hasLeadingWhitespace()Checks if the content has leading whitespace.- Returns:
- true if the content starts with whitespace characters
- See Also:
-
hasTrailingWhitespace
public boolean hasTrailingWhitespace()Checks if the content has trailing whitespace.- Returns:
- true if the content ends with whitespace characters
- See Also:
-
toXml
Appends this text node's XML representation to the provided StringBuilder. The method writes the node's preceding whitespace, then either: - serializes the content as a CDATA section when this node is marked as CDATA, or - appends the raw, unescaped content if available and the node has not been modified, otherwise appends the escaped text content. -
unescapeTextContent
Unescapes XML entities in text content, including numeric character references.- Parameters:
text- the text possibly containing XML entities or numeric references- Returns:
- the text with entities and numeric references decoded; empty string when
textisnull
-
trim
public void trim()Trims whitespace from the content while preserving internal structure -
normalizeWhitespace
public void normalizeWhitespace()Normalizes whitespace in the content (collapses multiple spaces to single space) -
copy
Creates a deep copy of this node.The copied node will have:
- All properties copied from the original
- All child nodes recursively copied (for container nodes)
- Whitespace and formatting properties preserved
- No parent (parent is set to null)
The copied node and its descendants will have their parent-child relationships properly established within the copied subtree.
-
clone
Deprecated.Usecopy()instead.Creates a deep copy of this text node. -
toString
-
of
-
cdata
-
of
Creates a text node with the specified content and CDATA flag.Factory method for creating text nodes with explicit CDATA control.
- Parameters:
content- the text contentisCData- true to create a CDATA section, false for regular text- Returns:
- a new Text node
-
asCData
Converts this text node to a CDATA section.Fluent setter for converting existing text to CDATA format.
- Returns:
- this text node for method chaining
-
copy()instead.