KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: IterateNextTag.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.util.Iterator JavaDoc;
29 import java.util.Map 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 /**
36  * IterateNextTag - JSP Tag to get the next element of the IteratorTag.
37  *
38  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
39  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
40  * @version 1.0
41  * @created August 4, 2001
42  */

43 public class IterateNextTag extends BodyTagSupport JavaDoc {
44
45     protected String JavaDoc name = null;
46     protected Class JavaDoc type = null;
47     protected Object JavaDoc element = null;
48     protected boolean expandMap = false;
49
50     public void setName(String JavaDoc name) {
51         this.name = name;
52     }
53
54     public void setType(String JavaDoc type) throws ClassNotFoundException JavaDoc {
55         ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
56         this.type = loader.loadClass(type);
57     }
58
59     public void setExpandMap(String JavaDoc expMap) {
60         // defaults to false, so if anything but true will be false:
61
expandMap = "true".equals(expMap);
62     }
63
64     public String JavaDoc getName() {
65         return name;
66     }
67
68     public String JavaDoc getExpandMap() {
69         return expandMap ? "true" : "false";
70     }
71
72     public Object JavaDoc getElement() {
73         return element;
74     }
75
76     public int doStartTag() throws JspTagException JavaDoc {
77         IteratorTag iteratorTag =
78             (IteratorTag) findAncestorWithClass(this, IteratorTag.class);
79
80         if (iteratorTag == null)
81             throw new JspTagException JavaDoc("IterateNextTag not inside IteratorTag.");
82
83         Iterator JavaDoc iterator = iteratorTag.getIterator();
84
85         if (iterator == null || !iterator.hasNext())
86             return SKIP_BODY;
87
88         if (name == null)
89             name = "next";
90
91         // get the next element from the iterator
92
Object JavaDoc element = iterator.next();
93
94         pageContext.setAttribute(name, element);
95
96         // expand a map element here if requested
97
if (expandMap) {
98             Map JavaDoc tempMap = (Map JavaDoc) element;
99             Iterator JavaDoc mapEntries = tempMap.entrySet().iterator();
100
101             while (mapEntries.hasNext()) {
102                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) mapEntries.next();
103                 Object JavaDoc value = entry.getValue();
104
105                 if (value == null) value = new String JavaDoc();
106                 pageContext.setAttribute((String JavaDoc) entry.getKey(), value);
107             }
108         }
109
110         // give the updated iterator back.
111
iteratorTag.setIterator(iterator);
112
113         return EVAL_BODY_AGAIN;
114     }
115
116     public int doAfterBody() {
117         return SKIP_BODY;
118     }
119
120     public int doEndTag() {
121         try {
122             BodyContent JavaDoc body = getBodyContent();
123
124             if (body != null) {
125                 JspWriter JavaDoc out = body.getEnclosingWriter();
126                 String JavaDoc bodyString = body.getString();
127                 body.clearBody();
128                 out.print(bodyString);
129             }
130         } catch (IOException JavaDoc e) {
131             System.out.println("IterateNext Tag error: " + e);
132         }
133         return EVAL_PAGE;
134     }
135 }
136
137
Popular Tags