Class AttributeSupport


  • public final class AttributeSupport
    extends Object
    Set of helper methods for working with DOM Attributes.
    • Constructor Detail

      • AttributeSupport

        private AttributeSupport()
        Constructor.
    • Method Detail

      • addXMLBase

        public static void addXMLBase​(@Nonnull
                                      Element element,
                                      @Nonnull
                                      String base)
        Adds a xml:base attribute to the given Element.
        Parameters:
        element - the element to which to add the attribute
        base - the base value
      • addXMLId

        public static void addXMLId​(@Nonnull
                                    Element element,
                                    @Nonnull
                                    String id)
        Adds a xml:id attribute to the given Element.
        Parameters:
        element - the element to which to add the attribute
        id - the Id value
      • addXMLLang

        public static void addXMLLang​(@Nonnull
                                      Element element,
                                      @Nonnull
                                      String lang)
        Adds a xml:lang attribute to the given Element.
        Parameters:
        element - the element to which to add the attribute
        lang - the lang value
      • addXMLSpace

        public static void addXMLSpace​(@Nonnull
                                       Element element,
                                       @Nonnull
                                       XMLSpace space)
        Adds a xml:space attribute to the given Element.
        Parameters:
        element - the element to which to add the attribute
        space - the space value
      • appendAttribute

        public static void appendAttribute​(@Nonnull
                                           Element element,
                                           @Nonnull
                                           QName attributeName,
                                           List<String> attributeValues,
                                           boolean isIDAttribute)
        Adds an attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.
        Parameters:
        attributeName - the attribute name in QName form
        attributeValues - the attribute values
        element - the target element to which to marshall
        isIDAttribute - flag indicating whether the attribute being marshalled should be handled as an ID-typed attribute
      • appendAttribute

        public static void appendAttribute​(@Nonnull
                                           Element element,
                                           @Nonnull
                                           QName attributeName,
                                           @Nonnull
                                           String attributeValue)
        Adds an non-id attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.
        Parameters:
        attributeName - the attribute name in QName form
        attributeValue - the attribute values
        element - the target element to which to marshall
      • appendAttribute

        public static void appendAttribute​(@Nonnull
                                           Element element,
                                           @Nonnull
                                           QName attributeName,
                                           @Nonnull
                                           String attributeValue,
                                           boolean isIDAttribute)
        Adds an attribute name and value to a DOM Element. This is particularly useful for attributes whose names appear in namespace-qualified form.
        Parameters:
        attributeName - the attribute name in QName form
        attributeValue - the attribute value
        element - the target element to which to marshall
        isIDAttribute - flag indicating whether the attribute being marshalled should be handled as an ID-typed attribute
      • appendDateTimeAttribute

        public static void appendDateTimeAttribute​(@Nonnull
                                                   Element element,
                                                   @Nonnull
                                                   QName attributeName,
                                                   @Nonnull
                                                   Instant instant)
        Adds an attribute to the given element. The value of the attribute is the given instant expressed in XML dateTime format. Note that simply using instant.toString() is equivalent for many use cases, but the result will be different on a system with a higher-resolution clock, as the resulting string value may have sub-millisecond precision. This method always works to millisecond precision.
        Parameters:
        element - element to which the attribute will be added, not null
        attributeName - name of the attribute, not null
        instant - instant to set into the attribute, not null
      • appendDurationAttribute

        public static void appendDurationAttribute​(@Nonnull
                                                   Element element,
                                                   @Nonnull
                                                   QName attributeName,
                                                   @Nonnull
                                                   Duration duration)
        Adds an attribute to given element. The value of the attribute is the given duration in XML duration format.
        Parameters:
        element - element to which the attribute will be added, not null
        attributeName - name of the attribute, not null
        duration - duration, must be greater than 0
      • constructAttribute

        @Nonnull
        public static Attr constructAttribute​(@Nonnull
                                              Document owningDocument,
                                              @Nonnull
                                              QName attributeName)
        Constructs an attribute owned by the given document with the given name.
        Parameters:
        owningDocument - the owning document
        attributeName - the name of that attribute
        Returns:
        the constructed attribute
      • constructAttribute

        @Nonnull
        public static Attr constructAttribute​(@Nonnull
                                              Document document,
                                              @Nullable
                                              String namespaceURI,
                                              @Nonnull
                                              String localName,
                                              @Nullable
                                              String prefix)
        Constructs an attribute owned by the given document with the given name.
        Parameters:
        document - the owning document
        namespaceURI - the URI for the namespace the attribute is in
        localName - the local name
        prefix - the prefix of the namespace that attribute is in
        Returns:
        the constructed attribute
      • getAttribute

        @Nullable
        public static Attr getAttribute​(@Nullable
                                        Element element,
                                        @Nullable
                                        QName attributeName)
        Gets the attribute with the given name.
        Parameters:
        element - element that may contain the attribute, may be null
        attributeName - name of the attribute, may be null
        Returns:
        the attribute or null if the given element or attribute was null or the given attribute did not contain an attribute with the given name
      • getAttributeValue

        @Nullable
        public static String getAttributeValue​(@Nullable
                                               Element element,
                                               @Nullable
                                               QName attributeName)
        Gets the value of an attribute from an element.
        Parameters:
        element - the element from which to retrieve the attribute value
        attributeName - the name of the attribute
        Returns:
        the value of the attribute or null if the element does not have such an attribute
      • getAttributeValue

        @Nullable
        public static String getAttributeValue​(@Nullable
                                               Element element,
                                               @Nullable
                                               String namespace,
                                               @Nullable
                                               String attributeLocalName)
        Gets the value of an attribute from an element.
        Parameters:
        element - the element from which to retrieve the attribute value
        namespace - the namespace URI of the attribute
        attributeLocalName - the local (unqualified) attribute name
        Returns:
        the value of the attribute or null if the element does not have such an attribute
      • getAttributeValueAsBoolean

        @Nullable
        public static Boolean getAttributeValueAsBoolean​(@Nullable
                                                         Attr attribute)
        Parses the attribute's value. If the value is 0 or "false" then false is returned, if the value is 1 or "true" then true is returned, if the value is anything else then null returned.
        Parameters:
        attribute - attribute whose value will be converted to a boolean
        Returns:
        boolean value of the attribute or null late enough to allow property replacement.
      • getAttributeValueAsList

        @Nonnull
        public static List<String> getAttributeValueAsList​(@Nullable
                                                           Attr attribute)
        Gets the value of a list-type attribute as a list.
        Parameters:
        attribute - attribute whose value will be turned into a list
        Returns:
        list of values, never null
      • getAttributeValueAsQName

        @Nullable
        public static QName getAttributeValueAsQName​(@Nullable
                                                     Attr attribute)
        Constructs a QName from an attributes value.
        Parameters:
        attribute - the attribute with a QName value
        Returns:
        a QName from an attributes value, or null if the given attribute is null
      • getDateTimeAttribute

        @Nullable
        public static Instant getDateTimeAttribute​(@Nullable
                                                   Attr attribute)
        Gets the value of a dateTime-type attribute as an Instant.
        Parameters:
        attribute - attribute from which to extract the value, may be null
        Returns:
        date/time as an Instant, or null if the attribute was null
      • getDurationAttributeValue

        @Nullable
        public static Duration getDurationAttributeValue​(@Nullable
                                                         Attr attribute)
        Gets the value of a duration-type attribute as a Duration.
        Parameters:
        attribute - attribute from which to extract the value, may be null
        Returns:
        duration, or null if the attribute was null
      • getIdAttribute

        @Nullable
        public static Attr getIdAttribute​(@Nullable
                                          Element element)
        Gets the ID attribute of a DOM element.
        Parameters:
        element - the DOM element
        Returns:
        the ID attribute or null if there isn't one
      • getXMLBase

        @Nullable
        public static String getXMLBase​(@Nullable
                                        Element element)
        Gets the xml:base attribute from a given Element.
        Parameters:
        element - the element from which to extract the attribute
        Returns:
        the value of the xml:base attribute, or null if not present
      • getXMLId

        @Nullable
        public static String getXMLId​(@Nullable
                                      Element element)
        Gets the xml:id attribute from a given Element.
        Parameters:
        element - the element from which to extract the attribute
        Returns:
        the value of the xml:id attribute, or null if not present
      • getXMLLang

        @Nullable
        public static String getXMLLang​(@Nullable
                                        Element element)
        Gets the xml:lang attribute from a given Element.
        Parameters:
        element - the element from which to extract the attribute
        Returns:
        the value of the xml:lang attribute, or null if not present
      • getXMLSpace

        @Nullable
        public static XMLSpace getXMLSpace​(@Nullable
                                           Element element)
        Gets the xml:space attribute from a given Element.
        Parameters:
        element - the element from which to extract the attribute
        Returns:
        the value of the xml:space attribute, or null if not present
      • hasAttribute

        public static boolean hasAttribute​(@Nullable
                                           Element element,
                                           @Nullable
                                           QName name)
        Checks if the given attribute has an attribute with the given name.
        Parameters:
        element - element to check
        name - name of the attribute
        Returns:
        true if the element has an attribute with the given name, false otherwise
      • removeAttribute

        public static boolean removeAttribute​(@Nullable
                                              Element element,
                                              @Nullable
                                              QName attributeName)
        Removes an attribute from an element.
        Parameters:
        element - element from which the attribute should be removed
        attributeName - name of the attribute to be removed
        Returns:
        true if the element contained the attribute and it was removed, false if the element did not contain such an attribute