View Javadoc

1   /*
2    * $Id: PutListAttributeModel.java 1305937 2012-03-27 18:15:15Z nlebas $
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 java.io.IOException;
25  import java.util.Deque;
26  
27  import org.apache.tiles.AttributeContext;
28  import org.apache.tiles.Definition;
29  import org.apache.tiles.ListAttribute;
30  import org.apache.tiles.TilesContainer;
31  import org.apache.tiles.access.TilesAccess;
32  import org.apache.tiles.autotag.core.runtime.ModelBody;
33  import org.apache.tiles.autotag.core.runtime.annotation.Parameter;
34  import org.apache.tiles.request.Request;
35  
36  /**
37   * <p>
38   * <strong>Declare a list that will be pass as attribute to tile. </strong>
39   * </p>
40   * <p>
41   * Declare a list that will be pass as attribute to tile. List elements are
42   * added using the tags 'addAttribute' or 'addListAttribute'. This tag can only
43   * be used inside 'insertTemplate', 'insertDefinition', 'definition' tags.
44   * </p>
45   *
46   * @version $Rev: 1305937 $ $Date: 2012-03-28 05:15:15 +1100 (Wed, 28 Mar 2012) $
47   * @since 2.2.0
48   */
49  public class PutListAttributeModel {
50  
51      /**
52       * Executes the model.
53       *
54       * @param name The name of the attribute to put.
55       * @param role A comma-separated list of roles. If present, the attribute
56       * will be rendered only if the current user belongs to one of the roles.
57       * @param inherit If <code>true</code>, the list attribute will use, as first elements, the
58       * list contained in the list attribute, put with the same name, of the containing definition.
59       * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.
60       * @param request The request.
61       * @param modelBody The body.
62       * @throws IOException If the body cannot be evaluated.
63       */
64      public void execute(@Parameter(required = true) String name, String role,
65              boolean inherit, boolean cascade, Request request,
66              ModelBody modelBody) throws IOException {
67          Deque<Object> composeStack = ComposeStackUtil.getComposeStack(request);
68          ListAttribute listAttribute = new ListAttribute();
69          listAttribute.setRole(role);
70          listAttribute.setInherit(inherit);
71          composeStack.push(listAttribute);
72          modelBody.evaluateWithoutWriting();
73          TilesContainer container = TilesAccess.getCurrentContainer(request);
74          listAttribute = (ListAttribute) composeStack.pop();
75          AttributeContext attributeContext = null;
76          if (!composeStack.isEmpty()) {
77              Object obj = composeStack.peek();
78              if (obj instanceof Definition) {
79                  attributeContext = (AttributeContext) obj;
80              }
81          }
82          if (attributeContext == null) {
83              attributeContext = container.getAttributeContext(request);
84          }
85          attributeContext.putAttribute(name, listAttribute, cascade);
86      }
87  }