Enum EmptyElementStyle
- All Implemented Interfaces:
Serializable, Comparable<EmptyElementStyle>, java.lang.constant.Constable
XML empty elements can be written in three different styles according to the XML specification. DomTrip can automatically detect the existing style in a document and preserve it, or you can explicitly configure which style to use for new empty elements.
Style Examples:
- EXPANDED -
<element></element>(separate opening and closing tags) - SELF_CLOSING -
<element/>(self-closing tag without space) - SELF_CLOSING_SPACED -
<element />(self-closing tag with space)
Auto-Detection:
When parsing existing XML documents, DomTrip can automatically detect the predominant empty element style and use it for new empty elements. This ensures consistency with the existing document formatting.
Usage Examples:
// Configure explicit style
DomTripConfig config = DomTripConfig.defaults()
.withEmptyElementStyle(EmptyElementStyle.SELF_CLOSING_SPACED);
// Create element that will use configured style when empty
Element element = Element.of("placeholder");
// When serialized: <placeholder />
// Auto-detect from existing document
Document doc = Document.of("<root><empty/><another/></root>");
EmptyElementStyle detected = EmptyElementStyle.detectFromDocument(doc);
// Returns: SELF_CLOSING
XML Specification Compliance:
All three styles are valid according to the XML specification. The choice between them is typically a matter of style preference, tool conventions, or organizational standards.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class Enum
Enum.EnumDesc<E> -
Enum Constant Summary
Enum ConstantsEnum ConstantDescriptionExpanded form with separate opening and closing tags.Self-closing tag without space before the slash.Self-closing tag with space before the slash. -
Method Summary
Modifier and TypeMethodDescriptionstatic EmptyElementStyledetectFromDocument(Document document) Detects the predominant empty element style used in a document.Formats an empty element according to this style.toString()Returns a human-readable description of this style.static EmptyElementStyleReturns the enum constant of this type with the specified name.static EmptyElementStyle[]values()Returns an array containing the constants of this enum type, in the order they are declared.
-
Enum Constant Details
-
EXPANDED
Expanded form with separate opening and closing tags.Example:
<element></element>This style is more verbose but may be preferred in some contexts for clarity or compatibility with older XML processors.
-
SELF_CLOSING
Self-closing tag without space before the slash.Example:
<element/>This is the most compact form and is commonly used in modern XML.
-
SELF_CLOSING_SPACED
Self-closing tag with space before the slash.Example:
<element />This style is often preferred for better readability and is compatible with XHTML conventions.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException- if this enum type has no constant with the specified nameNullPointerException- if the argument is null
-
detectFromDocument
Detects the predominant empty element style used in a document.This method analyzes all empty elements in the document and returns the most commonly used style. If no empty elements are found, or if there's a tie between styles, it returns
SELF_CLOSINGas the default.- Parameters:
document- the document to analyze- Returns:
- the detected empty element style, or SELF_CLOSING if none detected
-
format
-
toString
Returns a human-readable description of this style.- Overrides:
toStringin classEnum<EmptyElementStyle>- Returns:
- a description of the empty element style
-