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 org.apache.commons.io.IOUtils;
018    import org.apache.hivemind.ApplicationRuntimeException;
019    import org.apache.tapestry.AbstractComponent;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    
023    import java.io.IOException;
024    import java.io.LineNumberReader;
025    import java.io.StringReader;
026    import java.text.Format;
027    
028    /**
029     * Used to insert some text (from a parameter) into the HTML. [ <a
030     * href="../../../../../ComponentReference/Insert.html">Component Reference
031     * </a>]
032     *
033     * @author Howard Lewis Ship
034     */
035    
036    public abstract class Insert extends AbstractComponent
037    {
038    
039        /**
040         * Prints its value parameter, possibly formatted by its format parameter.
041         */
042    
043        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
044        {
045            if (cycle.isRewinding())
046                return;
047    
048            Object value = getValue();
049    
050            if (value == null)
051                return;
052    
053            String insert = null;
054    
055            Format format = getFormat();
056    
057            if (format == null)
058            {
059                insert = value.toString();
060            }
061            else
062            {
063                try
064                {
065                    insert = format.format(value);
066                }
067                catch (Exception ex)
068                {
069                    throw new ApplicationRuntimeException(ComponentMessages
070                            .unableToFormat(this, value, ex), this, getBinding(
071                            "format").getLocation(), ex);
072                }
073            }
074    
075            boolean render = getRenderTag() || isParameterBound("class");
076    
077            if (render)
078            {
079                writer.begin(getTemplateTagName());
080    
081                renderInformalParameters(writer, cycle);
082    
083                if (getId() != null && !isParameterBound("id"))
084                    renderIdAttribute(writer, cycle);
085            }
086    
087            printText(writer, cycle, insert);
088    
089            if (render)
090                writer.end();
091        }
092    
093        protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value)
094        {
095            if (getMode() == null)
096            {
097                writer.print(value, getRaw());
098                return;
099            }
100    
101            StringReader reader = null;
102            LineNumberReader lineReader = null;
103            InsertMode mode = getMode();
104            boolean raw = getRaw();
105    
106            try {
107                reader = new StringReader(value);
108                lineReader = new LineNumberReader(reader);
109    
110                int lineNumber = 0;
111    
112                while(true)
113                {
114                    String line = lineReader.readLine();
115    
116                    // Exit loop at end of file.
117    
118                    if (line == null)
119                        break;
120    
121                    mode.writeLine(lineNumber, line, writer, raw);
122    
123                    lineNumber++;
124                }
125    
126            } catch (IOException ex)
127            {
128                throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex);
129            } finally
130            {
131                IOUtils.closeQuietly(lineReader);
132                IOUtils.closeQuietly(reader);
133            }
134        }
135    
136        public abstract Object getValue();
137    
138        public abstract Format getFormat();
139    
140        public abstract boolean getRaw();
141    
142        public abstract boolean getRenderTag();
143    
144        public abstract InsertMode getMode();
145    }