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.assemblerbroker.TurbineAssemblerBroker;
29 import org.apache.turbine.util.RunData;
30
31 /**
32 * Returns output of a Screen module. An instance of this is
33 * placed in the Velocity context by the VelocityDirectLayout. This
34 * allows the screen to be executed only at rendering.
35 * Here's how it's used in a template:
36 *
37 * <p>
38 * <code>
39 * $screen_placeholder
40 * </code>
41 * <p>
42 * <code>
43 * $screen_placeholder.setScreen("Test")
44 * </code>
45 * </p>
46 *
47 * @author <a href="raphael@apache.org">Raphaƫl Luta</a>
48 * @version $Id: TemplateScreen.java 1709648 2015-10-20 17:08:10Z tv $
49 */
50 public class TemplateScreen
51 {
52 /** Logging */
53 private static Log log = LogFactory.getLog(TemplateScreen.class);
54
55 /* The RunData object. */
56 private final RunData data;
57
58 /* The name of the screen template. */
59 private String screen;
60
61 private final ScreenLoader screenLoader;
62
63 /**
64 * Constructor
65 *
66 * @param data A Turbine RunData object.
67 */
68 public TemplateScreen(RunData data)
69 {
70 this.data = data;
71 this.screen = data.getScreen();
72 this.screenLoader = (ScreenLoader)TurbineAssemblerBroker.getLoader(Screen.class);
73 }
74
75 /**
76 * Set the screen.
77 *
78 * @param screen A String with the name of the screen module
79 * @return A TemplateScreen (self).
80 */
81 public TemplateScreen setScreen(String screen)
82 {
83 this.screen = screen;
84 return this;
85 }
86
87 /**
88 * Builds the output of the navigation template.
89 *
90 * @return A String.
91 */
92 @Override
93 public String toString()
94 {
95 String returnValue = "";
96
97 try
98 {
99 String results = screenLoader.eval(data, this.screen);
100
101 if (results != null)
102 {
103 returnValue = results;
104 }
105 }
106 catch (Exception e)
107 {
108 log.error(e);
109 }
110
111 return returnValue;
112 }
113 }