View Javadoc

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