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) 
073                        break;
074    
075                    mode.writeLine(lineNumber, line, writer, raw);
076                    
077                    lineNumber++;
078                }
079    
080            }
081            catch (IOException ex)
082            {
083                throw new ApplicationRuntimeException(HTMLMessages
084                        .textConversionError(ex), this, null, ex);
085            }
086            finally
087            {
088                close(lineReader);
089                close(reader);
090            }
091    
092        }
093    
094        private void close(Reader reader)
095        {
096            if (reader == null) return;
097    
098            try
099            {
100                reader.close();
101            }
102            catch (IOException e)
103            {
104            }
105        }
106    
107        /** Parameter. */
108        public abstract InsertTextMode getMode();
109    
110        public abstract void setMode(InsertTextMode mode);
111    
112        /** Parameter. */
113    
114        public abstract String getValue();
115    
116        /** Parameter. */
117    
118        public abstract boolean getRaw();
119    
120        /**
121         * Sets the mode parameter property to its default,
122         * {@link InsertTextMode#BREAK}.
123         * 
124         * @since 3.0
125         */
126        protected void finishLoad()
127        {
128            setMode(InsertTextMode.BREAK);
129        }
130    
131    }