View Javadoc

1   /*
2    * $Id: DefinitionTag.java 727715 2008-12-18 13:06:06Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.jsp.taglib.definition;
22  
23  import org.apache.tiles.Attribute;
24  import org.apache.tiles.Definition;
25  import org.apache.tiles.TilesContainer;
26  import org.apache.tiles.TilesException;
27  import org.apache.tiles.jsp.taglib.PutAttributeTag;
28  import org.apache.tiles.jsp.taglib.PutAttributeTagParent;
29  import org.apache.tiles.jsp.taglib.TilesTag;
30  import org.apache.tiles.mgmt.MutableTilesContainer;
31  import org.apache.tiles.Attribute.AttributeType;
32  import org.apache.tiles.access.TilesAccess;
33  
34  import javax.servlet.jsp.JspException;
35  import java.util.Map;
36  import java.util.HashMap;
37  
38  /***
39   * This is the tag handler for <tiles:definition>, which creates a custom
40   * definition. If the configured container is a {@link MutableTilesContainer},
41   * this newly created definition can be used in <tiles:insertDefinition&gt.
42   *
43   * @version $Rev: 727715 $ $Date: 2008-12-18 14:06:06 +0100 (Thu, 18 Dec 2008) $
44   */
45  public class DefinitionTag extends TilesTag
46      implements PutAttributeTagParent {
47  
48  
49      /***
50       * Name of the definition to configure.
51       */
52      private String name;
53  
54      /***
55       * The template of the definition.
56       */
57      private String template;
58  
59      /***
60       * The (optional) definition name that this definition extends.
61       */
62      private String extend;
63  
64      /***
65       * The role to check when rendering this definition.
66       */
67      private String role;
68  
69      /***
70       * The definition view preparer.
71       */
72      private String preparer;
73  
74  
75      /***
76       * The mutable Tiles container to use.
77       */
78      private MutableTilesContainer container;
79  
80      /***
81       * Maps attribute names with their attributes.
82       */
83      private Map<String, Attribute> attributes;
84  
85  
86      /***
87       * Returns the name of the definition to configure.
88       *
89       * @return The definition name.
90       */
91      public String getName() {
92          return name;
93      }
94  
95      /***
96       * Sets the name of the definition to configure.
97       *
98       * @param name The definition name.
99       */
100     public void setName(String name) {
101         this.name = name;
102     }
103 
104     /***
105      * Returns the template URI of the definition.
106      *
107      * @return The template URI.
108      */
109     public String getTemplate() {
110         return template;
111     }
112 
113     /***
114      * Sets the template URI of the definition.
115      *
116      * @param template The template URI.
117      */
118     public void setTemplate(String template) {
119         this.template = template;
120     }
121 
122     /***
123      * Returns the (optional) definition name that this definition extends.
124      *
125      * @return The extending definition name.
126      */
127     public String getExtends() {
128         return extend;
129     }
130 
131     /***
132      * Sets the (optional) definition name that this definition extends.
133      *
134      * @param extend The extending definition name.
135      */
136     public void setExtends(String extend) {
137         this.extend = extend;
138     }
139 
140     /***
141      * Returns the role to check when rendering this definition.
142      *
143      * @return The role to check.
144      */
145     public String getRole() {
146         return role;
147     }
148 
149     /***
150      * Sets the role to check when rendering this definition.
151      *
152      * @param role The role to check.
153      */
154     public void setRole(String role) {
155         this.role = role;
156     }
157 
158     /***
159      * Returns the definition view preparer.
160      *
161      * @return The view preparer name.
162      */
163     public String getPreparer() {
164         return preparer;
165     }
166 
167     /***
168      * Sets the definition view preparer.
169      *
170      * @param preparer The view preparer name.
171      */
172     public void setPreparer(String preparer) {
173         this.preparer = preparer;
174     }
175 
176     /*** {@inheritDoc} */
177     @Override
178     protected void reset() {
179         super.reset();
180         name = null;
181         template = null;
182         extend = null;
183         role = null;
184         preparer = null;
185         attributes.clear();
186     }
187 
188     /*** {@inheritDoc} */
189     public int doStartTag() throws JspException {
190         attributes = new HashMap<String, Attribute>();
191 
192         TilesContainer c =
193             TilesAccess.getContainer(pageContext.getServletContext());
194 
195         if (c == null) {
196             throw new JspException("TilesContainer not initialized");
197         }
198         if (!(c instanceof MutableTilesContainer)) {
199             throw new JspException(
200                     "Unable to define definition for a "
201                             + "container which does not implement MutableTilesContainer");
202         }
203 
204         container = (MutableTilesContainer) c;
205         return EVAL_BODY_INCLUDE;
206     }
207 
208     /*** {@inheritDoc} */
209     public int doEndTag() throws JspException {
210         Definition d = new Definition();
211         d.setName(name);
212         d.setTemplate(template);
213         d.setExtends(extend);
214         d.setRole(role);
215         d.setPreparer(preparer);
216         d.getAttributes().putAll(attributes);
217 
218         try {
219             container.register(d, pageContext);
220         } catch (TilesException e) {
221             throw new JspException("Unable to add definition. " , e);
222         }
223         return EVAL_PAGE;
224     }
225 
226     /***
227      * Reset member values for reuse. This method calls super.release(),
228      * which invokes TagSupport.release(), which typically does nothing.
229      *
230      * @param nestedTag The nested <code>PutAttributeTag</code>
231      * @throws JspException Never thrown, it's here for API compatibility.
232      */
233     public void processNestedTag(PutAttributeTag nestedTag) throws JspException {
234         Attribute attr = new Attribute(nestedTag.getValue(),
235             nestedTag.getRole(), AttributeType.getType(nestedTag.getType()));
236         attributes.put(nestedTag.getName(), attr);
237     }
238 }