View Javadoc
1   package org.apache.turbine.util.template;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.turbine.modules.Screen;
27  import org.apache.turbine.modules.ScreenLoader;
28  import org.apache.turbine.services.TurbineServices;
29  import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
30  import org.apache.turbine.util.RunData;
31  
32  /**
33   * Returns output of a Screen module.  An instance of this is
34   * placed in the Velocity context by the VelocityDirectLayout.  This
35   * allows the screen to be executed only at rendering.
36   * Here's how it's used in a template:
37   *
38   * <p>
39   * <code>
40   * $screen_placeholder
41   * </code>
42   * <p>
43   * <code>
44   * $screen_placeholder.setScreen("Test")
45   * </code>
46   * </p>
47   *
48   * @author <a href="raphael@apache.org">Raphaƫl Luta</a>
49   * @version $Id: TemplateScreen.java 1723699 2016-01-08 11:29:21Z tv $
50   */
51  public class TemplateScreen
52  {
53      /** Logging */
54      private static Log log = LogFactory.getLog(TemplateScreen.class);
55  
56      /* The RunData object. */
57      private final RunData data;
58  
59      /* The name of the screen template. */
60      private String screen;
61  
62      private final ScreenLoader screenLoader;
63  
64      /**
65       * Constructor
66       *
67       * @param data A Turbine RunData object.
68       */
69      public TemplateScreen(RunData data)
70      {
71          this.data = data;
72          this.screen = data.getScreen();
73          AssemblerBrokerService abs = (AssemblerBrokerService)TurbineServices.getInstance()
74              .getService(AssemblerBrokerService.SERVICE_NAME);
75          this.screenLoader = (ScreenLoader)abs.getLoader(Screen.class);
76      }
77  
78      /**
79       * Set the screen.
80       *
81       * @param screen A String with the name of the screen module
82       * @return A TemplateScreen (self).
83       */
84      public TemplateScreen setScreen(String screen)
85      {
86          this.screen = screen;
87          return this;
88      }
89  
90      /**
91       * Builds the output of the navigation template.
92       *
93       * @return A String.
94       */
95      @Override
96      public String toString()
97      {
98          String returnValue = "";
99  
100         try
101         {
102             String results = screenLoader.eval(data, this.screen);
103 
104             if (results != null)
105             {
106                 returnValue = results;
107             }
108         }
109         catch (Exception e)
110         {
111             log.error(e);
112         }
113 
114         return returnValue;
115     }
116 }