|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LocationRenderStrategy.java | 92.9% | 97.6% | 100% | 96.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 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 | int lineNumber = l.getLineNumber(); |
52 | ||
53 | 23 | if (lineNumber < 1) |
54 | 1 | return; |
55 | ||
56 | 22 | URL url = l.getResource().getResourceURL(); |
57 | ||
58 | 22 | if (url == null) |
59 | 1 | return; |
60 | ||
61 | 21 | writeResourceContent(writer, url, lineNumber); |
62 | } | |
63 | ||
64 | 21 | private void writeResourceContent(IMarkupWriter writer, URL url, int lineNumber) |
65 | { | |
66 | 21 | LineNumberReader reader = null; |
67 | ||
68 | 21 | try |
69 | { | |
70 | 21 | reader = new LineNumberReader(new BufferedReader( |
71 | new InputStreamReader(url.openStream()))); | |
72 | ||
73 | 21 | writer.beginEmpty("br"); |
74 | 21 | writer.begin("table"); |
75 | 21 | writer.attribute("class", "location-content"); |
76 | ||
77 | 21 | while (true) |
78 | { | |
79 | 492 | String line = reader.readLine(); |
80 | ||
81 | 492 | if (line == null) |
82 | 14 | break; |
83 | ||
84 | 478 | int currentLine = reader.getLineNumber(); |
85 | ||
86 | 478 | if (currentLine > lineNumber + RANGE) |
87 | 7 | break; |
88 | ||
89 | 471 | if (currentLine < lineNumber - RANGE) |
90 | 276 | continue; |
91 | ||
92 | 195 | writer.begin("tr"); |
93 | ||
94 | 195 | if (currentLine == lineNumber) |
95 | 21 | writer.attribute("class", "target-line"); |
96 | ||
97 | 195 | writer.begin("td"); |
98 | 195 | writer.attribute("class", "line-number"); |
99 | 195 | writer.print(currentLine); |
100 | 195 | writer.end(); |
101 | ||
102 | 195 | writer.begin("td"); |
103 | 195 | writer.print(line); |
104 | 195 | writer.end("tr"); |
105 | 195 | writer.println(); |
106 | } | |
107 | ||
108 | 21 | reader.close(); |
109 | 21 | reader = null; |
110 | } | |
111 | catch (Exception ex) | |
112 | { | |
113 | // Ignore it. | |
114 | } | |
115 | finally | |
116 | { | |
117 | 21 | writer.end("table"); |
118 | 21 | close(reader); |
119 | } | |
120 | } | |
121 | ||
122 | 21 | private void close(Reader reader) |
123 | { | |
124 | 21 | try |
125 | { | |
126 | 21 | if (reader != null) |
127 | 0 | reader.close(); |
128 | } | |
129 | catch (IOException ex) | |
130 | { | |
131 | // Ignore | |
132 | } | |
133 | } | |
134 | ||
135 | } |
|