Enum EmptyElementStyle

java.lang.Object
java.lang.Enum<EmptyElementStyle>
eu.maveniverse.domtrip.EmptyElementStyle
All Implemented Interfaces:
Serializable, Comparable<EmptyElementStyle>, java.lang.constant.Constable

public enum EmptyElementStyle extends Enum<EmptyElementStyle>
Enumeration for XML empty element formatting styles.

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:
  • Enum Constant Details

    • EXPANDED

      public static final EmptyElementStyle 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

      public static final EmptyElementStyle 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

      public static final EmptyElementStyle 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

      public static EmptyElementStyle[] 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

      public static EmptyElementStyle valueOf(String name)
      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 name
      NullPointerException - if the argument is null
    • detectFromDocument

      public static EmptyElementStyle detectFromDocument(Document document)
      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_CLOSING as the default.

      Parameters:
      document - the document to analyze
      Returns:
      the detected empty element style, or SELF_CLOSING if none detected
    • format

      public String format(String elementName, String attributes)
      Formats an empty element according to this style.
      Parameters:
      elementName - the name of the element
      attributes - the attributes string (may be empty)
      Returns:
      the formatted empty element string
    • toString

      public String toString()
      Returns a human-readable description of this style.
      Overrides:
      toString in class Enum<EmptyElementStyle>
      Returns:
      a description of the empty element style