KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > logic > CompareTagBase


1 /*
2  * $Id: CompareTagBase.java 54929 2004-10-16 16:38:42Z 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
20 package org.apache.struts.taglib.logic;
21
22
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import javax.servlet.http.Cookie JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import org.apache.commons.beanutils.PropertyUtils;
28 import org.apache.struts.util.MessageResources;
29 import org.apache.struts.taglib.TagUtils;
30
31
32 /**
33  * Abstract base class for comparison tags. Concrete subclasses need only
34  * define values for desired1 and desired2.
35  *
36  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
37  */

38
39 public abstract class CompareTagBase extends ConditionalTagBase {
40
41
42     // ----------------------------------------------------- Instance Variables
43

44
45     /**
46      * We will do a double/float comparison.
47      */

48     protected static final int DOUBLE_COMPARE = 0;
49
50
51     /**
52      * We will do a long/int comparison.
53      */

54     protected static final int LONG_COMPARE = 1;
55
56
57     /**
58      * We will do a String comparison.
59      */

60     protected static final int STRING_COMPARE = 2;
61
62
63     /**
64      * The message resources for this package.
65      */

66     protected static MessageResources messages =
67      MessageResources.getMessageResources
68         ("org.apache.struts.taglib.logic.LocalStrings");
69
70
71     // ------------------------------------------------------------ Properties
72

73
74     /**
75      * The value to which the variable specified by other attributes of this
76      * tag will be compared.
77      */

78     public String JavaDoc value = null;
79
80     public String JavaDoc getValue() {
81         return (this.value);
82     }
83
84     public void setValue(String JavaDoc value) {
85         this.value = value;
86     }
87
88
89     // --------------------------------------------------------- Public Methods
90

91
92     /**
93      * Release all allocated resources.
94      */

95     public void release() {
96
97         super.release();
98         value = null;
99
100     }
101
102
103     // ------------------------------------------------------ Protected Methods
104

105
106     /**
107      * Evaluate the condition that is being tested by this particular tag,
108      * and return <code>true</code> if the nested body content of this tag
109      * should be evaluated, or <code>false</code> if it should be skipped.
110      * This method must be implemented by concrete subclasses.
111      *
112      * @exception JspException if a JSP exception occurs
113      */

114     protected abstract boolean condition() throws JspException JavaDoc;
115
116
117     /**
118      * Evaluate the condition that is being tested by this particular tag,
119      * and return <code>true</code> if the nested body content of this tag
120      * should be evaluated, or <code>false</code> if it should be skipped.
121      * This method must be implemented by concrete subclasses.
122      *
123      * @param desired1 First desired value for a true result (-1, 0, +1)
124      * @param desired2 Second desired value for a true result (-1, 0, +1)
125      *
126      * @exception JspException if a JSP exception occurs
127      */

128     protected boolean condition(int desired1, int desired2)
129         throws JspException JavaDoc {
130
131         // Acquire the value and determine the test type
132
int type = -1;
133         double doubleValue = 0.0;
134         long longValue = 0;
135         if ((type < 0) && (value.length() > 0)) {
136             try {
137                 doubleValue = Double.parseDouble(value);
138                 type = DOUBLE_COMPARE;
139             } catch (NumberFormatException JavaDoc e) {
140                 ;
141             }
142         }
143         if ((type < 0) && (value.length() > 0)) {
144             try {
145                 longValue = Long.parseLong(value);
146                 type = LONG_COMPARE;
147             } catch (NumberFormatException JavaDoc e) {
148                 ;
149             }
150         }
151         if (type < 0) {
152             type = STRING_COMPARE;
153         }
154
155         // Acquire the unconverted variable value
156
Object JavaDoc variable = null;
157         if (cookie != null) {
158             Cookie JavaDoc cookies[] =
159                 ((HttpServletRequest JavaDoc) pageContext.getRequest()).
160                 getCookies();
161             if (cookies == null)
162                 cookies = new Cookie JavaDoc[0];
163             for (int i = 0; i < cookies.length; i++) {
164                 if (cookie.equals(cookies[i].getName())) {
165                     variable = cookies[i].getValue();
166                     break;
167                 }
168             }
169         } else if (header != null) {
170             variable =
171                 ((HttpServletRequest JavaDoc) pageContext.getRequest()).
172                 getHeader(header);
173         } else if (name != null) {
174             Object JavaDoc bean = TagUtils.getInstance().lookup(pageContext, name, scope);
175             if (property != null) {
176                 if (bean == null) {
177                     JspException JavaDoc e = new JspException JavaDoc
178                         (messages.getMessage("logic.bean", name));
179                     TagUtils.getInstance().saveException(pageContext, e);
180                     throw e;
181                 }
182                 try {
183                     variable = PropertyUtils.getProperty(bean, property);
184                 } catch (InvocationTargetException JavaDoc e) {
185                     Throwable JavaDoc t = e.getTargetException();
186                     if (t == null)
187                         t = e;
188                     TagUtils.getInstance().saveException(pageContext, t);
189                     throw new JspException JavaDoc
190                         (messages.getMessage("logic.property", name, property,
191                                              t.toString()));
192                 } catch (Throwable JavaDoc t) {
193                     TagUtils.getInstance().saveException(pageContext, t);
194                     throw new JspException JavaDoc
195                         (messages.getMessage("logic.property", name, property,
196                                              t.toString()));
197                 }
198             } else {
199                 variable = bean;
200             }
201         } else if (parameter != null) {
202             variable =
203                 pageContext.getRequest().getParameter(parameter);
204         } else {
205             JspException JavaDoc e = new JspException JavaDoc
206                 (messages.getMessage("logic.selector"));
207             TagUtils.getInstance().saveException(pageContext, e);
208             throw e;
209         }
210         if (variable == null) {
211             variable = ""; // Coerce null to a zero-length String
212
}
213
214         // Perform the appropriate comparison
215
int result = 0;
216         if (type == DOUBLE_COMPARE) {
217             try {
218                 double doubleVariable =
219                     Double.parseDouble(variable.toString());
220                 if (doubleVariable < doubleValue)
221                     result = -1;
222                 else if (doubleVariable > doubleValue)
223                     result = +1;
224             } catch (NumberFormatException JavaDoc e) {
225                 result = variable.toString().compareTo(value);
226             }
227         } else if (type == LONG_COMPARE) {
228             try {
229                 long longVariable = Long.parseLong(variable.toString());
230                 if (longVariable < longValue)
231                     result = -1;
232                 else if (longVariable > longValue)
233                     result = +1;
234             } catch (NumberFormatException JavaDoc e) {
235                 result = variable.toString().compareTo(value);
236             }
237         } else {
238             result = variable.toString().compareTo(value);
239         }
240
241         // Normalize the result
242
if (result < 0)
243             result = -1;
244         else if (result > 0)
245             result = +1;
246
247         // Return true if the result matches either desired value
248
return ((result == desired1) || (result == desired2));
249
250     }
251
252
253 }
254
Popular Tags