Class Attributes

  • All Implemented Interfaces:
    java.lang.Cloneable, java.lang.Iterable<Attribute>

    public class Attributes
    extends java.lang.Object
    implements java.lang.Iterable<Attribute>, java.lang.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 generally 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static java.lang.String dataPrefix  
    • Constructor Summary

      Constructors 
      Constructor Description
      Attributes()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Attributes add​(java.lang.String key, java.lang.String value)
      Adds a new attribute.
      void addAll​(Attributes incoming)
      Add all the attributes from the incoming set to this set.
      java.util.List<Attribute> asList()
      Get the attributes as a List, for iteration.
      Attributes clone()  
      java.util.Map<java.lang.String,​java.lang.String> dataset()
      Retrieves a filtered view of attributes that are HTML5 custom data attributes; that is, attributes with keys starting with data-.
      int deduplicate​(ParseSettings settings)
      Internal method.
      boolean equals​(java.lang.Object o)
      Checks if these attributes are equal to another set of attributes, by comparing the two sets.
      java.lang.String get​(java.lang.String key)
      Get an attribute value by key.
      java.lang.String getIgnoreCase​(java.lang.String key)
      Get an attribute's value by case-insensitive key
      boolean hasDeclaredValueForKey​(java.lang.String key)
      Check if these attributes contain an attribute with a value for this key.
      boolean hasDeclaredValueForKeyIgnoreCase​(java.lang.String key)
      Check if these attributes contain an attribute with a value for this key.
      int hashCode()
      Calculates the hashcode of these attributes, by iterating all attributes and summing their hashcodes.
      boolean hasKey​(java.lang.String key)
      Tests if these attributes contain an attribute with this key.
      boolean hasKeyIgnoreCase​(java.lang.String key)
      Tests if these attributes contain an attribute with this key.
      java.lang.String html()
      Get the HTML representation of these attributes.
      boolean isEmpty()
      Test if this Attributes list is empty (size==0).
      java.util.Iterator<Attribute> iterator()  
      void normalize()
      Internal method.
      Attributes put​(java.lang.String key, boolean value)
      Set a new boolean attribute, remove attribute if value is false.
      Attributes put​(java.lang.String key, java.lang.String value)
      Set a new attribute, or replace an existing one by key.
      Attributes put​(Attribute attribute)
      Set a new attribute, or replace an existing one by key.
      void remove​(java.lang.String key)
      Remove an attribute by key.
      void removeIgnoreCase​(java.lang.String key)
      Remove an attribute by key.
      int size()
      Get the number of attributes in this set, including any jsoup internal-only attributes.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • Attributes

        public Attributes()
    • Method Detail

      • get

        public java.lang.String get​(java.lang.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 (or a boolean attribute).
        See Also:
        hasKey(String)
      • getIgnoreCase

        public java.lang.String getIgnoreCase​(java.lang.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 (ora boolean attribute).
      • add

        public Attributes add​(java.lang.String key,
                              @Nullable
                              java.lang.String value)
        Adds a new attribute. Will produce duplicates if the key already exists.
        See Also:
        put(String, String)
      • put

        public Attributes put​(java.lang.String key,
                              @Nullable
                              java.lang.String value)
        Set a new attribute, or replace an existing one by key.
        Parameters:
        key - case sensitive attribute key (not null)
        value - attribute value (may be null, to set a boolean attribute)
        Returns:
        these attributes, for chaining
      • put

        public Attributes put​(java.lang.String key,
                              boolean value)
        Set a new boolean attribute, remove attribute if value is false.
        Parameters:
        key - case insensitive attribute key
        value - attribute value
        Returns:
        these attributes, for chaining
      • put

        public Attributes put​(Attribute attribute)
        Set a new attribute, or replace an existing one by key.
        Parameters:
        attribute - attribute with case sensitive key
        Returns:
        these attributes, for chaining
      • remove

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

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

        public boolean hasKey​(java.lang.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​(java.lang.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
      • hasDeclaredValueForKey

        public boolean hasDeclaredValueForKey​(java.lang.String key)
        Check if these attributes contain an attribute with a value for this key.
        Parameters:
        key - key to check for
        Returns:
        true if key exists, and it has a value
      • hasDeclaredValueForKeyIgnoreCase

        public boolean hasDeclaredValueForKeyIgnoreCase​(java.lang.String key)
        Check if these attributes contain an attribute with a value for this key.
        Parameters:
        key - case-insensitive key to check for
        Returns:
        true if key exists, and it has a value
      • size

        public int size()
        Get the number of attributes in this set, including any jsoup internal-only attributes. Internal attributes are excluded from the html(), asList(), and iterator() methods.
        Returns:
        size
      • isEmpty

        public boolean isEmpty()
        Test if this Attributes list is empty (size==0).
      • 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.
      • iterator

        public java.util.Iterator<Attribute> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<Attribute>
      • asList

        public java.util.List<Attribute> asList()
        Get the attributes as a List, for iteration.
        Returns:
        an view of the attributes as an unmodifiable List.
      • dataset

        public java.util.Map<java.lang.String,​java.lang.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 java.lang.String html()
        Get the HTML representation of these attributes.
        Returns:
        HTML
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(@Nullable
                              java.lang.Object o)
        Checks if these attributes are equal to another set of attributes, by comparing the two sets. Note that the order of the attributes does not impact this equality (as per the Map interface equals()).
        Overrides:
        equals in class java.lang.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 java.lang.Object
        Returns:
        calculated hashcode
      • clone

        public Attributes clone()
        Overrides:
        clone in class java.lang.Object
      • normalize

        public void normalize()
        Internal method. Lowercases all keys.
      • deduplicate

        public int deduplicate​(ParseSettings settings)
        Internal method. Removes duplicate attribute by name. Settings for case sensitivity of key names.
        Parameters:
        settings - case sensitivity
        Returns:
        number of removed dupes