Class Comment
XML comments are used to include human-readable notes and documentation within XML documents. DomTrip preserves comments exactly as they appear in the source XML, including any internal formatting, whitespace, and content.
Comment Handling:
- Content Preservation - Maintains exact comment text
- Position Preservation - Keeps comments in their original locations
- Whitespace Preservation - Preserves surrounding whitespace
XML Comment Syntax:
XML comments follow the syntax: <!-- comment content -->
Comments cannot contain the string "--" and cannot end with "-".
Usage Examples:
// Create a comment
Comment comment = new Comment("This is a comment");
// Create using factory method
Comment factoryComment = Comment.of("This is a factory comment");
// Create and modify with fluent API
Comment fluentComment = Comment.of("Initial content")
.content("Updated content");
// Add to document
document.addChild(comment);
// Check comment properties
if (comment.isEmpty()) {
// Handle empty comment
}
if (comment.isWhitespaceOnly()) {
// Handle whitespace-only comment
}
Best Practices:
- Avoid using "--" within comment content
- Use comments for documentation and metadata
- Consider comment placement for readability
- 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.clone()Deprecated.content()Gets the content of this comment.Sets the content of this comment.copy()Creates a deep copy of this node.booleanisEmpty()Checks if this comment is completely empty.booleanChecks if this comment contains only whitespace characters.static CommentCreates a comment with the specified content.parent(ContainerNode parent) Sets the parent container node of this node.precedingWhitespace(String whitespace) Sets the whitespace that precedes this node.toString()Returns a string representation of this comment for debugging purposes.voidtoXml(StringBuilder sb) Serializes this comment to XML, appending to the provided StringBuilder.type()Returns the node type for this comment.Methods inherited from class Node
clearModified, depth, document, isDescendantOf, isModified, markModified, nextSibling, nextSiblingElement, parent, parentElement, precedingWhitespace, previousSibling, previousSiblingElement, siblingIndex, toXml
-
Constructor Details
-
Comment
Creates a new XML comment with the specified content.- Parameters:
content- the comment content (without the delimiters)
-
-
Method Details
-
type
Returns the node type for this comment.- Specified by:
typein classNode- Returns:
Node.NodeType.COMMENT
-
content
Gets the content of this comment.Returns the text content without the surrounding comment delimiters.
- Returns:
- the comment content
- See Also:
-
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 comment for method chaining
- See Also:
-
content
-
toXml
Serializes this comment to XML, appending to the provided StringBuilder.Appends the complete comment including preceding whitespace, comment delimiters, content, and following whitespace.
-
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()Checks if this comment contains only whitespace characters.Returns true if the comment content consists entirely of whitespace characters (spaces, tabs, newlines, etc.).
- Returns:
- true if the comment contains only whitespace, false otherwise
- See Also:
-
isEmpty
public boolean isEmpty()Checks if this comment is completely empty.Returns true if the comment has no content at all.
- Returns:
- true if the comment is empty, false otherwise
- See Also:
-
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 comment. -
toString
Returns a string representation of this comment for debugging purposes.The string includes the comment content, truncated if longer than 50 characters, with newlines escaped for readability.
-
of
-
copy()instead.