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.TurbineServices;
029import org.apache.turbine.services.assemblerbroker.AssemblerBrokerService;
030import org.apache.turbine.util.RunData;
031
032/**
033 * Returns output of a Screen module.  An instance of this is
034 * placed in the Velocity context by the VelocityDirectLayout.  This
035 * allows the screen to be executed only at rendering.
036 * Here's how it's used in a template:
037 *
038 * <p>
039 * <code>
040 * $screen_placeholder
041 * </code>
042 * <p>
043 * <code>
044 * $screen_placeholder.setScreen("Test")
045 * </code>
046 * </p>
047 *
048 * @author <a href="raphael@apache.org">Raphaël Luta</a>
049 * @version $Id: TemplateScreen.java 1723699 2016-01-08 11:29:21Z tv $
050 */
051public class TemplateScreen
052{
053    /** Logging */
054    private static Log log = LogFactory.getLog(TemplateScreen.class);
055
056    /* The RunData object. */
057    private final RunData data;
058
059    /* The name of the screen template. */
060    private String screen;
061
062    private final ScreenLoader screenLoader;
063
064    /**
065     * Constructor
066     *
067     * @param data A Turbine RunData object.
068     */
069    public TemplateScreen(RunData data)
070    {
071        this.data = data;
072        this.screen = data.getScreen();
073        AssemblerBrokerService abs = (AssemblerBrokerService)TurbineServices.getInstance()
074            .getService(AssemblerBrokerService.SERVICE_NAME);
075        this.screenLoader = (ScreenLoader)abs.getLoader(Screen.class);
076    }
077
078    /**
079     * Set the screen.
080     *
081     * @param screen A String with the name of the screen module
082     * @return A TemplateScreen (self).
083     */
084    public TemplateScreen setScreen(String screen)
085    {
086        this.screen = screen;
087        return this;
088    }
089
090    /**
091     * Builds the output of the navigation template.
092     *
093     * @return A String.
094     */
095    @Override
096    public String toString()
097    {
098        String returnValue = "";
099
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}