Source Code Cross Referenced for IteratorGeneratorTag.java in  » Web-Framework » struts-2.0.11 » org » apache » struts2 » 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 » Web Framework » struts 2.0.11 » org.apache.struts2.views.jsp.iterator 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * &lt;s:generator val="%{'aaa,bbb,ccc,ddd,eee'}"&gt;
059:         *  &lt;s:iterator&gt;
060:         *      &lt;s:property /&gt;&lt;br/&gt;
061:         *  &lt;/s:iterator&gt;
062:         * &lt;/s:generator&gt;
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:         * &lt;s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="3"&gt;
070:         *  &lt;s:iterator&gt;
071:         *      &lt;s:property /&gt;&lt;br/&gt;
072:         *  &lt;/s:iterator&gt;
073:         * &lt;/s:generator&gt;
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:         * &lt;s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" count="4" separator="," id="myAtt" /&gt;
082:         * &lt;%
083:         *  Iterator i = (Iterator) pageContext.getAttribute("myAtt");
084:         *  while(i.hasNext()) {
085:         *      String s = (String) i.next(); %>
086:         *      &lt;%=s%&gt; &lt;br/&gt;
087:         * &lt;%    }
088:         * %&gt;
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:         * &lt;s:generator val="%{'aaa,bbb,ccc,ddd,eee'}" converter="%{myConverter}"&gt;
098:         *  &lt;s:iterator&gt;
099:         *      &lt;s:property /&gt;&lt;br/&gt;
100:         *  &lt;/s:iterator&gt;
101:         * &lt;/s:generator&gt;
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.