You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/02/16 02:24:04 UTC

svn commit: r508268 - /velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java

Author: nbubna
Date: Thu Feb 15 17:24:03 2007
New Revision: 508268

URL: http://svn.apache.org/viewvc?view=rev&rev=508268
Log:
add ability to configure default locale and to lock down configure method to keep DateTool safe for use in application scope

Modified:
    velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java

Modified: velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java?view=diff&rev=508268&r1=508267&r2=508268
==============================================================================
--- velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java (original)
+++ velocity/tools/trunk/src/java/org/apache/velocity/tools/generic/DateTool.java Thu Feb 15 17:24:03 2007
@@ -81,32 +81,62 @@
      */
     public static final String DEFAULT_FORMAT_KEY = "format";
 
+    /**
+     * The key used for specifying a default locale via toolbox params.
+     * @since VelocityTools 1.4
+     */
+    public static final String DEFAULT_LOCALE_KEY = "locale";
+
+    /**
+     * The key used for specifying whether or not to prevent templates
+     * from reconfiguring this tool.  The default is true.
+     * @since VelocityTools 1.4
+     */
+    public static final String LOCK_CONFIG_KEY = "lock-config";
+
     private String format = DEFAULT_FORMAT;
+    private Locale locale = Locale.getDefault();
+    private boolean configLocked = false;
 
     /**
-     * Default constructor.
+     * Looks for configuration values in the given params.
+     * @since VelocityTools 1.3
      */
-    public DateTool()
+    public void configure(Map params)
     {
-        // do nothing
+        if (!configLocked)
+        {
+            ValueParser values = new ValueParser(params);
+            configure(values);
+
+            // by default, lock down this method after use
+            // to prevent templates from re-configuring this instance
+            configLocked = values.getBoolean(LOCK_CONFIG_KEY, true);
+        }
     }
 
     /**
-     * Looks for a default format value in the given params.
-     * @since VelocityTools 1.3
+     * 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.
+     * @since VelocityTools 1.4
      */
-    public void configure(Map params)
+    protected void configure(ValueParser values)
     {
-        ValueParser parser = new ValueParser(params);
-        String format = parser.getString(DEFAULT_FORMAT_KEY);
+        String format = values.getString(DEFAULT_FORMAT_KEY);
         if (format != null)
         {
             setFormat(format);
         }
+        Locale locale = values.getLocale(DEFAULT_LOCALE_KEY);
+        if (locale != null)
+        {
+            setLocale(locale);
+        }
     }
 
 
-
     // ------------------------- system date access ------------------
 
     /**
@@ -148,7 +178,19 @@
      */
     public Locale getLocale()
     {
-        return Locale.getDefault();
+        return this.locale;
+    }
+
+    /**
+     * Sets the default locale for this instance. This is protected,
+     * because templates ought not to be using it; that would not
+     * be threadsafe so far as templates are concerned.
+     *
+     * @since VelocityTools 1.4
+     */
+    protected void setLocale(Locale locale)
+    {
+        this.locale = locale;
     }
 
     /**
@@ -213,7 +255,7 @@
 
     /**
      * Sets the default format for this instance. This is protected,
-     * because templates ought not to be using it; hat would not
+     * because templates ought not to be using it; that would not
      * be threadsafe so far as templates are concerned.
      *
      * @since VelocityTools 1.3