KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > frontoffice > taglib > IterateOverTag


1 /*
2  * ĻON content management system.
3  * Copyright (C) 2002 Guillaume Bort(gbort@msn.com). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  * Copyright 2000-2002 (C) Intalio Inc. All Rights Reserved.
7  *
8  * ĻON is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * ĻON core framework, ĻON content server, ĻON backoffice, ĻON frontoffice
14  * and ĻON admin application are parts of ĻON and are distributed under
15  * same terms of licence.
16  *
17  *
18  * ĻON includes software developed by the Apache Software Foundation (http://www.apache.org/)
19  * and software developed by the Exolab Project (http://www.exolab.org).
20  *
21  * ĻON is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  */

26  
27 package org.nextime.ion.frontoffice.taglib;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import javax.servlet.jsp.JspException JavaDoc;
34 import javax.servlet.jsp.JspTagException JavaDoc;
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
37 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
38 import javax.servlet.jsp.tagext.Tag JavaDoc;
39
40 public class IterateOverTag extends BodyTagSupport JavaDoc {
41
42     private String JavaDoc _var;
43     private Collection JavaDoc _collection;
44     private Iterator JavaDoc _iterator;
45     private IterateStatus _status;
46
47     public IterateOverTag() {
48         super();
49         init();
50     }
51
52     public void setVar(String JavaDoc var) {
53         _var = var;
54     }
55
56     public int doStartTag() throws JspException JavaDoc {
57
58         Tag JavaDoc t = findAncestorWithClass(this, SelectObjectsTag.class);
59         if (t == null)
60             throw new JspTagException JavaDoc("Le tag iterateOver doit etre dans un tag selectObjects");
61
62         _collection = ((SelectObjectsTag) t).getCollection();
63
64         if (_collection == null)
65             return (SKIP_BODY);
66             
67         _status = new IterateStatus();
68         _status._size = _collection.size();
69         _status._index = 0;
70
71         _iterator = _collection.iterator();
72
73         if (_iterator.hasNext()) {
74             Object JavaDoc element = _iterator.next();
75             if (element == null)
76                 pageContext.removeAttribute(_var);
77             else
78                 pageContext.setAttribute(_var, element);
79             _status._index++;
80             pageContext.setAttribute("iterateStatus", _status);
81             return (EVAL_BODY_TAG);
82         } else
83             return (SKIP_BODY);
84     }
85
86     public int doAfterBody() throws JspException JavaDoc {
87
88         if (bodyContent != null) {
89             JspWriter JavaDoc writer = pageContext.getOut();
90             if (writer instanceof BodyContent JavaDoc)
91                 writer = ((BodyContent JavaDoc) writer).getEnclosingWriter();
92             try {
93                 writer.print(bodyContent.getString());
94             } catch (IOException JavaDoc e) {
95                 throw new JspException JavaDoc(e);
96             }
97             bodyContent.clearBody();
98         }
99
100         if (_iterator.hasNext()) {
101             Object JavaDoc element = _iterator.next();
102             if (element == null)
103                 pageContext.removeAttribute(_var);
104             else
105                 pageContext.setAttribute(_var, element);
106             _status._index++;
107             pageContext.setAttribute("iterateStatus", _status);
108             return (EVAL_BODY_TAG);
109         } else
110             return (SKIP_BODY);
111
112     }
113
114     public int doEndTag() throws JspException JavaDoc {
115         return (EVAL_PAGE);
116     }
117
118     public void release() {
119         super.release();
120         init();
121     }
122
123     private void init() {
124         _var = null;
125         _status = null;
126     }
127
128 }
Popular Tags