1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jelly.tags.core;
18
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.MissingAttributeException;
21 import org.apache.commons.jelly.TagSupport;
22 import org.apache.commons.jelly.XMLOutput;
23 import org.apache.commons.jelly.expression.Expression;
24 import org.apache.commons.jelly.impl.BreakException;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /***
29 * A tag which performs an iteration while the result of an expression is true.
30 *
31 * @author <a href="mailto:eric@ericalexander.net">Eric Alexander</a>
32 * @author dIon Gillard
33 * @version $Revision: 155420 $
34 */
35 public class WhileTag extends TagSupport {
36
37 /*** The Log to which logging calls will be made. */
38 private static final Log log = LogFactory.getLog(WhileTag.class);
39
40 /*** The expression to use to determine if the while should continue */
41 private Expression test;
42
43 /***
44 * Create a new while tag
45 */
46 public WhileTag() {
47 }
48
49 /***
50 * Tag interface
51 * @param output destination for xml output
52 * @throws MissingAttributeException when the test attribute is missing
53 * @throws Exception for anything else
54 */
55 public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
56 if (test != null) {
57 try {
58 while (test.evaluateAsBoolean(getContext())) {
59 if (log.isDebugEnabled()) {
60 log.debug("evaluated to true! gonna keep on chuggin!");
61 }
62 invokeBody(output);
63 }
64 }
65 catch (BreakException e) {
66 if (log.isDebugEnabled()) {
67 log.debug("loop terminated by break: " + e, e);
68 }
69 }
70 }
71 else {
72 throw new MissingAttributeException("test");
73 }
74 }
75
76
77
78
79 /***
80 * Setter for the expression
81 * @param e the expression to test
82 */
83 public void setTest(Expression e) {
84 this.test = e;
85 }
86 }
87