Source Code Cross Referenced for IteratorGeneratorTag.java in  » J2EE » webwork-2.2.6 » com » opensymphony » webwork » views » jsp » iterator » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » webwork 2.2.6 » com.opensymphony.webwork.views.jsp.iterator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * &lt;ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}"&gt;
042:         *	&lt;ww:iterator&gt;
043:         *		&lt;ww:property /&gt;&lt;br/&gt;
044:         *	&lt;/ww:iterator&gt;
045:         * &lt;/ww:generator&gt;
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:         * &lt;ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3"&gt;
053:         *	&lt;ww:iterator&gt;
054:         *		&lt;ww:property /&gt;&lt;br/&gt;
055:         *	&lt;/ww:iterator&gt;
056:         * &lt;/ww:generator&gt;
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:         * &lt;ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," id="myAtt" /&gt;
065:         * &lt;%
066:         * 	Iterator i = (Iterator) pageContext.getAttribute("myAtt");
067:         * 	while(i.hasNext()) {
068:         * 		String s = (String) i.next(); %>
069:         * 		&lt;%=s%&gt; &lt;br/&gt;
070:         * &lt;% 	}
071:         * %&gt;
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:         * &lt;ww:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}"&gt;
081:         *	&lt;ww:iterator&gt;
082:         * 		&lt;ww:property /&gt;&lt;br/&gt;
083:         * 	&lt;/ww:iterator&gt;
084:         * &lt;/ww:generator&gt;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.