Coverage Report - org.apache.tapestry.components.Insert
 
Classes in this File Line Coverage Branch Coverage Complexity
Insert
93% 
100% 
3.286
 
 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 org.apache.commons.io.IOUtils;
 18  
 import org.apache.hivemind.ApplicationRuntimeException;
 19  
 import org.apache.tapestry.AbstractComponent;
 20  
 import org.apache.tapestry.IMarkupWriter;
 21  
 import org.apache.tapestry.IRequestCycle;
 22  
 
 23  
 import java.io.IOException;
 24  
 import java.io.LineNumberReader;
 25  
 import java.io.StringReader;
 26  
 import java.text.Format;
 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  
         {
 79  1
             writer.begin(getTemplateTagName());
 80  
 
 81  1
             renderInformalParameters(writer, cycle);
 82  
 
 83  1
             if (getId() != null && !isParameterBound("id"))
 84  0
                 renderIdAttribute(writer, cycle);
 85  
         }
 86  
 
 87  8
         printText(writer, cycle, insert);
 88  
 
 89  8
         if (render)
 90  1
             writer.end();
 91  8
     }
 92  
 
 93  
     protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value)
 94  
     {
 95  8
         if (getMode() == null)
 96  
         {
 97  4
             writer.print(value, getRaw());
 98  4
             return;
 99  
         }
 100  
 
 101  4
         StringReader reader = null;
 102  4
         LineNumberReader lineReader = null;
 103  4
         InsertMode mode = getMode();
 104  4
         boolean raw = getRaw();
 105  
 
 106  
         try {
 107  4
             reader = new StringReader(value);
 108  4
             lineReader = new LineNumberReader(reader);
 109  
 
 110  4
             int lineNumber = 0;
 111  
 
 112  
             while(true)
 113  
             {
 114  14
                 String line = lineReader.readLine();
 115  
 
 116  
                 // Exit loop at end of file.
 117  
 
 118  14
                 if (line == null)
 119  4
                     break;
 120  
 
 121  10
                 mode.writeLine(lineNumber, line, writer, raw);
 122  
 
 123  10
                 lineNumber++;
 124  10
             }
 125  
 
 126  0
         } catch (IOException ex)
 127  
         {
 128  0
             throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex);
 129  
         } finally
 130  
         {
 131  4
             IOUtils.closeQuietly(lineReader);
 132  4
             IOUtils.closeQuietly(reader);
 133  4
         }
 134  4
     }
 135  
 
 136  
     public abstract Object getValue();
 137  
 
 138  
     public abstract Format getFormat();
 139  
 
 140  
     public abstract boolean getRaw();
 141  
 
 142  
     public abstract boolean getRenderTag();
 143  
 
 144  
     public abstract InsertMode getMode();
 145  
 }