jvanzyl 00/10/20 16:53:44
Modified: src/java/org/apache/velocity/runtime/directive
Directive.java Dummy.java Foreach.java
Log:
- making it easier to catch/report errors in directives.
Revision Changes Path
1.6 +2 -2
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java
Index: Directive.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Directive.java 2000/10/12 16:41:00 1.5
+++ Directive.java 2000/10/20 23:53:42 1.6
@@ -58,7 +58,7 @@
import java.io.IOException;
import org.apache.velocity.Context;
-import org.apache.velocity.runtime.parser.Node;
+import org.apache.velocity.runtime.parser.node.Node;
/**
* Base class for all directives used in Velocity.
@@ -91,6 +91,6 @@
/**
* How this directive is to be rendered
*/
- public abstract void render(Context context, Writer writer, Node node)
+ public abstract boolean render(Context context, Writer writer, Node node)
throws IOException;
}
1.5 +3 -2
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Dummy.java
Index: Dummy.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Dummy.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- Dummy.java 2000/10/12 16:42:09 1.4
+++ Dummy.java 2000/10/20 23:53:43 1.5
@@ -59,7 +59,7 @@
import org.apache.velocity.Context;
-import org.apache.velocity.runtime.parser.Node;
+import org.apache.velocity.runtime.parser.node.Node;
/**
* A dummy directive used for testing purposes.
@@ -73,9 +73,10 @@
{
}
- public void render(Context context, Writer writer, Node node)
+ public boolean render(Context context, Writer writer, Node node)
throws IOException
{
+ return true;
}
}
1.10 +22 -4
jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java
Index: Foreach.java
===================================================================
RCS file:
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Foreach.java 2000/10/20 02:18:43 1.9
+++ Foreach.java 2000/10/20 23:53:43 1.10
@@ -67,9 +67,11 @@
import org.apache.velocity.util.ClassUtils;
import org.apache.velocity.util.ArrayIterator;
-import org.apache.velocity.runtime.parser.Node;
import org.apache.velocity.runtime.parser.Token;
+import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.runtime.exception.ReferenceException;
+
/**
* Foreach directive used for moving through arrays,
* or objects that provide an Iterator.
@@ -107,14 +109,25 @@
// This is a refence node and it needs to
// be inititialized.
-
node.jjtGetChild(2).init(context, null);
listObject = node.jjtGetChild(2).value(context);
+ // If the listObject is null then we know that this
+ // whole foreach directive is useless. We need to
+ // throw a ReferenceException which is caught by
+ // the SimpleNode.init() and logged. But we also need
+ // to set a flag so that the rendering of this node
+ // is ignored as the output would be useless.
+ if (listObject == null)
+ {
+ node.setInvalid();
+ throw new ReferenceException(node.jjtGetChild(2).literal() +
+ " is not a valid reference.");
+ }
+
// Figure out what type of object the list
// element is so that we don't have to do it
// everytime the node is traversed.
-
if (listObject instanceof Object[])
{
node.setInfo(ARRAY);
@@ -138,9 +151,12 @@
}
}
- public void render(Context context, Writer writer, Node node)
+ public boolean render(Context context, Writer writer, Node node)
throws IOException
{
+ if (node.isInvalid())
+ return false;
+
Iterator i;
listObject = node.jjtGetChild(2).value(context);
@@ -159,5 +175,7 @@
}
context.remove(COUNTER_IDENTIFIER);
context.remove(elementKey);
+
+ return true;
}
}