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