Coverage Report - org.apache.tapestry.describe.LocationRenderStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
LocationRenderStrategy
0%
0/64
0%
0/28
6
 
 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 org.apache.hivemind.Location;
 18  
 import org.apache.tapestry.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 
 21  
 import java.io.*;
 22  
 import java.net.URL;
 23  
 
 24  
 /**
 25  
  * Adapter for displaying {@link org.apache.hivemind.Location} objects as
 26  
  * HTML. This may include showing the content of the
 27  
  * {@link org.apache.hivemind.Resource}, with the line indicated in the
 28  
  * Location highlighted.
 29  
  * 
 30  
  * @author Howard M. Lewis Ship
 31  
  * @since 4.0
 32  
  */
 33  0
 public class LocationRenderStrategy implements RenderStrategy
 34  
 {
 35  
 
 36  
     /**
 37  
      * Lines before and after the actual location to display.
 38  
      */
 39  
     private static final int RANGE = 5;
 40  
 
 41  
     public void renderObject(Object object, IMarkupWriter writer,
 42  
             IRequestCycle cycle)
 43  
     {
 44  0
         Location l = (Location) object;
 45  
 
 46  
         // Always print out the location as a string.
 47  
 
 48  0
         writer.print(l.toString());
 49  
 
 50  0
         int lineNumber = l.getLineNumber();
 51  
 
 52  0
         if (lineNumber < 1)
 53  0
             return;
 54  
 
 55  0
         URL url = l.getResource().getResourceURL();
 56  
 
 57  0
         if (url == null)
 58  0
             return;
 59  
 
 60  0
         writeResourceContent(writer, url, lineNumber);
 61  0
     }
 62  
 
 63  
     private void writeResourceContent(IMarkupWriter writer, URL url, int lineNumber)
 64  
     {
 65  0
         LineNumberReader reader = null;
 66  
 
 67  
         try
 68  
         {
 69  0
             reader = new LineNumberReader(new BufferedReader( new InputStreamReader(url.openStream())));
 70  
 
 71  0
             writer.beginEmpty("br");
 72  0
             writer.begin("table");
 73  0
             writer.attribute("class", "location-content");
 74  0
             writer.attribute("cellspacing", "0");
 75  0
             writer.attribute("cellpadding", "0");
 76  
 
 77  
             while(true)
 78  
             {
 79  0
                 String line = reader.readLine();
 80  
 
 81  0
                 if (line == null)
 82  0
                     break;
 83  
 
 84  0
                 int currentLine = reader.getLineNumber();
 85  
 
 86  0
                 if (currentLine > lineNumber + RANGE)
 87  0
                     break;
 88  
 
 89  0
                 if (currentLine < lineNumber - RANGE)
 90  0
                     continue;
 91  
 
 92  0
                 writer.begin("tr");
 93  
 
 94  0
                 if (currentLine == lineNumber)
 95  0
                     writer.attribute("class", "target-line");
 96  
                 
 97  0
                 writer.begin("td");
 98  0
                 writer.attribute("class", "line-number");
 99  0
                 writer.print(currentLine);
 100  0
                 writer.end();
 101  
 
 102  0
                 writer.begin("td");
 103  
 
 104  
                 // pretty print tabs and spaces properly
 105  
                 
 106  0
                 String spacers = extractWhitespaceStart(line);
 107  
 
 108  0
                 if (spacers != null && spacers.length() > 0)
 109  
                 {
 110  0
                     writer.printRaw(spacers);
 111  
                 }
 112  
 
 113  0
                 writer.print(line);
 114  0
                 writer.end("tr");
 115  0
                 writer.println();
 116  0
             }
 117  
 
 118  0
             reader.close();
 119  0
             reader = null;
 120  
         }
 121  0
         catch (Exception ex)
 122  
         {
 123  
             // Ignore it.
 124  
         }
 125  
         finally
 126  
         {
 127  0
             writer.end("table");
 128  0
             close(reader);
 129  0
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Finds any tab or whitespace characters in the beginning of this string - up
 134  
      * to the first occurrence of normal character data and returns it.
 135  
      *
 136  
      * @param input The string to extract whitespace/tab characters from.
 137  
      *
 138  
      * @return The whitespace/tab characters found, or null if none found.
 139  
      */
 140  
     String extractWhitespaceStart(String input)
 141  
     {
 142  0
         if (input == null || input.length() < 1)
 143  0
             return null;
 144  
 
 145  0
         char[] vals = input.toCharArray();
 146  0
         StringBuffer ret = new StringBuffer();
 147  
         
 148  0
         for (int i=0; i < vals.length; i++)
 149  
         {
 150  0
             if (Character.isWhitespace(vals[i]))
 151  
             {
 152  0
                 ret.append("&nbsp;");
 153  0
                 continue;
 154  
             }
 155  
 
 156  0
             if (vals[i] == '\t')
 157  
             {
 158  0
                 ret.append("&nbsp;&nbsp;");
 159  
                 continue;
 160  
             }
 161  
 
 162  
             break;
 163  
         }
 164  
         
 165  0
         return ret.toString();
 166  
     }
 167  
 
 168  
     private void close(Reader reader)
 169  
     {
 170  
         try
 171  
         {
 172  0
             if (reader != null)
 173  0
                 reader.close();
 174  
         }
 175  0
         catch (IOException ex)
 176  
         {
 177  
             // Ignore
 178  0
         }
 179  0
     }
 180  
 
 181  
 }