jstrachan 02/05/30 09:47:17
Modified: jelly/src/test/org/apache/commons/jelly run_all.jelly
jelly/src/java/org/apache/commons/jelly JellyContext.java
Added: jelly/src/test/org/apache/commons/jelly
testFindVariable.jelly Copy of test_args.jelly
Removed: jelly/src/test/org/apache/commons/jelly
example_ant_tasks.jelly
Log:
Added a getParent() relationship to JellyContext so that parent relationships can be
traversed (such as across <j:include> scripts).
Also now the variable "context" is available in the EL for the current context.
You can refer to a parent context as context.parent or context.parent.parent etc.
Finally I've added a helper method (along with a little test script) that provides a
helper method to find a varaible in any context, starting with the current context
traversing up the parent hierarchy. e.g.
<j:set var="foo" value='${context.findVariable("something")}'/>
Revision Changes Path
1.3 +2 -3
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/run_all.jelly
Index: run_all.jelly
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/run_all.jelly,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- run_all.jelly 28 May 2002 07:20:06 -0000 1.2
+++ run_all.jelly 30 May 2002 16:47:17 -0000 1.3
@@ -1,9 +1,8 @@
-<?xml version="1.0"?>
<j:jelly xmlns:j="jelly:core">
- <!-- try an absolute path -->
+<?xml version="1.0"?>
<j:jelly xmlns:j="jelly:core">
<!-- try an absolute path
-->
<j:include uri="/src/test/org/apache/commons/jelly/hello_world.jelly"/>
<!-- include other relative scripts... -->
<j:include uri="example2.jelly"/>
<j:include uri="jsl/example.jelly"/>
- <!-- loading all dynamic tags libraries -->
+ <!-- now lets try pass in a variable to a child script -->
<j:set
var="foo">bar</j:set>
<j:include uri="testFindVariable.jelly"/>
<!-- loading
all dynamic tags libraries -->
<j:include uri="define/babelfishTaglib.jelly"/>
<!-- now run a script using the new taglib -->
<j:include uri="define/example.jelly"/>
1.1
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/testFindVariable.jelly
Index: testFindVariable.jelly
===================================================================
<?xml version="1.0"?>
<j:jelly xmlns:j="jelly:core">
<j:if
test='${context.findVariable("foo") != "bar"}'>
<fail>The value of foo in a
parent scope does not equal to 'bar'</fail>
</j:if>
The value of foo is <j:expr
value='${context.findVariable("foo")}'/>
</j:jelly>
No revision
No revision
1.1
jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/test_args.jelly
Index: test_args.jelly
===================================================================
<?xml version="1.0"?>
<!-- displays the current command line arguments -->
<j:jelly xmlns:j="jelly:core">
<j:forEach var="arg" items="${args}"><j:expr
value="${arg}"/> </j:forEach>
</j:jelly>
1.8 +35 -5
jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java
Index: JellyContext.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/JellyContext.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- JellyContext.java 28 May 2002 10:28:36 -0000 1.7
+++ JellyContext.java 30 May 2002 16:47:17 -0000 1.8
@@ -95,6 +95,9 @@
/** The Log to which logging calls will be made. */
private Log log = LogFactory.getLog(JellyContext.class);
+ /** The parent context */
+ private JellyContext parent;
+
/**
* The class loader to use for instantiating application objects.
* If not specified, the context class loader, or the class loader
@@ -121,20 +124,47 @@
public JellyContext(URL rootURL, URL currentURL) {
this.rootURL = rootURL;
this.currentURL = currentURL;
+ this.variables.put("context", this);
}
- public JellyContext(JellyContext parentJellyContext) {
- this.rootURL = parentJellyContext.rootURL;
- this.currentURL = parentJellyContext.currentURL;
- this.taglibs = parentJellyContext.taglibs;
- this.variables.put("parentScope", parentJellyContext.variables);
+ public JellyContext(JellyContext parent) {
+ this.parent = parent;
+ this.rootURL = parent.rootURL;
+ this.currentURL = parent.currentURL;
+ this.taglibs = parent.taglibs;
+ this.variables.put("parentScope", parent.variables);
+ this.variables.put("context", this);
}
public JellyContext(JellyContext parentJellyContext, URL currentURL) {
this(parentJellyContext);
this.currentURL = currentURL;
}
+
+ /**
+ * @return the parent context for this context
+ */
+ public JellyContext getParent() {
+ return parent;
+ }
+ /**
+ * Finds the variable value of the given name in this context or in any other
parent context.
+ * If this context does not contain the variable, then its parent is used and
then its parent
+ * and so forth until the context with no parent is found.
+ *
+ * @return the value of the variable in this or one of its descendant contexts
or null
+ * if the variable could not be found.
+ */
+ public Object findVariable(String name) {
+ Object answer = variables.get(name);
+ if ( answer == null && parent != null ) {
+ return parent.findVariable(name);
+ }
+ return answer;
+ }
+
+
/** @return the value of the given variable name */
public Object getVariable(String name) {
return variables.get(name);
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>