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