KickJava   Java API By Example, From Geeks To Geeks.

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


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

20 public class ContainerTag extends BodyTagSupport JavaDoc {
21
22
23     private Enumeration JavaDoc containers;
24
25     private boolean first = true;
26     private boolean last = true;
27     private JahiaContainer container = null;
28     private JahiaContainer firstContainer = null;
29     private int counter = 1;
30
31     private boolean initLoop = true;
32
33     private boolean display = true;
34
35     public boolean displayBody() {
36         return display;
37     }
38
39     public boolean isFirst() {
40         return this.first;
41     }
42
43     public boolean isLast() {
44         return this.last;
45     }
46
47     public JahiaContainer getContainer() {
48         return this.container;
49     }
50
51     public int getCounter() {
52         return this.counter;
53     }
54
55     public boolean isDeclarationPass() {
56         // gets the current container list
57
ContainerListTag cListTag = (ContainerListTag) getParent();
58         if (cListTag == null) {
59             return false;
60         } else {
61             return cListTag.isDeclarationPass();
62         }
63     }
64
65
66     public int doStartTag() {
67         initLoop = true;
68
69         ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class);
70         if (cListTag.isDeclarationPass()) {
71             // JahiaConsole.println("ContainerTag.doStartTag",
72
// cListTag.getName() + " : Declaration Pass Beginning...");
73
} else {
74             JahiaContainerList containerList = cListTag.getContainerList();
75             if (containerList == null)
76                 return SKIP_BODY;
77
78             if (initLoop) {
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(), this.container);
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(), this.container);
103                     }
104                 } else {
105                     return SKIP_BODY;
106                 }
107                 initLoop = false;
108             }
109         }
110         return EVAL_BODY_BUFFERED;
111     }
112
113
114     // loops through the next elements
115
public int doAfterBody() throws JspException JavaDoc {
116         // gets the current container list
117
ContainerListTag cListTag = (ContainerListTag) findAncestorWithClass(this, ContainerListTag.class);
118         if (cListTag.isDeclarationPass()) {
119             //JahiaConsole.println("ContainerTag.doAfterBody",
120
// cListTag.getName() + " : Declaration Pass Ended.");
121
return SKIP_BODY;
122         }
123
124         /*
125         JahiaConsole.println("ContainerTag.doAfterBody",
126                              cListTag.getName() + " : Post-declaration pass (counter=" + Integer.toString(this.counter) + ")");
127         */

128
129
130         if (this.display) {
131             try {
132                 getBodyContent().writeOut(getPreviousOut());
133                 getBodyContent().clear();
134                 this.counter++;
135             } catch (IOException JavaDoc ioe) {
136                 throw new JspTagException JavaDoc();
137             }
138             if (containers.hasMoreElements()) {
139                 // JahiaConsole.println("ContainerTag.doAfterBody", "Processing next container in list");
140
this.container = (JahiaContainer)containers.nextElement();
141                 if (getId() != null) {
142                     pageContext.setAttribute(getId(), this.container);
143                 }
144                 // not the first one anymore
145
this.first = false;
146                 // checks if it's the last one
147
if (!containers.hasMoreElements()) {
148                     this.last = true;
149                 }
150                 return EVAL_BODY_BUFFERED;
151             } else {
152                 this.display = false;
153             }
154         }
155         return SKIP_BODY;
156     }
157
158     public int doEndTag() throws JspException JavaDoc {
159         // let's reinitialize the tag variables to allow tag object reuse in
160
// pooling.
161
super.doEndTag();
162         containers = null;
163
164         first = true;
165         last = true;
166         container = null;
167         firstContainer = null;
168         counter = 1;
169
170         initLoop = true;
171
172         display = true;
173         if (getId() != null) {
174             pageContext.removeAttribute(getId());
175         }
176         return EVAL_PAGE;
177     }
178
179
180 }
181
Popular Tags