This project has retired. For details please refer to its Attic page.
DefinitionModel xref
View Javadoc

1   /*
2    * $Id: DefinitionModel.java 797765 2009-07-25 13:20:26Z 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  
22  package org.apache.tiles.template;
23  
24  import org.apache.tiles.ArrayStack;
25  import org.apache.tiles.Attribute;
26  import org.apache.tiles.Definition;
27  import org.apache.tiles.mgmt.MutableTilesContainer;
28  
29  /***
30   * <p>
31   * <strong>Create a definition at runtime. </strong>
32   * </p>
33   * <p>
34   * Create a new definition at runtime. Newly created definition will be
35   * available across the entire request.
36   * </p>
37   *
38   * @version $Rev: 797765 $ $Date: 2009-07-25 15:20:26 +0200 (sab, 25 lug 2009) $
39   * @since 2.2.0
40   */
41  public class DefinitionModel {
42  
43      /***
44       * Starts the operation.
45       *
46       * @param composeStack The compose stack,
47       * @param name The name of the definition to create. If not specified, an anonymous definition will be created.
48       * @param template The template of this definition.
49       * @param role A comma-separated list of roles. If present, the definition
50       * will be rendered only if the current user belongs to one of the roles.
51       * @param extendsParam The definition name that this definition extends.
52       * @param preparer The preparer to use to invoke before the definition is rendered.
53       * @since 2.2.0
54       */
55      public void start(ArrayStack<Object> composeStack, String name, String template,
56              String role, String extendsParam, String preparer) {
57          Definition definition = createDefinition(name, template, role,
58                  extendsParam, preparer);
59          composeStack.push(definition);
60      }
61  
62      /***
63       * Ends the operation.
64       *
65       * @param container The Tiles container to use. It must be "mutable".
66       * @param composeStack The compose stack.
67       * @param requestItems The request objects.
68       * @since 2.2.0
69       */
70      public void end(MutableTilesContainer container,
71              ArrayStack<Object> composeStack, Object... requestItems) {
72          Definition definition = (Definition) composeStack.pop();
73          registerDefinition(definition, container, composeStack, requestItems);
74      }
75  
76      /***
77       * Executes the operation.
78       *
79       * @param container The Tiles container to use. It must be "mutable".
80       * @param composeStack The compose stack.
81       * @param name The name of the definition to create. If not specified, an anonymous definition will be created.
82       * @param template The template of this definition.
83       * @param role A comma-separated list of roles. If present, the definition
84       * will be rendered only if the current user belongs to one of the roles.
85       * @param extendsParam The definition name that this definition extends.
86       * @param preparer The preparer to use to invoke before the definition is rendered.
87       * @param requestItems The request objects.
88       * @since 2.2.0
89       */
90      public void execute(MutableTilesContainer container,
91              ArrayStack<Object> composeStack, String name, String template,
92              String role, String extendsParam, String preparer,
93              Object... requestItems) {
94          Definition definition = createDefinition(name, template, role,
95                  extendsParam, preparer);
96          registerDefinition(definition, container, composeStack, requestItems);
97      }
98  
99      /***
100      * Creates the definition to store.
101      *
102      * @param name The name of the definition to create. If not specified, an anonymous definition will be created.
103      * @param template The template of this definition.
104      * @param role A comma-separated list of roles. If present, the definition
105      * will be rendered only if the current user belongs to one of the roles.
106      * @param extendsParam The definition name that this definition extends.
107      * @param preparer The preparer to use to invoke before the definition is rendered.
108      * @return The created definition.
109      */
110     private Definition createDefinition(String name, String template,
111             String role, String extendsParam, String preparer) {
112         Definition definition = new Definition();
113         definition.setName(name);
114         Attribute templateAttribute = Attribute
115                 .createTemplateAttribute(template);
116         templateAttribute.setRole(role);
117         definition.setTemplateAttribute(templateAttribute);
118         definition.setExtends(extendsParam);
119         definition.setPreparer(preparer);
120         return definition;
121     }
122 
123     /***
124      * Registers a definition in the container.
125      *
126      * @param definition The definition to register.
127      * @param container The container into which the definition will be registered.
128      * @param composeStack The compose stack,
129      * @param requestItems The request object.
130      */
131     private void registerDefinition(Definition definition,
132             MutableTilesContainer container, ArrayStack<Object> composeStack,
133             Object... requestItems) {
134         container.register(definition, requestItems);
135 
136         if (composeStack.isEmpty()) {
137             return;
138         }
139 
140         Object obj = composeStack.peek();
141         if (obj instanceof Attribute) {
142             Attribute attribute = (Attribute) obj;
143             attribute.setValue(definition.getName());
144             if (attribute.getRenderer() == null) {
145                 attribute.setRenderer("definition");
146             }
147         }
148     }
149 }