Coverage Report - org.apache.tiles.template.InsertDefinitionModel
 
Classes in this File Line Coverage Branch Coverage Complexity
InsertDefinitionModel
94%
16/17
50%
1/2
1.5
 
 1  
 /*
 2  
  * $Id: InsertDefinitionModel.java 1058106 2011-01-12 12:22:58Z 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 java.io.IOException;
 25  
 
 26  
 import org.apache.tiles.Attribute;
 27  
 import org.apache.tiles.AttributeContext;
 28  
 import org.apache.tiles.TilesContainer;
 29  
 import org.apache.tiles.access.TilesAccess;
 30  
 import org.apache.tiles.autotag.core.runtime.ModelBody;
 31  
 import org.apache.tiles.autotag.core.runtime.annotation.Parameter;
 32  
 import org.apache.tiles.request.Request;
 33  
 
 34  
 /**
 35  
  * <p>
 36  
  * <strong>Insert a definition.</strong>
 37  
  * </p>
 38  
  * <p>
 39  
  * Insert a definition with the possibility to override and specify parameters
 40  
  * (called attributes). A definition can be seen as a (partially or totally)
 41  
  * filled template that can override or complete attribute values.
 42  
  * <code>&lt;tiles:insertDefinition&gt;</code> allows to define these attributes
 43  
  * and pass them to the inserted jsp page, called template. Attributes are
 44  
  * defined using nested tag <code>&lt;tiles:putAttribute&gt;</code> or
 45  
  * <code>&lt;tiles:putListAttribute&gt;</code>.
 46  
  * </p>
 47  
  * <p>
 48  
  * You must specify <code>name</code> tag attribute, for inserting a definition
 49  
  * from definitions factory.
 50  
  * </p>
 51  
  * <p>
 52  
  * <strong>Example : </strong>
 53  
  * </p>
 54  
  *
 55  
  * <pre>
 56  
  * &lt;code&gt;
 57  
  *           &lt;tiles:insertDefinition name=&quot;.my.tiles.defininition flush=&quot;true&quot;&gt;
 58  
  *              &lt;tiles:putAttribute name=&quot;title&quot; value=&quot;My first page&quot; /&gt;
 59  
  *              &lt;tiles:putAttribute name=&quot;header&quot; value=&quot;/common/header.jsp&quot; /&gt;
 60  
  *              &lt;tiles:putAttribute name=&quot;footer&quot; value=&quot;/common/footer.jsp&quot; /&gt;
 61  
  *              &lt;tiles:putAttribute name=&quot;menu&quot; value=&quot;/basic/menu.jsp&quot; /&gt;
 62  
  *              &lt;tiles:putAttribute name=&quot;body&quot; value=&quot;/basic/helloBody.jsp&quot; /&gt;
 63  
  *           &lt;/tiles:insertDefinition&gt;
 64  
  *         &lt;/code&gt;
 65  
  * </pre>
 66  
  *
 67  
  * @version $Rev: 1058106 $ $Date: 2011-01-12 23:22:58 +1100 (Wed, 12 Jan 2011) $
 68  
  * @since 2.2.0
 69  
  */
 70  1
 public class InsertDefinitionModel {
 71  
 
 72  
     /**
 73  
      * Executes the operation.
 74  
      * @param definitionName The name of the definition to render.
 75  
      * @param template If specified, this template will be used instead of the
 76  
      * one used by the definition.
 77  
      * @param templateType The type of the template attribute.
 78  
      * @param templateExpression The expression to evaluate to get the value of the template.
 79  
      * @param role A comma-separated list of roles. If present, the definition
 80  
      * will be rendered only if the current user belongs to one of the roles.
 81  
      * @param preparer The preparer to use to invoke before the definition is
 82  
      * rendered. If specified, it overrides the preparer specified in the
 83  
      * definition itself.
 84  
      * @param flush If <code>true</code>, the response will be flushed after the insert.
 85  
      * @param request The request.
 86  
      * @param modelBody The body.
 87  
      * @throws IOException If something goes wrong.
 88  
      * @since 2.2.0
 89  
      */
 90  
     public void execute(
 91  
             @Parameter(name = "name", required = true) String definitionName,
 92  
             String template, String templateType, String templateExpression,
 93  
             String role, String preparer, boolean flush, Request request, ModelBody modelBody)
 94  
             throws IOException {
 95  1
         TilesContainer container = TilesAccess.getCurrentContainer(request);
 96  1
         container.startContext(request);
 97  1
         modelBody.evaluateWithoutWriting();
 98  1
         container = TilesAccess.getCurrentContainer(request);
 99  1
         renderDefinition(container, definitionName, template, templateType,
 100  
                 templateExpression, role, preparer, flush, request);
 101  1
     }
 102  
 
 103  
     /**
 104  
      * Renders a definition.
 105  
      *
 106  
      * @param container The container to use.
 107  
      * @param definitionName The name of the definition to render.
 108  
      * @param template If specified, this template will be used instead of the
 109  
      * one used by the definition.
 110  
      * @param templateType The type of the template attribute.
 111  
      * @param templateExpression The expression to evaluate to get the value of the template.
 112  
      * @param role A comma-separated list of roles. If present, the definition
 113  
      * will be rendered only if the current user belongs to one of the roles.
 114  
      * @param preparer The preparer to use to invoke before the definition is
 115  
      * rendered. If specified, it overrides the preparer specified in the
 116  
      * definition itself.
 117  
      * @param flush If <code>true</code>, the response will be flushed after the insert.
 118  
      * @param request The request.
 119  
      * @throws IOException If something goes wrong.
 120  
      */
 121  
     private void renderDefinition(TilesContainer container,
 122  
             String definitionName, String template, String templateType,
 123  
             String templateExpression, String role, String preparer,
 124  
             boolean flush, Request request) throws IOException {
 125  
         try {
 126  1
             AttributeContext attributeContext = container
 127  
                     .getAttributeContext(request);
 128  1
             Attribute templateAttribute = Attribute.createTemplateAttribute(template,
 129  
                     templateExpression, templateType, role);
 130  1
             attributeContext.setPreparer(preparer);
 131  1
             attributeContext.setTemplateAttribute(templateAttribute);
 132  1
             container.render(definitionName, request);
 133  1
             if (flush) {
 134  0
                 request.getWriter().flush();
 135  
             }
 136  
         } finally {
 137  1
             container.endContext(request);
 138  1
         }
 139  1
     }
 140  
 }