You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by di...@apache.org on 2004/08/12 06:58:04 UTC
cvs commit: jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core BreakTag.java
dion 2004/08/11 21:58:04
Modified: jelly/src/java/org/apache/commons/jelly/tags/core
BreakTag.java
Added: jelly/src/test/org/apache/commons/jelly/core
testBreakTag.jelly TestBreakTag.java
Log:
Apply JELLY-115
Revision Changes Path
1.1 jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/testBreakTag.jelly
Index: testBreakTag.jelly
===================================================================
<!--
Copyright 2002,2004 The Apache Software Foundation.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<j:jelly xmlns:j="jelly:core">
<j:set var="simpleResult" value=""/>
<j:forEach var="counter" begin="1" end="10">
<j:set var="simpleResult" value="${simpleResult}${counter}"/>
<j:if test="${counter==5}">
<j:break/>
</j:if>
</j:forEach>
<j:set var="conditionalResult" value=""/>
<j:forEach var="counter" begin="1" end="10">
<j:set var="conditionalResult" value="${conditionalResult}${counter}"/>
<j:break test="${counter==5}"/>
</j:forEach>
<j:set var="varBreaksResult" value=""/>
<j:forEach var="counter" begin="1" end="10">
<j:set var="varBreaksResult" value="${varBreaksResult}${counter}"/>
<j:break var="varBroken" test="${counter==5}"/>
</j:forEach>
<j:set var="varNoBreaksResult" value=""/>
<j:forEach var="counter" begin="1" end="10">
<j:set var="varNoBreaksResult" value="${varNoBreaksResult}${counter}"/>
<j:break var="varNotBroken" test="${counter==11}"/>
</j:forEach>
</j:jelly>
1.1 jakarta-commons/jelly/src/test/org/apache/commons/jelly/core/TestBreakTag.java
Index: TestBreakTag.java
===================================================================
/*
* Copyright 2002,2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jelly.core;
import junit.framework.TestSuite;
import org.apache.commons.jelly.Script;
public class TestBreakTag extends BaseJellyTest
{
public TestBreakTag(String name)
{
super(name);
}
public static TestSuite suite() throws Exception
{
return new TestSuite(TestBreakTag.class);
}
public void testSimpleBreakTag() throws Exception
{
setUpScript("testBreakTag.jelly");
Script script = getJelly().compileScript();
script.run(getJellyContext(), getXMLOutput());
String simpleResult = (String) getJellyContext().getVariable("simpleResult");
assertEquals("simpleResult", "12345", simpleResult);
}
public void testConditionalBreakTag() throws Exception
{
setUpScript("testBreakTag.jelly");
Script script = getJelly().compileScript();
script.run(getJellyContext(), getXMLOutput());
String simpleResult = (String) getJellyContext().getVariable("conditionalResult");
assertEquals("conditionalResult", "12345", simpleResult);
}
public void testVarBreakTag() throws Exception
{
setUpScript("testBreakTag.jelly");
Script script = getJelly().compileScript();
script.run(getJellyContext(), getXMLOutput());
String varBroken = (String) getJellyContext().getVariable("varBroken");
assertEquals("varBroken", "true", varBroken);
}
public void testVarNoBreakTag() throws Exception
{
setUpScript("testBreakTag.jelly");
Script script = getJelly().compileScript();
script.run(getJellyContext(), getXMLOutput());
String varNotBroken = (String) getJellyContext().getVariable("varNotBroken");
assertEquals("varNotBroken", "false", varNotBroken);
}
}
1.8 +23 -1 jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/BreakTag.java
Index: BreakTag.java
===================================================================
RCS file: /home/cvs/jakarta-commons/jelly/src/java/org/apache/commons/jelly/tags/core/BreakTag.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- BreakTag.java 24 Feb 2004 14:10:38 -0000 1.7
+++ BreakTag.java 12 Aug 2004 04:58:04 -0000 1.8
@@ -33,14 +33,27 @@
/** The expression to evaluate. */
private Expression test;
+ /**
+ * If specified, the given variable will hold a true/false value
+ * indicating if the loop was broken.
+ */
+ private String var;
+
public BreakTag() {
}
// Tag interface
//-------------------------------------------------------------------------
public void doTag(XMLOutput output) throws BreakException {
+ boolean broken = false;
if (test == null || test.evaluateAsBoolean(context)) {
- throw new BreakException();
+ broken = true;
+ }
+ if ( var != null ) {
+ context.setVariable( this.var, String.valueOf(broken));
+ }
+ if ( broken ) {
+ throw new BreakException();
}
}
@@ -54,4 +67,13 @@
public void setTest(Expression test) {
this.test = test;
}
+
+ /**
+ * Sets the variable name to export indicating if the item was broken
+ * @param var name of the variable to be exported
+ */
+ public void setVar(String var) {
+ this.var = var;
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org