KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > strutsel > taglib > logic > ELMatchSupport


1 /*
2  * $Id: ELMatchSupport.java 54933 2004-10-16 17:04:52Z germuska $
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
19 package org.apache.strutsel.taglib.logic;
20
21 import org.apache.struts.util.MessageResources;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import org.apache.struts.taglib.TagUtils;
25
26 /**
27  * This class is used as a helper class for both the
28  * <code>org.apache.strutsel.taglib.logic.ELMatchTag</code> and
29  * <code>org.apache.strutsel.taglib.logic.ELNotMatchTag</code> classes. It's
30  * <code>condition</code> method encapsulates the common logic needed to
31  * examine the <code>location</code> attribute to determine how to do the
32  * comparison.
33  */

34 class ELMatchSupport {
35     /**
36      * Performs a comparison of an expression and a value, with an optional
37      * location specifier in the expression (start or end).
38      *
39      * @param desired Indication of whether the "truth" value of the comparison
40      * is whether the expression and value are 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 the
44      * start or end of the expression for the value. If null, look anywhere in
45      * the expression.
46      * @param messages <code>MessageResources</code> object to reference for
47      * 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,
53                                     String JavaDoc expr,
54                                     String JavaDoc value,
55                                     String JavaDoc location,
56                                     MessageResources messages,
57                                     PageContext JavaDoc pageContext)
58         throws JspException JavaDoc
59     {
60         boolean result = false;
61
62         if (expr != null) {
63             // Perform the comparison requested by the location attribute
64
boolean matched = false;
65             if (location == null) {
66                 matched = (expr.indexOf(value) >= 0);
67             } else if (location.equals("start")) {
68                 matched = expr.startsWith(value);
69             } else if (location.equals("end")) {
70                 matched = expr.endsWith(value);
71             } else {
72                 JspException JavaDoc e = new JspException JavaDoc
73                     (messages.getMessage("logic.location", location));
74                 TagUtils.getInstance().saveException(pageContext, e);
75                 throw e;
76             }
77
78             result = (matched == desired);
79         }
80
81         return (result);
82     }
83 }
84
Popular Tags