Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 183   Methods: 7
NCLOC: 100   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
TextToken.java 85% 83.7% 71.4% 82.9%
coverage coverage
 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.parse;
 16   
 
 17   
 import org.apache.commons.lang.builder.ToStringBuilder;
 18   
 import org.apache.hivemind.Location;
 19   
 import org.apache.tapestry.IMarkupWriter;
 20   
 import org.apache.tapestry.IRender;
 21   
 import org.apache.tapestry.IRequestCycle;
 22   
 import org.apache.tapestry.Tapestry;
 23   
 
 24   
 /**
 25   
  *  Represents static text in the template that may be passed through
 26   
  *  to the client unchanged (except, perhaps, for the removal of
 27   
  *  some whitespace).
 28   
  *
 29   
  *  @see TokenType#TEXT
 30   
  *  @author Howard Lewis Ship
 31   
  *  @since 3.0
 32   
  *
 33   
  **/
 34   
 
 35   
 public class TextToken extends TemplateToken implements IRender
 36   
 {
 37   
     private char[] _templateData;
 38   
 
 39   
     private int _startIndex = -1;
 40   
     private int _endIndex = -1;
 41   
 
 42   
     private int _offset;
 43   
     private int _length;
 44   
     private boolean _needsTrim = true;
 45   
 
 46  1452
     public TextToken(char[] templateData, int startIndex, int endIndex, Location location)
 47   
     {
 48  1452
         super(TokenType.TEXT, location);
 49   
 
 50  1452
         if (startIndex < 0
 51   
             || endIndex < 0
 52   
             || startIndex > templateData.length
 53   
             || endIndex > templateData.length)
 54  0
             throw new IllegalArgumentException(
 55   
                 Tapestry.format(
 56   
                     "TextToken.range-error",
 57   
                     this,
 58   
                     Integer.toString(templateData.length)));
 59   
 
 60  1452
         _templateData = templateData;
 61  1452
         _startIndex = startIndex;
 62  1452
         _endIndex = endIndex;
 63   
 
 64   
         // Values actually used to render, may be adjusted to remove excess
 65   
         // leading and trailing whitespace.
 66   
 
 67  1452
         _offset = startIndex;
 68  1452
         _length = endIndex - startIndex + 1;
 69   
     }
 70   
 
 71  4069
     public synchronized void render(IMarkupWriter writer, IRequestCycle cycle)
 72   
     {
 73  4069
         if (_needsTrim)
 74   
         {
 75  1251
             trim();
 76  1251
             _needsTrim = false;
 77   
         }
 78   
 
 79  4069
         if (_length == 0)
 80  0
             return;
 81   
 
 82   
         // At one time, we would check to see if the cycle was rewinding and
 83   
         // only invoke printRaw() if it was.  However, that slows down
 84   
         // normal rendering (microscopically) and, with the new
 85   
         // NullResponseWriter class, the "cost" of invoking cycle.isRewinding()
 86   
         // is approximately the same as the "cost" of invoking writer.printRaw().
 87   
 
 88  4069
         writer.printRaw(_templateData, _offset, _length);
 89   
     }
 90   
 
 91   
     /**
 92   
       *  Strip off all leading and trailing whitespace by adjusting offset and length.
 93   
       *
 94   
       **/
 95   
 
 96  1251
     private void trim()
 97   
     {
 98  1251
         if (_length == 0)
 99  0
             return;
 100   
 
 101  1251
         try
 102   
         {
 103  1251
             boolean didTrim = false;
 104   
 
 105   
             // Shave characters off the end until we hit a non-whitespace
 106   
             // character.
 107   
 
 108  1251
             while (_length > 0)
 109   
             {
 110  5130
                 char ch = _templateData[_offset + _length - 1];
 111   
 
 112  5130
                 if (!Character.isWhitespace(ch))
 113  706
                     break;
 114   
 
 115  4424
                 _length--;
 116  4424
                 didTrim = true;
 117   
             }
 118   
 
 119   
             // Restore one character of whitespace to the end
 120   
 
 121  1251
             if (didTrim)
 122  1006
                 _length++;
 123   
 
 124  1251
             didTrim = false;
 125   
 
 126   
             // Strip characters off the front until we hit a non-whitespace
 127   
             // character.
 128   
 
 129  1251
             while (_length > 0)
 130   
             {
 131  2931
                 char ch = _templateData[_offset];
 132   
 
 133  2931
                 if (!Character.isWhitespace(ch))
 134  706
                     break;
 135   
 
 136  2225
                 _offset++;
 137  2225
                 _length--;
 138  2225
                 didTrim = true;
 139   
             }
 140   
 
 141   
             // Again, restore one character of whitespace.
 142   
 
 143  1251
             if (didTrim)
 144   
             {
 145  970
                 _offset--;
 146  970
                 _length++;
 147   
             }
 148   
 
 149   
         }
 150   
         catch (IndexOutOfBoundsException ex)
 151   
         {
 152  0
             throw new RuntimeException(Tapestry.format("TextToken.error-trimming", this));
 153   
         }
 154   
 
 155   
         // Ok, this isn't perfect.  I don't want to write into templateData[] even
 156   
         // though I'd prefer that my single character of whitespace was always a space.
 157   
         // It would also be kind of neat to shave whitespace within the static HTML, rather
 158   
         // than just on the edges.
 159   
     }
 160   
 
 161  0
     protected void extendDescription(ToStringBuilder builder)
 162   
     {
 163  0
         builder.append("startIndex", _startIndex);
 164  0
         builder.append("endIndex", _endIndex);
 165   
     }
 166   
 
 167  16
     public int getEndIndex()
 168   
     {
 169  16
         return _endIndex;
 170   
     }
 171   
 
 172  16
     public int getStartIndex()
 173   
     {
 174  16
         return _startIndex;
 175   
     }
 176   
 
 177  0
     public char[] getTemplateData()
 178   
     {
 179  0
         return _templateData;
 180   
     }
 181   
 
 182   
 }
 183