Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]

    1   // Copyright 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.corelib.components;
   16   
   17   import org.apache.tapestry5.ComponentResources;
   18   import org.apache.tapestry5.MarkupWriter;
   19   import org.apache.tapestry5.annotations.Parameter;
   20   import org.apache.tapestry5.annotations.SupportsInformalParameters;
   21   import org.apache.tapestry5.ioc.annotations.Inject;
   22   import org.apache.tapestry5.ioc.internal.util.InternalUtils;
   23   
   24   import java.text.Format;
   25   
   26   /**
   27    * A component for formatting output. If the component is represented in the template using an element, then the element
   28    * (plus any informal parameters) will be output around the formatted value.
   29    */
   30   @SupportsInformalParameters
   31   public class Output
   32   {
   33       /**
   34        * The value to be output (before formatting). If the formatted value is blank, no output is produced.
   35        */
   36       @Parameter(required = true, autoconnect = true)
   37       private Object value;
   38   
   39       /**
   40        * The format to be applied to the object.
   41        */
   42       @Parameter(required = true, allowNull = false)
   43       private Format format;
   44   
   45       /**
   46        * If true, the default, then output is filtered, escaping any reserved characters. If false, the output is written
   47        * raw.
   48        */
   49       @Parameter
   50       private boolean filter = true;
   51   
   52       /**
   53        * The element name, derived from the component template. This can even be overridden manually if desired (for
   54        * example, to sometimes render a surrounding element and other times not).
   55        */
   56       @Parameter("componentResources.elementName")
   57       private String elementName;
   58   
   59       @Inject
   60       private ComponentResources resources;
   61   
   62   
   63       boolean beginRender(MarkupWriter writer)
   64       {
   65           if (value == null) return false;
   66   
   67           String formatted = format.format(value);
   68   
   69           if (InternalUtils.isNonBlank(formatted))
   70           {
   71               if (elementName != null)
   72               {
   73                   writer.element(elementName);
   74   
   75                   resources.renderInformalParameters(writer);
   76               }
   77   
   78               if (filter) writer.write(formatted);
   79               else writer.writeRaw(formatted);
   80   
   81               if (elementName != null) writer.end();
   82           }
   83   
   84           return false;
   85       }
   86   
   87       // For testing.
   88   
   89       void setup(Object value, Format format, boolean filter, String elementName, ComponentResources resources)
   90       {
   91           this.value = value;
   92           this.format = format;
   93           this.filter = filter;
   94           this.elementName = elementName;
   95           this.resources = resources;
   96       }
   97   }

Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]