Class AttrBuilder


  • public class AttrBuilder
    extends java.lang.Object
    A builder to generate HTML attributes. This builder is designed to be secured using XSSAPI. It will encode the value automatically. If the value is null, it will be ignored.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.String name, java.lang.Boolean value)
      Adds attribute with the given name.
      void add​(java.lang.String name, java.lang.Double value)
      Adds attribute with the given name.
      void add​(java.lang.String name, java.lang.Integer value)
      Adds attribute with the given name.
      void add​(java.lang.String name, java.lang.String value)
      Adds attribute with the given name.
      void addBoolean​(java.lang.String name, boolean value)
      Adds boolean attribute (behaves like disabled) for the given name.
      void addChecked​(boolean checked)
      Adds checked attribute.
      void addClass​(java.lang.String value)
      Adds class attribute with the given value.
      void addDisabled​(boolean disabled)
      Adds disabled attribute.
      void addHref​(java.lang.String name, java.lang.String value)
      Adds an attribute that behave like href attribute.
      void addMultiple​(boolean multiple)
      Adds multiple attribute.
      void addOther​(java.lang.String name, java.lang.String value)
      Adds the given name as data-* attribute.
      void addOthers​(java.util.Map<java.lang.String,​java.lang.Object> data, java.lang.String... exclusions)
      Adds the given data as data-* attributes.
      void addRel​(java.lang.String value)
      Adds relationship.
      void addSelected​(boolean selected)
      Adds selected attribute.
      java.lang.String build()
      Builds the attributes in the form of <attr-name>='<attr-value>'*.
      void build​(java.io.Writer out)
      Builds the attributes in the form of <attr-name>='<attr-value>'**.
      java.util.Map<java.lang.String,​java.lang.String> getData()
      Gets the raw Map of attributes, with un-encoded values
      boolean isEmpty()
      Returns true if there is no attribute in this builder, false otherwise.
      void set​(java.lang.String name, java.lang.String value)
      Sets attribute with the given name.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • getData

        @Nonnull
        public java.util.Map<java.lang.String,​java.lang.String> getData()
        Gets the raw Map of attributes, with un-encoded values
        Returns:
        Map of attributes
      • addRel

        public void addRel​(@CheckForNull
                           java.lang.String value)
        Adds relationship. Currently it is implemented as class attribute.
        Parameters:
        value - the relationship to add
      • addClass

        public void addClass​(@CheckForNull
                             java.lang.String value)
        Adds class attribute with the given value.
        Parameters:
        value - the class attribute to add
      • addHref

        public void addHref​(@CheckForNull
                            java.lang.String name,
                            @CheckForNull
                            java.lang.String value)
        Adds an attribute that behave like href attribute. i.e. the value will be prepended with context path (if absolute path) and checked using XSSAPI.getValidHref(String).
        Parameters:
        name - the name of the attribute to add
        value - the value of the specified attribute
      • addDisabled

        public void addDisabled​(boolean disabled)
        Adds disabled attribute.
        Parameters:
        disabled - the boolean value of the disabled attribute
      • addChecked

        public void addChecked​(boolean checked)
        Adds checked attribute.
        Parameters:
        checked - the boolean value of the checked attribute
      • addSelected

        public void addSelected​(boolean selected)
        Adds selected attribute.
        Parameters:
        selected - the boolean value of the selected attribute
      • addMultiple

        public void addMultiple​(boolean multiple)
        Adds multiple attribute.
        Parameters:
        multiple - the boolean value of the multiple attribute
      • addBoolean

        public void addBoolean​(@CheckForNull
                               java.lang.String name,
                               boolean value)
        Adds boolean attribute (behaves like disabled) for the given name. When the given value is true, it will be printed as "disabled=''", instead of "disabled='true'". When the given value is false, it will NOT be printed, instead of "disabled='false'".
        Parameters:
        name - the name of the boolean attribute to add
        value - the boolean value of the attribute
      • addOther

        public void addOther​(@CheckForNull
                             java.lang.String name,
                             @CheckForNull
                             java.lang.String value)
        Adds the given name as data-* attribute.
        Parameters:
        name - the name of the data-* attribute to add
        value - the value of the attribute
      • addOthers

        public void addOthers​(@Nonnull
                              java.util.Map<java.lang.String,​java.lang.Object> data,
                              @Nonnull
                              java.lang.String... exclusions)
        Adds the given data as data-* attributes. Entries with keys specified in exclusions parameter or having namespace (e.g. "jcr:primaryType") will be excluded.
        Parameters:
        data - the map containing key/value pairs to add as data-* attributes
        exclusions - the keys which must not be added as data-* attributes
      • add

        public void add​(@CheckForNull
                        java.lang.String name,
                        @CheckForNull
                        java.lang.Boolean value)
        Adds attribute with the given name. The value will be added to existing attribute using space-delimited convention. e.g. class="class1 class2"
        Parameters:
        name - the name of the attribute to add
        value - the boolean value of the attribute
      • add

        public void add​(@CheckForNull
                        java.lang.String name,
                        @CheckForNull
                        java.lang.Integer value)
        Adds attribute with the given name. The value will be added to existing attribute using space-delimited convention. e.g. class="class1 class2"
        Parameters:
        name - the name of the attribute to add
        value - the integer value of the attribute
      • add

        public void add​(@CheckForNull
                        java.lang.String name,
                        @CheckForNull
                        java.lang.Double value)
        Adds attribute with the given name. The value will be added to existing attribute using space-delimited convention. e.g. class="class1 class2"
        Parameters:
        name - the name of the attribute to add
        value - the double value of the attribute
      • add

        public void add​(@CheckForNull
                        java.lang.String name,
                        @CheckForNull
                        java.lang.String value)
        Adds attribute with the given name. The value will be added to existing attribute using space-delimited convention. e.g. class="class1 class2"
        Parameters:
        name - the name of the attribute to add
        value - the string value of the attribute
      • set

        public void set​(@CheckForNull
                        java.lang.String name,
                        @CheckForNull
                        java.lang.String value)
        Sets attribute with the given name. Existing value previously set will be replaced by the given value.
        Parameters:
        name - the name of the attribute to set or replace (if exists)
        value - the string value of the attribute
      • isEmpty

        public boolean isEmpty()
        Returns true if there is no attribute in this builder, false otherwise.
        Returns:
        true if there is no attribute in this builder, false otherwise
      • build

        public java.lang.String build()
        Builds the attributes in the form of <attr-name>='<attr-value>'*.
        Returns:
        the string containing the built attributes
      • build

        public void build​(@Nonnull
                          java.io.Writer out)
                   throws java.io.IOException
        Builds the attributes in the form of <attr-name>='<attr-value>'**.
        Parameters:
        out - the writer
        Throws:
        java.io.IOException - in case there's an error when appending to the writer
      • toString

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