Class ComponentHelper
- java.lang.Object
-
- com.adobe.granite.ui.components.ComponentHelper
-
public class ComponentHelper extends java.lang.Object
A convenient helper for development of Granite UI components that are implemented using JSP.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ComponentHelper.Options
An options to be passed to the included resource's renderer.
-
Constructor Summary
Constructors Constructor Description ComponentHelper(PageContext pageContext)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DataSource
asDataSource(Resource datasource)
Returns the datasource given its datasource resource.DataSource
asDataSource(Resource datasource, Resource context)
Returns the datasource given its datasource resource.void
call(java.lang.String script, ComponentHelper.Options options)
Calls the given script and passes the given options to its renderer.Resource
consumeLayoutResource()
Consumes the current available layout resource for current page.Tag
consumeTag()
Consumes the current available tag for current page.Config
getConfig()
Returns the config of current resource of the page.ExpressionHelper
getExpressionHelper()
Returns the ExpressionHelper appropriate for the current page.I18n
getI18n()
Returns I18n appropriate for the current page.java.lang.String
getIconClass(java.lang.String icon)
Returns the icon class(es) for the given icon string from the content property.DataSource
getItemDataSource()
Returns the datasource for items of the current resource.DataSource
getItemDataSource(Resource resource)
Returns the datasource for items of the given resource.LayoutBuilder
getLayout()
Returns the layout config of current resource of the page.ComponentHelper.Options
getOptions()
Returns the options passed from another page.java.lang.String
getReadOnlyResourceType()
Returns the associated resource type of current resource for the purpose rendering read only version.java.lang.String
getReadOnlyResourceType(Resource resource)
Returns the associated resource type of the given content resource for the purpose rendering read only version.RenderCondition
getRenderCondition()
Returns the render condition of the current resource.RenderCondition
getRenderCondition(Resource resource)
Returns the render condition of the given resource.RenderCondition
getRenderCondition(Resource resource, boolean cache)
Returns the render condition of the given resource.State
getState()
Returns the client state.Value
getValue()
Returns the values that is applicable for the current page.XSSAPI
getXss()
Returns XSSAPI based on the current request.void
include(Resource resource, ComponentHelper.Options options)
Includes the given resource and passes the given options to its renderer.void
include(Resource resource, Tag tag)
Includes the given resource and passes the given tag to its renderer.void
include(Resource resource, java.lang.String resourceType, ComponentHelper.Options options)
Includes the given resource with the given resourceType and passes the given options to its renderer.void
include(Resource resource, java.lang.String resourceType, Tag tag)
Includes the given resource with the given resourceType and passes the given tag to its renderer.void
include(Resource resource, java.lang.String resourceType, java.lang.String selectors, ComponentHelper.Options options)
Includes the given resource with the given resourceType and passes the given options to its renderer.void
includeForLayout(Resource resource, ComponentHelper.Options options)
A convenient overload toincludeForLayout(Resource, Resource, Options)
, with layoutResource asnull
.void
includeForLayout(Resource resource, Resource layoutResource, ComponentHelper.Options options)
Includes the given resource to be rendered by the given layoutResource.void
populateCommonAttrs(AttrBuilder attrs)
The overload ofpopulateCommonAttrs(AttrBuilder, Resource)
using the current request's resource as the source.void
populateCommonAttrs(AttrBuilder attrs, Resource src)
Populates the common attributes to the givenAttrBuilder
.
-
-
-
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 returnConfig.LAYOUT
child node, otherwisenull
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)
The overload ofpopulateCommonAttrs(AttrBuilder, Resource)
using the current request's resource as the source.- Parameters:
attrs
- the attribute builder
-
populateCommonAttrs
public void populateCommonAttrs(@Nonnull AttrBuilder attrs, @Nonnull Resource src)
Populates the common attributes to the givenAttrBuilder
.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 tosrc
- 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 toreadonly
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 toreadonly
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 ofgetItemDataSource(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 datajava.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 usingConfig.ITEMS
subresource; or specified as datasource usingConfig.DATASOURCE
subresource. If there is noConfig.ITEMS
orConfig.DATASOURCE
subresource, thenEmptyDataSource
is returned. In contrast withasDataSource(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 callingasDataSource(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 datajava.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 ofasDataSource(Resource, Resource)
with context isnull
.- 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 datajava.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 datasourcecontext
- The context resource that is returned when callingSlingHttpServletRequest.getResource()
at the datasource implementation. If this isnull
, the given datasource is used.- Returns:
null
if the given datasource isnull
.- Throws:
ServletException
- in case there's a servlet error while fetching datajava.io.IOException
- in case there's an i/o error while fetching data
-
getRenderCondition
@Nonnull public RenderCondition getRenderCondition() throws ServletException, java.io.IOException
Returns the render condition of the current resource. This method is an overload ofgetRenderCondition(Resource)
using the current resource. The render condition is specified bygranite:rendercondition
orrendercondition
subresource. Contrast this withgetRenderCondition(Resource, boolean)
, where onlygranite:rendercondition
is checked. This method is meant for backward compatibility; otherwise it is better to usegetRenderCondition(Resource, boolean)
for performance. Once the transition is over, this method will have the same behaviour asgetRenderCondition(Resource, boolean)
withcache
=false
.- Returns:
- the render condition of the current resource
- Throws:
ServletException
- in case there's a servlet errorjava.io.IOException
- in case there's an i/o error
-
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 bygranite:rendercondition
orrendercondition
subresource. Contrast this withgetRenderCondition(Resource, boolean)
, where onlygranite:rendercondition
is checked. This method is meant for backward compatibility; otherwise it is better to usegetRenderCondition(Resource, boolean)
for performance. Once the transition is over, this method will have the same behaviour asgetRenderCondition(Resource, boolean)
withcache
=false
.- Parameters:
resource
- the resource- Returns:
- the render condition of the given resource
- Throws:
ServletException
- in case there's a servlet errorjava.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 bygranite:rendercondition
subresource, unlikegetRenderCondition(Resource)
.- Parameters:
resource
- The resourcecache
-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 errorjava.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 includetag
- the tag- Throws:
ServletException
- in case there's a servlet errorjava.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 includeoptions
- the options- Throws:
ServletException
- in case there's a servlet errorjava.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 includeresourceType
- the resource typetag
- the tag- Throws:
ServletException
- in case there's a servlet errorjava.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 includeresourceType
- the resource typeoptions
- the options- Throws:
ServletException
- in case there's a servlet errorjava.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 includeresourceType
- the resource typeselectors
- the selectors to be included as part of the request.options
- the options- Throws:
ServletException
- in case there's a servlet errorjava.io.IOException
- in case there's an i/o error
-
includeForLayout
public void includeForLayout(@Nonnull Resource resource, @Nonnull ComponentHelper.Options options) throws ServletException, java.io.IOException
A convenient overload toincludeForLayout(Resource, Resource, Options)
, with layoutResource asnull
.- Parameters:
resource
- the resource to includeoptions
- the options- Throws:
ServletException
- in case there's a servlet errorjava.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
, theComponentHelper.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:- layoutResource is not null, the resourceType is layoutResource's RT
- layoutResource is null, the resourceType is
Config.LAYOUT
child node's RT - the resourceType is default layout as a catch-all fallback
- Parameters:
resource
- the resource to includelayoutResource
- the layout resource to render the given resource withoptions
- the options- Throws:
ServletException
- in case there's a servlet errorjava.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 calledoptions
- the options- Throws:
ServletException
- in case there's a servlet errorjava.io.IOException
- in case there's an i/o error
-
-