001: /*
002: * $Id: CompareTagBase.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.logic;
022:
023: import org.apache.commons.beanutils.PropertyUtils;
024: import org.apache.struts.taglib.TagUtils;
025: import org.apache.struts.util.MessageResources;
026:
027: import javax.servlet.http.Cookie;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.jsp.JspException;
030:
031: import java.lang.reflect.InvocationTargetException;
032:
033: /**
034: * Abstract base class for comparison tags. Concrete subclasses need only
035: * define values for desired1 and desired2.
036: *
037: * @version $Rev: 471754 $ $Date: 2004-10-16 12:38:42 -0400 (Sat, 16 Oct 2004)
038: * $
039: */
040: public abstract class CompareTagBase extends ConditionalTagBase {
041: // ----------------------------------------------------- Instance Variables
042:
043: /**
044: * We will do a double/float comparison.
045: */
046: protected static final int DOUBLE_COMPARE = 0;
047:
048: /**
049: * We will do a long/int comparison.
050: */
051: protected static final int LONG_COMPARE = 1;
052:
053: /**
054: * We will do a String comparison.
055: */
056: protected static final int STRING_COMPARE = 2;
057:
058: /**
059: * The message resources for this package.
060: */
061: protected static MessageResources messages = MessageResources
062: .getMessageResources("org.apache.struts.taglib.logic.LocalStrings");
063:
064: // ------------------------------------------------------------ Properties
065:
066: /**
067: * The value to which the variable specified by other attributes of this
068: * tag will be compared.
069: */
070: public String value = null;
071:
072: public String getValue() {
073: return (this .value);
074: }
075:
076: public void setValue(String value) {
077: this .value = value;
078: }
079:
080: // --------------------------------------------------------- Public Methods
081:
082: /**
083: * Release all allocated resources.
084: */
085: public void release() {
086: super .release();
087: value = null;
088: }
089:
090: // ------------------------------------------------------ Protected Methods
091:
092: /**
093: * Evaluate the condition that is being tested by this particular tag, and
094: * return <code>true</code> if the nested body content of this tag should
095: * be evaluated, or <code>false</code> if it should be skipped. This
096: * method must be implemented by concrete subclasses.
097: *
098: * @throws JspException if a JSP exception occurs
099: */
100: protected abstract boolean condition() throws JspException;
101:
102: /**
103: * Evaluate the condition that is being tested by this particular tag, and
104: * return <code>true</code> if the nested body content of this tag should
105: * be evaluated, or <code>false</code> if it should be skipped. This
106: * method must be implemented by concrete subclasses.
107: *
108: * @param desired1 First desired value for a true result (-1, 0, +1)
109: * @param desired2 Second desired value for a true result (-1, 0, +1)
110: * @throws JspException if a JSP exception occurs
111: */
112: protected boolean condition(int desired1, int desired2)
113: throws JspException {
114: // Acquire the value and determine the test type
115: int type = -1;
116: double doubleValue = 0.0;
117: long longValue = 0;
118:
119: if ((type < 0) && (value.length() > 0)) {
120: try {
121: doubleValue = Double.parseDouble(value);
122: type = DOUBLE_COMPARE;
123: } catch (NumberFormatException e) {
124: ;
125: }
126: }
127:
128: if ((type < 0) && (value.length() > 0)) {
129: try {
130: longValue = Long.parseLong(value);
131: type = LONG_COMPARE;
132: } catch (NumberFormatException e) {
133: ;
134: }
135: }
136:
137: if (type < 0) {
138: type = STRING_COMPARE;
139: }
140:
141: // Acquire the unconverted variable value
142: Object variable = null;
143:
144: if (cookie != null) {
145: Cookie[] cookies = ((HttpServletRequest) pageContext
146: .getRequest()).getCookies();
147:
148: if (cookies == null) {
149: cookies = new Cookie[0];
150: }
151:
152: for (int i = 0; i < cookies.length; i++) {
153: if (cookie.equals(cookies[i].getName())) {
154: variable = cookies[i].getValue();
155:
156: break;
157: }
158: }
159: } else if (header != null) {
160: variable = ((HttpServletRequest) pageContext.getRequest())
161: .getHeader(header);
162: } else if (name != null) {
163: Object bean = TagUtils.getInstance().lookup(pageContext,
164: name, scope);
165:
166: if (property != null) {
167: if (bean == null) {
168: JspException e = new JspException(messages
169: .getMessage("logic.bean", name));
170:
171: TagUtils.getInstance()
172: .saveException(pageContext, e);
173: throw e;
174: }
175:
176: try {
177: variable = PropertyUtils
178: .getProperty(bean, property);
179: } catch (InvocationTargetException e) {
180: Throwable t = e.getTargetException();
181:
182: if (t == null) {
183: t = e;
184: }
185:
186: TagUtils.getInstance()
187: .saveException(pageContext, t);
188: throw new JspException(messages.getMessage(
189: "logic.property", name, property, t
190: .toString()));
191: } catch (Throwable t) {
192: TagUtils.getInstance()
193: .saveException(pageContext, t);
194: throw new JspException(messages.getMessage(
195: "logic.property", name, property, t
196: .toString()));
197: }
198: } else {
199: variable = bean;
200: }
201: } else if (parameter != null) {
202: variable = pageContext.getRequest().getParameter(parameter);
203: } else {
204: JspException e = new JspException(messages
205: .getMessage("logic.selector"));
206:
207: TagUtils.getInstance().saveException(pageContext, e);
208: throw e;
209: }
210:
211: if (variable == null) {
212: variable = ""; // Coerce null to a zero-length String
213: }
214:
215: // Perform the appropriate comparison
216: int result = 0;
217:
218: if (type == DOUBLE_COMPARE) {
219: try {
220: double doubleVariable = Double.parseDouble(variable
221: .toString());
222:
223: if (doubleVariable < doubleValue) {
224: result = -1;
225: } else if (doubleVariable > doubleValue) {
226: result = +1;
227: }
228: } catch (NumberFormatException e) {
229: result = variable.toString().compareTo(value);
230: }
231: } else if (type == LONG_COMPARE) {
232: try {
233: long longVariable = Long.parseLong(variable.toString());
234:
235: if (longVariable < longValue) {
236: result = -1;
237: } else if (longVariable > longValue) {
238: result = +1;
239: }
240: } catch (NumberFormatException e) {
241: result = variable.toString().compareTo(value);
242: }
243: } else {
244: result = variable.toString().compareTo(value);
245: }
246:
247: // Normalize the result
248: if (result < 0) {
249: result = -1;
250: } else if (result > 0) {
251: result = +1;
252: }
253:
254: // Return true if the result matches either desired value
255: return ((result == desired1) || (result == desired2));
256: }
257: }
|