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 |
469
| public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy strategy,
|
57 |
| HTMLDescriptionReceiverStyles styles) |
58 |
| { |
59 |
469
| Defense.notNull(writer, "writer");
|
60 |
469
| Defense.notNull(strategy, "strategy");
|
61 |
469
| Defense.notNull(styles, "styles");
|
62 |
| |
63 |
469
| _writer = writer;
|
64 |
469
| _strategy = strategy;
|
65 |
469
| _styles = styles;
|
66 |
| } |
67 |
| |
68 |
455
| public void describe(Object object)
|
69 |
| { |
70 |
455
| if (object == null)
|
71 |
| { |
72 |
1
| _writer.print(NULL_VALUE);
|
73 |
1
| return;
|
74 |
| } |
75 |
| |
76 |
454
| _strategy.describeObject(object, this);
|
77 |
| |
78 |
454
| finishUp(object);
|
79 |
| } |
80 |
| |
81 |
69
| public void describeAlternate(Object alternate)
|
82 |
| { |
83 |
69
| _strategy.describeObject(alternate, this);
|
84 |
| } |
85 |
| |
86 |
457
| void finishUp(Object object)
|
87 |
| { |
88 |
457
| if (_emitDefault)
|
89 |
| { |
90 |
388
| String value = _title != null ? _title : object.toString();
|
91 |
| |
92 |
388
| _writer.print(value);
|
93 |
| } |
94 |
| |
95 |
| |
96 |
| else |
97 |
69
| _writer.end("table");
|
98 |
| |
99 |
457
| _writer.println();
|
100 |
| } |
101 |
| |
102 |
81
| public void title(String title)
|
103 |
| { |
104 |
81
| Defense.notNull(title, "title");
|
105 |
| |
106 |
81
| if (_title != null)
|
107 |
1
| throw new IllegalStateException(DescribeMessages.setTitleOnce());
|
108 |
| |
109 |
80
| _title = title;
|
110 |
| } |
111 |
| |
112 |
134
| public void section(String section)
|
113 |
| { |
114 |
134
| Defense.notNull(section, "section");
|
115 |
| |
116 |
134
| if (_title == null)
|
117 |
1
| throw new IllegalStateException(DescribeMessages.mustSetTitleBeforeSection());
|
118 |
| |
119 |
133
| _section = section;
|
120 |
| } |
121 |
| |
122 |
641
| private void assertTitleSet()
|
123 |
| { |
124 |
641
| if (_title == null)
|
125 |
0
| throw new IllegalStateException(DescribeMessages.mustSetTitleBeforeProperty());
|
126 |
| } |
127 |
| |
128 |
616
| private void emitSection()
|
129 |
| { |
130 |
616
| if (_emitDefault)
|
131 |
| { |
132 |
75
| _emitDefault = false;
|
133 |
| |
134 |
75
| _writer.begin("div");
|
135 |
75
| _writer.attribute("class", _styles.getHeaderClass());
|
136 |
75
| _writer.print(_title);
|
137 |
75
| _writer.end();
|
138 |
75
| _writer.println();
|
139 |
| |
140 |
75
| _writer.begin("table");
|
141 |
75
| _writer.attribute("class", _styles.getTableClass());
|
142 |
75
| _writer.println();
|
143 |
| } |
144 |
| |
145 |
616
| if (_section != null)
|
146 |
| { |
147 |
78
| _writer.begin("tr");
|
148 |
78
| _writer.attribute("class", _styles.getSubheaderClass());
|
149 |
78
| _writer.begin("th");
|
150 |
78
| _writer.attribute("colspan", 2);
|
151 |
78
| _writer.print(_section);
|
152 |
78
| _writer.end("tr");
|
153 |
78
| _writer.println();
|
154 |
| |
155 |
78
| _section = null;
|
156 |
| } |
157 |
| } |
158 |
| |
159 |
87
| private void pair(String key, String value)
|
160 |
| { |
161 |
87
| assertTitleSet();
|
162 |
87
| emitSection();
|
163 |
| |
164 |
87
| _writer.begin("tr");
|
165 |
87
| _writer.begin("th");
|
166 |
87
| _writer.print(key);
|
167 |
87
| _writer.end();
|
168 |
87
| _writer.begin("td");
|
169 |
87
| _writer.print(value);
|
170 |
87
| _writer.end("tr");
|
171 |
87
| _writer.println();
|
172 |
| } |
173 |
| |
174 |
478
| public void property(String key, Object value)
|
175 |
| { |
176 |
478
| Defense.notNull(key, "key");
|
177 |
| |
178 |
478
| assertTitleSet();
|
179 |
478
| emitSection();
|
180 |
| |
181 |
478
| _writer.begin("tr");
|
182 |
478
| _writer.begin("th");
|
183 |
478
| _writer.print(key);
|
184 |
478
| _writer.end();
|
185 |
478
| _writer.begin("td");
|
186 |
| |
187 |
478
| describeNested(value);
|
188 |
| |
189 |
478
| _writer.end("tr");
|
190 |
478
| _writer.println();
|
191 |
| } |
192 |
| |
193 |
533
| private void describeNested(Object value)
|
194 |
| { |
195 |
533
| if (value == null)
|
196 |
| { |
197 |
169
| _writer.print(NULL_VALUE);
|
198 |
169
| return;
|
199 |
| } |
200 |
| |
201 |
364
| new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value);
|
202 |
| } |
203 |
| |
204 |
27
| public void property(String key, boolean value)
|
205 |
| { |
206 |
27
| Defense.notNull(key, "key");
|
207 |
| |
208 |
| |
209 |
| |
210 |
27
| 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 |
54
| public void property(String key, int value)
|
228 |
| { |
229 |
54
| Defense.notNull(key, "key");
|
230 |
| |
231 |
54
| 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 |
73
| public void array(String key, Object[] values)
|
263 |
| { |
264 |
73
| Defense.notNull(key, "key");
|
265 |
| |
266 |
73
| assertTitleSet();
|
267 |
| |
268 |
73
| if (values == null || values.length == 0)
|
269 |
23
| return;
|
270 |
| |
271 |
50
| emitSection();
|
272 |
| |
273 |
50
| for (int i = 0; i < values.length; i++)
|
274 |
| { |
275 |
53
| _writer.begin("tr");
|
276 |
53
| _writer.begin("th");
|
277 |
| |
278 |
53
| if (i == 0)
|
279 |
50
| _writer.print(key);
|
280 |
| |
281 |
53
| _writer.end();
|
282 |
| |
283 |
53
| _writer.begin("td");
|
284 |
| |
285 |
53
| describeNested(values[i]);
|
286 |
| |
287 |
53
| _writer.end("tr");
|
288 |
53
| _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 |
| } |