001    // Copyright 2007, 2008 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    
015    package org.apache.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.ComponentResources;
018    import org.apache.tapestry5.MarkupWriter;
019    import org.apache.tapestry5.annotations.Parameter;
020    import org.apache.tapestry5.annotations.SupportsInformalParameters;
021    import org.apache.tapestry5.ioc.annotations.Inject;
022    import org.apache.tapestry5.ioc.internal.util.InternalUtils;
023    
024    import 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    @SupportsInformalParameters
031    public class Output
032    {
033        /**
034         * The value to be output (before formatting). If the formatted value is blank, no output is produced.
035         */
036        @Parameter(required = true, autoconnect = true)
037        private Object value;
038    
039        /**
040         * The format to be applied to the object.
041         */
042        @Parameter(required = true, allowNull = false)
043        private Format format;
044    
045        /**
046         * If true, the default, then output is filtered, escaping any reserved characters. If false, the output is written
047         * raw.
048         */
049        @Parameter
050        private boolean filter = true;
051    
052        /**
053         * The element name, derived from the component template. This can even be overridden manually if desired (for
054         * example, to sometimes render a surrounding element and other times not).
055         */
056        @Parameter("componentResources.elementName")
057        private String elementName;
058    
059        @Inject
060        private ComponentResources resources;
061    
062    
063        boolean beginRender(MarkupWriter writer)
064        {
065            if (value == null) return false;
066    
067            String formatted = format.format(value);
068    
069            if (InternalUtils.isNonBlank(formatted))
070            {
071                if (elementName != null)
072                {
073                    writer.element(elementName);
074    
075                    resources.renderInformalParameters(writer);
076                }
077    
078                if (filter) writer.write(formatted);
079                else writer.writeRaw(formatted);
080    
081                if (elementName != null) writer.end();
082            }
083    
084            return false;
085        }
086    
087        // For testing.
088    
089        void setup(Object value, Format format, boolean filter, String elementName, ComponentResources resources)
090        {
091            this.value = value;
092            this.format = format;
093            this.filter = filter;
094            this.elementName = elementName;
095            this.resources = resources;
096        }
097    }