View Javadoc

1   package org.apache.turbine.modules.layouts;
2   
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  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import org.apache.ecs.ConcreteElement;
26  
27  import org.apache.turbine.TurbineConstants;
28  import org.apache.turbine.modules.Layout;
29  import org.apache.turbine.modules.ScreenLoader;
30  import org.apache.turbine.services.velocity.TurbineVelocity;
31  import org.apache.turbine.util.RunData;
32  import org.apache.turbine.util.template.TemplateNavigation;
33  
34  import org.apache.velocity.context.Context;
35  
36  /***
37   * This Layout module allows Velocity templates to be used as layouts.
38   * Since dynamic content is supposed to be primarily located in
39   * screens and navigations there should be relatively few reasons to
40   * subclass this Layout.
41   *
42   * This class uses ECS to render the layout.
43   *
44   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
45   * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
46   * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47   * @version $Id: VelocityECSLayout.java 534527 2007-05-02 16:10:59Z tv $
48   * @deprecated you should use VelocityOnlyLayout
49   */
50  public class VelocityECSLayout
51      extends Layout
52  {
53      /*** Logging */
54      private static Log log = LogFactory.getLog(VelocityECSLayout.class);
55  
56      /*** The prefix for lookup up layout pages */
57      private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
58  
59      /*** Warn only once */
60      private static boolean hasWarned = false;
61  
62      /***
63       * C'tor warns once about deprecation.
64       */
65      public VelocityECSLayout()
66      {
67          if (!hasWarned)
68          {
69              log.warn("The VelocityECSLayout is deprecated. "
70                       + "Please switch to VelocityOnlyLayout.");
71              hasWarned = true;
72          }
73      }
74  
75      /***
76       * Build the layout.  Also sets the ContentType and Locale headers
77       * of the HttpServletResponse object.
78       *
79       * @param data Turbine information.
80       * @exception Exception a generic exception.
81       */
82      public void doBuild(RunData data)
83          throws Exception
84      {
85          // Get the context needed by Velocity.
86          Context context = TurbineVelocity.getContext(data);
87  
88          String screenName = data.getScreen();
89  
90          log.debug("Loading Screen " + screenName);
91  
92          // First, generate the screen and put it in the context so
93          // we can grab it the layout template.
94          ConcreteElement results =
95              ScreenLoader.getInstance().eval(data, screenName);
96  
97          String returnValue = (results == null) ? "" : results.toString();
98  
99          // variable for the screen in the layout template
100         context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
101 
102         // variable to reference the navigation screen in the layout template
103         context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
104                     new TemplateNavigation(data));
105 
106         // Grab the layout template set in the VelocityPage.
107         // If null, then use the default layout template
108         // (done by the TemplateInfo object)
109         String templateName = data.getTemplateInfo().getLayoutTemplate();
110 
111         log.debug("Now trying to render layout " + templateName);
112 
113         // Finally, generate the layout template and add it to the body of the
114         // Document in the RunData.
115         data.getPage().getBody().addElement(TurbineVelocity
116                 .handleRequest(context, prefix +  templateName));
117     }
118 }