Class ComponentHelper


  • public class ComponentHelper
    extends Object
    A convenient helper for development of Granite UI components that are implemented using JSP.
    • Method Detail

      • getI18n

        @Nonnull
        public I18n getI18n()
        Returns I18n appropriate for the current page.
        Returns:
        I18n appropriate for the current page
      • getXss

        @Nonnull
        public XSSAPI getXss()
        Returns XSSAPI based on the current request.
        Returns:
        XSSAPI based on the current request
      • getConfig

        @Nonnull
        public Config getConfig()
        Returns the config of current resource of the page.
        Returns:
        the config of current resource of the page
      • getValue

        @Nonnull
        public Value getValue()
        Returns the values that is applicable for the current page.
        Returns:
        the values that is applicable for the current page
      • getExpressionHelper

        @Nonnull
        public ExpressionHelper getExpressionHelper()
        Returns the ExpressionHelper appropriate for the current page.
        Returns:
        the ExpressionHelper appropriate for the current page
      • getState

        @Nonnull
        public State getState()
        Returns the client state.
        Returns:
        the client state
      • consumeTag

        @Nonnull
        public Tag consumeTag()
        Consumes the current available tag for current page. If the request doesn't have the tag applicable to the page, a new tag is created.

        There is a mechanism such that a tag can be passed to another page when including that page. This method is intended as a way to consume the tag passed by other page. Component developer can make use this method to get the main tag of the component regardless if there is a tag passed by other page or not.

        Returns:
        the tag
        See Also:
        include(Resource, Tag), include(Resource, String, Tag)
      • populateCommonAttrs

        public void populateCommonAttrs​(@Nonnull
                                        AttrBuilder attrs,
                                        @Nonnull
                                        Resource src)
        Populates the common attributes to the given AttrBuilder.

        Currently the following common properties and nodes are read and processed from the given resource:

        Name Type Description
        granite:id Property: StringEL The id attribute.
        granite:rel Property: StringEL The class attribute. This is used to indicate the semantic relationship of the component similar to rel attribute.
        granite:class Property: StringEL The class attribute.
        granite:title Property: String The title attribute. This property is i18nable.
        granite:hidden Property: Boolean The hidden attribute.
        granite:itemscope Property: Boolean The itemscope attribute.
        granite:itemtype Property: String The itemtype attribute.
        granite:itemprop Property: String The itemprop attribute.
        granite:data Node Each property of this node is converted into a data-* attribute. If the property value is an instance of a String, it will be interpreted as StringEL. The property having a prefixed name is ignored.
        Parameters:
        attrs - The attribute builder to populate to
        src - The resource of the source of the config
      • getLayout

        @Nonnull
        public LayoutBuilder getLayout()
        Returns the layout config of current resource of the page. This method is setting the default resource type of the layout.
        Returns:
        the layout config of current resource of the page
        See Also:
        LayoutBuilder.getResourceType()
      • getReadOnlyResourceType

        @CheckForNull
        public String getReadOnlyResourceType()
        Returns the associated resource type of current resource for the purpose rendering read only version. First the granite:readOnlyResourceType property of the resource type of the current resource (the RT) is used. Otherwise it is defaulted to readonly child resource of the RT.
        Returns:
        the associated resource type of current resource
      • getReadOnlyResourceType

        @CheckForNull
        public String getReadOnlyResourceType​(@Nonnull
                                              Resource resource)
        Returns the associated resource type of the given content resource for the purpose rendering read only version. First the granite:readOnlyResourceType property of the resource type of the content resource (the RT) is used. Otherwise it is defaulted to readonly child resource of the RT.
        Parameters:
        resource - the resource
        Returns:
        the associated resource type of the given content resource
      • getRenderCondition

        @Nonnull
        public RenderCondition getRenderCondition​(@Nonnull
                                                  Resource resource,
                                                  boolean cache)
                                           throws ServletException,
                                                  IOException
        Returns the render condition of the given resource. The render condition is specified by granite:rendercondition subresource, unlike getRenderCondition(Resource).
        Parameters:
        resource - The resource
        cache - true to cache the result; Use it when checking render condition of other resource (typically the item resource) so that the render condition is only resolved once.
        Returns:
        The render condition of the given resource; never null.
        Throws:
        ServletException - in case there's a servlet error
        IOException - in case there's an i/o error
      • getIconClass

        public String getIconClass​(@CheckForNull
                                   String icon)
        Returns the icon class(es) for the given icon string from the content property.
        Parameters:
        icon - the icon string
        Returns:
        the icon class(es) for the given icon string from the content property, or null if the given icon is null
      • include

        public void include​(@Nonnull
                            Resource resource,
                            @Nonnull
                            Tag tag)
                     throws ServletException,
                            IOException
        Includes the given resource and passes the given tag to its renderer. This method performs similarly to <sling:include resource="" />.
        Parameters:
        resource - the resource to include
        tag - the tag
        Throws:
        ServletException - in case there's a servlet error
        IOException - in case there's an i/o error
      • include

        public void include​(@Nonnull
                            Resource resource,
                            @CheckForNull
                            String resourceType,
                            @Nonnull
                            Tag tag)
                     throws ServletException,
                            IOException
        Includes the given resource with the given resourceType and passes the given tag to its renderer. This method performs similarly to <sling:include resource="" resourceType="" />.
        Parameters:
        resource - the resource to include
        resourceType - the resource type
        tag - the tag
        Throws:
        ServletException - in case there's a servlet error
        IOException - in case there's an i/o error