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