Package org.devacfr.maven.skins.reflow
Class SkinConfigTool
- java.lang.Object
-
- org.apache.velocity.tools.generic.SafeConfig
-
- org.devacfr.maven.skins.reflow.SkinConfigTool
-
- All Implemented Interfaces:
ISkinConfig
@DefaultKey("config") public class SkinConfigTool extends org.apache.velocity.tools.generic.SafeConfig implements ISkinConfigAn Apache Velocity tool that simplifies retrieval of custom configuration values for a Maven Site.The tool is configured to access Maven site configuration of a skin inside
<custom>element of site descriptor. It supports global properties (defined at skin level) and per-page properties (defined in<page><mypage>element). The per-page properties override the global ones.A sample configuration would be like that:
<custom> <reflowSkin> <prop1>value1</prop1> <prop2> <prop21>value2</prop21> </prop2> <pages> <mypage project="myproject"> <prop1>override value1</prop1> </mypage> </pages> </reflowSkin> </custom>To get the value of
prop1, one would simply use$config.prop1. This would return "override value1". Then$config.prop2would return "value2" - the global value.The tool allows querying the value easily, falling back from page to global configuration to
null, if none is available. It also provides convenience accessors for common values.Note
- Since:
- 1.0
- Author:
- Andrius Velykis, Christophe Friederich
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_KEYstatic StringSKIN_KEYBy default use Reflow skin configuration tag.
-
Constructor Summary
Constructors Constructor Description SkinConfigTool()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidconfigure(org.apache.velocity.tools.generic.ValueParser values)<T> Teval(String vtl, Class<T> requiredClass)Evaluate a velocity expression in the current context.org.codehaus.plexus.util.xml.Xpp3Domget(String property)Default accessor for config properties.<T> Optional<T>getAttributeValue(String property, String attribute, Class<T> targetType)Gets the attribute value of the givenattributeofproperty.<T> TgetAttributeValue(String property, String attribute, Class<T> targetType, T defaultValue)Gets the attribute value of the givenattributeofproperty.<T> Optional<T>getAttributeValue(org.codehaus.plexus.util.xml.Xpp3Dom element, String attribute, Class<T> targetType)Get the value contained in specific attribute ofelementparameter.<T> TgetAttributeValue(org.codehaus.plexus.util.xml.Xpp3Dom element, String attribute, Class<T> targetType, T defaultValue)Get the value contained in specific attribute ofelementparameter.StringgetBodyContent()DategetBuildOutputTimestamp()Gets the reproduce build timestamp whether property 'project.build.outputTimestamp' is fill in.List<String>getChildren(org.codehaus.plexus.util.xml.Xpp3Dom parentNode)Gets the list of all children name for theparentNode.<T extends Context<Context<?>>>
TgetContext()<T> TgetContextValue(String key, Class<T> type)Gets the associated value tokeystored in Velocity context.StringgetCurrentFileLocation()StringgetCurrentFileName()See Doxia Sitetools - Site Renderer for more information.StringgetDatePosition()Gets the date position of the site.StringgetFileId()org.codehaus.plexus.util.xml.Xpp3DomgetGlobalProperties()HtmlToolgetHtmlTool()Gets the toolHtmlTool.StringgetKey()StringgetNamespace()org.codehaus.plexus.util.xml.Xpp3DomgetPageProperties()org.apache.maven.project.MavenProjectgetProject()StringgetProjectId()StringgetProjectLocation()<T> Optional<T>getPropertyValue(String property, Class<T> targetType)Gets the text value of the givenproperty.<T> TgetPropertyValue(String property, Class<T> targetType, T defaultValue)Gets the text value of the givenproperty.StringgetPublishDate()Gets the publish date of the site.StringgetPublishDateMessage()Gets the publish date message of the site.StringgetResourcePath()StringgetShortTitle()Gets the short title of the site.org.apache.maven.doxia.site.SiteModelgetSiteModel()StringgetTitle()Gets the title of the site.<T> TgetToolbox(String toolName, Class<T> toolType)Gets the associated tool tonamestored in toolbox of Velocity context.org.apache.velocity.context.ContextgetVelocityContext()StringgetVersion()Gets the version of the site.StringgetVersionMessage()Gets the version message of the site.StringgetVersionPosition()Gets the position of the version in the site.booleanis(String property)A convenience method to check if the value of thepropertyis"true".booleanisActiveLink(String href)Gets the indicating if the link is active.booleanisExternalLink(String url)booleanisValue(String property, String value)A convenience method to check if thepropertyis set to a specific value.booleannot(String property)A convenience method to check if the value of thepropertyis"false".StringrelativeLink(String href)StringrenderSnippet(String snippet)StringrenderSnippets(String bodyContent)Renders the snippets found in the body content.voidsetContextValue(String key, Object value)Sets the value in Velocity context associated tokey.protected voidsetKey(String key)Sets the key under which this tool has been configured.static StringslugFilename(String fileName)Converts a filename to pageId format.Stringvalue(String property)Retrieves the text value of the givenproperty, e.g. as in<myprop>value</myprop>.
-
-
-
Field Detail
-
DEFAULT_KEY
public static final String DEFAULT_KEY
- See Also:
- Constant Field Values
-
SKIN_KEY
public static final String SKIN_KEY
By default use Reflow skin configuration tag.- See Also:
- Constant Field Values
-
-
Method Detail
-
configure
protected void configure(org.apache.velocity.tools.generic.ValueParser values)
- Overrides:
configurein classorg.apache.velocity.tools.generic.SafeConfig- See Also:
SafeConfig.configure(ValueParser)
-
renderSnippets
@Nonnull public String renderSnippets(String bodyContent) throws Exception
Renders the snippets found in the body content.- Specified by:
renderSnippetsin interfaceISkinConfig- Parameters:
bodyContent- the body content.- Returns:
- Returns the
Stringrepresenting the transformed body content. - Throws:
Exception- if any.
-
renderSnippet
@Nonnull public String renderSnippet(String snippet) throws Exception
- Specified by:
renderSnippetin interfaceISkinConfig- Throws:
Exception
-
setKey
protected void setKey(String key)
Sets the key under which this tool has been configured.- Parameters:
key- the key of config- Since:
- 1.0
-
getKey
public String getKey()
- Returns:
- Returns the key under which this tool has been configured. The default is `config`.
- Since:
- 1.0
-
getContextValue
@Nullable public <T> T getContextValue(@Nonnull String key, @Nonnull Class<T> type)
Gets the associated value tokeystored in Velocity context.- Specified by:
getContextValuein interfaceISkinConfig- Type Parameters:
T- the type of expected value.- Parameters:
key- the key name of associated value in Velocity context.type- the the type of expected value.- Returns:
- Returns the associated value to
keystored in Velocity context.
-
setContextValue
public void setContextValue(@Nonnull String key, @Nullable Object value)
Sets the value in Velocity context associated tokey.- Specified by:
setContextValuein interfaceISkinConfig- Parameters:
key- the key name of associated value in Velocity context.value- the new value
-
getToolbox
@Nullable public <T> T getToolbox(@Nonnull String toolName, @Nonnull Class<T> toolType)
Gets the associated tool tonamestored in toolbox of Velocity context.- Specified by:
getToolboxin interfaceISkinConfig- Type Parameters:
T- the type of expected tool.- Parameters:
toolName- the name of tool associated in Velocity context.toolType- the expected class of tool.- Returns:
- Returns the associated tool to
namestored in toolbox of Velocity context.
-
getHtmlTool
@Nonnull public HtmlTool getHtmlTool()
Gets the toolHtmlTool.- Specified by:
getHtmlToolin interfaceISkinConfig- Returns:
- Returns the tool
HtmlTool. - Since:
- 2.1
-
get
@Nullable public org.codehaus.plexus.util.xml.Xpp3Dom get(@Nonnull String property)
Default accessor for config properties. Instead of using$config.get("myproperty"), one can utilise Velocity fallback onto the default getter and use$config.myproperty.- Specified by:
getin interfaceISkinConfig- Parameters:
property- the property of interest- Returns:
- configuration node if found in the following sequence:
- In page configuration
- In global configuration
nullotherwise
-
value
@Nullable public String value(@Nonnull String property)
Retrieves the text value of the givenproperty, e.g. as in<myprop>value</myprop>.- Parameters:
property- the property of interest- Returns:
- the configuration value if found in page or globally,
nullotherwise. - Since:
- 1.0
- See Also:
get(String)
-
getPropertyValue
@Nullable public <T> T getPropertyValue(@Nonnull String property, @Nonnull Class<T> targetType, @Nullable T defaultValue)
Gets the text value of the givenproperty.- Specified by:
getPropertyValuein interfaceISkinConfig- Type Parameters:
T- the type of returned object.- Parameters:
property- the property to usetargetType- the returned target type use to convert value.defaultValue- the default value used if property doesn't exist.- Returns:
- Returns a converted value of the given
property.
-
getPropertyValue
@Nonnull public <T> Optional<T> getPropertyValue(@Nonnull String property, @Nonnull Class<T> targetType)
Gets the text value of the givenproperty.- Specified by:
getPropertyValuein interfaceISkinConfig- Type Parameters:
T- the type of returned object.- Parameters:
property- the property to usetargetType- the returned target type use to convert value.- Returns:
- Returns a converted value of the given
property.
-
getChildren
public List<String> getChildren(org.codehaus.plexus.util.xml.Xpp3Dom parentNode)
Gets the list of all children name for theparentNode.- Parameters:
parentNode- the parent node to use (can benull.- Returns:
- Returns a list of
Stringrepresenting the name of all children, which may be empty but nevernull. - Since:
- 1.3
-
getAttributeValue
@Nullable public <T> T getAttributeValue(@Nonnull String property, @Nonnull String attribute, @Nonnull Class<T> targetType, @Nullable T defaultValue)
Gets the attribute value of the givenattributeofproperty.- Specified by:
getAttributeValuein interfaceISkinConfig- Type Parameters:
T- the type of returned object.- Parameters:
property- the property to useattribute- the attribute to use.targetType- the returned target type use to convert value.defaultValue- the default value used if property doesn't exist.- Returns:
- Returns a converted value of the given
property.
-
getAttributeValue
@Nonnull public <T> Optional<T> getAttributeValue(@Nonnull String property, @Nonnull String attribute, @Nonnull Class<T> targetType)
Gets the attribute value of the givenattributeofproperty.- Specified by:
getAttributeValuein interfaceISkinConfig- Type Parameters:
T- the type of returned object.- Parameters:
property- the property to useattribute- the attribute to use.targetType- the returned target type use to convert value.- Returns:
- Returns a converted value of the given
property.
-
getAttributeValue
@Nullable public <T> T getAttributeValue(@Nonnull org.codehaus.plexus.util.xml.Xpp3Dom element, @Nonnull String attribute, @Nonnull Class<T> targetType, @Nullable T defaultValue)
Get the value contained in specific attribute ofelementparameter.- Specified by:
getAttributeValuein interfaceISkinConfig- Type Parameters:
T- the type of returned value.- Parameters:
element- the xml element.attribute- the attribute name.targetType- the class of converted returned value.defaultValue- the value to return if attribute is empty ornull.- Returns:
- Returns the converted value of specific attribute of
elementparameter if exists, otherwise returns the default value.
-
getAttributeValue
@Nonnull public <T> Optional<T> getAttributeValue(@Nonnull org.codehaus.plexus.util.xml.Xpp3Dom element, @Nonnull String attribute, @Nonnull Class<T> targetType)
Get the value contained in specific attribute ofelementparameter.- Specified by:
getAttributeValuein interfaceISkinConfig- Type Parameters:
T- the type of returned value.- Parameters:
element- the xml element.attribute- the attribute name.targetType- the class of converted returned value.- Returns:
- Returns the converted value of specific attribute of
elementparameter if exists, otherwise returns an emptyOptional.
-
is
public boolean is(String property)
A convenience method to check if the value of thepropertyis"true".- Parameters:
property- the property of interest- Returns:
trueif the configuration value is set either in page or globally, and is equal to"true".- Since:
- 1.0
- See Also:
get(String)
-
not
public boolean not(String property)
A convenience method to check if the value of thepropertyis"false". Useful for properties that are enabled by default - checks if the property is set to"false"explicitly.- Specified by:
notin interfaceISkinConfig- Parameters:
property- the property of interest- Returns:
trueif the configuration value is set either in page or globally, and is equal to"false". Note that this will returnfalseif property is not set at all.- See Also:
ISkinConfig.get(String)
-
isValue
public boolean isValue(String property, String value)
A convenience method to check if thepropertyis set to a specific value.- Parameters:
property- the property of interestvalue- the property value to check- Returns:
trueif the configuration value is set either in page or globally, and is equal tovalue.- Since:
- 1.0
- See Also:
get(String)
-
getProjectId
@Nullable public String getProjectId()
- Specified by:
getProjectIdin interfaceISkinConfig- Returns:
- Returns the
Stringrepresenting the projectId.
-
getFileId
@Nullable public String getFileId()
- Specified by:
getFileIdin interfaceISkinConfig- Returns:
- Returns the
Stringrepresenting the fileId.
-
getContext
@Nonnull public <T extends Context<Context<?>>> T getContext()
- Specified by:
getContextin interfaceISkinConfig- Returns:
- the context
-
getVelocityContext
public org.apache.velocity.context.Context getVelocityContext()
- Specified by:
getVelocityContextin interfaceISkinConfig- Returns:
- the velocity Context
-
getProject
@Nonnull public org.apache.maven.project.MavenProject getProject()
- Specified by:
getProjectin interfaceISkinConfig- Returns:
- the project
-
getSiteModel
@Nonnull public org.apache.maven.doxia.site.SiteModel getSiteModel()
- Specified by:
getSiteModelin interfaceISkinConfig- Returns:
- the SiteModel
-
getPageProperties
@Nonnull public org.codehaus.plexus.util.xml.Xpp3Dom getPageProperties()
- Specified by:
getPagePropertiesin interfaceISkinConfig- Returns:
- Returns the page level
Xpp3Dom.
-
getGlobalProperties
@Nonnull public org.codehaus.plexus.util.xml.Xpp3Dom getGlobalProperties()
- Specified by:
getGlobalPropertiesin interfaceISkinConfig- Returns:
- Returns the root level
Xpp3Dom.
-
getShortTitle
@Nonnull public String getShortTitle()
Gets the short title of the site.- Specified by:
getShortTitlein interfaceISkinConfig- Returns:
- Returns the short title of the site.
-
getTitle
@Nonnull public String getTitle()
Gets the title of the site.- Specified by:
getTitlein interfaceISkinConfig- Returns:
- Returns the title of the site.
-
getPublishDate
@Nonnull public String getPublishDate()
Gets the publish date of the site.- Specified by:
getPublishDatein interfaceISkinConfig- Returns:
- Returns the publish date of the site.
-
getPublishDateMessage
@Nonnull public String getPublishDateMessage()
Gets the publish date message of the site.- Specified by:
getPublishDateMessagein interfaceISkinConfig- Returns:
- Returns the publish date message of the site.
-
getDatePosition
@Nonnull public String getDatePosition()
Gets the date position of the site.- Specified by:
getDatePositionin interfaceISkinConfig- Returns:
- Returns the date position of the site.
-
getVersion
@Nonnull public String getVersion()
Gets the version of the site.- Specified by:
getVersionin interfaceISkinConfig- Returns:
- Returns the version of the site.
-
getVersionPosition
@Nonnull public String getVersionPosition()
Gets the position of the version in the site.- Specified by:
getVersionPositionin interfaceISkinConfig- Returns:
- Returns the position of the version in the site.
-
getVersionMessage
@Nonnull public String getVersionMessage()
Gets the version message of the site.- Specified by:
getVersionMessagein interfaceISkinConfig- Returns:
- Returns the version message of the site.
-
getNamespace
@Nonnull public String getNamespace()
- Specified by:
getNamespacein interfaceISkinConfig- Returns:
- Returns a
Stringrepresenting the namespace.
-
getBodyContent
@Nonnull public String getBodyContent()
- Specified by:
getBodyContentin interfaceISkinConfig- Returns:
- Returns the html body content stored in velocity context.
- Since:
- 2.1
-
getProjectLocation
@Nonnull public String getProjectLocation()
- Returns:
- Returns the location of project.
-
getCurrentFileName
@Nonnull public String getCurrentFileName()
See Doxia Sitetools - Site Renderer for more information.
- Returns:
- Returns a
Stringrepresenting the name of current file of the (HTML) document being rendered, relative to the site root.
-
getCurrentFileLocation
@Nonnull public String getCurrentFileLocation()
- Returns:
- Returns a
Stringrepresenting the location path of current rendered file.
-
eval
@Nullable public <T> T eval(@Nullable String vtl, @Nonnull Class<T> requiredClass)
Evaluate a velocity expression in the current context.- Specified by:
evalin interfaceISkinConfig- Type Parameters:
T- Tthe type of expected returned value.- Parameters:
vtl- The velocity expression to evaluaterequiredClass- the class of returned value.- Returns:
- Returns the value returned by the evaluated velocity expression.
-
relativeLink
public String relativeLink(String href)
- Specified by:
relativeLinkin interfaceISkinConfig- Parameters:
href- link to relative.- Returns:
- Returns Relativizes the link.
-
isExternalLink
public boolean isExternalLink(String url)
- Specified by:
isExternalLinkin interfaceISkinConfig- Parameters:
url- a url.- Returns:
- Returns
truewhether the link is a external link to the site.
-
isActiveLink
public boolean isActiveLink(@Nullable String href)
Gets the indicating if the link is active.- Specified by:
isActiveLinkin interfaceISkinConfig- Parameters:
href- the link to check.- Returns:
- Returns
truethe link is active, otherwisefalse.
-
slugFilename
@Nullable public static String slugFilename(@Nullable String fileName)
Converts a filename to pageId format.- Parameters:
fileName- the filename to convert- Returns:
- Returns a
Stringrepresenting the pageId offilename.
-
getResourcePath
@Nonnull public String getResourcePath()
- Specified by:
getResourcePathin interfaceISkinConfig- Returns:
- Returns a
Stringrepresenting the relative path to root site.
-
getBuildOutputTimestamp
@Nullable public Date getBuildOutputTimestamp() throws ParseException
Gets the reproduce build timestamp whether property 'project.build.outputTimestamp' is fill in.- Specified by:
getBuildOutputTimestampin interfaceISkinConfig- Returns:
- Returns a instance of
Daterepresenting the reproduce build timestamp whether property 'project.build.outputTimestamp' is fill in. - Throws:
ParseException- if the build timestamp can not be parsed.
-
-