Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 130   Methods: 3
NCLOC: 77   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
LocationRenderStrategy.java 91.7% 97.4% 100% 96.3%
coverage coverage
 1   
 // Copyright 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.describe;
 16   
 
 17   
 import java.io.BufferedReader;
 18   
 import java.io.IOException;
 19   
 import java.io.InputStreamReader;
 20   
 import java.io.LineNumberReader;
 21   
 import java.io.Reader;
 22   
 import java.net.URL;
 23   
 
 24   
 import org.apache.hivemind.Location;
 25   
 import org.apache.tapestry.IMarkupWriter;
 26   
 import org.apache.tapestry.IRequestCycle;
 27   
 
 28   
 /**
 29   
  * Adapter for displaying {@link org.apache.hivemind.Location} objects as HTML. This may
 30   
  * include showing the content of the {@link org.apache.hivemind.Resource}, with the line indicated
 31   
  * in the Location highlighted.
 32   
  * 
 33   
  * @author Howard M. Lewis Ship
 34   
  * @since 4.0
 35   
  */
 36   
 public class LocationRenderStrategy implements RenderStrategy
 37   
 {
 38   
     /**
 39   
      * Lines before and after the actual location to display.
 40   
      */
 41   
     private static final int RANGE = 5;
 42   
 
 43  23
     public void renderObject(Object object, IMarkupWriter writer, IRequestCycle cycle)
 44   
     {
 45  23
         Location l = (Location) object;
 46   
 
 47   
         // Always print out the location as a string.
 48   
 
 49  23
         writer.print(l.toString());
 50   
 
 51  23
         URL url = l.getResource().getResourceURL();
 52   
 
 53  23
         if (url == null)
 54  1
             return;
 55   
 
 56  22
         writeResourceContent(writer, url, l.getLineNumber());
 57   
     }
 58   
 
 59  22
     private void writeResourceContent(IMarkupWriter writer, URL url, int lineNumber)
 60   
     {
 61  22
         LineNumberReader reader = null;
 62   
 
 63  22
         try
 64   
         {
 65  22
             reader = new LineNumberReader(new BufferedReader(
 66   
                     new InputStreamReader(url.openStream())));
 67   
 
 68  22
             writer.beginEmpty("br");
 69  22
             writer.begin("table");
 70  22
             writer.attribute("class", "location-content");
 71   
 
 72  22
             while (true)
 73   
             {
 74  541
                 String line = reader.readLine();
 75   
 
 76  541
                 if (line == null)
 77  14
                     break;
 78   
 
 79  527
                 int currentLine = reader.getLineNumber();
 80   
 
 81  527
                 if (currentLine > lineNumber + RANGE)
 82  8
                     break;
 83   
 
 84  519
                 if (currentLine < lineNumber - RANGE)
 85  310
                     continue;
 86   
 
 87  209
                 writer.begin("tr");
 88   
 
 89  209
                 if (currentLine == lineNumber)
 90  22
                     writer.attribute("class", "target-line");
 91   
 
 92  209
                 writer.begin("td");
 93  209
                 writer.attribute("class", "line-number");
 94  209
                 writer.print(currentLine);
 95  209
                 writer.end();
 96   
 
 97  209
                 writer.begin("td");
 98  209
                 writer.print(line);
 99  209
                 writer.end("tr");
 100  209
                 writer.println();
 101   
             }
 102   
 
 103  22
             reader.close();
 104  22
             reader = null;
 105   
         }
 106   
         catch (Exception ex)
 107   
         {
 108   
             // Ignore it.
 109   
         }
 110   
         finally
 111   
         {
 112  22
             writer.end("table");
 113  22
             close(reader);
 114   
         }
 115   
     }
 116   
 
 117  22
     private void close(Reader reader)
 118   
     {
 119  22
         try
 120   
         {
 121  22
             if (reader != null)
 122  0
                 reader.close();
 123   
         }
 124   
         catch (IOException ex)
 125   
         {
 126   
             // Ignore
 127   
         }
 128   
     }
 129   
 
 130   
 }