org.apache.velocity.tools.generic
Class ResourceTool

java.lang.Object
  extended by org.apache.velocity.tools.generic.AbstractLockConfig
      extended by org.apache.velocity.tools.generic.LocaleConfig
          extended by org.apache.velocity.tools.generic.ResourceTool
Direct Known Subclasses:
ViewResourceTool

@DefaultKey(value="text")
public class ResourceTool
extends LocaleConfig

Tool for accessing ResourceBundles and formatting messages therein.

 Template example(s):
   $text.foo                      ->  bar
   $text.hello.world              ->  Hello World!
   #set( $otherText = $text.bundle('otherBundle') )
   $otherText.foo                 ->  woogie
   $otherText.bar                 ->  The args are {0} and {1}.
   $otherText.bar.insert(4)       ->  The args are 4 and {1}.
   $otherText.bar.insert(4,true)  ->  The args are 4 and true.

 Toolbox configuration example:
 <tools>
   <toolbox scope="application">
     <tool class="org.apache.velocity.tools.generic.ResourceTool"
              bundles="resources,com.foo.moreResources"
              locale="en_US"/>
   </toolbox>
 </tools>
 

This comes in very handy when internationalizing templates. Note that the default resource bundle baseName is "resources", and the default locale is the system locale. These may both be overridden in your toolbox config as demonstrated above.

Also, be aware that very few performance considerations have been made in this initial version. It should do fine, but if you have performance issues, please report them to dev@velocity.apache.org, so we can make improvements.

Since:
VelocityTools 1.3
Version:
$Revision: 545419 $ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
Author:
Nathan Bubna

Nested Class Summary
 class ResourceTool.Key
          Internal class used to enable an elegant syntax for accessing resources.
 
Field Summary
private  java.lang.String[] bundles
           
static java.lang.String BUNDLES_KEY
           
 
Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig
DEFAULT_LOCALE, LOCALE_KEY
 
Fields inherited from class org.apache.velocity.tools.generic.AbstractLockConfig
LOCK_CONFIG_KEY
 
Constructor Summary
ResourceTool()
           
 
Method Summary
 ResourceTool.Key bundle(java.lang.String bundle)
           
protected  void configure(ValueParser parser)
          Does the actual configuration.
 ResourceTool.Key get(java.lang.String key)
           
 java.lang.Object get(java.lang.String key, java.lang.String[] bundles, java.util.Locale locale)
          Retrieve a resource for the specified key from the first of the specified bundles in which a matching resource is found.
 java.lang.Object get(java.lang.String key, java.lang.String baseName, java.util.Locale locale)
          Returns the value for the specified key in the ResourceBundle for the specified basename and locale.
protected  java.lang.String getDefaultBundle()
           
protected  java.util.Locale getDefaultLocale()
          Deprecated. 
 ResourceTool.Key insert(java.util.List args)
           
 ResourceTool.Key insert(java.lang.Object arg)
           
 ResourceTool.Key insert(java.lang.Object[] args)
           
 ResourceTool.Key insert(java.lang.Object arg0, java.lang.Object arg1)
           
 ResourceTool.Key locale(java.util.Locale locale)
           
 java.lang.String render(java.lang.Object resource, java.lang.Object[] args)
          Renders the specified resource value and arguments as a String.
protected  void setDefaultBundle(java.lang.String bundle)
           
protected  void setDefaultLocale(java.util.Locale locale)
          Deprecated. 
 
Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig
getLocale, setLocale
 
Methods inherited from class org.apache.velocity.tools.generic.AbstractLockConfig
configure, isConfigLocked
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUNDLES_KEY

public static final java.lang.String BUNDLES_KEY
See Also:
Constant Field Values

bundles

private java.lang.String[] bundles
Constructor Detail

ResourceTool

public ResourceTool()
Method Detail

setDefaultBundle

protected final void setDefaultBundle(java.lang.String bundle)

getDefaultBundle

protected final java.lang.String getDefaultBundle()

setDefaultLocale

@Deprecated
protected final void setDefaultLocale(java.util.Locale locale)
Deprecated. 


getDefaultLocale

@Deprecated
protected final java.util.Locale getDefaultLocale()
Deprecated. 


configure

protected void configure(ValueParser parser)
Description copied from class: LocaleConfig
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.

Overrides:
configure in class LocaleConfig

get

public ResourceTool.Key get(java.lang.String key)

bundle

public ResourceTool.Key bundle(java.lang.String bundle)

locale

public ResourceTool.Key locale(java.util.Locale locale)

insert

public ResourceTool.Key insert(java.lang.Object[] args)

insert

public ResourceTool.Key insert(java.util.List args)

insert

public ResourceTool.Key insert(java.lang.Object arg)

insert

public ResourceTool.Key insert(java.lang.Object arg0,
                               java.lang.Object arg1)

get

public java.lang.Object get(java.lang.String key,
                            java.lang.String baseName,
                            java.util.Locale locale)
Returns the value for the specified key in the ResourceBundle for the specified basename and locale. If no such resource can be found, no errors are thrown and null is returned.


get

public java.lang.Object get(java.lang.String key,
                            java.lang.String[] bundles,
                            java.util.Locale locale)
Retrieve a resource for the specified key from the first of the specified bundles in which a matching resource is found. If no resource is found, no exception will be thrown and null will be returned.


render

public java.lang.String render(java.lang.Object resource,
                               java.lang.Object[] args)
Renders the specified resource value and arguments as a String. If there are no arguments, then the String value of the resource is returned directly. If there are arguments, then the resource is treated as a MessageFormat pattern and used to format the specified argument values.



Copyright (c) 2003-2007 Apache Software Foundation