Coverage Report - org.apache.tapestry.components.Insert
 
Classes in this File Line Coverage Branch Coverage Complexity
Insert
94% 
100% 
3.25
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.components;
 16  
 
 17  
 import java.io.IOException;
 18  
 import java.io.LineNumberReader;
 19  
 import java.io.Reader;
 20  
 import java.io.StringReader;
 21  
 import java.text.Format;
 22  
 
 23  
 import org.apache.hivemind.ApplicationRuntimeException;
 24  
 import org.apache.tapestry.AbstractComponent;
 25  
 import org.apache.tapestry.IMarkupWriter;
 26  
 import org.apache.tapestry.IRequestCycle;
 27  
 
 28  
 /**
 29  
  * Used to insert some text (from a parameter) into the HTML. [ <a
 30  
  * href="../../../../../ComponentReference/Insert.html">Component Reference
 31  
  * </a>]
 32  
  * 
 33  
  * @author Howard Lewis Ship
 34  
  */
 35  
 
 36  11
 public abstract class Insert extends AbstractComponent
 37  
 {
 38  
 
 39  
     /**
 40  
      * Prints its value parameter, possibly formatted by its format parameter.
 41  
      */
 42  
 
 43  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 44  
     {
 45  11
         if (cycle.isRewinding()) 
 46  1
             return;
 47  
         
 48  10
         Object value = getValue();
 49  
         
 50  10
         if (value == null) 
 51  1
             return;
 52  
         
 53  9
         String insert = null;
 54  
         
 55  9
         Format format = getFormat();
 56  
         
 57  9
         if (format == null)
 58  
         {
 59  7
             insert = value.toString();
 60  
         }
 61  
         else
 62  
         {
 63  
             try
 64  
             {
 65  2
                 insert = format.format(value);
 66  
             }
 67  1
             catch (Exception ex)
 68  
             {
 69  1
                 throw new ApplicationRuntimeException(ComponentMessages
 70  
                         .unableToFormat(this, value, ex), this, getBinding(
 71  
                         "format").getLocation(), ex);
 72  1
             }
 73  
         }
 74  
         
 75  8
         boolean render = getRenderTag() || isParameterBound("class");
 76  
         
 77  8
         if (render) {
 78  1
             writer.begin(getTemplateTagName());
 79  
             
 80  1
             renderInformalParameters(writer, cycle);
 81  
         }
 82  
         
 83  8
         printText(writer, cycle, insert);
 84  
         
 85  8
         if (render) {
 86  1
             writer.end();
 87  
         }
 88  8
     }
 89  
     
 90  
     protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value)
 91  
     {
 92  8
         if (getMode() == null) {
 93  
             
 94  4
             writer.print(value, getRaw());
 95  4
             return;
 96  
         }
 97  
         
 98  4
         StringReader reader = null;
 99  4
         LineNumberReader lineReader = null;
 100  4
         InsertMode mode = getMode();
 101  4
         boolean raw = getRaw();
 102  
         
 103  
         try {
 104  4
             reader = new StringReader(value);
 105  4
             lineReader = new LineNumberReader(reader);
 106  
             
 107  4
             int lineNumber = 0;
 108  
             
 109  
             while(true) {
 110  14
                 String line = lineReader.readLine();
 111  
                 
 112  
                 // Exit loop at end of file.
 113  
                 
 114  14
                 if (line == null) 
 115  4
                     break;
 116  
                 
 117  10
                 mode.writeLine(lineNumber, line, writer, raw);
 118  
                 
 119  10
                 lineNumber++;
 120  10
             }
 121  
             
 122  0
         } catch (IOException ex) {
 123  
             
 124  0
             throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex);
 125  
         } finally {
 126  
             
 127  4
             close(lineReader);
 128  4
             close(reader);
 129  4
         }
 130  4
     }
 131  
     
 132  
     private void close(Reader reader)
 133  
     {
 134  8
         if (reader == null) 
 135  0
             return;
 136  
 
 137  
         try
 138  
         {
 139  8
             reader.close();
 140  8
         } catch (IOException e){}
 141  8
     }
 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  
 }