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  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 &lt;forEach&gt; or &lg;while&gt;
    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      // Tag interface
    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  }