KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > container > ContainerTag


1 package org.jahia.taglibs.container;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5
6 import javax.servlet.ServletRequest JavaDoc;
7 import javax.servlet.jsp.JspException JavaDoc;
8 import javax.servlet.jsp.JspTagException JavaDoc;
9 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
10
11 import org.jahia.data.JahiaData;
12 import org.jahia.data.beans.ContainerBean;
13 import org.jahia.data.containers.JahiaContainer;
14 import org.jahia.data.containers.JahiaContainerList;
15
16
17 /**
18  * Class ContainerTag : retrieves the container list which it belongs to, loops through the
19  * containers and displays the fields of each container
20  *
21  * @author Jerome Tamiotti
22  */

23 public class ContainerTag extends BodyTagSupport JavaDoc {
24
25     private static org.apache.log4j.Logger logger =
26         org.apache.log4j.Logger.getLogger(ContainerTag.class);
27
28     private Enumeration JavaDoc containers;
29
30     private boolean first = true;
31     private boolean last = true;
32     private JahiaContainer container = null;
33     private JahiaContainer firstContainer = null;
34     private int counter = 1;
35     private JahiaData jData = null;
36
37     private boolean initLoop = true;
38
39     private boolean display = true;
40
41     public boolean displayBody() {
42         return display;
43     }
44
45     public boolean isFirst() {
46         return this.first;
47     }
48
49     public boolean isLast() {
50         return this.last;
51     }
52
53     public JahiaContainer getContainer() {
54         return this.container;
55     }
56
57     public ContainerBean getContainerBean() {
58         if (this.container == null) {
59             return null;
60         }
61         return new ContainerBean(this.container, jData.params());
62     }
63
64     public int getCounter() {
65         return this.counter;
66     }
67
68     public int doStartTag() {
69         initLoop = true;
70
71         ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class);
72             JahiaContainerList containerList = cListTag.getContainerList();
73             if (containerList == null)
74                 return SKIP_BODY;
75
76             if (initLoop) {
77                 ServletRequest JavaDoc request = pageContext.getRequest();
78                 jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
79                 // here is stuff we need to do only once...
80
// reads the containers of this list
81
containers = containerList.getContainers();
82                 int ct = 0;
83                 if (containers.hasMoreElements()) {
84                     this.container = (JahiaContainer)containers.nextElement();
85                     if (getId() != null) {
86                         pageContext.setAttribute(getId(), getContainerBean());
87                     }
88                     if (ct == 0) {
89                         this.firstContainer = this.container;
90                     }
91                     if (containers.hasMoreElements()) {
92                         this.last = false;
93                     }
94                 } else {
95                     this.counter = 0;
96                     this.display = false;
97                 }
98                 containers = containerList.getContainers();
99                 if (containers.hasMoreElements()) {
100                     this.container = (JahiaContainer) containers.nextElement();
101                     if (getId() != null) {
102                         pageContext.setAttribute(getId(), getContainerBean());
103                     }
104                 } else {
105                     return SKIP_BODY;
106                 }
107                 initLoop = false;
108             }
109         return EVAL_BODY_BUFFERED;
110     }
111
112
113     // loops through the next elements
114
public int doAfterBody() throws JspException JavaDoc {
115         // gets the current container list
116
ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class);
117
118         if (this.display) {
119             try {
120                 getBodyContent().writeOut(getPreviousOut());
121                 getBodyContent().clear();
122                 this.counter++;
123             } catch (IOException JavaDoc ioe) {
124                 throw new JspTagException JavaDoc();
125             }
126             if (containers.hasMoreElements()) {
127                 // JahiaConsole.println("ContainerTag.doAfterBody", "Processing next container in list");
128
this.container = (JahiaContainer)containers.nextElement();
129                 if (getId() != null) {
130                     pageContext.setAttribute(getId(), getContainerBean());
131                 }
132                 // not the first one anymore
133
this.first = false;
134                 // checks if it's the last one
135
if (!containers.hasMoreElements()) {
136                     this.last = true;
137                 }
138                 return EVAL_BODY_BUFFERED;
139             } else {
140                 this.display = false;
141             }
142         }
143         return SKIP_BODY;
144     }
145
146     public int doEndTag() throws JspException JavaDoc {
147         // let's reinitialize the tag variables to allow tag object reuse in
148
// pooling.
149
super.doEndTag();
150         containers = null;
151
152         first = true;
153         last = true;
154         container = null;
155         firstContainer = null;
156         counter = 1;
157
158         initLoop = true;
159         jData = null;
160
161         display = true;
162         if (getId() != null) {
163             pageContext.removeAttribute(getId());
164         }
165         return EVAL_PAGE;
166     }
167
168
169 }
170
Popular Tags