KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > taglib > IfTag


1 /*
2  * $Id: IfTag.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2002-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.taglib;
26
27 import java.io.IOException JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29 import java.util.Collection JavaDoc;
30 import javax.servlet.jsp.JspTagException JavaDoc;
31 import javax.servlet.jsp.JspWriter JavaDoc;
32 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
33 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
34
35 import org.ofbiz.base.util.Debug;
36
37 /**
38  * IfTag - Conditional Tag.
39  *
40  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
41  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
42  * @version 1.0
43  * @created August 31, 2001
44  */

45 public class IfTag extends BodyTagSupport JavaDoc {
46     
47     public static final String JavaDoc module = IfTag.class.getName();
48
49     private String JavaDoc name = null;
50     private String JavaDoc value = null;
51     private String JavaDoc type = null;
52     private Integer JavaDoc size = null;
53
54     public void setName(String JavaDoc name) {
55         this.name = name;
56     }
57
58     public void setValue(String JavaDoc value) {
59         this.value = value;
60     }
61
62     public void setType(String JavaDoc type) {
63         this.type = type;
64     }
65
66     public void setSize(String JavaDoc size) throws NumberFormatException JavaDoc {
67         this.size = Integer.valueOf(size);
68     }
69
70     public String JavaDoc getName() {
71         return name;
72     }
73
74     public String JavaDoc getValue() {
75         return value;
76     }
77
78     public String JavaDoc getType() {
79         return type;
80     }
81
82     public String JavaDoc getSize() {
83         if (size == null)
84             return null;
85         return size.toString();
86     }
87
88     public int doStartTag() throws JspTagException JavaDoc {
89         Object JavaDoc object = null;
90
91         try {
92             object = pageContext.findAttribute(name);
93             if (object == null) {
94                 object = pageContext.getRequest().getParameter(name);
95             }
96             if (object == null) {
97                 return SKIP_BODY;
98             }
99         } catch (RuntimeException JavaDoc e) {
100             Debug.logError(e, module);
101             return SKIP_BODY;
102         }
103
104         if (size != null) {
105             int localSize = size.intValue();
106
107             try {
108                 if (object instanceof Collection JavaDoc) {
109                     // the object is a Collection so compare the size.
110
if (((Collection JavaDoc) object).size() > localSize)
111                         return EVAL_BODY_AGAIN;
112                 } else if (object instanceof String JavaDoc) {
113                     // the object is a Collection so compare the size.
114
if (((String JavaDoc) object).length() > localSize)
115                         return EVAL_BODY_AGAIN;
116                 } else {
117                     // use reflection to find a size() method
118
try {
119                         Method JavaDoc sizeMethod = object.getClass().getMethod("size", (Class JavaDoc[]) null);
120                         int objectSize = ((Integer JavaDoc) sizeMethod.invoke(object, (Object JavaDoc[]) null)).intValue();
121
122                         if (objectSize > localSize)
123                             return EVAL_BODY_AGAIN;
124                     } catch (Exception JavaDoc e) {
125                         Debug.logError(e, module);
126                         return SKIP_BODY;
127                     }
128                 }
129             } catch (RuntimeException JavaDoc e) {
130                 Debug.logError(e, module);
131                 return SKIP_BODY;
132             }
133         } else if (object instanceof Boolean JavaDoc || "Boolean".equalsIgnoreCase(type)) {
134             // Assume the object is a Boolean and compare to the Boolean value of value.
135
try {
136                 Boolean JavaDoc b = (Boolean JavaDoc) object;
137
138                 if (value != null) {
139                     Boolean JavaDoc v = new Boolean JavaDoc(value);
140
141                     if (b.equals(v))
142                         return EVAL_BODY_AGAIN;
143                 } else {
144                     if (b.booleanValue())
145                         return EVAL_BODY_AGAIN;
146                 }
147             } catch (RuntimeException JavaDoc e) {
148                 Debug.logError(e, module);
149                 return SKIP_BODY;
150             }
151         } else if (value != null) {
152             if (object instanceof String JavaDoc || "String".equalsIgnoreCase(type)) {
153                 // Assume the object is a string and compare to the String value of value.
154
try {
155                     String JavaDoc s = (String JavaDoc) object;
156
157                     if (s.equals(value))
158                         return EVAL_BODY_AGAIN;
159                 } catch (RuntimeException JavaDoc e) {
160                     Debug.logError(e, module);
161                     return SKIP_BODY;
162                 }
163             } else if (object instanceof Integer JavaDoc || "Integer".equalsIgnoreCase(type)) {
164                 // Assume the object is a Integer and compare to the Integer value of value.
165
try {
166                     Integer JavaDoc i = (Integer JavaDoc) object;
167                     Integer JavaDoc v = Integer.valueOf(value);
168
169                     if (i.equals(v))
170                         return EVAL_BODY_AGAIN;
171                 } catch (RuntimeException JavaDoc e) {
172                     Debug.logError(e, module);
173                     return SKIP_BODY;
174                 }
175             } else if (object instanceof Long JavaDoc || "Long".equalsIgnoreCase(type)) {
176                 // Assume the object is a Integer and compare to the Integer value of value.
177
try {
178                     Long JavaDoc i = (Long JavaDoc) object;
179                     Long JavaDoc v = Long.valueOf(value);
180
181                     if (i.equals(v))
182                         return EVAL_BODY_AGAIN;
183                 } catch (RuntimeException JavaDoc e) {
184                     Debug.logError(e, module);
185                     return SKIP_BODY;
186                 }
187             } else if (object instanceof Float JavaDoc || "Float".equalsIgnoreCase(type)) {
188                 // Assume the object is a Double and compare to the Double value of value.
189
try {
190                     Float JavaDoc d = (Float JavaDoc) object;
191                     Float JavaDoc v = Float.valueOf(value);
192
193                     if (d.equals(v))
194                         return EVAL_BODY_AGAIN;
195                 } catch (RuntimeException JavaDoc e) {
196                     Debug.logError(e, module);
197                     return SKIP_BODY;
198                 }
199             } else if (object instanceof Double JavaDoc || "Double".equalsIgnoreCase(type)) {
200                 // Assume the object is a Double and compare to the Double value of value.
201
try {
202                     Double JavaDoc d = (Double JavaDoc) object;
203                     Double JavaDoc v = Double.valueOf(value);
204
205                     if (d.equals(v))
206                         return EVAL_BODY_AGAIN;
207                 } catch (RuntimeException JavaDoc e) {
208                     Debug.logError(e, module);
209                     return SKIP_BODY;
210                 }
211             } else {
212                 // Assume the object is an Object and compare to the Object named value.
213
Object JavaDoc valueObject = null;
214
215                 try {
216                     valueObject = pageContext.findAttribute(value);
217                     if (valueObject != null && valueObject.equals(object))
218                         return EVAL_BODY_AGAIN;
219                 } catch (RuntimeException JavaDoc e) {
220                     Debug.logError(e, module);
221                     return SKIP_BODY;
222                 }
223             }
224         } else {
225             // basicly if no other comparisons available, just check to see if
226
// the thing is null or not, and since we've already checked that,
227
// treat as true here
228
return EVAL_BODY_AGAIN;
229         }
230
231         return SKIP_BODY;
232     }
233
234     public int doAfterBody() {
235         return SKIP_BODY;
236     }
237
238     public int doEndTag() {
239         try {
240             BodyContent JavaDoc body = getBodyContent();
241
242             if (body != null) {
243                 JspWriter JavaDoc out = body.getEnclosingWriter();
244                 String JavaDoc bodyString = body.getString();
245                 body.clearBody();
246                 //Debug.logInfo("printing string: " + bodyString, module);
247
out.print(bodyString);
248             }
249         } catch (IOException JavaDoc e) {
250             Debug.logError(e, "IfTag Error.", module);
251         }
252         return EVAL_PAGE;
253     }
254 }
255
256
Popular Tags