1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.describe; |
16 |
|
|
17 |
|
import java.util.ArrayList; |
18 |
|
import java.util.Collection; |
19 |
|
import java.util.Iterator; |
20 |
|
|
21 |
|
import org.apache.hivemind.util.Defense; |
22 |
|
import org.apache.tapestry.IMarkupWriter; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class HTMLDescriptionReceiver implements RootDescriptionReciever |
36 |
|
{ |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
static final String NULL_VALUE = "<NULL>"; |
41 |
|
|
42 |
|
private final IMarkupWriter _writer; |
43 |
|
|
44 |
23 |
private boolean _emitDefault = true; |
45 |
|
|
46 |
|
private String _title; |
47 |
|
|
48 |
|
private String _section; |
49 |
|
|
50 |
|
private DescribableStrategy _strategy; |
51 |
|
|
52 |
|
private HTMLDescriptionReceiverStyles _styles; |
53 |
|
|
54 |
23 |
private boolean _even = true; |
55 |
|
|
56 |
|
public HTMLDescriptionReceiver(IMarkupWriter writer, |
57 |
|
DescribableStrategy adapter) |
58 |
|
{ |
59 |
17 |
this(writer, adapter, new HTMLDescriptionReceiverStyles()); |
60 |
17 |
} |
61 |
|
|
62 |
|
public HTMLDescriptionReceiver(IMarkupWriter writer, |
63 |
|
DescribableStrategy strategy, HTMLDescriptionReceiverStyles styles) |
64 |
23 |
{ |
65 |
23 |
Defense.notNull(writer, "writer"); |
66 |
23 |
Defense.notNull(strategy, "strategy"); |
67 |
23 |
Defense.notNull(styles, "styles"); |
68 |
|
|
69 |
23 |
_writer = writer; |
70 |
23 |
_strategy = strategy; |
71 |
23 |
_styles = styles; |
72 |
23 |
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public void describe(Object object) |
80 |
|
{ |
81 |
8 |
if (object == null) |
82 |
|
{ |
83 |
1 |
_writer.print(NULL_VALUE); |
84 |
1 |
return; |
85 |
|
} |
86 |
|
|
87 |
7 |
_strategy.describeObject(object, this); |
88 |
|
|
89 |
7 |
finishUp(object); |
90 |
7 |
} |
91 |
|
|
92 |
|
public void describeAlternate(Object alternate) |
93 |
|
{ |
94 |
1 |
_strategy.describeObject(alternate, this); |
95 |
1 |
} |
96 |
|
|
97 |
|
public void finishUp() |
98 |
|
{ |
99 |
|
|
100 |
|
|
101 |
10 |
if (!_emitDefault) _writer.end("table"); |
102 |
|
|
103 |
10 |
_writer.println(); |
104 |
|
|
105 |
10 |
_emitDefault = true; |
106 |
10 |
_title = null; |
107 |
10 |
_section = null; |
108 |
10 |
_even = true; |
109 |
10 |
} |
110 |
|
|
111 |
|
void finishUp(Object object) |
112 |
|
{ |
113 |
10 |
if (_emitDefault) |
114 |
|
{ |
115 |
9 |
String value = _title != null ? _title : object.toString(); |
116 |
|
|
117 |
9 |
_writer.print(value); |
118 |
|
} |
119 |
|
|
120 |
10 |
finishUp(); |
121 |
10 |
} |
122 |
|
|
123 |
|
public void title(String title) |
124 |
|
{ |
125 |
13 |
Defense.notNull(title, "title"); |
126 |
|
|
127 |
13 |
if (_title != null) |
128 |
1 |
throw new IllegalStateException(DescribeMessages.setTitleOnce()); |
129 |
|
|
130 |
12 |
_title = title; |
131 |
12 |
} |
132 |
|
|
133 |
|
public void section(String section) |
134 |
|
{ |
135 |
3 |
Defense.notNull(section, "section"); |
136 |
|
|
137 |
3 |
if (_title == null) |
138 |
1 |
throw new IllegalStateException(DescribeMessages |
139 |
|
.mustSetTitleBeforeSection()); |
140 |
|
|
141 |
2 |
_section = section; |
142 |
2 |
} |
143 |
|
|
144 |
|
private void assertTitleSet() |
145 |
|
{ |
146 |
22 |
if (_title == null) |
147 |
0 |
throw new IllegalStateException(DescribeMessages |
148 |
|
.mustSetTitleBeforeProperty()); |
149 |
22 |
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
private void emitSection() |
157 |
|
{ |
158 |
18 |
if (_emitDefault) |
159 |
|
{ |
160 |
8 |
_emitDefault = false; |
161 |
|
|
162 |
8 |
_writer.begin("div"); |
163 |
8 |
_writer.attribute("class", _styles.getHeaderClass()); |
164 |
8 |
_writer.print(_title); |
165 |
8 |
_writer.end(); |
166 |
8 |
_writer.println(); |
167 |
|
|
168 |
8 |
_writer.begin("table"); |
169 |
8 |
_writer.attribute("class", _styles.getTableClass()); |
170 |
8 |
_writer.println(); |
171 |
|
|
172 |
8 |
_even = true; |
173 |
|
} |
174 |
|
|
175 |
18 |
if (_section != null) |
176 |
|
{ |
177 |
2 |
_writer.begin("tr"); |
178 |
2 |
_writer.attribute("class", _styles.getSubheaderClass()); |
179 |
2 |
_writer.begin("th"); |
180 |
2 |
_writer.attribute("colspan", 2); |
181 |
2 |
_writer.print(_section); |
182 |
2 |
_writer.end("tr"); |
183 |
2 |
_writer.println(); |
184 |
|
|
185 |
2 |
_section = null; |
186 |
|
|
187 |
2 |
_even = true; |
188 |
|
} |
189 |
|
|
190 |
18 |
} |
191 |
|
|
192 |
|
private void pair(String key, String value) |
193 |
|
{ |
194 |
14 |
assertTitleSet(); |
195 |
14 |
emitSection(); |
196 |
|
|
197 |
14 |
_writer.begin("tr"); |
198 |
14 |
writeRowClass(); |
199 |
|
|
200 |
14 |
_writer.begin("th"); |
201 |
14 |
_writer.print(key); |
202 |
14 |
_writer.end(); |
203 |
14 |
_writer.begin("td"); |
204 |
14 |
_writer.print(value); |
205 |
14 |
_writer.end("tr"); |
206 |
14 |
_writer.println(); |
207 |
|
|
208 |
14 |
} |
209 |
|
|
210 |
|
private void writeRowClass() |
211 |
|
{ |
212 |
21 |
_writer.attribute("class", _even ? "even" : "odd"); |
213 |
21 |
_even = !_even; |
214 |
21 |
} |
215 |
|
|
216 |
|
public void property(String key, Object value) |
217 |
|
{ |
218 |
1 |
Defense.notNull(key, "key"); |
219 |
|
|
220 |
1 |
assertTitleSet(); |
221 |
1 |
emitSection(); |
222 |
|
|
223 |
1 |
_writer.begin("tr"); |
224 |
1 |
writeRowClass(); |
225 |
|
|
226 |
1 |
_writer.begin("th"); |
227 |
1 |
_writer.print(key); |
228 |
1 |
_writer.end(); |
229 |
1 |
_writer.begin("td"); |
230 |
|
|
231 |
1 |
describeNested(value); |
232 |
|
|
233 |
1 |
_writer.end("tr"); |
234 |
1 |
_writer.println(); |
235 |
1 |
} |
236 |
|
|
237 |
|
private void describeNested(Object value) |
238 |
|
{ |
239 |
7 |
if (value == null) |
240 |
|
{ |
241 |
1 |
_writer.print(NULL_VALUE); |
242 |
1 |
return; |
243 |
|
} |
244 |
|
|
245 |
6 |
new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value); |
246 |
6 |
} |
247 |
|
|
248 |
|
public void property(String key, boolean value) |
249 |
|
{ |
250 |
1 |
Defense.notNull(key, "key"); |
251 |
|
|
252 |
|
|
253 |
|
|
254 |
1 |
pair(key, value ? "true" : "false"); |
255 |
1 |
} |
256 |
|
|
257 |
|
public void property(String key, byte value) |
258 |
|
{ |
259 |
1 |
Defense.notNull(key, "key"); |
260 |
|
|
261 |
1 |
pair(key, Byte.toString(value)); |
262 |
1 |
} |
263 |
|
|
264 |
|
public void property(String key, short value) |
265 |
|
{ |
266 |
1 |
Defense.notNull(key, "key"); |
267 |
|
|
268 |
1 |
pair(key, Short.toString(value)); |
269 |
1 |
} |
270 |
|
|
271 |
|
public void property(String key, int value) |
272 |
|
{ |
273 |
7 |
Defense.notNull(key, "key"); |
274 |
|
|
275 |
7 |
pair(key, Integer.toString(value)); |
276 |
7 |
} |
277 |
|
|
278 |
|
public void property(String key, long value) |
279 |
|
{ |
280 |
1 |
Defense.notNull(key, "key"); |
281 |
|
|
282 |
1 |
pair(key, Long.toString(value)); |
283 |
1 |
} |
284 |
|
|
285 |
|
public void property(String key, float value) |
286 |
|
{ |
287 |
1 |
Defense.notNull(key, "key"); |
288 |
|
|
289 |
1 |
pair(key, Float.toString(value)); |
290 |
1 |
} |
291 |
|
|
292 |
|
public void property(String key, double value) |
293 |
|
{ |
294 |
1 |
Defense.notNull(key, "key"); |
295 |
|
|
296 |
1 |
pair(key, Double.toString(value)); |
297 |
1 |
} |
298 |
|
|
299 |
|
public void property(String key, char value) |
300 |
|
{ |
301 |
1 |
Defense.notNull(key, "key"); |
302 |
|
|
303 |
1 |
pair(key, Character.toString(value)); |
304 |
1 |
} |
305 |
|
|
306 |
|
public void array(String key, Object[] values) |
307 |
|
{ |
308 |
3 |
Defense.notNull(key, "key"); |
309 |
|
|
310 |
3 |
assertTitleSet(); |
311 |
|
|
312 |
3 |
if (values == null || values.length == 0) return; |
313 |
|
|
314 |
1 |
emitSection(); |
315 |
|
|
316 |
3 |
for(int i = 0; i < values.length; i++) |
317 |
|
{ |
318 |
2 |
_writer.begin("tr"); |
319 |
2 |
writeRowClass(); |
320 |
|
|
321 |
2 |
_writer.begin("th"); |
322 |
|
|
323 |
2 |
if (i == 0) _writer.print(key); |
324 |
|
|
325 |
2 |
_writer.end(); |
326 |
|
|
327 |
2 |
_writer.begin("td"); |
328 |
|
|
329 |
2 |
describeNested(values[i]); |
330 |
|
|
331 |
2 |
_writer.end("tr"); |
332 |
2 |
_writer.println(); |
333 |
|
} |
334 |
|
|
335 |
1 |
} |
336 |
|
|
337 |
|
public void collection(String key, Collection values) |
338 |
|
{ |
339 |
4 |
Defense.notNull(key, "key"); |
340 |
|
|
341 |
4 |
assertTitleSet(); |
342 |
|
|
343 |
4 |
if (values == null || values.isEmpty()) return; |
344 |
|
|
345 |
2 |
emitSection(); |
346 |
|
|
347 |
2 |
Iterator i = new ArrayList(values).iterator(); |
348 |
2 |
boolean first = true; |
349 |
|
|
350 |
6 |
while(i.hasNext()) |
351 |
|
{ |
352 |
4 |
_writer.begin("tr"); |
353 |
4 |
writeRowClass(); |
354 |
|
|
355 |
4 |
_writer.begin("th"); |
356 |
|
|
357 |
4 |
if (first) |
358 |
2 |
_writer.print(key); |
359 |
|
|
360 |
4 |
_writer.end(); |
361 |
4 |
_writer.begin("td"); |
362 |
|
|
363 |
4 |
describeNested(i.next()); |
364 |
|
|
365 |
4 |
_writer.end("tr"); |
366 |
4 |
_writer.println(); |
367 |
|
|
368 |
4 |
first = false; |
369 |
|
} |
370 |
2 |
} |
371 |
|
} |