001: /**
002: * $Id: CollectionTag.java,v 1.8 2005/09/21 10:50:30 dg154973 Exp $
003: * Copyright 2002 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.wireless.taglibs.base;
014:
015: import com.sun.portal.log.common.PortalLogger;
016:
017: import java.util.*;
018: import java.util.logging.Logger;
019: import java.util.logging.Level;
020: import javax.servlet.jsp.*;
021: import javax.servlet.jsp.tagext.*;
022:
023: public abstract class CollectionTag extends BaseBodyTagSupport
024: implements BeanHolder {
025:
026: // Create a logger for this class
027: private static Logger debugLogger = PortalLogger
028: .getLogger(CollectionTag.class);
029:
030: String beanid = null;
031:
032: /**
033: * Start index
034: */
035: int start = 0;
036: boolean doIteration = false;
037:
038: /**
039: * Internal iterator over the items in the collection
040: */
041: CollectionIterator iterator = null;
042:
043: /**
044: * The current item in the iteration
045: */
046: Object item;
047:
048: /**
049: * Get the collection-iterator as a bean
050: *
051: * @return the collection-iterator bean
052: */
053: public Object getBean() {
054: return iterator;
055: }
056:
057: /**
058: * Get the current item in the iteration
059: *
060: * @return the current item
061: */
062: public Object getItem() {
063: return item;
064: }
065:
066: /**
067: * Find the collection this tag represents
068: *
069: * @return the collection
070: */
071: public abstract Collection findCollection() throws Exception;
072:
073: /**
074: * Prepare to iterate over the collection
075: *
076: * @return EVAL_BODY_INCLUDE to iterate over the collection
077: * SKIP_BODY if the collection is empty
078: */
079: public int doStartTag() throws JspException {
080: Collection collection;
081:
082: if (name != null) {
083: Object col = pageContext.getAttribute(name);
084: if (col instanceof CollectionIterator) {
085: iterator = (CollectionIterator) col;
086: collection = iterator.getCollection();
087: } else if (col == null) {
088: // null collection: just skip body via an empty collection
089: collection = new Vector();
090: // Array - convert to collection
091: } else if (col instanceof Object[]) {
092: collection = new Vector(Arrays.asList((Object[]) col));
093: } else if (col instanceof Collection) {
094: // behavior described in taglibs docs: any arbitrary collection
095: collection = (Collection) col;
096: iterator = new CollectionIterator(collection);
097: } else {
098: debugLogger.log(Level.FINE, "PSMA_CSPWTB0012");
099: throw new JspException(this .getClass().getName()
100: + ".doStartTag(): failed: " + col
101: + " is not a collection");
102: }
103: } else {
104: try {
105: collection = findCollection();
106: iterator = new CollectionIterator(collection);
107: } catch (Exception e) {
108: debugLogger.log(Level.FINE, "PSMA_CSPWTB0001", e);
109: throw new JspException(this .getClass().getName()
110: + ".doStartTag(): failed: " + e.getMessage());
111: }
112: }
113:
114: if (collection == null) {
115: debugLogger.fine("PSMA_CSPWTB0012");
116: throw new JspException(this .getClass().getName()
117: + ".doStartTag(): No collection found");
118: }
119:
120: if (doIteration) {
121: if ((collection.isEmpty())
122: || (iterator.getStartInt() >= collection.size()))
123: return SKIP_BODY;
124: }
125:
126: if (id != null) {
127: pageContext.setAttribute(id, iterator);
128: }
129:
130: return EVAL_BODY_TAG;
131: }
132:
133: /**
134: * Start the iteration by getting the first object
135: *
136: * @exception JspException
137: */
138: public void doInitBody() throws JspException {
139: if (doIteration && iterator.hasNext()) {
140: item = iterator.next();
141: if (beanid != null) {
142: pageContext.setAttribute(beanid, item);
143: }
144: }
145: }
146:
147: /**
148: * Iterate through the items in the collection.
149: *
150: * Write the previous body context to the page output
151: * and if there are still more items then return
152: * EVAL_BODY_TAG to loop again. When all the
153: * items have been consumed return SKIP_BODY to
154: * end the loop.
155: *
156: * @return EVAL_BODY_TAG if there are more items
157: * SKIP_BODY when the iteration is complete
158: * @exception JspException
159: */
160: public int doAfterBody() throws JspException {
161: BodyContent body = getBodyContent();
162:
163: try {
164: body.writeOut(getPreviousOut());
165: body.clearBody();
166: } catch (Exception e) {
167: debugLogger.log(Level.FINE, "PSMA_CSPWTB0013", e);
168: throw new JspException(this .getClass().getName()
169: + ".doAfterBody(): couldn't write previous out"
170: + e.getMessage());
171: }
172:
173: if (doIteration && iterator.hasNext()) {
174: item = iterator.next();
175: if (beanid != null) {
176: pageContext.setAttribute(beanid, item);
177: }
178: return EVAL_BODY_TAG;
179: } else {
180: return SKIP_BODY;
181: }
182: }
183:
184: /**
185: * Continue evaluating the page
186: *
187: * @return EVAL_PAGE
188: */
189: public int doEndTag() {
190: return EVAL_PAGE;
191: }
192:
193: /**
194: * Cleanup
195: */
196: public void release() {
197: super .release();
198: iterator = null;
199: item = null;
200: start = 0;
201: doIteration = false;
202: beanid = null;
203: }
204:
205: /***********************
206: * Process attributes...
207: **********************/
208:
209: /**
210: * Process "iterate" attribute...
211: */
212:
213: public void setIterate(String s) {
214: if (s != null && s.equalsIgnoreCase("true"))
215: doIteration = true;
216: else
217: doIteration = false;
218: }
219:
220: /*
221: * Process "beanid" attribute...
222: */
223: public void setBeanid(String s) {
224: beanid = s;
225: }
226: }
|