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.io.IOException;
018    import java.io.LineNumberReader;
019    import java.io.Reader;
020    import java.io.StringReader;
021    import java.text.Format;
022    
023    import org.apache.hivemind.ApplicationRuntimeException;
024    import org.apache.tapestry.AbstractComponent;
025    import org.apache.tapestry.IMarkupWriter;
026    import org.apache.tapestry.IRequestCycle;
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                writer.begin(getTemplateTagName());
079                
080                renderInformalParameters(writer, cycle);
081            }
082            
083            printText(writer, cycle, insert);
084            
085            if (render) {
086                writer.end();
087            }
088        }
089        
090        protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value)
091        {
092            if (getMode() == null) {
093                
094                writer.print(value, getRaw());
095                return;
096            }
097            
098            StringReader reader = null;
099            LineNumberReader lineReader = null;
100            InsertMode mode = getMode();
101            boolean raw = getRaw();
102            
103            try {
104                reader = new StringReader(value);
105                lineReader = new LineNumberReader(reader);
106                
107                int lineNumber = 0;
108                
109                while(true) {
110                    String line = lineReader.readLine();
111                    
112                    // Exit loop at end of file.
113                    
114                    if (line == null) 
115                        break;
116                    
117                    mode.writeLine(lineNumber, line, writer, raw);
118                    
119                    lineNumber++;
120                }
121                
122            } catch (IOException ex) {
123                
124                throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex);
125            } finally {
126                
127                close(lineReader);
128                close(reader);
129            }
130        }
131        
132        private void close(Reader reader)
133        {
134            if (reader == null) 
135                return;
136    
137            try
138            {
139                reader.close();
140            } catch (IOException e){}
141        }
142        
143        public abstract Object getValue();
144    
145        public abstract Format getFormat();
146        
147        public abstract boolean getRaw();
148        
149        public abstract boolean getRenderTag();
150        
151        public abstract InsertMode getMode();
152    }