velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r753027 - in /velocity/engine/trunk/src: java/org/apache/velocity/context/ java/org/apache/velocity/runtime/defaults/ test/org/apache/velocity/test/
Date Thu, 12 Mar 2009 21:47:56 GMT
Author: nbubna
Date: Thu Mar 12 21:47:56 2009
New Revision: 753027

URL: http://svn.apache.org/viewvc?rev=753027&view=rev
Log:
VELOCITY-704 do not provide local context for #evaluate by default, now that $evaluate is
provided

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
    velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateContextTestCase.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateTestCase.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java?rev=753027&r1=753026&r2=753027&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/context/EvaluateContext.java Thu Mar
12 21:47:56 2009
@@ -46,6 +46,7 @@
  *  @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
  *  @version $Id$
  *  @since 1.6
+ *  @deprecated Will be removed in 2.0
  */
 public class EvaluateContext extends ChainedInternalContextAdapter
 {
@@ -73,6 +74,12 @@
 
         if (contextClass != null && contextClass.length() > 0)
         {
+            rsvc.getLog().warn("The "+RuntimeConstants.EVALUATE_CONTEXT_CLASS+
+                " property has been deprecated. It will be removed in Velocity 2.0. "+
+                " Instead, please use the automatically provided $evaluate"+
+                " namespace to get and set local references"+
+                " (e.g. #set($evaluate.foo = 'bar') and $evaluate.foo).");
+            
             Object o = null;
 
             try
@@ -107,9 +114,15 @@
         }
         else
         {
-            String err = "No class specified for #evaluate() context.";
-            rsvc.getLog().error(err);
-            throw new RuntimeException(err);
+            if (rsvc.getLog().isDebugEnabled())
+            {
+                rsvc.getLog().debug("No class specified for #evaluate() context, "+
+                    "so #set calls will now alter the global context and no longer be local.
 "+
+                    "This is a change from earlier versions due to VELOCITY-704.  "+
+                    "If you need references within #evaluate to stay local, "+
+                    "please use the automatically provided $evaluate namespace instead "+
+                    "(e.g. #set($evaluate.foo = 'bar') and $evaluate.foo).");
+            }
         }
         
     }
@@ -123,11 +136,11 @@
      */
     public Object put(String key, Object value)
     {
-        /*
-         *  just put in the local context
-         */
-        return localContext.put(key, value);
-
+        if (localContext != null)
+        {
+            return localContext.put(key, value);
+        }
+        return super.put(key, value);
     }
 
     /**
@@ -141,14 +154,15 @@
         /*
          *  always try the local context then innerContext
          */
-
-        Object o = localContext.get( key );
-
-        if ( o == null)
+        Object o = null;
+        if (localContext != null)
+        {
+            o = localContext.get(key);
+        }
+        if (o == null)
         {
             o = super.get( key );
         }
-
         return o;
     }
 
@@ -157,7 +171,8 @@
      */
     public boolean containsKey(Object key)
     {
-        return localContext.containsKey(key) || super.containsKey(key);
+        return (localContext != null && localContext.containsKey(key)) ||
+               super.containsKey(key);
     }
 
     /**
@@ -165,19 +180,23 @@
      */
     public Object[] getKeys()
     {
-        Set keys = new HashSet();
-        Object[] localKeys = localContext.getKeys();
-        for (int i=0; i < localKeys.length; i++)
+        if (localContext != null)
         {
-            keys.add(localKeys[i]);
-        }
-        
-        Object[] innerKeys = super.getKeys();
-        for (int i=0; i < innerKeys.length; i++)
-        {
-            keys.add(innerKeys[i]);
+            Set keys = new HashSet();
+            Object[] localKeys = localContext.getKeys();
+            for (int i=0; i < localKeys.length; i++)
+            {
+                keys.add(localKeys[i]);
+            }
+
+            Object[] innerKeys = super.getKeys();
+            for (int i=0; i < innerKeys.length; i++)
+            {
+                keys.add(innerKeys[i]);
+            }
+            return keys.toArray();
         }
-        return keys.toArray();
+        return super.getKeys();
     }
 
     /**
@@ -185,7 +204,11 @@
      */
     public Object remove(Object key)
     {
-        return localContext.remove( key );
+        if (localContext != null)
+        {
+            return localContext.remove(key);
+        }
+        return super.remove(key);
     }
 
     /**
@@ -199,7 +222,11 @@
      */
     public Object localPut(final String key, final Object value)
     {
-        return localContext.put(key, value);
+        if (localContext != null)
+        {
+            return localContext.put(key, value);
+        }
+        return super.localPut(key, value);
     }
 
 }

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties?rev=753027&r1=753026&r2=753027&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/defaults/velocity.properties
Thu Mar 12 21:47:56 2009
@@ -192,10 +192,12 @@
 # ----------------------------------------------------------------------------
 # EVALUATE
 # ----------------------------------------------------------------------------
-# Evaluate VTL dynamically in template.  Select a class for the Context
+# Evaluate VTL dynamically in template.  Select a class for the Context, if
+# you want all #set calls within it to be locally scoped.  This feature is
+# deprecated; please use $evaluate to hold local references instead.
 # ----------------------------------------------------------------------------
 
-directive.evaluate.context.class = org.apache.velocity.VelocityContext
+#directive.evaluate.context.class = org.apache.velocity.VelocityContext
 
 
 # ----------------------------------------------------------------------------

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateContextTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateContextTestCase.java?rev=753027&r1=753026&r2=753027&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateContextTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateContextTestCase.java Thu
Mar 12 21:47:56 2009
@@ -42,7 +42,7 @@
         RuntimeInstance instance;
         
         instance = new RuntimeInstance();
-        instance.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE);
+        instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, VelocityContext.class.getName());
         instance.init();
 
         VelocityContext base = new VelocityContext();

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateTestCase.java?rev=753027&r1=753026&r2=753027&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateTestCase.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/EvaluateTestCase.java Thu Mar
12 21:47:56 2009
@@ -27,10 +27,6 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
 import org.apache.velocity.Template;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.VelocityEngine;
@@ -88,16 +84,12 @@
         super(name);
     }
 
-    public void setUp()
+    public void setUp() throws Exception
     {
+        super.setUp();
         assureResultsDirectoryExists(RESULTS_DIR);
     }
 
-    public static Test suite()
-    {
-       return new TestSuite(EvaluateTestCase.class);
-    }
-
     /**
      * Test basic functionality.
      * @throws Exception
@@ -105,7 +97,9 @@
     public void testEvaluate()
     throws Exception
     {
-        testFile("eval1", new HashMap());
+        Map props = new HashMap();
+        props.put(RuntimeConstants.EVALUATE_CONTEXT_CLASS, VelocityContext.class.getName());
+        testFile("eval1", props);
     }
 
     /**
@@ -142,25 +136,16 @@
     }
 
     /**
-     * Test #stop (since it is attached to context).
+     * Test #stop and #break
      * @throws Exception
      */
-    public void testStop()
-    throws Exception
+    public void testStopAndBreak()
     {
-        VelocityEngine ve = new VelocityEngine();
-        ve.init();
-        
-        Context context = new VelocityContext();        
-        StringWriter writer = new StringWriter();
-        ve.evaluate(context, writer, "test","test #stop test2 #evaluate('test3')");
-        assertEquals("test ", writer.toString());
-        
-        context = new VelocityContext();        
-        writer = new StringWriter();
-        ve.evaluate(context, writer, "test","test test2 #evaluate('test3 #stop test4') test5");
-        assertEquals("test test2 test3  test5", writer.toString());
-        
+        assertEvalEquals("t ", "t #stop t2 #evaluate('t3')");
+        assertEvalEquals("t ", "t #break t2 #evaluate('t3')");
+        //assertEvalEquals("t t2 t3 ", "t t2 #evaluate('t3 #stop t4') t5");
+        assertEvalEquals("t t2 t3  t5", "t t2 #evaluate('t3 #break t4') t5");
+        assertEvalEquals("t t2 t3 ", "t t2 #evaluate('t3 #break($evaluate.topmost) t4') t5");
     }
 
     /**
@@ -270,7 +255,8 @@
     private void testFile(String basefilename, Map properties)
     throws Exception
     {
-        VelocityEngine ve = new VelocityEngine();
+        info("Test file: "+basefilename);
+        VelocityEngine ve = engine;
         ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
      
         for (Iterator i = properties.keySet().iterator(); i.hasNext();)
@@ -278,6 +264,7 @@
             String key = (String) i.next();
             String value = (String) properties.get(key);
             ve.addProperty(key, value);
+            info("Add property: "+key+" = "+value);
         }
         
         ve.init();
@@ -285,7 +272,6 @@
         Template template;
         FileOutputStream fos;
         Writer fwriter;
-        Context context;
         
         template = ve.getTemplate( getFileName(null, basefilename, TMPL_FILE_EXT) );
         
@@ -294,8 +280,6 @@
         
         fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
         
-        context = new VelocityContext();
-        setupContext(context);
         template.merge(context, fwriter);
         fwriter.flush();
         fwriter.close();
@@ -313,10 +297,5 @@
             fail(msg);
         }
     }
-        
-    public void setupContext(Context context)
-    {
-    } 
-    
-    
+
 }



Mime
View raw message