Coverage Report - org.apache.tiles.template.PutAttributeModel
 
Classes in this File Line Coverage Branch Coverage Complexity
PutAttributeModel
89%
25/28
38%
7/18
5.5
 
 1  
 /*
 2  
  * $Id: PutAttributeModel.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.Attribute;
 28  
 import org.apache.tiles.AttributeContext;
 29  
 import org.apache.tiles.Expression;
 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>Put an attribute in enclosing attribute container tag.</strong>
 39  
  * </p>
 40  
  * <p>
 41  
  * Enclosing attribute container tag can be :
 42  
  * <ul>
 43  
  * <li>&lt;initContainer&gt;</li>
 44  
  * <li>&lt;definition&gt;</li>
 45  
  * <li>&lt;insertAttribute&gt;</li>
 46  
  * <li>&lt;insertDefinition&gt;</li>
 47  
  * <li>&lt;putListAttribute&gt;</li>
 48  
  * </ul>
 49  
  * (or any other tag which implements the <code>PutAttributeTagParent</code>
 50  
  * interface. Exception is thrown if no appropriate tag can be found.
 51  
  * </p>
 52  
  * <p>
 53  
  * Put tag can have following atributes :
 54  
  * <ul>
 55  
  * <li>name : Name of the attribute</li>
 56  
  * <li>value : value to put as attribute</li>
 57  
  * <li>type : value type. Possible type are : string (value is used as direct
 58  
  * string), template (value is used as a page url to insert), definition (value
 59  
  * is used as a definition name to insert), object (value is used as it is)</li>
 60  
  * <li>role : Role to check when 'insertAttribute' will be called.</li>
 61  
  * </ul>
 62  
  * </p>
 63  
  * <p>
 64  
  * Value can also come from tag body. Tag body is taken into account only if
 65  
  * value is not set by one of the tag attributes. In this case Attribute type is
 66  
  * "string", unless tag body define another type.
 67  
  * </p>
 68  
  *
 69  
  * @version $Rev: 1305937 $ $Date: 2012-03-28 05:15:15 +1100 (Wed, 28 Mar 2012) $
 70  
  * @since 2.2.0
 71  
  */
 72  1
 public class PutAttributeModel {
 73  
 
 74  
     /**
 75  
      * Executes the operation.
 76  
      * @param name The name of the attribute to put.
 77  
      * @param value The value of the attribute. Use this parameter, or
 78  
      * expression, or body.
 79  
      * @param expression The expression to calculate the value from. Use this
 80  
      * parameter, or value, or body.
 81  
      * @param role A comma-separated list of roles. If present, the attribute
 82  
      * will be rendered only if the current user belongs to one of the roles.
 83  
      * @param type The type (renderer) of the attribute.
 84  
      * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.
 85  
      * @param request The request.
 86  
      * @param modelBody The body.
 87  
      * @throws IOException If the body cannot be evaluated.
 88  
      * @since 2.2.0
 89  
      */
 90  
     public void execute(@Parameter(required = true) String name, Object value,
 91  
             String expression, String role, String type, boolean cascade,
 92  
             Request request, ModelBody modelBody) throws IOException {
 93  1
         Deque<Object> composeStack = ComposeStackUtil.getComposeStack(request);
 94  1
         Attribute attribute = new Attribute();
 95  1
         composeStack.push(attribute);
 96  1
         String currentBody = modelBody.evaluateAsString();
 97  1
         TilesContainer container = TilesAccess.getCurrentContainer(request);
 98  1
         attribute = (Attribute) composeStack.pop();
 99  1
         putAttributeInParent(attribute, container, composeStack, name,
 100  
                 value, expression, currentBody, role, type, cascade, request);
 101  1
     }
 102  
 
 103  
     /**
 104  
      * Determines the parent and puts the attribute inside it.
 105  
      *
 106  
      * @param attribute The attribute to put;
 107  
      * @param container The Tiles container to use.
 108  
      * @param composeStack The composing stack.
 109  
      * @param name The name of the attribute to put.
 110  
      * @param value The value of the attribute. Use this parameter, or
 111  
      * expression, or body.
 112  
      * @param expression The expression to calculate the value from. Use this
 113  
      * parameter, or value, or body.
 114  
      * @param body The body of the tag. Use this parameter, or value, or
 115  
      * expression.
 116  
      * @param role A comma-separated list of roles. If present, the attribute
 117  
      * will be rendered only if the current user belongs to one of the roles.
 118  
      * @param type The type (renderer) of the attribute.
 119  
      * @param cascade If <code>true</code> the attribute will be cascaded to all nested attributes.
 120  
      * @param request The request.
 121  
      */
 122  
     private void putAttributeInParent(Attribute attribute,
 123  
             TilesContainer container, Deque<Object> composeStack, String name,
 124  
             Object value, String expression, String body, String role,
 125  
             String type, boolean cascade, Request request) {
 126  1
         AttributeContext attributeContext = null;
 127  1
         if (!composeStack.isEmpty()) {
 128  1
             Object obj = composeStack.peek();
 129  1
             if (obj instanceof AttributeContext) {
 130  0
                 attributeContext = (AttributeContext) obj;
 131  
             }
 132  
         }
 133  1
         if (attributeContext == null) {
 134  1
             attributeContext = container.getAttributeContext(request);
 135  
         }
 136  1
         if (value != null) {
 137  1
             attribute.setValue(value);
 138  0
         } else if (attribute.getValue() == null && body != null) {
 139  0
             attribute.setValue(body);
 140  
         }
 141  1
         if (expression != null) {
 142  1
             attribute.setExpressionObject(Expression
 143  
                     .createExpressionFromDescribedExpression(expression));
 144  
         }
 145  1
         if (role != null) {
 146  1
             attribute.setRole(role);
 147  
         }
 148  1
         if (type != null) {
 149  1
             attribute.setRenderer(type);
 150  
         }
 151  
 
 152  1
         attributeContext.putAttribute(name, attribute, cascade);
 153  1
     }
 154  
 }