Class ComponentHelper


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

      • ComponentHelper

        public ComponentHelper​(@Nonnull
                               PageContext pageContext)
    • 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)
      • consumeLayoutResource

        @CheckForNull
        public Resource consumeLayoutResource()
        Consumes the current available layout resource for current page. This method first attempts to return the layout resource from the options, second it will attempt to return Config.LAYOUT child node, otherwise null is returned.

        This method is usually called by layout implementation to get the layout resource that can be passed by the caller.

        Returns:
        the resource
        See Also:
        includeForLayout(Resource, Options), includeForLayout(Resource, Resource, Options)
      • 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
      • getOptions

        @Nonnull
        public ComponentHelper.Options getOptions()
        Returns the options passed from another page. If no options is passed, empty options is returned.

        There is a mechanism such that options can be passed to another page when including that page.

        Returns:
        the options passed from another page. if no options is passed, empty options is returned.
        See Also:
        include(Resource, Options), include(Resource, String, Options)
      • 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 java.lang.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 java.lang.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
      • getItemDataSource

        @Nonnull
        public DataSource getItemDataSource()
                                     throws ServletException,
                                            java.io.IOException
        Returns the datasource for items of the current resource. This is an overload of getItemDataSource(Resource) with resource is the current request resource.
        Returns:
        the data source for items of the current resource
        Throws:
        ServletException - in case there's a servlet error while fetching data
        java.io.IOException - in case there's an i/o error while fetching data
      • getItemDataSource

        @Nonnull
        public DataSource getItemDataSource​(@Nonnull
                                            Resource resource)
                                     throws ServletException,
                                            java.io.IOException
        Returns the datasource for items of the given resource. This method can be used to fetch the items that are specified literally using Config.ITEMS subresource; or specified as datasource using Config.DATASOURCE subresource. If there is no Config.ITEMS or Config.DATASOURCE subresource, then EmptyDataSource is returned. In contrast with asDataSource(Resource, Resource), this method looks for the datasource resource of the given resource. i.e. The given resource is the parent of the items, not the datasource resource itself. The given resource is also used as the context resource when calling asDataSource(Resource, Resource) internally.
        Parameters:
        resource - the resource
        Returns:
        the data source for items of the given resource
        Throws:
        ServletException - in case there's a servlet error while fetching data
        java.io.IOException - in case there's an i/o error while fetching data
      • asDataSource

        public DataSource asDataSource​(@CheckForNull
                                       Resource datasource)
                                throws ServletException,
                                       java.io.IOException
        Returns the datasource given its datasource resource. This method is an overload of asDataSource(Resource, Resource) with context is null.
        Parameters:
        datasource - the resource representing the datasource
        Returns:
        the datasource given its datasource resource
        Throws:
        ServletException - in case there's a servlet error while fetching data
        java.io.IOException - in case there's an i/o error while fetching data
      • asDataSource

        public DataSource asDataSource​(@CheckForNull
                                       Resource datasource,
                                       @CheckForNull
                                       Resource context)
                                throws ServletException,
                                       java.io.IOException
        Returns the datasource given its datasource resource.
        Parameters:
        datasource - The resource representing the datasource
        context - The context resource that is returned when calling SlingHttpServletRequest.getResource() at the datasource implementation. If this is null, the given datasource is used.
        Returns:
        null if the given datasource is null.
        Throws:
        ServletException - in case there's a servlet error while fetching data
        java.io.IOException - in case there's an i/o error while fetching data
      • getRenderCondition

        @Nonnull
        public RenderCondition getRenderCondition​(@Nonnull
                                                  Resource resource)
                                           throws ServletException,
                                                  java.io.IOException
        Returns the render condition of the given resource. The render condition is specified by granite:rendercondition or rendercondition subresource. Contrast this with getRenderCondition(Resource, boolean), where only granite:rendercondition is checked. This method is meant for backward compatibility; otherwise it is better to use getRenderCondition(Resource, boolean) for performance. Once the transition is over, this method will have the same behaviour as getRenderCondition(Resource, boolean) with cache = false.
        Parameters:
        resource - the resource
        Returns:
        the render condition of the given resource
        Throws:
        ServletException - in case there's a servlet error
        java.io.IOException - in case there's an i/o error
      • getRenderCondition

        @Nonnull
        public RenderCondition getRenderCondition​(@Nonnull
                                                  Resource resource,
                                                  boolean cache)
                                           throws ServletException,
                                                  java.io.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
        java.io.IOException - in case there's an i/o error
      • getIconClass

        public java.lang.String getIconClass​(@CheckForNull
                                             java.lang.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,
                            java.io.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
        java.io.IOException - in case there's an i/o error
      • include

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

        public void include​(@Nonnull
                            Resource resource,
                            @CheckForNull
                            java.lang.String resourceType,
                            @Nonnull
                            Tag tag)
                     throws ServletException,
                            java.io.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
        java.io.IOException - in case there's an i/o error
      • include

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

        public void include​(@Nonnull
                            Resource resource,
                            @CheckForNull
                            java.lang.String resourceType,
                            @CheckForNull
                            java.lang.String selectors,
                            @Nonnull
                            ComponentHelper.Options options)
                     throws ServletException,
                            java.io.IOException
        Includes the given resource with the given resourceType and passes the given options to its renderer. This method performs similarly to <sling:include resource="" replaceSelectors="" resourceType="" />.
        Parameters:
        resource - the resource to include
        resourceType - the resource type
        selectors - the selectors to be included as part of the request.
        options - the options
        Throws:
        ServletException - in case there's a servlet error
        java.io.IOException - in case there's an i/o error
      • includeForLayout

        public void includeForLayout​(@Nonnull
                                     Resource resource,
                                     @CheckForNull
                                     Resource layoutResource,
                                     @Nonnull
                                     ComponentHelper.Options options)
                              throws ServletException,
                                     java.io.IOException
        Includes the given resource to be rendered by the given layoutResource. This method is used by a component to delegate the rendering process to a layout.

        If layoutResource is not null, the ComponentHelper.Options.layoutResource(Resource) is set.

        This method will attempt to derive the resourceType to be passed to include(Resource, String, Options) based the following priorities:

        1. layoutResource is not null, the resourceType is layoutResource's RT
        2. layoutResource is null, the resourceType is Config.LAYOUT child node's RT
        3. the resourceType is default layout as a catch-all fallback
        Parameters:
        resource - the resource to include
        layoutResource - the layout resource to render the given resource with
        options - the options
        Throws:
        ServletException - in case there's a servlet error
        java.io.IOException - in case there's an i/o error
      • call

        public void call​(@Nonnull
                         java.lang.String script,
                         @Nonnull
                         ComponentHelper.Options options)
                  throws ServletException,
                         java.io.IOException
        Calls the given script and passes the given options to its renderer. This method performs similarly to <sling:call script="" />.
        Parameters:
        script - the script to be called
        options - the options
        Throws:
        ServletException - in case there's a servlet error
        java.io.IOException - in case there's an i/o error