This project has retired. For details please refer to its Attic page.
VelocityAutotagRuntime xref
View Javadoc

1   /*
2    * $Id: VelocityAutotagRuntime.java 1360343 2012-07-11 18:35:52Z 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  package org.apache.tiles.request.velocity.autotag;
22  
23  import java.io.Writer;
24  import java.util.Map;
25  import javax.servlet.ServletContext;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import org.apache.tiles.autotag.core.runtime.ModelBody;
29  import org.apache.tiles.autotag.core.runtime.AutotagRuntime;
30  import org.apache.tiles.request.Request;
31  import org.apache.tiles.request.servlet.ServletUtil;
32  import org.apache.tiles.request.velocity.VelocityRequest;
33  import org.apache.velocity.context.InternalContextAdapter;
34  import org.apache.velocity.runtime.directive.Directive;
35  import org.apache.velocity.runtime.parser.node.ASTBlock;
36  import org.apache.velocity.runtime.parser.node.ASTMap;
37  import org.apache.velocity.runtime.parser.node.Node;
38  import org.apache.velocity.tools.view.ViewContext;
39  
40  /**
41   * A Runtime for implementing Velocity Directives.
42   */
43  public class VelocityAutotagRuntime extends Directive implements AutotagRuntime<Request> {
44      private InternalContextAdapter context;
45      private Writer                 writer;
46      private Node                   node;
47      private Map<String, Object>    params;
48  
49      /** {@inheritDoc} */
50      @Override
51      public Request createRequest() {
52          ViewContext viewContext = (ViewContext) context.getInternalUserContext();
53          HttpServletRequest request = viewContext.getRequest();
54          HttpServletResponse response = viewContext.getResponse();
55          ServletContext servletContext = viewContext.getServletContext();
56          return VelocityRequest.createVelocityRequest(ServletUtil.getApplicationContext(servletContext),
57                                                       request,
58                                                       response,
59                                                       context,
60                                                       writer);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public ModelBody createModelBody() {
66          ASTBlock block = (ASTBlock) node.jjtGetChild(1);
67          return new VelocityModelBody(context, block, writer);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      @SuppressWarnings("unchecked")
73      public <T> T getParameter(String name, Class<T> type, T defaultValue) {
74          if (params == null) {
75              ASTMap astMap = (ASTMap) node.jjtGetChild(0);
76              params = (Map<String, Object>) astMap.value(context);
77          }
78          T result = (T) params.get(name);
79          if (result == null) {
80              result = defaultValue;
81          }
82          return result;
83      }
84  
85      /** {@inheritDoc} */
86      @Override
87      public String getName() {
88          return null;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public int getType() {
94          return 0;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public boolean render(InternalContextAdapter context, Writer writer, Node node) {
100         this.context = context;
101         this.writer = writer;
102         this.node = node;
103         return false;
104     }
105 
106 }