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.html;
016    
017    import java.io.IOException;
018    import java.io.LineNumberReader;
019    import java.io.Reader;
020    import java.io.StringReader;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.tapestry.AbstractComponent;
024    import org.apache.tapestry.IMarkupWriter;
025    import org.apache.tapestry.IRequestCycle;
026    
027    /**
028     * Inserts formatted text (possibly collected using a
029     * {@link org.apache.tapestry.form.TextArea} component. [<a
030     * href="../../../../../ComponentReference/InsertText.html">Component Reference</a>]
031     * <p>
032     * To maintain the line breaks provided originally, this component will break
033     * the input into individual lines and insert additional HTML to make each line
034     * seperate.
035     * <p>
036     * This can be down more simply, using the &lt;pre&gt; HTML element, but that
037     * usually renders the text in a non-proportional font.
038     * 
039     * @author Howard Lewis Ship
040     */
041    
042    public abstract class InsertText extends AbstractComponent
043    {
044    
045        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
046        {
047            if (cycle.isRewinding()) return;
048    
049            String value = getValue();
050    
051            if (value == null) return;
052    
053            StringReader reader = null;
054            LineNumberReader lineReader = null;
055            InsertTextMode mode = getMode();
056            boolean raw = getRaw();
057    
058            try
059            {
060                reader = new StringReader(value);
061    
062                lineReader = new LineNumberReader(reader);
063    
064                int lineNumber = 0;
065    
066                while(true)
067                {
068                    String line = lineReader.readLine();
069    
070                    // Exit loop at end of file.
071    
072                    if (line == null) break;
073    
074                    mode.writeLine(lineNumber, line, writer, raw);
075    
076                    lineNumber++;
077                }
078    
079            }
080            catch (IOException ex)
081            {
082                throw new ApplicationRuntimeException(HTMLMessages
083                        .textConversionError(ex), this, null, ex);
084            }
085            finally
086            {
087                close(lineReader);
088                close(reader);
089            }
090    
091        }
092    
093        private void close(Reader reader)
094        {
095            if (reader == null) return;
096    
097            try
098            {
099                reader.close();
100            }
101            catch (IOException e)
102            {
103            }
104        }
105    
106        /** Parameter. */
107        public abstract InsertTextMode getMode();
108    
109        public abstract void setMode(InsertTextMode mode);
110    
111        /** Parameter. */
112    
113        public abstract String getValue();
114    
115        /** Parameter. */
116    
117        public abstract boolean getRaw();
118    
119        /**
120         * Sets the mode parameter property to its default,
121         * {@link InsertTextMode#BREAK}.
122         * 
123         * @since 3.0
124         */
125        protected void finishLoad()
126        {
127            setMode(InsertTextMode.BREAK);
128        }
129    
130    }