01: /*
02: * Copyright 2002,2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
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)
56: throws MissingAttributeException, JellyTagException {
57: if (test != null) {
58: try {
59: while (test.evaluateAsBoolean(getContext())) {
60: if (log.isDebugEnabled()) {
61: log
62: .debug("evaluated to true! gonna keep on chuggin!");
63: }
64: invokeBody(output);
65: }
66: } catch (BreakException e) {
67: if (log.isDebugEnabled()) {
68: log.debug("loop terminated by break: " + e, e);
69: }
70: }
71: } else {
72: throw new MissingAttributeException("test");
73: }
74: }
75:
76: // Properties
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: }
|