1 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 36 public class LocationRenderStrategy implements RenderStrategy 37 { 38 41 private static final int RANGE = 5; 42 43 public void renderObject(Object object, IMarkupWriter writer, IRequestCycle cycle) 44 { 45 Location l = (Location) object; 46 47 49 writer.print(l.toString()); 50 51 int lineNumber = l.getLineNumber(); 52 53 if (lineNumber < 1) 54 return; 55 56 URL url = l.getResource().getResourceURL(); 57 58 if (url == null) 59 return; 60 61 writeResourceContent(writer, url, lineNumber); 62 } 63 64 private void writeResourceContent(IMarkupWriter writer, URL url, int lineNumber) 65 { 66 LineNumberReader reader = null; 67 68 try 69 { 70 reader = new LineNumberReader (new BufferedReader ( 71 new InputStreamReader (url.openStream()))); 72 73 writer.beginEmpty("br"); 74 writer.begin("table"); 75 writer.attribute("class", "location-content"); 76 77 while (true) 78 { 79 String line = reader.readLine(); 80 81 if (line == null) 82 break; 83 84 int currentLine = reader.getLineNumber(); 85 86 if (currentLine > lineNumber + RANGE) 87 break; 88 89 if (currentLine < lineNumber - RANGE) 90 continue; 91 92 writer.begin("tr"); 93 94 if (currentLine == lineNumber) 95 writer.attribute("class", "target-line"); 96 97 writer.begin("td"); 98 writer.attribute("class", "line-number"); 99 writer.print(currentLine); 100 writer.end(); 101 102 writer.begin("td"); 103 writer.print(line); 104 writer.end("tr"); 105 writer.println(); 106 } 107 108 reader.close(); 109 reader = null; 110 } 111 catch (Exception ex) 112 { 113 } 115 finally 116 { 117 writer.end("table"); 118 close(reader); 119 } 120 } 121 122 private void close(Reader reader) 123 { 124 try 125 { 126 if (reader != null) 127 reader.close(); 128 } 129 catch (IOException ex) 130 { 131 } 133 } 134 135 } | Popular Tags |