Coverage Report - org.apache.tapestry5.internal.renderers.LocationRenderer
 
Classes in this File Line Coverage Branch Coverage Complexity
LocationRenderer
95%
39/41
89%
16/18
0
 
 1  
 // Copyright 2007, 2008 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.tapestry5.internal.renderers;
 16  
 
 17  
 import org.apache.tapestry5.MarkupWriter;
 18  
 import org.apache.tapestry5.dom.Element;
 19  
 import org.apache.tapestry5.ioc.Location;
 20  
 import org.apache.tapestry5.ioc.Resource;
 21  
 import org.apache.tapestry5.ioc.ScopeConstants;
 22  
 import org.apache.tapestry5.ioc.annotations.Scope;
 23  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 24  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 25  
 import org.apache.tapestry5.services.ObjectRenderer;
 26  
 
 27  
 import java.io.*;
 28  
 import java.util.Set;
 29  
 
 30  
 /**
 31  
  * Responsible for rendering a {@link Location}. It is designed to only perform the full output (which includes a
 32  
  * snippet of the source file) once per render. This requires the use of the "perthread" scope (since the service
 33  
  * tracks, internally, which locations have already been rendered, to avoid repetition).
 34  
  */
 35  
 @Scope(ScopeConstants.PERTHREAD)
 36  118
 public class LocationRenderer implements ObjectRenderer<Location>
 37  
 {
 38  
     private static final int RANGE = 5;
 39  
 
 40  44
     private final Set<Location> rendered = CollectionFactory.newSet();
 41  
 
 42  
     public void render(Location location, MarkupWriter writer)
 43  
     {
 44  74
         writer.write(location.toString());
 45  
 
 46  
         /** If the full details were already rendered this request, then skip the rest. */
 47  74
         if (rendered.contains(location)) return;
 48  
 
 49  58
         rendered.add(location);
 50  
 
 51  58
         Resource r = location.getResource();
 52  58
         int line = location.getLine();
 53  
 
 54  
         // No line number? then nothing more to render.
 55  
 
 56  58
         if (line <= 0) return;
 57  
 
 58  58
         if (!r.exists()) return;
 59  
 
 60  
 
 61  58
         int start = line - RANGE;
 62  58
         int end = line + RANGE;
 63  
 
 64  58
         writer.element("table", "class", "t-location-outer");
 65  
 
 66  58
         LineNumberReader reader = null;
 67  
 
 68  
         try
 69  
         {
 70  58
             InputStream is = r.openStream();
 71  58
             InputStreamReader isr = new InputStreamReader(is);
 72  58
             reader = new LineNumberReader(new BufferedReader(isr));
 73  
 
 74  
             while (true)
 75  
             {
 76  924
                 String input = reader.readLine();
 77  
 
 78  924
                 if (input == null) break;
 79  
 
 80  886
                 int current = reader.getLineNumber();
 81  
 
 82  886
                 if (current < start) continue;
 83  
 
 84  486
                 if (current > end) break;
 85  
 
 86  466
                 writer.element("tr");
 87  
 
 88  466
                 writer.element("td", "class", "t-location-line");
 89  
 
 90  466
                 if (line == current) writer.getElement().addClassName("t-location-current");
 91  
 
 92  466
                 writer.write(Integer.toString(current));
 93  466
                 writer.end();
 94  
 
 95  466
                 Element td = writer.element("td", "class", "t-location-content");
 96  
 
 97  466
                 if (line == current) td.addClassName("t-location-current");
 98  
 
 99  466
                 if (start == current) td.addClassName("t-location-content-first");
 100  
 
 101  466
                 writer.write(input);
 102  466
                 writer.end();
 103  
 
 104  466
                 writer.end(); // tr
 105  466
             }
 106  
 
 107  58
             reader.close();
 108  58
             reader = null;
 109  
         }
 110  0
         catch (IOException ex)
 111  
         {
 112  0
             writer.write(ex.toString());
 113  
         }
 114  
         finally
 115  
         {
 116  58
             InternalUtils.close(reader);
 117  58
         }
 118  
 
 119  58
         writer.end(); // div
 120  58
     }
 121  
 }