com.uwyn.rife.template
Class AbstractTemplate

java.lang.Object
  extended by com.uwyn.rife.template.AbstractTemplate
All Implemented Interfaces:
Template, Cloneable

public abstract class AbstractTemplate
extends Object
implements Template


Field Summary
protected  BeanHandler mBeanHandler
           
protected  Map<String,Object> mCache
           
protected  Map<String,InternalValue> mConstructedValues
           
protected  String mDefaultContentType
           
protected  List<ResourceBundle> mDefaultResourceBundles
           
protected  TemplateEncoder mEncoder
           
protected  Map<String,Object> mExpressionVars
           
protected  Map<String,InternalString> mFixedValues
           
protected  TemplateInitializer mInitializer
           
protected  String mLanguage
           
protected  List<ResourceBundle> mResourceBundles
           
 
Constructor Summary
protected AbstractTemplate()
           
 
Method Summary
 void addResourceBundle(ResourceBundle resourceBundle)
          Adds a resource bundle to this template.
 void appendBlock(String valueId, String blockId)
          Appends the content of a block to a value.
protected abstract  boolean appendBlockExternalForm(String id, ExternalValue result)
           
protected abstract  boolean appendBlockInternalForm(String id, InternalValue result)
           
protected abstract  boolean appendDefaultValueExternalForm(String id, ExternalValue result)
           
protected abstract  boolean appendDefaultValueInternalForm(String id, InternalValue result)
           
protected  void appendTextInternal(InternalValue value, CharSequence text)
           
 void appendValue(String id, boolean value)
          Appends "true" or "false" to the specified value in this template, depending on the given value.
 void appendValue(String id, char value)
          Appends the single specified character to the specified value in this template.
 void appendValue(String id, char[] value)
          Appends the given characters to the specified value in this template.
 void appendValue(String id, char[] value, int offset, int count)
          Appends the specified range of the given character string to the specified value in this template.
 void appendValue(String id, double value)
          Appends the given double precision floating point value to the specified value in this template.
 void appendValue(String id, float value)
          Appends the given floating point value to the specified value in this template.
 void appendValue(String id, int value)
          Appends the given integer to the specified value in this template.
 void appendValue(String id, long value)
          Appends the given long to the specified value in this template.
 void appendValue(String id, Object value)
          Appends the result of calling String.valueOf on the given value to the specified value in this template.
 void appendValue(String id, String value)
          Appends the given string, or an empty string if value is null, to the specified value in this template.
protected  void appendValueExternalForm(String id, String tag, ExternalValue result)
           
protected  void appendValueInternalForm(String id, String tag, InternalValue result)
           
 void blankValue(String id)
          Set the content of the specified value to an empte string.
 void cacheObject(String key, Object value)
          Stores the given value in a cache, associated with the given key.
 void clear()
          Resets all values in this template and removes any resource bundles.
 Template clone()
          Returns a shallow copy of this template, with the same values, expression variables, and so on.
 int countValues()
          Returns the number of values in this template which have been set.
 InternalValue createInternalValue()
          Returns an anonymous value that can be used to construct complex content for use within this template.
 List<String> evaluateConfigTags()
          Fills all values in this template which match "CONFIG:key", where "key" is a configuration value name in the Config instance returned by Config.getRepInstance().
 List<String> evaluateExpressionConfigTags(String id)
          Evaluates the specified OGNL, Groovy, or Janino configuration expression tag.
 List<String> evaluateExpressionTags(String id)
          Evaluates the specified OGNL, Groovy, or Janino expression tag.
 List<String> evaluateL10nTags()
          Fills all values in this template which match "L10N:key", where "key" is a key in a resource bundle registered for this template.
 List<String> evaluateLangTags(String id)
          Fills the value "LANG:id" with the value of the block "LANG:id:langid", where "id" is the given ID, and "langid" is this template's current language ID.
 List<String> evaluateRenderTags()
          Evalutes all values in this template with ID's of the form "RENDER:class" or "RENDER:class:differentiator", where "class" is the fully-qualified name of a class which extends ValueRenderer, the result of calling ValueRenderer.render on a new instance of the class.
abstract  String[] getAvailableValueIds()
          Returns a list of the ID's of all values present in this template, including set and unset values.
 BeanHandler getBeanHandler()
          Returns this template's bean handler.
 String getBlock(String id)
          Returns the evaluated content of the specified block as a text.
 Object getCacheObject(String key)
          Returns the value corresponding to the given key in this template's cache, or null if no such cached object exists.
 String getContent()
          Returns the entire content of the template and finalize all non evaluated values.
 String getDefaultContentType()
          Returns this template's default content type, for example text/html.
abstract  String getDefaultValue(String id)
          Returns the original text of the specified value, before any modification that may have been made using Template.setValue(java.lang.String, java.util.List) or similar methods.
 List<CharSequence> getDeferredBlock(String id)
          Returns the content of the specified block as a list with all the individual parts.
 List<CharSequence> getDeferredContent()
          Returns the content of this template as a list with all the individual parts.
 TemplateEncoder getEncoder()
          Returns the encoder that this template uses to convert strings to values in the template's generated text output.
 Map<String,Object> getExpressionVars()
          Returns the name and value of all of the expression variables which have been set in this template.
abstract  List<String[]> getFilteredBlocks(String filter)
          Each template type supports a set of special block tags that are used for adding automated features like localization, block value scripting, config value setting, ...
abstract  List<String[]> getFilteredValues(String filter)
          Each template type supports a set of special value tags that are used for adding automated features like embedded elements, localization, block value scripting, config value setting, ...
 String getLanguage()
          Returns this template's current 2-letter language code.
abstract  long getModificationTime()
          Returns when the file corresponding to this template was modified, in milliseconds since the Unix epoch.
 Collection<ResourceBundle> getResourceBundles()
          Returns a list of the resource bundles used by this template.
abstract  Collection<String> getUnsetValueIds()
          Returns a list of the ID's of all values in this template which have not been set.
 String getValue(String id)
          Returns the current content of the specified value as a string.
 boolean hasBlock(String id)
          Returns whether this template contains a block with the given ID.
 boolean hasDefaultValue(String id)
          Returns whether a default value was specified in this template for the specified value.
abstract  boolean hasFilteredBlocks(String filter)
          Returns whether any block matched a particular filter at template compilation.
abstract  boolean hasFilteredValues(String filter)
          Returns whether any value matched a particular filter at template compilation.
 boolean hasResourceBundles()
          Returns whether this template has any resource bundles registered.
abstract  boolean hasValueId(String id)
          Returns whether this template contains a value with the given ID.
protected  void increasePartsCapacityInternal(InternalValue value, int size)
           
protected  void increaseValuesCapacityInternal(InternalValue value, int size)
           
protected static boolean isTemplateClassModified(URL templateResource, long templateModificationTime, Map templateDependencies, String templateModificationState, ResourceFinder resourceFinder, String modificationState)
           
 boolean isValueSet(String id)
          Returns whether the specified value has been set.
 void removeBean(Object bean)
          Reverts all values to their defaults when the identifiers match properties of the given bean, whether or not those values were set with a previous call to setBean.
 void removeBean(Object bean, String prefix)
          Reverts all values to their defaults when the identifiers match properties of the given bean preceded by the given prefix, whether or not those values were set with a previous call to setBean.
 void removeValue(String id)
          Reverts the specified value back to its default value.
 void removeValues(List<String> ids)
          Reverts the specified values back to their default value.
 void setBean(Object bean)
          Sets all values in this template whose identifiers match names of properties in the given bean.
 void setBean(Object bean, String prefix)
          Sets all values in this template whose names match names of properties in the given bean, preceded by the given prefix.
 void setBean(Object bean, String prefix, boolean encode)
          Sets all values in this template whose names match names of properties in the given bean, preceded by the given prefix, if present.
 void setBlock(String valueId, String blockId)
          Replaces the specified value with the content of the specified block.
 void setDefaultContentType(String defaultContentType)
           
 void setExpressionVar(String name, Object value)
          Sets a variable which can be accessed by expression tags in OGNL, Groovy, or Janino.
 void setExpressionVars(Map<String,Object> map)
          Sets the given variables to the given corresponding values, for use in expression tags.
 void setLanguage(String lang)
          Sets this template's current language code, such as "en".
 void setValue(String id, boolean value)
          Sets the specified value in this template to true or false depending on the given value.
 void setValue(String id, char value)
          Sets the specified value in this template to the single specified character.
 void setValue(String id, char[] value)
          Sets the specified value in this template to the given characters.
 void setValue(String id, char[] value, int offset, int count)
          Sets the specified value in this template to the specified range of the given character string.
 void setValue(String id, CharSequence value)
          Sets the specified value in this template to the given character sequence, or an empty character sequence if value is null.
 void setValue(String id, double value)
          Sets the specified value in this template to the given double precision floating point value.
 void setValue(String id, float value)
          Sets the specified value in this template to the given floating point value.
 void setValue(String id, int value)
          Sets the specified value in this template to the given integer.
 void setValue(String id, InternalValue internalValue)
          Sets the specified value in this template to the value of the given internal value.
 void setValue(String id, List<CharSequence> deferredContent)
          Sets the specified value in this template to content that's structured in the internal format.
 void setValue(String id, long value)
          Sets the specified value in this template to the given long.
 void setValue(String id, Object value)
          Sets the specified value in this template to the result of calling String.valueOf on the given value.
 void setValue(String id, String value)
          Sets the specified value in this template to the given string, or an empty string if value is null.
 void setValue(String id, Template template)
          Sets the specified value in this template to the current content of the given template.
 void write(OutputStream out)
          This method is a shorthand for Template.writeContent(OutputStream).
 void writeBlock(String id, OutputStream out)
          Writes the evaluated contents of the specified block to the given output stream, using UTF-8 encoding.
 void writeBlock(String id, OutputStream out, String charsetName)
           
 void writeContent(OutputStream out)
          Writes the complete evaluated template content to the given stream, using UTF-8 encoding.
 void writeContent(OutputStream out, String charsetName)
          Writes the complete evaluated template content to the given stream, using the specified charset for encoding.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.uwyn.rife.template.Template
getDependencies, getFullName, getName
 

Field Detail

mInitializer

protected TemplateInitializer mInitializer

mFixedValues

protected Map<String,InternalString> mFixedValues

mConstructedValues

protected Map<String,InternalValue> mConstructedValues

mBeanHandler

protected BeanHandler mBeanHandler

mEncoder

protected TemplateEncoder mEncoder

mDefaultResourceBundles

protected List<ResourceBundle> mDefaultResourceBundles

mResourceBundles

protected List<ResourceBundle> mResourceBundles

mExpressionVars

protected Map<String,Object> mExpressionVars

mLanguage

protected String mLanguage

mCache

protected Map<String,Object> mCache

mDefaultContentType

protected String mDefaultContentType
Constructor Detail

AbstractTemplate

protected AbstractTemplate()
Method Detail

appendBlock

public final void appendBlock(String valueId,
                              String blockId)
                       throws TemplateException
Description copied from interface: Template
Appends the content of a block to a value. The values used by the block will be captured when this method is called, so any future changes to template values will not affect text which was appended when this method is called.

Specified by:
appendBlock in interface Template
Parameters:
valueId - the ID of the value
blockId - the ID of the block
Throws:
TemplateException - when the valueId or blockId aren't known
See Also:
Template.setBlock(java.lang.String, java.lang.String), Template.getBlock(java.lang.String), Template.getContent(), Template.hasBlock(java.lang.String)

setBlock

public final void setBlock(String valueId,
                           String blockId)
                    throws TemplateException
Description copied from interface: Template
Replaces the specified value with the content of the specified block. The values used by the block will be captured when this method is called, so any future changes to template values will not affect the specified value text.

Specified by:
setBlock in interface Template
Parameters:
valueId - the ID of the value
blockId - the ID of the block
Throws:
TemplateException - when the valueId or blockId aren't known
See Also:
Template.appendBlock(java.lang.String, java.lang.String), Template.getBlock(java.lang.String), Template.getContent(), Template.hasBlock(java.lang.String)

getBlock

public String getBlock(String id)
                throws TemplateException
Description copied from interface: Template
Returns the evaluated content of the specified block as a text.

Specified by:
getBlock in interface Template
Parameters:
id - the ID of the block in the template
Returns:
the evaluated textual content of the specified block
Throws:
TemplateException - when the block ID isn't known
See Also:
Template.appendBlock(java.lang.String, java.lang.String), Template.setBlock(java.lang.String, java.lang.String), Template.getContent(), Template.hasBlock(java.lang.String)

getContent

public final String getContent()
                        throws TemplateException
Description copied from interface: Template
Returns the entire content of the template and finalize all non evaluated values. The content is the root block with has an empty string as identifier.

Values without content will either use their default value if it has been provided, or the tag that was used to declare the value will be output as-is.

All specialized tags will also be evaluated (resourcebundle localization, block localization, value renderers, expressions, ...).

Specified by:
getContent in interface Template
Returns:
the entire textual content of the template
Throws:
TemplateException - when an error occurred during the processing of the specialized tags
See Also:
Template.appendBlock(java.lang.String, java.lang.String), Template.setBlock(java.lang.String, java.lang.String), Template.getBlock(java.lang.String), Template.hasBlock(java.lang.String)

writeBlock

public void writeBlock(String id,
                       OutputStream out)
                throws IOException,
                       TemplateException
Description copied from interface: Template
Writes the evaluated contents of the specified block to the given output stream, using UTF-8 encoding.

Specified by:
writeBlock in interface Template
Parameters:
id - the ID of the block
out - the stream to write to
Throws:
IOException - when errors occur during the manipulation of the output stream
TemplateException - when the block ID isn't known
See Also:
Template.writeContent(OutputStream), Template.writeContent(OutputStream, String), Template.write(java.io.OutputStream)

writeBlock

public void writeBlock(String id,
                       OutputStream out,
                       String charsetName)
                throws IOException,
                       TemplateException
Throws:
IOException
TemplateException

writeContent

public final void writeContent(OutputStream out)
                        throws IOException,
                               TemplateException
Description copied from interface: Template
Writes the complete evaluated template content to the given stream, using UTF-8 encoding.

Specified by:
writeContent in interface Template
Parameters:
out - the stream to which the template contents should be written
Throws:
IOException - when errors occur during the manipulation of the output stream
TemplateException - when an error occurs during the template content evaluation
See Also:
Template.writeBlock(java.lang.String, java.io.OutputStream), Template.writeContent(OutputStream, String), Template.write(java.io.OutputStream)

writeContent

public final void writeContent(OutputStream out,
                               String charsetName)
                        throws IOException,
                               TemplateException
Description copied from interface: Template
Writes the complete evaluated template content to the given stream, using the specified charset for encoding.

Specified by:
writeContent in interface Template
Parameters:
out - the stream to which the template contents should be written
charsetName - the name of the charset to use
Throws:
IOException - when errors occur during the manipulation of the output stream; or

when the character set isn't valid

TemplateException - when an error occurs during the template content evaluation
See Also:
Template.writeBlock(java.lang.String, java.io.OutputStream), Template.writeContent(OutputStream), Template.write(java.io.OutputStream)

write

public final void write(OutputStream out)
                 throws IOException,
                        TemplateException
Description copied from interface: Template
This method is a shorthand for Template.writeContent(OutputStream).

Specified by:
write in interface Template
Parameters:
out - the stream to which the template contents should be written
Throws:
IOException - when errors occur during the manipulation of the output stream; or

when the character set isn't valid

TemplateException - when an error occurs during the template content evaluation
See Also:
Template.writeBlock(java.lang.String, java.io.OutputStream), Template.writeContent(OutputStream), Template.writeContent(OutputStream, String)

getDeferredBlock

public final List<CharSequence> getDeferredBlock(String id)
                                          throws TemplateException
Description copied from interface: Template
Returns the content of the specified block as a list with all the individual parts.

This list is the internal representation of all content with placeholders for the values that aren't filled in yet. This structure is mainly used internally by the framework. The list structure also makes it possible to optimize performance and memory usage.

Specified by:
getDeferredBlock in interface Template
Parameters:
id - the ID of a block in this template
Returns:
a list of the contents of the specified block
Throws:
TemplateException - if no such block exists; or

if an error occurred during the retrieval

See Also:
Template.getDeferredContent()

getDeferredContent

public final List<CharSequence> getDeferredContent()
                                            throws TemplateException
Description copied from interface: Template
Returns the content of this template as a list with all the individual parts.

This list is the internal representation of all content with placeholders for the values that aren't filled in yet. This structure is mainly used internally by the framework. The list structure also makes it possible to optimize performance and memory usage.

Specified by:
getDeferredContent in interface Template
Returns:
a list of the contents of this template
Throws:
TemplateException - if an error occurred during the retrieval
See Also:
Template.getDeferredBlock(java.lang.String)

evaluateRenderTags

public List<String> evaluateRenderTags()
                                throws TemplateException
Description copied from interface: Template
Evalutes all values in this template with ID's of the form "RENDER:class" or "RENDER:class:differentiator", where "class" is the fully-qualified name of a class which extends ValueRenderer, the result of calling ValueRenderer.render on a new instance of the class. The class must contain a zero-argument ("no-arg") constructor.

For example, given a class MyRenderer in the package "org.rifers.something", which extends ValueRenderer, a value "RENDER:org.rifers.something.MyRenderer:test" would create a new instance of MyRenderer (using its no-arg constructor), call render(this, "RENDER:org.rifers.something.MyRenderer:test", "test"), and set the value in this template to whatever value the call returns.

Value renderer tags are evaluated automatically when the output is generated (such as when calling Template.getContent()). You can manually call this method to force evaluation of the tags earlier than that.

Specified by:
evaluateRenderTags in interface Template
Returns:
the list of names of the template values that were generated
Throws:
TemplateException - if a class in a render tag cannot be instantiated

evaluateConfigTags

public List<String> evaluateConfigTags()
Description copied from interface: Template
Fills all values in this template which match "CONFIG:key", where "key" is a configuration value name in the Config instance returned by Config.getRepInstance(). Each valuev will be filled with the value of the configuration option with the corresponding key.

This method is normally called automatically during template initialization. You should call it if you wish to re-evaluate the tags at any time during the template's life.

Specified by:
evaluateConfigTags in interface Template
Returns:
the list of names of the template values that were generated

evaluateL10nTags

public List<String> evaluateL10nTags()
Description copied from interface: Template
Fills all values in this template which match "L10N:key", where "key" is a key in a resource bundle registered for this template. Each value will be filled with the value in the resource bundle with the corresponding key.

This method is normally called automatically during template initialization. You should call it if you wish to re-evaluate the tags at any time during the template's life.

Specified by:
evaluateL10nTags in interface Template
Returns:
the list of names of the template values that were generated

evaluateLangTags

public List<String> evaluateLangTags(String id)
Description copied from interface: Template
Fills the value "LANG:id" with the value of the block "LANG:id:langid", where "id" is the given ID, and "langid" is this template's current language ID.

If no matching block for the current language is found, the content of the specified value will not be modified.

This method is called automatically when the output is generated (such as when calling Template.getContent()). You can manually call this method to force evaluation of the tags earlier than that.

Specified by:
evaluateLangTags in interface Template
Parameters:
id - the ID whose language tag should be filled with the appropriate block
Returns:
the list of names of the template values that were generated

evaluateExpressionTags

public List<String> evaluateExpressionTags(String id)
Description copied from interface: Template
Evaluates the specified OGNL, Groovy, or Janino expression tag. For example, if a value exists with ID "OGNL:id:[[script]]", where "id" is the given ID and "script" is some OGNL expression, this method will replace this value with the value of the evaluated OGNL expression, using the current set of expression variables.

The prefix for OGNL is "OGNL:", the prefix for Groovy is "GROOVY:" and the prefix for Janino is "JANINO:".

This method is called automatically when the output is generated (such as when calling Template.getContent()). You can manually call this method to force evaluation of the tags earlier than that.

Specified by:
evaluateExpressionTags in interface Template
Parameters:
id - the ID whose expression tag will be replaced with the value of the evaluated expression in the tag ID
Returns:
the list of names of the template values that were generated

evaluateExpressionConfigTags

public List<String> evaluateExpressionConfigTags(String id)
Description copied from interface: Template
Evaluates the specified OGNL, Groovy, or Janino configuration expression tag. For example, if a value exists with ID "OGNL:CONFIG:id:[[script]]", where "id" is the given ID and "script" is some OGNL expression, this method will replace this value with the value of the evaluated OGNL expression, using the current set of expression variables.

The prefix for OGNL is "OGNL:", the prefix for Groovy is "GROOVY:" and the prefix for Janino is "JANINO:".

The context for the expressions will be the Config object returned by Config.getRepInstance().

Expression config tags are evaluated automatically when the output is generated (such as when calling Template.getContent()). You can manually call this method to force evaluation of the tags earlier than that.

Specified by:
evaluateExpressionConfigTags in interface Template
Parameters:
id - the ID whose expression tag will be replaced with the value of the evaluated expression in the tag ID
Returns:
the list of names of the template values that were generated

createInternalValue

public final InternalValue createInternalValue()
Description copied from interface: Template
Returns an anonymous value that can be used to construct complex content for use within this template. See InternalValue for details.

The returned internal value is tied closely to the template it was obtained from, methods like InternalValue.appendBlock reference blocks within this template.

Specified by:
createInternalValue in interface Template
Returns:
a new internal value instance for constructing more complex parts of this template
See Also:
InternalValue

setValue

public final void setValue(String id,
                           List<CharSequence> deferredContent)
                    throws TemplateException
Description copied from interface: Template
Sets the specified value in this template to content that's structured in the internal format.

Specified by:
setValue in interface Template
Parameters:
id - the ID of the value in this template
deferredContent - content in the internal format
Throws:
TemplateException - if the specified value ID does not exist in this template
See Also:
Template.appendValue(java.lang.String, java.lang.Object), Template.isValueSet(java.lang.String), Template.removeValue(java.lang.String), Template.removeValues(java.util.List), Template.blankValue(java.lang.String), Template.hasValueId(java.lang.String)

setValue

public final void setValue(String id,