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 {@link org.apache.tapestry.form.TextArea}
029     * component. [<a href="../../../../../ComponentReference/InsertText.html">Component Reference</a>]
030     * <p>
031     * To maintain the line breaks provided originally, this component will break the input into
032     * individual lines and insert additional HTML to make each line seperate.
033     * <p>
034     * This can be down more simply, using the <pre> HTML element, but that usually renders the
035     * text in a non-proportional font.
036     * 
037     * @author Howard Lewis Ship
038     */
039    
040    public abstract class InsertText extends AbstractComponent
041    {
042        protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
043        {
044            if (cycle.isRewinding())
045                return;
046    
047            String value = getValue();
048    
049            if (value == null)
050                return;
051    
052            StringReader reader = null;
053            LineNumberReader lineReader = null;
054            InsertTextMode mode = getMode();
055            boolean raw = getRaw();
056    
057            try
058            {
059                reader = new StringReader(value);
060    
061                lineReader = new LineNumberReader(reader);
062    
063                int lineNumber = 0;
064    
065                while (true)
066                {
067                    String line = lineReader.readLine();
068    
069                    // Exit loop at end of file.
070    
071                    if (line == null)
072                        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.textConversionError(ex), this, null,
083                        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)
096                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, {@link InsertTextMode#BREAK}.
122         * 
123         * @since 3.0
124         */
125        protected void finishLoad()
126        {
127            setMode(InsertTextMode.BREAK);
128        }
129    
130    }