KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > taglibs > declarations > DeclareContainerListTag


1 package org.jahia.taglibs.declarations;
2
3 import java.util.Vector JavaDoc;
4
5 import javax.servlet.ServletRequest JavaDoc;
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
8 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
9
10 import org.jahia.data.JahiaData;
11 import org.jahia.exceptions.JahiaException;
12 import org.jahia.resourcebundle.ResourceBundleMarker;
13
14 /**
15  * <p>Title: ContainerList declaration tag. </p>
16      * <p>Description: This tag is used to declare a container list. A container list
17  * declaration might be embedded within a container declaration.</p>
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: </p>
20  * @author Serge Huber
21  * @version 1.0
22  * @todo implement resource bundle marker support
23  */

24
25 public class DeclareContainerListTag extends BodyTagSupport JavaDoc {
26
27     private static org.apache.log4j.Logger logger =
28         org.apache.log4j.Logger.getLogger(DeclareContainerListTag.class);
29
30     public DeclareContainerListTag () {
31     }
32
33     private String JavaDoc listName = "";
34     private String JavaDoc title = "";
35
36     // we have two versions of these properties because we have to respect the
37
// JavaBeans design pattern.
38
private int intWindowSize = -1; // -1 means functionality is deactivated by default
39
private int intWindowOffset = -1;
40
41     private String JavaDoc windowSize;
42     private String JavaDoc windowOffset;
43
44     private String JavaDoc containerBeanName;
45     private String JavaDoc validatorKey;
46
47     private JahiaData jData = null;
48     private boolean hasParent = false;
49     private String JavaDoc parentListName = "";
50     private String JavaDoc titleKey = null;
51     private String JavaDoc bundleKey = null;
52     private java.util.Vector JavaDoc children = new Vector JavaDoc();
53
54     public void setName (String JavaDoc name) {
55         this.listName = name;
56     }
57
58     public String JavaDoc getName () {
59         return this.listName;
60     }
61
62     public void setTitle (String JavaDoc title) {
63         this.title = title;
64     }
65
66     public String JavaDoc getTitle () {
67         return this.title;
68     }
69
70     public void setWindowSize (String JavaDoc windowSize) {
71         try {
72             this.windowSize = windowSize;
73             this.intWindowSize = Integer.parseInt(windowSize);
74         } catch (NumberFormatException JavaDoc nfe) {
75             this.windowSize = "";
76             this.intWindowSize = -1;
77         }
78     }
79
80     public String JavaDoc getWindowSize () {
81         return this.windowSize;
82     }
83
84     public void setWindowOffset (String JavaDoc windowOffset) {
85         try {
86             this.windowOffset = windowOffset;
87             this.intWindowOffset = Integer.parseInt(windowOffset);
88         } catch (NumberFormatException JavaDoc nfe) {
89             this.intWindowOffset = -1;
90         }
91     }
92
93     public String JavaDoc getWindowOffset () {
94         return this.windowOffset;
95     }
96
97     public String JavaDoc getTitleKey () {
98         return titleKey;
99     }
100
101     public void setTitleKey (String JavaDoc titleKey) {
102         this.titleKey = titleKey;
103     }
104
105     public String JavaDoc getBundleKey () {
106         return bundleKey;
107     }
108
109     public void setBundleKey (String JavaDoc bundleKey) {
110         this.bundleKey = bundleKey;
111     }
112
113
114     public void addChild (String JavaDoc childName) {
115         this.children.add(childName);
116     }
117
118     public int doStartTag () {
119
120         ServletRequest JavaDoc request = pageContext.getRequest();
121         jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
122
123         // but we must also test the case where this containerList is enclosed
124
// in a parent containerList in which case we just trickle down the
125
// state of the parent list...
126
DeclareContainerListTag parentDeclareContainerListTag = (
127             DeclareContainerListTag) findAncestorWithClass(this,
128             DeclareContainerListTag.class);
129         if (parentDeclareContainerListTag != null) {
130             parentListName = parentDeclareContainerListTag.getName();
131             hasParent = true;
132         }
133
134         logger.debug("Beginning declaration of container list '" +
135                      this.listName + "'");
136
137         return EVAL_BODY_BUFFERED;
138     }
139
140     // Body is evaluated one time, so just writes it on standard output
141
public int doAfterBody () {
142
143         /* let's do all declarative pass stuff here */
144
145         if ( (intWindowSize >= 1) && (intWindowOffset == -1)) {
146             // we allow a windowSize alone.
147
intWindowOffset = 0;
148         }
149         if ( (intWindowSize >= 1) && (intWindowOffset >= 0) &&
150             (intWindowOffset < intWindowSize)) {
151             // let's set the parameter to be retrieved by the display
152
// system. This is why we do this at declaration time.
153
if (jData.params().getParameter("ctnscroll_" + this.listName) == null) {
154                 logger.debug("Setting window parameters to window size " +
155                              Integer.toString(this.intWindowSize) +
156                              " and offset " +
157                              Integer.toString(this.intWindowOffset));
158                 jData.params().setParameter("ctnscroll_" + this.listName,
159                                             Integer.toString(this.intWindowSize) +
160                                             "_" +
161                                             Integer.toString(this.
162                     intWindowOffset));
163             }
164         }
165
166         // searches the current container list inside a container or at root
167
DeclareContainerListTag parentDeclareContainerListTag = (
168             DeclareContainerListTag) findAncestorWithClass(this,
169             DeclareContainerListTag.class);
170         DeclareContainerTag declareContainerTag = (DeclareContainerTag)
171                                                   findAncestorWithClass(this,
172             DeclareContainerTag.class);
173         if (parentDeclareContainerListTag != null) {
174             // we are in an embedded containerList tag
175

176             // checks if the container list has been declared in the current container
177
try {
178                 logger.debug("Declaring embedded container list with name " +
179                              this.listName + " title=" + this.title +
180                              " size=" + this.children.size());
181                 jData.containers().declareContainer(this.listName, resolveTitle(),
182                     getChildren(), intWindowSize, intWindowOffset, validatorKey,
183                     containerBeanName);
184
185                 parentDeclareContainerListTag.addChild(this.listName);
186
187                 return SKIP_BODY; // exit the parsing of this containerList while still in parent declaration pass...
188
// update attribute
189
} catch (JahiaException je) {
190                 logger.error("Error:", je);
191             }
192         } else {
193             // at root: WARNING : the container list can be absolute !
194
// that explains the usage of the methods "checkAttributes()" and "checkDeclaration()"
195
// whose code differs in case of absolute container list
196
try {
197                 // first, check if the list has been declared
198
checkAttributes(jData);
199                 try {
200                     checkDeclaration(jData);
201                 } catch (JahiaException je) {
202                     logger.info("Container has already been defined, ignoring re-definition and loading content...");
203                 }
204             } catch (JahiaException je) {
205                 logger.error("Error:", je);
206             }
207         }
208
209         // logger.debug("End of declaration pass for container list '" + this.listName + "'");
210
// let's clear the evaluated body content since we won't use it on this pass...
211
BodyContent JavaDoc body = getBodyContent();
212         body.clearBody();
213         return SKIP_BODY;
214     }
215
216     // the methods below are defined in order to be overrided in the subclasses
217
// AbsoluteContainerListTag and RelativeContainerListTag
218

219     protected void checkAttributes (JahiaData jData)
220         throws JahiaException {
221     }
222
223     protected void checkDeclaration (JahiaData jData)
224         throws JahiaException {
225         logger.debug("Declaring container list with name " +
226                      this.listName + " title=" + this.title +
227                      " size=" + this.children.size());
228         jData.containers().declareContainer(this.listName, resolveTitle(),
229                                             getChildren(), intWindowSize,
230                                             intWindowOffset, validatorKey,
231                                             containerBeanName);
232
233         logger.debug("End declareContainer call with name " +
234                      this.listName);
235
236     }
237
238     //--------------------------------------------------------------------------
239
/**
240      * Called by AbstractFieldTag to check if this container list already
241      * contains a field of the given name.
242      *
243      * Author NK
244      */

245     public boolean isChildAlreadyDeclared (String JavaDoc name) {
246         int size = children.size();
247         String JavaDoc fieldName = "";
248         for (int i = 0; i < size; i++) {
249             fieldName = (String JavaDoc) children.get(i);
250             if (fieldName.equals(name))
251                 return true;
252         }
253         return false;
254     }
255
256     public int doEndTag ()
257         throws JspException JavaDoc {
258         // let's reinitialize the tag variables to allow tag object reuse in
259
// pooling.
260
listName = "";
261         title = "";
262         children = new Vector JavaDoc();
263
264         intWindowSize = -1; // -1 means functionality is deactivated by default
265
intWindowOffset = -1;
266
267         windowSize = null;
268         windowOffset = null;
269
270         jData = null;
271
272         hasParent = false;
273         parentListName = "";
274
275         titleKey = null;
276         bundleKey = null;
277
278         return EVAL_PAGE;
279     }
280
281
282     public java.util.Vector JavaDoc getChildren() {
283         return children;
284     }
285
286     private String JavaDoc resolveTitle() {
287         if ((titleKey != null) && (bundleKey != null)) {
288             return ResourceBundleMarker.drawMarker(bundleKey, titleKey, title);
289         } else {
290             return title;
291         }
292     }
293
294     /**
295      * @return
296      */

297     public String JavaDoc getContainerBeanName() {
298         return containerBeanName;
299     }
300
301     /**
302      * @return
303      */

304     public String JavaDoc getValidatorKey() {
305         return validatorKey;
306     }
307
308     /**
309      * @param string
310      */

311     public void setContainerBeanName(String JavaDoc string) {
312         containerBeanName = string;
313     }
314
315     /**
316      * @param string
317      */

318     public void setValidatorKey(String JavaDoc string) {
319         validatorKey = string;
320     }
321 }
322
Popular Tags