001    // Copyright 2004, 2005 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.tapestry.components;
016    
017    import java.text.Format;
018    
019    import org.apache.hivemind.ApplicationRuntimeException;
020    import org.apache.tapestry.AbstractComponent;
021    import org.apache.tapestry.IMarkupWriter;
022    import org.apache.tapestry.IRequestCycle;
023    
024    /**
025     * Used to insert some text (from a parameter) into the HTML. [ <a
026     * href="../../../../../ComponentReference/Insert.html">Component Reference
027     * </a>]
028     * 
029     * @author Howard Lewis Ship
030     */
031    
032    public abstract class Insert extends AbstractComponent
033    {
034    
035        /**
036         * Prints its value parameter, possibly formatted by its format parameter.
037         */
038    
039        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
040        {
041            if (cycle.isRewinding()) return;
042    
043            Object value = getValue();
044    
045            if (value == null) return;
046    
047            String insert = null;
048    
049            Format format = getFormat();
050    
051            if (format == null)
052            {
053                insert = value.toString();
054            }
055            else
056            {
057                try
058                {
059                    insert = format.format(value);
060                }
061                catch (Exception ex)
062                {
063                    throw new ApplicationRuntimeException(ComponentMessages
064                            .unableToFormat(this, value, ex), this, getBinding(
065                            "format").getLocation(), ex);
066                }
067            }
068    
069            String styleClass = getStyleClass();
070    
071            if (styleClass != null)
072            {
073                writer.begin("span");
074                writer.attribute("class", styleClass);
075    
076                renderInformalParameters(writer, cycle);
077            }
078    
079            writer.print(insert, getRaw());
080    
081            if (styleClass != null) writer.end(); // <span>
082        }
083    
084        public abstract Object getValue();
085    
086        public abstract Format getFormat();
087    
088        public abstract String getStyleClass();
089    
090        public abstract boolean getRaw();
091    
092    }