View Javadoc

1   /*
2    * $Id: VelocityStyleTilesTool.java 1058140 2011-01-12 13:49:44Z 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.velocity.template;
23  
24  import java.io.IOException;
25  import java.io.Writer;
26  
27  import javax.servlet.ServletContext;
28  
29  import org.apache.tiles.Attribute;
30  import org.apache.tiles.AttributeContext;
31  import org.apache.tiles.TilesContainer;
32  import org.apache.tiles.access.TilesAccess;
33  import org.apache.tiles.request.Request;
34  import org.apache.tiles.request.servlet.ServletUtil;
35  import org.apache.tiles.request.velocity.VelocityRequest;
36  import org.apache.velocity.context.InternalContextAdapter;
37  import org.apache.velocity.runtime.Renderable;
38  
39  /**
40   * Tiles Tool to be used "the classic way".
41   *
42   * @version $Rev: 1058140 $ $Date: 2011-01-13 00:49:44 +1100 (Thu, 13 Jan 2011) $
43   * @since 2.2.0
44   */
45  public class VelocityStyleTilesTool extends ContextHolder {
46  
47      /**
48       * Returns an attribute.
49       *
50       * @param key The name of the attribute to get.
51       * @return The Attribute.
52       * @since 2.2.0
53       */
54      public Attribute get(String key) {
55          Request velocityRequest = createVelocityRequest(getServletContext(),
56                  null);
57          TilesContainer container = TilesAccess.getCurrentContainer(velocityRequest);
58          AttributeContext attributeContext = container
59                  .getAttributeContext(velocityRequest);
60          Attribute attribute = attributeContext.getAttribute(key);
61          return attribute;
62      }
63  
64      /**
65       * Creates a new empty attribute.
66       *
67       * @return The created attribute.
68       * @since 2.2.0
69       */
70      public Attribute createAttribute() {
71          return new Attribute();
72      }
73  
74      /**
75       * Creates an attribute that is a copy of the one passed as a parameter.
76       *
77       * @param attribute The attribute to copy.
78       * @return The copied attribute.
79       * @since 2.2.0
80       */
81      public Attribute clone(Attribute attribute) {
82          return new Attribute(attribute);
83      }
84  
85      /**
86       * Creates an attribute that represents a template.
87       *
88       * @param template The template.
89       * @return The attribute.
90       * @since 2.2.0
91       */
92      public Attribute createTemplateAttribute(String template) {
93          return Attribute.createTemplateAttribute(template);
94      }
95  
96      /**
97       * Renders an attribute.
98       *
99       * @param attribute The attribute to render.
100      * @return The renderable object, ready to be rendered.
101      * @since 2.2.0
102      */
103     public Renderable render(final Attribute attribute) {
104         return new AbstractDefaultToStringRenderable(getVelocityContext(),
105                 null, getResponse(), getRequest()) {
106 
107             public boolean render(InternalContextAdapter context, Writer writer)
108                     throws IOException {
109                 Request velocityRequest = createVelocityRequest(
110                         getServletContext(), writer);
111                 TilesContainer container = TilesAccess
112                         .getCurrentContainer(velocityRequest);
113                 container.render(attribute, velocityRequest);
114                 return true;
115             }
116 
117         };
118     }
119 
120     /**
121      * Renders a definition. It can be used in conjunction with
122      * {@link #startAttributeContext()} and {@link #endAttributeContext()} to
123      * customize appearance.
124      *
125      * @param definitionName The name of the definition to render.
126      * @return The renderable that renders the definition.
127      * @since 2.2.0
128      */
129     public Renderable renderDefinition(final String definitionName) {
130         return new AbstractDefaultToStringRenderable(getVelocityContext(),
131                 null, getResponse(), getRequest()) {
132 
133             public boolean render(InternalContextAdapter context, Writer writer) {
134                 Request velocityRequest = createVelocityRequest(
135                         getServletContext(), writer);
136                 TilesContainer container = TilesAccess
137                         .getCurrentContainer(velocityRequest);
138                 container.render(definitionName, velocityRequest);
139                 return true;
140             }
141 
142         };
143     }
144 
145     /**
146      * Renders the current attribute context. It can be used in conjunction with
147      * {@link #startAttributeContext()} and {@link #endAttributeContext()} to
148      * customize appearance.
149      *
150      * @return The renderable that renders the current attribute context.
151      * @since 2.2.0
152      */
153     public Renderable renderAttributeContext() {
154         return new AbstractDefaultToStringRenderable(getVelocityContext(),
155                 null, getResponse(), getRequest()) {
156 
157             public boolean render(InternalContextAdapter context, Writer writer) {
158                 Request velocityRequest = createVelocityRequest(
159                         getServletContext(), writer);
160                 TilesContainer container = TilesAccess
161                         .getCurrentContainer(velocityRequest);
162                 container.renderContext(velocityRequest);
163                 return true;
164             }
165 
166         };
167     }
168 
169     /**
170      * Starts the attribute context. Remember to call
171      * {@link #endAttributeContext()} when finished!
172      *
173      * @return The started attribute context, ready to be customized.
174      * @since 2.2.0
175      */
176     public AttributeContext startAttributeContext() {
177         Request velocityRequest = createVelocityRequest(
178                 getServletContext(), null);
179         TilesContainer container = TilesAccess
180                 .getCurrentContainer(velocityRequest);
181         return container.startContext(velocityRequest);
182     }
183 
184     /**
185      * Ends the current attribute context. To be called after
186      * {@link #startAttributeContext()}.
187      *
188      * @return The tool itself.
189      * @since 2.2.0
190      */
191     public VelocityStyleTilesTool endAttributeContext() {
192         Request velocityRequest = createVelocityRequest(getServletContext(),
193                 null);
194         TilesContainer container = TilesAccess
195                 .getCurrentContainer(velocityRequest);
196         container.endContext(velocityRequest);
197         return this;
198     }
199 
200     /**
201      * Returns the current attribute context.
202      *
203      * @return The current attribute context.
204      * @since 2.2.0
205      */
206     public AttributeContext getAttributeContext() {
207         Request velocityRequest = createVelocityRequest(
208                 getServletContext(), null);
209         TilesContainer container = TilesAccess
210                 .getCurrentContainer(velocityRequest);
211         return container.getAttributeContext(velocityRequest);
212     }
213 
214     /**
215      * Sets the current container for the current request.
216      *
217      * @param containerKey The key of the container to set as "current" for the current request.
218      * @return The tool itself.
219      * @since 2.2.0
220      */
221     public VelocityStyleTilesTool setCurrentContainer(String containerKey) {
222         Request velocityRequest = createVelocityRequest(
223                 getServletContext(), null);
224         TilesAccess.setCurrentContainer(velocityRequest, containerKey);
225         return this;
226     }
227 
228     /** {@inheritDoc} */
229     @Override
230     public String toString() {
231         return "";
232     }
233 
234     /**
235      * Creates a Velocity request.
236      *
237      * @param servletContext The servlet context.
238      * @param writer The writer.
239      * @return The created request.
240      */
241     protected Request createVelocityRequest(
242             ServletContext servletContext, Writer writer) {
243         return VelocityRequest.createVelocityRequest(ServletUtil
244                 .getApplicationContext(servletContext), getRequest(),
245                 getResponse(), getVelocityContext(), writer);
246     }
247 }