ELMatchSupport xref

View Javadoc

    1   /*
    2    * $Id: ELMatchSupport.java 471754 2006-11-06 14:55:09Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.strutsel.taglib.logic;
    22
    23  import org.apache.struts.taglib.TagUtils;
    24  import org.apache.struts.util.MessageResources;
    25
    26  import javax.servlet.jsp.JspException;
    27  import javax.servlet.jsp.PageContext;
    28
    29  /**
    30   * This class is used as a helper class for both the <code>org.apache.strutsel.taglib.logic.ELMatchTag</code>
    31   * and <code>org.apache.strutsel.taglib.logic.ELNotMatchTag</code> classes.
    32   * It's <code>condition</code> method encapsulates the common logic needed to
    33   * examine the <code>location</code> attribute to determine how to do the
    34   * comparison.
    35   */
    36  class ELMatchSupport {
    37      /**
    38       * Performs a comparison of an expression and a value, with an optional
    39       * location specifier in the expression (start or end).
    40       *
    41       * @param desired     Indication of whether the "truth" value of the
    42       *                    comparison is whether the expression and value are
    43       *                    equal, or not equal.
    44       * @param expr        Expression to test against a value.
    45       * @param value       Value to test against an expression.
    46       * @param location    if set, is "start" or "end" to indicate to look at
    47       *                    the start or end of the expression for the value.
    48       *                    If null, look anywhere in the expression.
    49       * @param messages    <code>MessageResources</code> object to reference
    50       *                    for error message text.
    51       * @param pageContext used to save exception information, if needed.
    52       * @return true if comparison result equals desired value, false
    53       *         otherwise.
    54       */
    55      public static boolean condition(boolean desired, String expr, String value,
    56          String location, MessageResources messages, PageContext pageContext)
    57          throws JspException {
    58          boolean result = false;
    59
    60          if (expr != null) {
    61              // Perform the comparison requested by the location attribute
    62              boolean matched = false;
    63
    64              if (location == null) {
    65                  matched = (expr.indexOf(value) >= 0);
    66              } else if (location.equals("start")) {
    67                  matched = expr.startsWith(value);
    68              } else if (location.equals("end")) {
    69                  matched = expr.endsWith(value);
    70              } else {
    71                  JspException e =
    72                      new JspException(messages.getMessage("logic.location",
    73                              location));
    74
    75                  TagUtils.getInstance().saveException(pageContext, e);
    76                  throw e;
    77              }
    78
    79              result = (matched == desired);
    80          }
    81
    82          return (result);
    83      }
    84  }