Enum EmptyElementStyle

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<EmptyElementStyle>

    public enum EmptyElementStyle
    extends java.lang.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:
    DomTripConfig.withEmptyElementStyle(EmptyElementStyle), Element, Serializer
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      EXPANDED
      Expanded form with separate opening and closing tags.
      SELF_CLOSING
      Self-closing tag without space before the slash.
      SELF_CLOSING_SPACED
      Self-closing tag with space before the slash.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static EmptyElementStyle detectFromDocument​(Document document)
      Detects the predominant empty element style used in a document.
      java.lang.String format​(java.lang.String elementName, java.lang.String attributes)
      Formats an empty element according to this style.
      java.lang.String toString()
      Returns a human-readable description of this style.
      static EmptyElementStyle valueOf​(java.lang.String name)
      Returns 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.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • 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 Detail

      • values

        public static EmptyElementStyle[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (EmptyElementStyle c : EmptyElementStyle.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static EmptyElementStyle valueOf​(java.lang.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:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.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 java.lang.String format​(java.lang.String elementName,
                                       java.lang.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 java.lang.String toString()
        Returns a human-readable description of this style.
        Overrides:
        toString in class java.lang.Enum<EmptyElementStyle>
        Returns:
        a description of the empty element style