velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r593549 - in /velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools: generic/ValueParser.java view/ParameterTool.java
Date Fri, 09 Nov 2007 14:49:37 GMT
Author: cbrisson
Date: Fri Nov  9 06:49:36 2007
New Revision: 593549

URL: http://svn.apache.org/viewvc?rev=593549&view=rev
Log:
sub keys getter (not yet tested but doesnt break anything at least)

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ParameterTool.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ValueParser.java?rev=593549&r1=593548&r2=593549&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
(original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/generic/ValueParser.java
Fri Nov  9 06:49:36 2007
@@ -21,6 +21,9 @@
 
 import java.util.Map;
 import java.util.Locale;
+import java.util.Set;
+import java.util.HashMap;
+
 import org.apache.velocity.tools.config.DefaultKey;
 
 /**
@@ -40,6 +43,14 @@
 {
     private Map source = null;
 
+    private boolean allowSubkeys = true; /* default to whatever, should be overridden by
depreprecatedMode default value anyway */
+
+    /* when using subkeys, cache at least the presence of any subkey,
+    so that the rendering of templates not using subkeys will only
+    look once for subkeys
+     */
+    private boolean hasSubkeys = true;
+
     public ValueParser() {}
 
     public ValueParser(Map source)
@@ -98,7 +109,11 @@
         {
             return null;
         }
-        return getSource().get(key);
+        Object value = getSource().get(key);
+        if (value == null && getAllowSubkeys()) {
+            value = getSubkey(key);
+        }
+        return value;
     }
 
     public Object[] getValues(String key)
@@ -359,4 +374,29 @@
         return toLocales(getValues(key));
     }
 
+    protected boolean getAllowSubkeys() {
+        return allowSubkeys;
+    }
+
+    protected void setAllowSubkeys(boolean allow) {
+        allowSubkeys = allow;
+    }
+
+    protected Object getSubkey(String subkey) {
+        if (!hasSubkeys || subkey == null || subkey.length() == 0) {
+            return null;
+        }
+        Map<String,Object> values = null;
+        subkey = subkey.concat(".");
+        for(Map.Entry<String,Object> entry:(Set<Map.Entry>)getSource().entrySet())
{
+            if(entry.getKey().startsWith(subkey)) {
+                if(values == null) {
+                    values = new HashMap<String,Object>();
+                }
+                values.put(entry.getKey().substring(subkey.length()),entry.getValue());
+            }
+        }
+        hasSubkeys = (values == null);
+        return values;
+    }
 }

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ParameterTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ParameterTool.java?rev=593549&r1=593548&r2=593549&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ParameterTool.java
(original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/view/ParameterTool.java
Fri Nov  9 06:49:36 2007
@@ -109,7 +109,11 @@
      */
     public Object getValue(String key)
     {
-        return getRequest().getParameter(key);
+        Object value = getRequest().getParameter(key);
+        if(value == null && getAllowSubkeys()) {
+            value = getSubkey(key);
+        }
+        return value;
     }
 
 



Mime
View raw message