001 package 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 024 import org.apache.commons.logging.Log; 025 import org.apache.commons.logging.LogFactory; 026 import org.apache.ecs.ConcreteElement; 027 import org.apache.turbine.modules.Screen; 028 import org.apache.turbine.modules.ScreenLoader; 029 import org.apache.turbine.services.assemblerbroker.TurbineAssemblerBroker; 030 import 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 1071044 2011-02-15 20:47:31Z tv $ 050 */ 051 public class TemplateScreen 052 { 053 /** Logging */ 054 private static Log log = LogFactory.getLog(TemplateScreen.class); 055 056 /* The RunData object. */ 057 private RunData data; 058 059 /* The name of the screen template. */ 060 private String screen; 061 062 private 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 this.screenLoader = (ScreenLoader)TurbineAssemblerBroker.getLoader(Screen.NAME); 074 } 075 076 /** 077 * Set the screen. 078 * 079 * @param screen A String with the name of the screen module 080 * @return A TemplateScreen (self). 081 */ 082 public TemplateScreen setScreen(String screen) 083 { 084 this.screen = screen; 085 return this; 086 } 087 088 /** 089 * Builds the output of the navigation template. 090 * 091 * @return A String. 092 */ 093 public String toString() 094 { 095 String returnValue = ""; 096 097 try 098 { 099 ConcreteElement results = screenLoader.eval(data, this.screen); 100 101 if (results != null) 102 { 103 returnValue = results.toString(); 104 } 105 } 106 catch (Exception e) 107 { 108 log.error(e); 109 } 110 111 return returnValue; 112 } 113 }