001package org.apache.turbine.util.template;
002
003
004/*
005 * Licensed to the Apache Software Foundation (ASF) under one
006 * or more contributor license agreements.  See the NOTICE file
007 * distributed with this work for additional information
008 * regarding copyright ownership.  The ASF licenses this file
009 * to you under the Apache License, Version 2.0 (the
010 * "License"); you may not use this file except in compliance
011 * with the License.  You may obtain a copy of the License at
012 *
013 *   http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing,
016 * software distributed under the License is distributed on an
017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
018 * KIND, either express or implied.  See the License for the
019 * specific language governing permissions and limitations
020 * under the License.
021 */
022
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.apache.turbine.modules.Screen;
027import org.apache.turbine.modules.ScreenLoader;
028import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker;
029import org.apache.turbine.util.RunData;
030
031/**
032 * Returns output of a Screen module.  An instance of this is
033 * placed in the Velocity context by the VelocityDirectLayout.  This
034 * allows the screen to be executed only at rendering.
035 * Here's how it's used in a template:
036 *
037 * <p>
038 * <code>
039 * $screen_placeholder
040 * </code>
041 * <p>
042 * <code>
043 * $screen_placeholder.setScreen("Test")
044 * </code>
045 * </p>
046 *
047 * @author <a href="raphael@apache.org">Raphaƫl Luta</a>
048 * @version $Id: TemplateScreen.java 1709648 2015-10-20 17:08:10Z tv $
049 */
050public class TemplateScreen
051{
052    /** Logging */
053    private static Log log = LogFactory.getLog(TemplateScreen.class);
054
055    /* The RunData object. */
056    private final RunData data;
057
058    /* The name of the screen template. */
059    private String screen;
060
061    private final ScreenLoader screenLoader;
062
063    /**
064     * Constructor
065     *
066     * @param data A Turbine RunData object.
067     */
068    public TemplateScreen(RunData data)
069    {
070        this.data = data;
071        this.screen = data.getScreen();
072        this.screenLoader = (ScreenLoader)TurbineAssemblerBroker.getLoader(Screen.class);
073    }
074
075    /**
076     * Set the screen.
077     *
078     * @param screen A String with the name of the screen module
079     * @return A TemplateScreen (self).
080     */
081    public TemplateScreen setScreen(String screen)
082    {
083        this.screen = screen;
084        return this;
085    }
086
087    /**
088     * Builds the output of the navigation template.
089     *
090     * @return A String.
091     */
092    @Override
093    public String toString()
094    {
095        String returnValue = "";
096
097        try
098        {
099            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}