Coverage Report - org.apache.tapestry.describe.LocationRenderStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
LocationRenderStrategy
95% 
100% 
5
 
 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
 30  
  * HTML. This may include showing the content of the
 31  
  * {@link org.apache.hivemind.Resource}, with the line indicated in the
 32  
  * Location highlighted.
 33  
  * 
 34  
  * @author Howard M. Lewis Ship
 35  
  * @since 4.0
 36  
  */
 37  4
 public class LocationRenderStrategy implements RenderStrategy
 38  
 {
 39  
 
 40  
     /**
 41  
      * Lines before and after the actual location to display.
 42  
      */
 43  
     private static final int RANGE = 5;
 44  
 
 45  
     public void renderObject(Object object, IMarkupWriter writer,
 46  
             IRequestCycle cycle)
 47  
     {
 48  4
         Location l = (Location) object;
 49  
 
 50  
         // Always print out the location as a string.
 51  
 
 52  4
         writer.print(l.toString());
 53  
 
 54  4
         int lineNumber = l.getLineNumber();
 55  
 
 56  4
         if (lineNumber < 1) return;
 57  
 
 58  3
         URL url = l.getResource().getResourceURL();
 59  
 
 60  3
         if (url == null) return;
 61  
 
 62  2
         writeResourceContent(writer, url, lineNumber);
 63  2
     }
 64  
 
 65  
     private void writeResourceContent(IMarkupWriter writer, URL url,
 66  
             int lineNumber)
 67  
     {
 68  2
         LineNumberReader reader = null;
 69  
 
 70  
         try
 71  
         {
 72  2
             reader = new LineNumberReader(new BufferedReader(
 73  
                     new InputStreamReader(url.openStream())));
 74  
 
 75  2
             writer.beginEmpty("br");
 76  2
             writer.begin("table");
 77  2
             writer.attribute("class", "location-content");
 78  
 
 79  
             while(true)
 80  
             {
 81  19
                 String line = reader.readLine();
 82  
 
 83  19
                 if (line == null) break;
 84  
 
 85  18
                 int currentLine = reader.getLineNumber();
 86  
 
 87  18
                 if (currentLine > lineNumber + RANGE) break;
 88  
 
 89  17
                 if (currentLine < lineNumber - RANGE) continue;
 90  
 
 91  16
                 writer.begin("tr");
 92  
 
 93  16
                 if (currentLine == lineNumber)
 94  2
                     writer.attribute("class", "target-line");
 95  
 
 96  16
                 writer.begin("td");
 97  16
                 writer.attribute("class", "line-number");
 98  16
                 writer.print(currentLine);
 99  16
                 writer.end();
 100  
 
 101  16
                 writer.begin("td");
 102  16
                 writer.print(line);
 103  16
                 writer.end("tr");
 104  16
                 writer.println();
 105  16
             }
 106  
 
 107  2
             reader.close();
 108  2
             reader = null;
 109  
         }
 110  0
         catch (Exception ex)
 111  
         {
 112  
             // Ignore it.
 113  
         }
 114  
         finally
 115  
         {
 116  2
             writer.end("table");
 117  2
             close(reader);
 118  2
         }
 119  2
     }
 120  
 
 121  
     private void close(Reader reader)
 122  
     {
 123  
         try
 124  
         {
 125  2
             if (reader != null) reader.close();
 126  
         }
 127  0
         catch (IOException ex)
 128  
         {
 129  
             // Ignore
 130  2
         }
 131  2
     }
 132  
 
 133  
 }