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 |
388
| public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy strategy,
|
59 |
| HTMLDescriptionReceiverStyles styles) |
60 |
| { |
61 |
388
| Defense.notNull(writer, "writer");
|
62 |
388
| Defense.notNull(strategy, "strategy");
|
63 |
388
| Defense.notNull(styles, "styles");
|
64 |
| |
65 |
388
| _writer = writer;
|
66 |
388
| _strategy = strategy;
|
67 |
388
| _styles = styles;
|
68 |
| } |
69 |
| |
70 |
374
| public void describe(Object object)
|
71 |
| { |
72 |
374
| if (object == null)
|
73 |
| { |
74 |
1
| _writer.print(NULL_VALUE);
|
75 |
1
| return;
|
76 |
| } |
77 |
| |
78 |
373
| _strategy.describeObject(object, this);
|
79 |
| |
80 |
373
| finishUp(object);
|
81 |
| } |
82 |
| |
83 |
57
| public void describeAlternate(Object alternate)
|
84 |
| { |
85 |
57
| _strategy.describeObject(alternate, this);
|
86 |
| } |
87 |
| |
88 |
376
| void finishUp(Object object)
|
89 |
| { |
90 |
376
| if (_emitDefault)
|
91 |
| { |
92 |
319
| String value = _title != null ? _title : object.toString();
|
93 |
| |
94 |
319
| _writer.print(value);
|
95 |
| } |
96 |
| |
97 |
| |
98 |
| else |
99 |
57
| _writer.end("table");
|
100 |
| |
101 |
376
| _writer.println();
|
102 |
| } |
103 |
| |
104 |
69
| public void title(String title)
|
105 |
| { |
106 |
69
| Defense.notNull(title, "title");
|
107 |
| |
108 |
69
| if (_title != null)
|
109 |
1
| throw new IllegalStateException(DescribeMessages.setTitleOnce());
|
110 |
| |
111 |
68
| _title = title;
|
112 |
| } |
113 |
| |
114 |
110
| public void section(String section)
|
115 |
| { |
116 |
110
| Defense.notNull(section, "section");
|
117 |
| |
118 |
110
| if (_title == null)
|
119 |
1
| throw new IllegalStateException(DescribeMessages.mustSetTitleBeforeSection());
|
120 |
| |
121 |
109
| _section = section;
|
122 |
| } |
123 |
| |
124 |
529
| private void assertTitleSet()
|
125 |
| { |
126 |
529
| if (_title == null)
|
127 |
0
| throw new IllegalStateException(DescribeMessages.mustSetTitleBeforeProperty());
|
128 |
| } |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
508
| private void emitSection()
|
136 |
| { |
137 |
508
| if (_emitDefault)
|
138 |
| { |
139 |
63
| _emitDefault = false;
|
140 |
| |
141 |
63
| _writer.begin("div");
|
142 |
63
| _writer.attribute("class", _styles.getHeaderClass());
|
143 |
63
| _writer.print(_title);
|
144 |
63
| _writer.end();
|
145 |
63
| _writer.println();
|
146 |
| |
147 |
63
| _writer.begin("table");
|
148 |
63
| _writer.attribute("class", _styles.getTableClass());
|
149 |
63
| _writer.println();
|
150 |
| |
151 |
63
| _even = true;
|
152 |
| } |
153 |
| |
154 |
508
| if (_section != null)
|
155 |
| { |
156 |
64
| _writer.begin("tr");
|
157 |
64
| _writer.attribute("class", _styles.getSubheaderClass());
|
158 |
64
| _writer.begin("th");
|
159 |
64
| _writer.attribute("colspan", 2);
|
160 |
64
| _writer.print(_section);
|
161 |
64
| _writer.end("tr");
|
162 |
64
| _writer.println();
|
163 |
| |
164 |
64
| _section = null;
|
165 |
| |
166 |
64
| _even = true;
|
167 |
| } |
168 |
| |
169 |
| } |
170 |
| |
171 |
75
| private void pair(String key, String value)
|
172 |
| { |
173 |
75
| assertTitleSet();
|
174 |
75
| emitSection();
|
175 |
| |
176 |
75
| _writer.begin("tr");
|
177 |
75
| writeRowClass();
|
178 |
| |
179 |
75
| _writer.begin("th");
|
180 |
75
| _writer.print(key);
|
181 |
75
| _writer.end();
|
182 |
75
| _writer.begin("td");
|
183 |
75
| _writer.print(value);
|
184 |
75
| _writer.end("tr");
|
185 |
75
| _writer.println();
|
186 |
| |
187 |
| } |
188 |
| |
189 |
512
| private void writeRowClass()
|
190 |
| { |
191 |
512
| _writer.attribute("class", _even ? "even" : "odd");
|
192 |
512
| _even = !_even;
|
193 |
| } |
194 |
| |
195 |
392
| public void property(String key, Object value)
|
196 |
| { |
197 |
392
| Defense.notNull(key, "key");
|
198 |
| |
199 |
392
| assertTitleSet();
|
200 |
392
| emitSection();
|
201 |
| |
202 |
392
| _writer.begin("tr");
|
203 |
392
| writeRowClass();
|
204 |
| |
205 |
392
| _writer.begin("th");
|
206 |
392
| _writer.print(key);
|
207 |
392
| _writer.end();
|
208 |
392
| _writer.begin("td");
|
209 |
| |
210 |
392
| describeNested(value);
|
211 |
| |
212 |
392
| _writer.end("tr");
|
213 |
392
| _writer.println();
|
214 |
| } |
215 |
| |
216 |
437
| private void describeNested(Object value)
|
217 |
| { |
218 |
437
| if (value == null)
|
219 |
| { |
220 |
137
| _writer.print(NULL_VALUE);
|
221 |
137
| return;
|
222 |
| } |
223 |
| |
224 |
300
| new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value);
|
225 |
| } |
226 |
| |
227 |
23
| public void property(String key, boolean value)
|
228 |
| { |
229 |
23
| Defense.notNull(key, "key");
|
230 |
| |
231 |
| |
232 |
| |
233 |
23
| 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 |
46
| public void property(String key, int value)
|
251 |
| { |
252 |
46
| Defense.notNull(key, "key");
|
253 |
| |
254 |
46
| 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 |
59
| public void array(String key, Object[] values)
|
286 |
| { |
287 |
59
| Defense.notNull(key, "key");
|
288 |
| |
289 |
59
| assertTitleSet();
|
290 |
| |
291 |
59
| if (values == null || values.length == 0)
|
292 |
19
| return;
|
293 |
| |
294 |
40
| emitSection();
|
295 |
| |
296 |
40
| for (int i = 0; i < values.length; i++)
|
297 |
| { |
298 |
43
| _writer.begin("tr");
|
299 |
43
| writeRowClass();
|
300 |
| |
301 |
43
| _writer.begin("th");
|
302 |
| |
303 |
43
| if (i == 0)
|
304 |
40
| _writer.print(key);
|
305 |
| |
306 |
43
| _writer.end();
|
307 |
| |
308 |
43
| _writer.begin("td");
|
309 |
| |
310 |
43
| describeNested(values[i]);
|
311 |
| |
312 |
43
| _writer.end("tr");
|
313 |
43
| _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 |
| } |