KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: IteratorTag.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.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import javax.servlet.jsp.JspTagException JavaDoc;
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import javax.servlet.jsp.PageContext JavaDoc;
37 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
38 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
39
40 import org.ofbiz.base.util.Debug;
41 import org.ofbiz.base.util.ObjectType;
42
43
44 /**
45  * IterateTag - JSP Tag to iterate over a Collection or any object with an iterator() method.
46  *
47  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
48  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
49  * @version 1.0
50  * @created August 4, 2001
51  */

52 public class IteratorTag extends BodyTagSupport JavaDoc {
53
54     public static final String JavaDoc module = IteratorTag.class.getName();
55
56     protected Iterator JavaDoc iterator = null;
57     protected String JavaDoc name = null;
58     protected String JavaDoc property = null;
59     protected Object JavaDoc element = null;
60     protected Class JavaDoc type = null;
61     protected int limit = 0;
62     protected int offset = 0;
63     protected boolean expandMap = false;
64
65     public void setName(String JavaDoc name) {
66         this.name = name;
67     }
68
69     public void setProperty(String JavaDoc property) {
70         this.property = property;
71     }
72
73     public void setType(String JavaDoc type) throws ClassNotFoundException JavaDoc {
74         this.type = ObjectType.loadClass(type);
75     }
76
77     public void setLimit(int limit) {
78         this.limit = limit;
79     }
80
81     public void setOffset(int offset) {
82         this.offset = offset;
83     }
84
85     public void setExpandMap(String JavaDoc expMap) {
86         // defaults to false, so if anything but true will be false:
87
expandMap = "true".equals(expMap);
88     }
89
90     public void setIterator(Iterator JavaDoc iterator) {
91         this.iterator = iterator;
92     }
93
94     public String JavaDoc getName() {
95         return name;
96     }
97
98     public String JavaDoc getProperty() {
99         return property;
100     }
101
102     public Object JavaDoc getElement() {
103         return element;
104     }
105
106     public Iterator JavaDoc getIterator() {
107         return this.iterator;
108     }
109
110     public String JavaDoc getType() {
111         return type.getName();
112     }
113
114     public int getLimit() {
115         return this.limit;
116     }
117
118     public int getOffset() {
119         return this.offset;
120     }
121
122     public String JavaDoc getExpandMap() {
123         return expandMap ? "true" : "false";
124     }
125
126     public int doStartTag() throws JspTagException JavaDoc {
127         Debug.logVerbose("Starting Iterator Tag...", module);
128
129         if (!defineIterator())
130             return SKIP_BODY;
131
132         Debug.logVerbose("We now have an iterator.", module);
133
134         if (defineElement())
135             return EVAL_BODY_AGAIN;
136         else
137             return SKIP_BODY;
138     }
139
140     public int doAfterBody() {
141         if (defineElement()) {
142             return EVAL_BODY_AGAIN;
143         } else {
144             return SKIP_BODY;
145         }
146     }
147
148     public int doEndTag() {
149         try {
150             BodyContent JavaDoc body = getBodyContent();
151
152             if (body != null) {
153                 JspWriter JavaDoc out = body.getEnclosingWriter();
154                 String JavaDoc bodyString = body.getString();
155                 body.clearBody();
156                 out.print(bodyString);
157             }
158         } catch (IOException JavaDoc e) {
159             Debug.logInfo("IteratorTag IO Error", module);
160             Debug.logInfo(e, module);
161         }
162         return EVAL_PAGE;
163     }
164
165     private boolean defineIterator() {
166         // clear the iterator, after this it may be set directly
167
Iterator JavaDoc newIterator = null;
168         Collection JavaDoc thisCollection = null;
169
170         if (property != null) {
171             if (Debug.verboseOn()) Debug.logVerbose("Getting iterator from property: " + property, module);
172             Object JavaDoc propertyObject = pageContext.findAttribute(property);
173
174             if (propertyObject instanceof Iterator JavaDoc) {
175                 newIterator = (Iterator JavaDoc) propertyObject;
176             } else {
177                 // if ClassCastException, it should indicate looking for a Collection
178
thisCollection = (Collection JavaDoc) propertyObject;
179             }
180         } else {
181             // Debug.logInfo("No property, check for Object Tag.", module);
182
ObjectTag objectTag =
183                 (ObjectTag) findAncestorWithClass(this, ObjectTag.class);
184
185             if (objectTag == null)
186                 return false;
187             if (objectTag.getType().equals("java.util.Collection")) {
188                 thisCollection = (Collection JavaDoc) objectTag.getObject();
189             } else {
190                 try {
191                     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
192                     Method JavaDoc[] m = loader.loadClass(objectTag.getType()).getDeclaredMethods();
193
194                     for (int i = 0; i < m.length; i++) {
195                         if (m[i].getName().equals("iterator")) {
196                             Debug.logVerbose("Found iterator method. Using it.", module);
197                             newIterator = (Iterator JavaDoc) m[i].invoke(
198                                         objectTag.getObject(), (Object JavaDoc[]) null);
199                             break;
200                         }
201                     }
202                 } catch (Exception JavaDoc e) {
203                     return false;
204                 }
205             }
206         }
207
208         if (newIterator == null) {
209             if (thisCollection == null || thisCollection.size() < 1)
210                 return false;
211
212             if (limit > 0 || offset > 0) {
213                 ArrayList JavaDoc colList = new ArrayList JavaDoc(thisCollection);
214                 int startIndex = offset > 0 ? offset : 0;
215                 int endIndex = limit > 0 ? offset + limit : colList.size();
216
217                 endIndex = endIndex > colList.size() ? colList.size() : endIndex;
218                 List JavaDoc subList = colList.subList(startIndex, endIndex);
219
220                 newIterator = subList.iterator();
221             } else {
222                 newIterator = thisCollection.iterator();
223             }
224
225             Debug.logVerbose("Got iterator.", module);
226         } else {// already set
227
Debug.logVerbose("iterator already set.", module);
228         }
229         this.iterator = newIterator;
230         return true;
231     }
232
233     private boolean defineElement() {
234         element = null;
235         pageContext.removeAttribute(name, PageContext.PAGE_SCOPE);
236         boolean verboseOn = Debug.verboseOn();
237
238         if (this.iterator.hasNext()) {
239             element = this.iterator.next();
240             if (verboseOn) Debug.logVerbose("iterator has another object: " + element, module);
241         } else {
242             if (verboseOn) Debug.logVerbose("iterator has no more objects", module);
243         }
244         if (element != null) {
245             if (verboseOn) Debug.logVerbose("set attribute " + name + " to be " + element + " as next value from iterator", module);
246             pageContext.setAttribute(name, element);
247
248             // expand a map element here if requested
249
if (expandMap) {
250                 Map JavaDoc tempMap = (Map JavaDoc) element;
251                 Iterator JavaDoc mapEntries = tempMap.entrySet().iterator();
252
253                 while (mapEntries.hasNext()) {
254                     Map.Entry JavaDoc entry = (Map.Entry JavaDoc) mapEntries.next();
255                     Object JavaDoc value = entry.getValue();
256
257                     if (value == null) value = new String JavaDoc();
258                     pageContext.setAttribute((String JavaDoc) entry.getKey(), value);
259                 }
260             }
261
262             return true;
263         }
264         if (verboseOn) Debug.logVerbose("no more iterations; element = " + element, module);
265         return false;
266     }
267 }
268
269
Popular Tags