View Javadoc

1   /*
2    * $Id: CompareTagBase.java 376842 2006-02-10 21:02:03Z 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.struts.taglib.logic;
19  
20  import org.apache.commons.beanutils.PropertyUtils;
21  import org.apache.struts.taglib.TagUtils;
22  import org.apache.struts.util.MessageResources;
23  
24  import javax.servlet.http.Cookie;
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  
28  import java.lang.reflect.InvocationTargetException;
29  
30  /***
31   * Abstract base class for comparison tags.  Concrete subclasses need only
32   * define values for desired1 and desired2.
33   *
34   * @version $Rev: 376842 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
35   *          $
36   */
37  public abstract class CompareTagBase extends ConditionalTagBase {
38      // ----------------------------------------------------- Instance Variables
39  
40      /***
41       * We will do a double/float comparison.
42       */
43      protected static final int DOUBLE_COMPARE = 0;
44  
45      /***
46       * We will do a long/int comparison.
47       */
48      protected static final int LONG_COMPARE = 1;
49  
50      /***
51       * We will do a String comparison.
52       */
53      protected static final int STRING_COMPARE = 2;
54  
55      /***
56       * The message resources for this package.
57       */
58      protected static MessageResources messages =
59          MessageResources.getMessageResources(
60              "org.apache.struts.taglib.logic.LocalStrings");
61  
62      // ------------------------------------------------------------ Properties
63  
64      /***
65       * The value to which the variable specified by other attributes of this
66       * tag will be compared.
67       */
68      public String value = null;
69  
70      public String getValue() {
71          return (this.value);
72      }
73  
74      public void setValue(String value) {
75          this.value = value;
76      }
77  
78      // --------------------------------------------------------- Public Methods
79  
80      /***
81       * Release all allocated resources.
82       */
83      public void release() {
84          super.release();
85          value = null;
86      }
87  
88      // ------------------------------------------------------ Protected Methods
89  
90      /***
91       * Evaluate the condition that is being tested by this particular tag, and
92       * return <code>true</code> if the nested body content of this tag should
93       * be evaluated, or <code>false</code> if it should be skipped. This
94       * method must be implemented by concrete subclasses.
95       *
96       * @throws JspException if a JSP exception occurs
97       */
98      protected abstract boolean condition()
99          throws JspException;
100 
101     /***
102      * Evaluate the condition that is being tested by this particular tag, and
103      * return <code>true</code> if the nested body content of this tag should
104      * be evaluated, or <code>false</code> if it should be skipped. This
105      * method must be implemented by concrete subclasses.
106      *
107      * @param desired1 First desired value for a true result (-1, 0, +1)
108      * @param desired2 Second desired value for a true result (-1, 0, +1)
109      * @throws JspException if a JSP exception occurs
110      */
111     protected boolean condition(int desired1, int desired2)
112         throws JspException {
113         // Acquire the value and determine the test type
114         int type = -1;
115         double doubleValue = 0.0;
116         long longValue = 0;
117 
118         if ((type < 0) && (value.length() > 0)) {
119             try {
120                 doubleValue = Double.parseDouble(value);
121                 type = DOUBLE_COMPARE;
122             } catch (NumberFormatException e) {
123                 ;
124             }
125         }
126 
127         if ((type < 0) && (value.length() > 0)) {
128             try {
129                 longValue = Long.parseLong(value);
130                 type = LONG_COMPARE;
131             } catch (NumberFormatException e) {
132                 ;
133             }
134         }
135 
136         if (type < 0) {
137             type = STRING_COMPARE;
138         }
139 
140         // Acquire the unconverted variable value
141         Object variable = null;
142 
143         if (cookie != null) {
144             Cookie[] cookies =
145                 ((HttpServletRequest) pageContext.getRequest()).getCookies();
146 
147             if (cookies == null) {
148                 cookies = new Cookie[0];
149             }
150 
151             for (int i = 0; i < cookies.length; i++) {
152                 if (cookie.equals(cookies[i].getName())) {
153                     variable = cookies[i].getValue();
154 
155                     break;
156                 }
157             }
158         } else if (header != null) {
159             variable =
160                 ((HttpServletRequest) pageContext.getRequest()).getHeader(header);
161         } else if (name != null) {
162             Object bean =
163                 TagUtils.getInstance().lookup(pageContext, name, scope);
164 
165             if (property != null) {
166                 if (bean == null) {
167                     JspException e =
168                         new JspException(messages.getMessage("logic.bean", name));
169 
170                     TagUtils.getInstance().saveException(pageContext, e);
171                     throw e;
172                 }
173 
174                 try {
175                     variable = PropertyUtils.getProperty(bean, property);
176                 } catch (InvocationTargetException e) {
177                     Throwable t = e.getTargetException();
178 
179                     if (t == null) {
180                         t = e;
181                     }
182 
183                     TagUtils.getInstance().saveException(pageContext, t);
184                     throw new JspException(messages.getMessage(
185                             "logic.property", name, property, t.toString()));
186                 } catch (Throwable t) {
187                     TagUtils.getInstance().saveException(pageContext, t);
188                     throw new JspException(messages.getMessage(
189                             "logic.property", name, property, t.toString()));
190                 }
191             } else {
192                 variable = bean;
193             }
194         } else if (parameter != null) {
195             variable = pageContext.getRequest().getParameter(parameter);
196         } else {
197             JspException e =
198                 new JspException(messages.getMessage("logic.selector"));
199 
200             TagUtils.getInstance().saveException(pageContext, e);
201             throw e;
202         }
203 
204         if (variable == null) {
205             variable = ""; // Coerce null to a zero-length String
206         }
207 
208         // Perform the appropriate comparison
209         int result = 0;
210 
211         if (type == DOUBLE_COMPARE) {
212             try {
213                 double doubleVariable = Double.parseDouble(variable.toString());
214 
215                 if (doubleVariable < doubleValue) {
216                     result = -1;
217                 } else if (doubleVariable > doubleValue) {
218                     result = +1;
219                 }
220             } catch (NumberFormatException e) {
221                 result = variable.toString().compareTo(value);
222             }
223         } else if (type == LONG_COMPARE) {
224             try {
225                 long longVariable = Long.parseLong(variable.toString());
226 
227                 if (longVariable < longValue) {
228                     result = -1;
229                 } else if (longVariable > longValue) {
230                     result = +1;
231                 }
232             } catch (NumberFormatException e) {
233                 result = variable.toString().compareTo(value);
234             }
235         } else {
236             result = variable.toString().compareTo(value);
237         }
238 
239         // Normalize the result
240         if (result < 0) {
241             result = -1;
242         } else if (result > 0) {
243             result = +1;
244         }
245 
246         // Return true if the result matches either desired value
247         return ((result == desired1) || (result == desired2));
248     }
249 }