1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.core;
17
18 import org.apache.commons.jelly.TagSupport;
19 import org.apache.commons.jelly.XMLOutput;
20 import org.apache.commons.jelly.impl.BreakException;
21 import org.apache.commons.jelly.expression.Expression;
22
23 /***
24 * A tag which terminates the execution of the current <forEach> or ≶while>
25 * loop. This tag can take an optional boolean test attribute which if its true
26 * then the break occurs otherwise the loop continues processing.
27 *
28 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
29 * @version $Revision: 155420 $
30 */
31 public class BreakTag extends TagSupport {
32
33 /*** The expression to evaluate. */
34 private Expression test;
35
36 /***
37 * If specified, the given variable will hold a true/false value
38 * indicating if the loop was broken.
39 */
40 private String var;
41
42 public BreakTag() {
43 }
44
45
46
47 public void doTag(XMLOutput output) throws BreakException {
48 boolean broken = false;
49 if (test == null || test.evaluateAsBoolean(context)) {
50 broken = true;
51 }
52 if ( var != null ) {
53 context.setVariable( this.var, String.valueOf(broken));
54 }
55 if ( broken ) {
56 throw new BreakException();
57 }
58 }
59
60 /***
61 * Sets the Jelly expression to evaluate (optional).
62 * If this is <code>null</code> or evaluates to
63 * <code>true</code> then the loop is terminated
64 *
65 * @param test the Jelly expression to evaluate
66 */
67 public void setTest(Expression test) {
68 this.test = test;
69 }
70
71 /***
72 * Sets the variable name to export indicating if the item was broken
73 * @param var name of the variable to be exported
74 */
75 public void setVar(String var) {
76 this.var = var;
77 }
78
79 }