1/*2 * $Id: DefinitionModel.java 797765 2009-07-25 13:20:26Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122package org.apache.tiles.template;
2324import org.apache.tiles.ArrayStack;
25import org.apache.tiles.Attribute;
26import org.apache.tiles.Definition;
27import org.apache.tiles.mgmt.MutableTilesContainer;
2829/***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 be35 * 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.040 */41publicclassDefinitionModel {
4243/***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 definition50 * 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.054 */55publicvoid 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 }
6162/***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.069 */70publicvoid end(MutableTilesContainer container,
71 ArrayStack<Object> composeStack, Object... requestItems) {
72 Definition definition = (Definition) composeStack.pop();
73 registerDefinition(definition, container, composeStack, requestItems);
74 }
7576/***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 definition84 * 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.089 */90publicvoid 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 }
9899/***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 definition105 * 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 */110private 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);
120return definition;
121 }
122123/***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 */131privatevoid registerDefinition(Definition definition,
132 MutableTilesContainer container, ArrayStack<Object> composeStack,
133 Object... requestItems) {
134 container.register(definition, requestItems);
135136if (composeStack.isEmpty()) {
137return;
138 }
139140 Object obj = composeStack.peek();
141if (obj instanceof Attribute) {
142 Attribute attribute = (Attribute) obj;
143 attribute.setValue(definition.getName());
144if (attribute.getRenderer() == null) {
145 attribute.setRenderer("definition");
146 }
147 }
148 }
149 }