View Javadoc

    1   /*
    2    * Copyright 2002,2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    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) 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      // 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  }
    87