001// Copyright 2007, 2008, 2011 The Apache Software Foundation 002// 003// Licensed under the Apache License, Version 2.0 (the "License"); 004// you may not use this file except in compliance with the License. 005// You may obtain a copy of the License at 006// 007// http://www.apache.org/licenses/LICENSE-2.0 008// 009// Unless required by applicable law or agreed to in writing, software 010// distributed under the License is distributed on an "AS IS" BASIS, 011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012// See the License for the specific language governing permissions and 013// limitations under the License. 014 015package org.apache.tapestry5.corelib.components; 016 017import org.apache.tapestry5.ComponentResources; 018import org.apache.tapestry5.MarkupWriter; 019import org.apache.tapestry5.annotations.Parameter; 020import org.apache.tapestry5.annotations.SupportsInformalParameters; 021import org.apache.tapestry5.ioc.annotations.Inject; 022import org.apache.tapestry5.ioc.internal.util.InternalUtils; 023 024import java.text.Format; 025 026/** 027 * A component for formatting output. If the component is represented in the template using an element, then the element 028 * (plus any informal parameters) will be output around the formatted value. 029 * 030 * @tapestrydoc 031 */ 032@SupportsInformalParameters 033public class Output 034{ 035 /** 036 * The value to be output (before formatting). If the formatted value is blank, no output is produced. 037 */ 038 @Parameter(required = true, autoconnect = true) 039 private Object value; 040 041 /** 042 * The format to be applied to the object. 043 */ 044 @Parameter(required = true, allowNull = false) 045 private Format format; 046 047 /** 048 * If true, the default, then output is filtered, escaping any reserved characters. If false, the output is written 049 * raw. 050 */ 051 @Parameter 052 private boolean filter = true; 053 054 /** 055 * The element name, derived from the component template. This can even be overridden manually if desired (for 056 * example, to sometimes render a surrounding element and other times not). 057 */ 058 @Parameter("componentResources.elementName") 059 private String elementName; 060 061 @Inject 062 private ComponentResources resources; 063 064 065 boolean beginRender(MarkupWriter writer) 066 { 067 if (value == null) return false; 068 069 String formatted = format.format(value); 070 071 if (InternalUtils.isNonBlank(formatted)) 072 { 073 if (elementName != null) 074 { 075 writer.element(elementName); 076 077 resources.renderInformalParameters(writer); 078 } 079 080 if (filter) writer.write(formatted); 081 else writer.writeRaw(formatted); 082 083 if (elementName != null) writer.end(); 084 } 085 086 return false; 087 } 088 089 // For testing. 090 091 void setup(Object value, Format format, boolean filter, String elementName, ComponentResources resources) 092 { 093 this.value = value; 094 this.format = format; 095 this.filter = filter; 096 this.elementName = elementName; 097 this.resources = resources; 098 } 099}