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