Coverage Report - org.apache.tapestry.components.Insert
 
Classes in this File Line Coverage Branch Coverage Complexity
Insert
0%
0/46
0%
0/22
3.571
 
 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="../../../../../components/general/insert.html">Component Reference
 31  
  * </a>]
 32  
  *
 33  
  * @author Howard Lewis Ship
 34  
  */
 35  
 
 36  0
 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  0
         if (cycle.isRewinding())
 46  0
             return;
 47  
 
 48  0
         Object value = getValue();
 49  
 
 50  0
         if (value == null)
 51  0
             return;
 52  
 
 53  0
         String insert = null;
 54  
 
 55  0
         Format format = getFormat();
 56  
 
 57  0
         if (format == null)
 58  
         {
 59  0
             insert = value.toString();
 60  
         }
 61  
         else
 62  
         {
 63  
             try
 64  
             {
 65  0
                 insert = format.format(value);
 66  
             }
 67  0
             catch (Exception ex)
 68  
             {
 69  0
                 throw new ApplicationRuntimeException(ComponentMessages
 70  
                         .unableToFormat(this, value, ex), this, getBinding(
 71  
                         "format").getLocation(), ex);
 72  0
             }
 73  
         }
 74  
 
 75  0
         boolean render = getRenderTag() || isParameterBound("class");
 76  
 
 77  0
         if (render)
 78  
         {
 79  0
             writer.begin(getTemplateTagName());
 80  
 
 81  0
             renderInformalParameters(writer, cycle);
 82  
 
 83  0
             if (getId() != null && !isParameterBound("id"))
 84  0
                 renderIdAttribute(writer, cycle);
 85  
         }
 86  
 
 87  0
         printText(writer, cycle, insert);
 88  
 
 89  0
         if (render)
 90  0
             writer.end();
 91  0
     }
 92  
 
 93  
     protected void printText(IMarkupWriter writer, IRequestCycle cycle, String value)
 94  
     {
 95  0
         if (getMode() == null)
 96  
         {
 97  0
             writer.print(value, getRaw());
 98  0
             return;
 99  
         }
 100  
 
 101  0
         StringReader reader = null;
 102  0
         LineNumberReader lineReader = null;
 103  0
         InsertMode mode = getMode();
 104  0
         boolean raw = getRaw();
 105  
 
 106  
         try {
 107  0
             reader = new StringReader(value);
 108  0
             lineReader = new LineNumberReader(reader);
 109  
 
 110  0
             int lineNumber = 0;
 111  
 
 112  
             while(true)
 113  
             {
 114  0
                 String line = lineReader.readLine();
 115  
 
 116  
                 // Exit loop at end of file.
 117  
 
 118  0
                 if (line == null)
 119  0
                     break;
 120  
 
 121  0
                 mode.writeLine(lineNumber, line, writer, raw);
 122  
 
 123  0
                 lineNumber++;
 124  0
             }
 125  
 
 126  0
         } catch (IOException ex)
 127  
         {
 128  0
             throw new ApplicationRuntimeException(ComponentMessages.textConversionError(ex), this, null, ex);
 129  
         } finally
 130  
         {
 131  0
             IOUtils.closeQuietly(lineReader);
 132  0
             IOUtils.closeQuietly(reader);
 133  0
         }
 134  0
     }
 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  
 }