KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > tiles > PutListTag


1 /*
2  * $Id: PutListTag.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.apache.struts.taglib.tiles;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
26
27 import org.apache.struts.tiles.AttributeDefinition;
28 import org.apache.struts.tiles.UntypedAttribute;
29
30 /**
31  * PutList tag implementation.
32  */

33 public class PutListTag
34     extends TagSupport JavaDoc
35     implements ComponentConstants, AddTagParent, PutListTagParent {
36
37     /**
38      * Name of this attribute.
39      */

40     private String JavaDoc attributeName = null;
41     
42     /**
43      * The list itself.
44      */

45     private List JavaDoc list = null;
46     
47     /**
48      * Role attribute.
49      */

50     private String JavaDoc role = null;
51
52     /**
53      * Default constructor.
54      */

55     public PutListTag() {
56         super();
57     }
58
59     /**
60      * Release all allocated resources.
61      */

62     public void release() {
63         super.release();
64         attributeName = null;
65         role = null;
66     }
67
68     /**
69      * Release all internal resources.
70      */

71     protected void releaseInternal() {
72         list = null;
73     }
74
75     /**
76      * Set property.
77      */

78     public void setName(String JavaDoc name) {
79         this.attributeName = name;
80     }
81
82     /**
83      * Get property.
84      */

85     public String JavaDoc getName() {
86         return attributeName;
87     }
88
89     /**
90      * Set role attribute.
91      * @param role The role the user must be in to store content.
92      */

93     public void setRole(String JavaDoc role) {
94         this.role = role;
95     }
96
97     /**
98      * Get role attribute.
99      */

100     public String JavaDoc getRole() {
101         return role;
102     }
103
104     /**
105      * Get list defined in tag.
106      */

107     public List JavaDoc getList() {
108         return list;
109     }
110
111     /**
112      * Set property.
113      */

114     public void addElement(Object JavaDoc value) {
115         if (list == null) {
116             list = new ArrayList JavaDoc();
117         }
118         
119         list.add(value);
120     }
121
122     /**
123      * Process nested ≶putList> tag.
124      * Method calls by nested ≶putList> tags.
125      * Nested list is added to current list.
126      * If role is defined, nested attribute is wrapped into an untypped definition
127      * containing attribute value and role.
128      */

129     public void processNestedTag(PutListTag nestedTag) throws JspException JavaDoc {
130         // Get real value and check role
131
// If role is set, add it in attribute definition if any.
132
// If no attribute definition, create untyped one, and set role.
133
Object JavaDoc attributeValue = nestedTag.getList();
134
135         if (nestedTag.getRole() != null) {
136             AttributeDefinition def = new UntypedAttribute(attributeValue);
137             def.setRole(nestedTag.getRole());
138             attributeValue = def;
139         }
140         
141         // now add attribute to enclosing parent (i.e. : this object)
142
addElement(attributeValue);
143     }
144
145     /**
146      * Process nested ≶add> tag.
147      * Method calls by nested ≶add> tags.
148      * Nested attribute is added to current list.
149      * If role is defined, nested attribute is wrapped into an untypped definition
150      * containing attribute value and role.
151      */

152     public void processNestedTag(AddTag nestedTag) throws JspException JavaDoc {
153         // Get real value and check role
154
// If role is set, add it in attribute definition if any.
155
// If no attribute definition, create untyped one, and set role.
156
Object JavaDoc attributeValue = nestedTag.getRealValue();
157         AttributeDefinition def;
158
159         if (nestedTag.getRole() != null) {
160             try {
161                 def = ((AttributeDefinition) attributeValue);
162             } catch (ClassCastException JavaDoc ex) {
163                 def = new UntypedAttribute(attributeValue);
164             }
165             def.setRole(nestedTag.getRole());
166             attributeValue = def;
167         }
168         
169         // now add attribute to enclosing parent (i.e. : this object)
170
addElement(attributeValue);
171     }
172
173     /**
174      * Do start tag.
175      */

176     public int doStartTag() throws JspException JavaDoc {
177         return EVAL_BODY_INCLUDE;
178     }
179
180     /**
181      * Do end tag.
182      */

183     public int doEndTag() throws JspException JavaDoc {
184         PutListTagParent enclosingParent = findEnclosingParent();
185         enclosingParent.processNestedTag(this);
186         // Clear list to avoid reuse
187
releaseInternal();
188         return EVAL_PAGE;
189     }
190
191     /**
192      * Find enclosing parent tag accepting this tag.
193      * @throws JspException If we can't find an appropriate enclosing tag.
194      */

195     protected PutListTagParent findEnclosingParent() throws JspException JavaDoc {
196         try {
197             PutListTagParent parent =
198                 (PutListTagParent) findAncestorWithClass(this,
199                     PutListTagParent.class);
200                     
201             if (parent == null) {
202                 throw new JspException JavaDoc("Error - tag putList : enclosing tag doesn't accept 'putList' tag.");
203             }
204             
205             return parent;
206             
207         } catch (ClassCastException JavaDoc ex) {
208             throw new JspException JavaDoc("Error - tag putList : enclosing tag doesn't accept 'putList' tag.");
209         }
210     }
211
212 }
213
Popular Tags