Class Template
- java.lang.Object
-
- net.shibboleth.utilities.java.support.velocity.Template
-
public final class Template extends Object
This is a helper class that wraps a velocity engine and template information into a single object. It provides methods,fromTemplate(VelocityEngine, String)andfromTemplate(VelocityEngine, String, Charset), for evaluating literal templates andfromTemplateName(VelocityEngine, String)andfromTemplateName(VelocityEngine, String, Charset)for evaluating templates referenced by name. It also ensures that the givenVelocityEngineis configured in such a way as to be able to use the literal or named template. Note, this check occurs only atTemplateconstruction time so, if you're loading a named template from a file and that file disappears before callingmerge(Context)ormerge(Context, Writer)you'll still end up getting aResourceNotFoundException. Many methods throw anVelocityExceptionto report template or argument errors, which is an unchecked exception type.
-
-
Field Summary
Fields Modifier and Type Field Description private org.apache.velocity.app.VelocityEngineengineTheVelocityEngineused when evaluating the template.private StringtemplateEncodingThe character encoding of the template.private StringtemplateNameThe name of the template to be evaluated.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)static TemplatefromTemplate(org.apache.velocity.app.VelocityEngine engine, String template)A convenience method that invokedfromTemplate(VelocityEngine, String, Charset)and assumes the given template is US ASCII encoded.static TemplatefromTemplate(org.apache.velocity.app.VelocityEngine engine, String template, Charset encoding)Constructs aTemplatefrom a given template.static TemplatefromTemplateName(org.apache.velocity.app.VelocityEngine engine, String templateName)A convenience method that invokedfromTemplateName(VelocityEngine, String, Charset)and assumes the named template is US ASCII encoded.static TemplatefromTemplateName(org.apache.velocity.app.VelocityEngine engine, String name, Charset encoding)Constructs aTemplatethat evaluates a named velocity template with a using the given velocity engine.StringgetTemplateName()Gets the name of the template.inthashCode()Stringmerge(org.apache.velocity.context.Context templateContext)Evaluates the template using the given context and returns the result as a string.voidmerge(org.apache.velocity.context.Context templateContext, Writer output)Evaluates the template using the given context and sends the result to a Writer.StringtoString()
-
-
-
Constructor Detail
-
Template
private Template(@Nonnull org.apache.velocity.app.VelocityEngine velocityEngine, @Nonnull @NotEmpty String velocityTemplateName, @Nonnull String velocityTemplateEncoding)Constructor.- Parameters:
velocityEngine- engine used to evaluate the templatevelocityTemplateName- name of the template to be evaluatedvelocityTemplateEncoding- encoding used by the template
-
-
Method Detail
-
fromTemplate
@Nonnull public static Template fromTemplate(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String template)
A convenience method that invokedfromTemplate(VelocityEngine, String, Charset)and assumes the given template is US ASCII encoded.See
fromTemplate(VelocityEngine, String, Charset)for full details.- Parameters:
engine- engine that will be used to evaluate the templatetemplate- the literal Velocity template, NOT a template name seefromTemplateName(VelocityEngine, String)andfromTemplateName(VelocityEngine, String, Charset)for that- Returns:
- an instance of this class that can be used to evaluate the given template using the given engine
-
fromTemplate
@Nonnull public static Template fromTemplate(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String template, @Nonnull Charset encoding)
Constructs aTemplatefrom a given template. This template is loaded in to the singletonStringResourceRepositoryused by theStringResourceLoaderunder a randomly generated ID. Therefore, calling this method multiple times with the same template will result in multiple instances of the template string being loaded in to theStringResourceRepository(each under its own unique ID).NOTE, in oder for subsequent calls to
merge(Context)ormerge(Context, Writer)to be successful, the givenVelocityEnginemust be configured to look up templates from theStringResourceLoader.- Parameters:
engine- engine that will be used to evaluate the templatetemplate- the literal Velocity template, NOT a template name seefromTemplateName(VelocityEngine, String)orfromTemplateName(VelocityEngine, String, Charset)for thatencoding- the encoding used by the template- Returns:
- an instance of this class that can be used to evaluate the given template using the given engine
-
fromTemplateName
public static Template fromTemplateName(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String templateName)
A convenience method that invokedfromTemplateName(VelocityEngine, String, Charset)and assumes the named template is US ASCII encoded.See
fromTemplateName(VelocityEngine, String, Charset)for full details.- Parameters:
engine- engine that will be used to evaluate the templatetemplateName- the name, as known to the given engine, of a velocity template- Returns:
- an instance of this class that can be used to evaluate the named template using the given engine
-
fromTemplateName
public static Template fromTemplateName(@Nonnull org.apache.velocity.app.VelocityEngine engine, @Nonnull @NotEmpty String name, @Nonnull Charset encoding)
Constructs aTemplatethat evaluates a named velocity template with a using the given velocity engine.- Parameters:
engine- the engine used to evaluate the templatename- the name of the templateencoding- the template encoding- Returns:
- an instance of this class that can be used to evaluate the named template using the given engine
-
getTemplateName
@Nonnull public String getTemplateName()
Gets the name of the template.- Returns:
- name of the template
-
merge
public String merge(org.apache.velocity.context.Context templateContext)
Evaluates the template using the given context and returns the result as a string.- Parameters:
templateContext- current template context- Returns:
- the generated output of the template
-
merge
public void merge(org.apache.velocity.context.Context templateContext, Writer output)Evaluates the template using the given context and sends the result to a Writer.- Parameters:
templateContext- current template contextoutput- writer that will receive the template output
-
-