Class Attributes

  • All Implemented Interfaces:
    Cloneable, Iterable<Attribute>

    public class Attributes
    extends Object
    implements Iterable<Attribute>, Cloneable
    The attributes of an Element.

    Attributes are treated as a map: there can be only one value associated with an attribute key/name.

    Attribute name and value comparisons are case sensitive. By default for HTML, attribute names are normalized to lower-case on parsing. That means you should use lower-case strings when referring to attributes by name.

    Author:
    Jonathan Hedley, jonathan@hedley.net
    • Constructor Detail

      • Attributes

        public Attributes()
    • Method Detail

      • get

        public String get​(String key)
        Get an attribute value by key.
        Parameters:
        key - the (case-sensitive) attribute key
        Returns:
        the attribute value if set; or empty string if not set.
        See Also:
        hasKey(String)
      • getIgnoreCase

        public String getIgnoreCase​(String key)
        Get an attribute's value by case-insensitive key
        Parameters:
        key - the attribute name
        Returns:
        the first matching attribute value if set; or empty string if not set.
      • put

        public void put​(String key,
                        String value)
        Set a new attribute, or replace an existing one by key.
        Parameters:
        key - attribute key
        value - attribute value
      • put

        public void put​(String key,
                        boolean value)
        Set a new boolean attribute, remove attribute if value is false.
        Parameters:
        key - attribute key
        value - attribute value
      • put

        public void put​(Attribute attribute)
        Set a new attribute, or replace an existing one by key.
        Parameters:
        attribute - attribute
      • remove

        public void remove​(String key)
        Remove an attribute by key. Case sensitive.
        Parameters:
        key - attribute key to remove
      • removeIgnoreCase

        public void removeIgnoreCase​(String key)
        Remove an attribute by key. Case insensitive.
        Parameters:
        key - attribute key to remove
      • hasKey

        public boolean hasKey​(String key)
        Tests if these attributes contain an attribute with this key.
        Parameters:
        key - case-sensitive key to check for
        Returns:
        true if key exists, false otherwise
      • hasKeyIgnoreCase

        public boolean hasKeyIgnoreCase​(String key)
        Tests if these attributes contain an attribute with this key.
        Parameters:
        key - key to check for
        Returns:
        true if key exists, false otherwise
      • size

        public int size()
        Get the number of attributes in this set.
        Returns:
        size
      • addAll

        public void addAll​(Attributes incoming)
        Add all the attributes from the incoming set to this set.
        Parameters:
        incoming - attributes to add to these attributes.
      • asList

        public List<Attribute> asList()
        Get the attributes as a List, for iteration. Do not modify the keys of the attributes via this view, as changes to keys will not be recognised in the containing set.
        Returns:
        an view of the attributes as a List.
      • dataset

        public Map<String,​String> dataset()
        Retrieves a filtered view of attributes that are HTML5 custom data attributes; that is, attributes with keys starting with data-.
        Returns:
        map of custom data attributes.
      • html

        public String html()
        Get the HTML representation of these attributes.
        Returns:
        HTML
        Throws:
        SerializationException - if the HTML representation of the attributes cannot be constructed.
      • equals

        public boolean equals​(Object o)
        Checks if these attributes are equal to another set of attributes, by comparing the two sets
        Overrides:
        equals in class Object
        Parameters:
        o - attributes to compare with
        Returns:
        if both sets of attributes have the same content
      • hashCode

        public int hashCode()
        Calculates the hashcode of these attributes, by iterating all attributes and summing their hashcodes.
        Overrides:
        hashCode in class Object
        Returns:
        calculated hashcode