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