001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.iterator;
006:
007: import com.opensymphony.webwork.util.IteratorGenerator;
008: import com.opensymphony.webwork.util.IteratorGenerator.Converter;
009: import com.opensymphony.webwork.views.jsp.WebWorkBodyTagSupport;
010:
011: import javax.servlet.jsp.JspException;
012:
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: /**
017: * <!-- START SNIPPET: javadoc -->
018: * <b>NOTE: JSP-TAG</b>
019: *
020: * <p>Generate an iterator based on the val attribute supplied.</P>
021: *
022: * <b>NOTE:</b> The generated iterator will <b>ALWAYS</b> be pushed into the top of the stack, and poped
023: * at the end of the tag.
024: * <!-- END SNIPPET: javadoc -->
025: *
026: * <!-- START SNIPPET: params -->
027: * <ul>
028: * <li>val* (Object) - the source to be parsed into an iterator </li>
029: * <li>count (Object) - the max number (Integer, Float, Double, Long, String) entries to be in the iterator</li>
030: * <li>separator (String) - the separator to be used in separating the <i>val</i> into entries of the iterator</li>
031: * <li>id (String) - the id to store the resultant iterator into page context, if such id is supplied</li>
032: * <li>converter (Object) - the converter (must extends off IteratorGenerator.Converter interface) to convert the String entry parsed from <i>val</i> into an object</li>
033: * </ul>
034: * <!-- END SNIPPET: params -->
035: *
036: *
037: * <!-- START SNIPPET: example -->
038: * Example One:
039: * <pre>
040: * Generate a simple iterator
041: * <ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}">
042: * <ww:iterator>
043: * <ww:property /><br/>
044: * </ww:iterator>
045: * </ww:generator>
046: * </pre>
047: * This generates an iterator and print it out using the iterator tag.
048: *
049: * Example Two:
050: * <pre>
051: * Generate an iterator with count attribute
052: * <ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3">
053: * <ww:iterator>
054: * <ww:property /><br/>
055: * </ww:iterator>
056: * </ww:generator>
057: * </pre>
058: * This generates an iterator, but only 3 entries will be available in the iterator
059: * generated, namely aaa, bbb and ccc respectively because count attribute is set to 3
060: *
061: * Example Three:
062: * <pre>
063: * Generate an iterator with id attribute
064: * <ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," id="myAtt" />
065: * <%
066: * Iterator i = (Iterator) pageContext.getAttribute("myAtt");
067: * while(i.hasNext()) {
068: * String s = (String) i.next(); %>
069: * <%=s%> <br/>
070: * <% }
071: * %>
072: * </pre>
073: * This generates an iterator and put it in the PageContext under the key as specified
074: * by the id attribute.
075: *
076: *
077: * Example Four:
078: * <pre>
079: * Generate an iterator with comparator attribute
080: * <ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}">
081: * <ww:iterator>
082: * <ww:property /><br/>
083: * </ww:iterator>
084: * </ww:generator>
085: *
086: *
087: * public class GeneratorTagAction extends ActionSupport {
088: *
089: * ....
090: *
091: * public Converter getMyConverter() {
092: * return new Converter() {
093: * public Object convert(String value) throws Exception {
094: * return "converter-"+value;
095: * }
096: * };
097: * }
098: *
099: * ...
100: *
101: * }
102: * </pre>
103: * This will generate an iterator with each entries decided by the converter supplied. With
104: * this converter, it simply add "converter-" to each entries.
105: * <!-- END SNIPPET: example -->
106: *
107: * @see com.opensymphony.webwork.util.IteratorGenerator
108: * @author Rickard �berg (rickard@dreambean.com)
109: * @author tm_jee ( tm_jee(at)yahoo.co.uk )
110: * @version $Revision: 1593 $
111: *
112: * @ww.tag name="generator" tld-body-content="JSP"
113: * description="Generate an iterator for a iterable source."
114: */
115: public class IteratorGeneratorTag extends WebWorkBodyTagSupport {
116:
117: private static final long serialVersionUID = 2968037295463973936L;
118:
119: public static final String DEFAULT_SEPARATOR = ",";
120:
121: private static final Log _log = LogFactory
122: .getLog(IteratorGeneratorTag.class);
123:
124: String countAttr;
125: String separatorAttr;
126: String valueAttr;
127: String converterAttr;
128:
129: IteratorGenerator iteratorGenerator = null;
130:
131: /**
132: * @ww.tagattribute required="false" type="Integer"
133: * description="the max number entries to be in the iterator"
134: */
135: public void setCount(String count) {
136: countAttr = count;
137: }
138:
139: /**
140: * @ww.tagattribute required="true" type="String"
141: * description="the separator to be used in separating the <i>val</i> into entries of the iterator"
142: */
143: public void setSeparator(String separator) {
144: separatorAttr = separator;
145: }
146:
147: /**
148: * @ww.tagattribute required="true"
149: * description="the source to be parsed into an iterator"
150: */
151: public void setVal(String val) {
152: valueAttr = val;
153: }
154:
155: /**
156: * @ww.tagattribute required="false" type="com.opensymphony.webwork.util.IteratorGenerator.Converter"
157: * description="the converter to convert the String entry parsed from <i>val</i> into an object"
158: */
159: public void setConverter(String aConverter) {
160: converterAttr = aConverter;
161: }
162:
163: /**
164: * @ww.tagattribute required="false" type="String"
165: * description="the id to store the resultant iterator into page context, if such id is supplied"
166: */
167: public void setId(String string) {
168: super .setId(string);
169: }
170:
171: public int doStartTag() throws JspException {
172:
173: // value
174: Object value = findValue(valueAttr);
175:
176: // separator
177: String separator = DEFAULT_SEPARATOR;
178: if (separatorAttr != null && separatorAttr.length() > 0) {
179: separator = findString(separatorAttr);
180: }
181:
182: // TODO: maybe this could be put into an Util class, or there is already one?
183: // count
184: int count = 0;
185: if (countAttr != null && countAttr.length() > 0) {
186: Object countObj = findValue(countAttr);
187: if (countObj instanceof Integer) {
188: count = ((Integer) countObj).intValue();
189: } else if (countObj instanceof Float) {
190: count = ((Float) countObj).intValue();
191: } else if (countObj instanceof Long) {
192: count = ((Long) countObj).intValue();
193: } else if (countObj instanceof Double) {
194: count = ((Long) countObj).intValue();
195: } else if (countObj instanceof String) {
196: try {
197: count = Integer.parseInt((String) countObj);
198: } catch (NumberFormatException e) {
199: _log.warn("unable to convert count attribute ["
200: + countObj
201: + "] to number, ignore count attribute", e);
202: }
203: }
204: }
205:
206: // converter
207: Converter converter = null;
208: if (converterAttr != null && converterAttr.length() > 0) {
209: converter = (Converter) findValue(converterAttr);
210: }
211:
212: iteratorGenerator = new IteratorGenerator();
213: iteratorGenerator.setValues(value);
214: iteratorGenerator.setCount(count);
215: iteratorGenerator.setSeparator(separator);
216: iteratorGenerator.setConverter(converter);
217:
218: iteratorGenerator.execute();
219:
220: // push resulting iterator into stack
221: getStack().push(iteratorGenerator);
222: if (getId() != null && getId().length() > 0) {
223: // if an id is specified, we have the resulting iterator set into
224: // the pageContext attribute as well
225: pageContext.setAttribute(getId(), iteratorGenerator);
226: }
227:
228: return EVAL_BODY_INCLUDE;
229: }
230:
231: public int doEndTag() throws JspException {
232: // pop resulting iterator from stack at end tag
233: getStack().pop();
234: iteratorGenerator = null; // clean up
235:
236: return EVAL_PAGE;
237: }
238: }
|