You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by by...@apache.org on 2008/12/31 10:41:07 UTC

svn commit: r730359 - in /velocity/engine/trunk: src/java/org/apache/velocity/runtime/directive/Foreach.java xdocs/docs/developer-guide.xml xdocs/docs/user-guide.xml

Author: byron
Date: Wed Dec 31 01:41:07 2008
New Revision: 730359

URL: http://svn.apache.org/viewvc?rev=730359&view=rev
Log:
Make skipInvalidIterator flag for a #foreach directive default to false in strict mode, improve documentation on strict mode

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
    velocity/engine/trunk/xdocs/docs/developer-guide.xml
    velocity/engine/trunk/xdocs/docs/user-guide.xml

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java?rev=730359&r1=730358&r2=730359&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java (original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/directive/Foreach.java Wed Dec 31 01:41:07 2008
@@ -22,11 +22,8 @@
 import java.io.IOException;
 import java.io.Writer;
 import java.util.Iterator;
-import java.util.List;
 
-import org.apache.velocity.app.event.EventCartridge;
 import org.apache.velocity.context.ChainedInternalContextAdapter;
-import org.apache.velocity.context.Context;
 import org.apache.velocity.context.InternalContextAdapter;
 import org.apache.velocity.exception.MethodInvocationException;
 import org.apache.velocity.exception.ParseErrorException;
@@ -35,12 +32,11 @@
 import org.apache.velocity.exception.VelocityException;
 import org.apache.velocity.runtime.RuntimeConstants;
 import org.apache.velocity.runtime.RuntimeServices;
+import org.apache.velocity.runtime.log.Log;
 import org.apache.velocity.runtime.parser.node.ASTReference;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.node.SimpleNode;
-import org.apache.velocity.runtime.resource.Resource;
 import org.apache.velocity.util.introspection.Info;
-import org.apache.velocity.util.introspection.IntrospectionCacheData;
 
 /**
  * Foreach directive used for moving through arrays,
@@ -218,6 +214,17 @@
         skipInvalidIterator =
             rsvc.getBoolean(RuntimeConstants.SKIP_INVALID_ITERATOR, true);
 
+        if (rsvc.getBoolean(RuntimeConstants.RUNTIME_REFERENCES_STRICT, false))
+        {
+          // If we are in strict mode then the default for skipInvalidItarator
+          // is true.  However, if the property is explicitly set, then honor the setting.
+          if (rsvc.getString(RuntimeConstants.SKIP_INVALID_ITERATOR) == null)
+          {
+            skipInvalidIterator = false;
+          }
+        }
+        
+        
         /*
          *  this is really the only thing we can do here as everything
          *  else is context sensitive
@@ -312,8 +319,7 @@
             {
                 Node pnode = node.jjtGetChild(2);
                 String msg = "#foreach parameter " + pnode.literal() + " at "
-                    + rsvc.getLog().formatFileString(uberInfo.getTemplateName(),
-                       pnode.getLine(), pnode.getColumn()) 
+                    + Log.formatFileString(pnode)
                     + " is of type " + listObject.getClass().getName()
                     + " and is either of wrong type or cannot be iterated.";
                 rsvc.getLog().error(msg);

Modified: velocity/engine/trunk/xdocs/docs/developer-guide.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/developer-guide.xml?rev=730359&r1=730358&r2=730359&view=diff
==============================================================================
--- velocity/engine/trunk/xdocs/docs/developer-guide.xml (original)
+++ velocity/engine/trunk/xdocs/docs/developer-guide.xml Wed Dec 31 01:41:07 2008
@@ -1874,15 +1874,17 @@
 <p>
 <code>runtime.references.strict = false</code><br/>
 
-New in Velocity 1.6, when true Velocity will throw a
+New in Velocity 1.6, when set to true Velocity will throw a
 <code>MethodInvocationException</code> for references that are not
 defined in the context, or have not been defined with a #set
 directive.  This setting will also throw an exception if an attempt is
 made to call a non-existing property on an object or if the object is
 null.  When this property is true then property
-'directive.set.null.allowed' is also set to true. For a complete
-discussion see <a href="user-guide.html#strictreferences">Strict
-References Setting</a>.
+'directive.set.null.allowed' is also set to true. Also,
+'directive.foreach.skip.invalid' defaults to true when this property
+is true, but explicitly setting 'directive.foreach.skip.invalid' will
+override this default. For a complete discussion see <a
+href="user-guide.html#strictreferences">Strict References Setting</a>.
 </p>
 
 <p>

Modified: velocity/engine/trunk/xdocs/docs/user-guide.xml
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/xdocs/docs/user-guide.xml?rev=730359&r1=730358&r2=730359&view=diff
==============================================================================
--- velocity/engine/trunk/xdocs/docs/user-guide.xml (original)
+++ velocity/engine/trunk/xdocs/docs/user-guide.xml Wed Dec 31 01:41:07 2008
@@ -702,39 +702,45 @@
 </section>
 <section name="Strict References Setting" href="strictreferences">
    <p>
-    Velocity 1.6 introduces the concept of strict references which is
-    activated by setting the velocity configuration property
-    'runtime.references.strict' to true.  With this setting references
-    are required to be either placed explicitly into the context or
-    defined with a #set directive, or Velocity will throw an
-    exception.  References that are in the context with a value of
-    null will not produce an exception.  Additionally, if an attempt
-    is made to call a method or a property on an object within a
-    reference that does not define the specified method or property
-    then Velocity will throw an exception.  This is also true if there
-    is an attempt to call a method or property on a null value.
+    Velocity 1.6 introduces the concept of strict reference mode which
+    is activated by setting the velocity configuration property
+    'runtime.references.strict' to true.  The general intent of this
+    setting is to make Velocity behave more strictly in cases that are
+    undefined or ambiguous, similar to a programming language, which
+    may be more appropriate for some uses of Velocity. In such
+    undefined or ambiguous cases Velocity will throw an exception.  The
+    following discussion outlines the cases that strict behavior is
+    different from traditional behavior.
+   </p>
+   <p>
+    With this setting references are required to be either placed
+    explicitly into the context or defined with a #set directive, or
+    Velocity will throw an exception.  References that are in the
+    context with a value of null will not produce an exception.
+    Additionally, if an attempt is made to call a method or a property
+    on an object within a reference that does not define the specified
+    method or property then Velocity will throw an exception.  This is
+    also true if there is an attempt to call a method or property on a
+    null value.
   </p>
   <p>
     In the following examples $bar is defined but $foo is not, and all
     these statements will throw an exception:
   </p>
-    <source><![CDATA[$foo                         ## Exception
+  <source><![CDATA[$foo                         ## Exception
 #set($bar = $foo)            ## Exception
 #if($foo == $bar)#end        ## Exception
-#foreach($item in $foo)#end  ## Exception]]></source>
-    
+#foreach($item in $foo)#end  ## Exception]]></source>    
   <p>
     Also, The following statements show examples in which Velocity will
     throw an exception when attempting to call methods or properties
     that do not exist. In these examples $bar contains an object that
     defines a property 'foo' which returns a string, and 'retnull' which
     returns null.
-  </p>
-    
-    <source><![CDATA[$bar.bogus          ## $bar does not provide property bogus, Exception
+  </p>    
+  <source><![CDATA[$bar.bogus          ## $bar does not provide property bogus, Exception
 $bar.foo.bogus      ## $bar.foo does not provide property bogus, Exception
 $bar.retnull.bogus  ## cannot call a property on null, Exception]]></source>
-
   <p>
     In general strict reference behavior is true for all situations
     in which references are used except for a special case within the
@@ -750,12 +756,13 @@
 #if ($foo && $foo.bar)#end      ## False and $foo.bar will not be evaluated
 #if ($foo && $foo == "bar")#end ## False and $foo == "bar" wil not be evaluated
 #if ($foo1 || $foo2)#end        ## False $foo1 and $foo2 are not defined]]></source>
-  
   <p>
-    One additional note, undefined macro references will also throw an
-    exception with the strict reference setting.
+    Strict mode requires that comparisons of >, <, >= or <= within an
+    #if directive make sense. Also, the argument to #foreach must be
+    iterable (this behavior can be modified with the property
+    directive.foreach.skip.invalid). Finally, undefined macro
+    references will also throw an exception in strict mode.
   </p>
-        
 </section>
 
 <section name="Case Substitution" href="case_substitution">