KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > describe > HTMLDescriptionReceiver


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.describe;
16
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.hivemind.util.Defense;
21 import org.apache.tapestry.IMarkupWriter;
22
23 /**
24  * Implementation of {@link org.apache.tapestry.describe.DescriptionReceiver}that produces HTML
25  * output using a {@link org.apache.tapestry.IMarkupWriter}.
26  * <p>
27  * TODO: Make {@link #describeAlternate(Object)}&nbsp;exclusive with the other methods
28  * {@link #title(String)},{@link #property(String, Object)}, etc.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class HTMLDescriptionReceiver implements DescriptionReceiver
34 {
35     // Emitted for null values.
36

37     static final String JavaDoc NULL_VALUE = "<NULL>";
38
39     private IMarkupWriter _writer;
40
41     private boolean _emitDefault = true;
42
43     private String JavaDoc _title;
44
45     private String JavaDoc _section;
46
47     private DescribableStrategy _strategy;
48
49     private HTMLDescriptionReceiverStyles _styles;
50
51     public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy adapter)
52     {
53         this(writer, adapter, new HTMLDescriptionReceiverStyles());
54     }
55
56     public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy strategy,
57             HTMLDescriptionReceiverStyles styles)
58     {
59         Defense.notNull(writer, "writer");
60         Defense.notNull(strategy, "strategy");
61         Defense.notNull(styles, "styles");
62
63         _writer = writer;
64         _strategy = strategy;
65         _styles = styles;
66     }
67
68     public void describe(Object JavaDoc object)
69     {
70         if (object == null)
71         {
72             _writer.print(NULL_VALUE);
73             return;
74         }
75
76         _strategy.describeObject(object, this);
77
78         finishUp(object);
79     }
80
81     public void describeAlternate(Object JavaDoc alternate)
82     {
83         _strategy.describeObject(alternate, this);
84     }
85
86     void finishUp(Object JavaDoc object)
87     {
88         if (_emitDefault)
89         {
90             String JavaDoc value = _title != null ? _title : object.toString();
91
92             _writer.print(value);
93         }
94         // Not emit default .. means a property was emitted, so a table was started and must be
95
// finished.
96
else
97             _writer.end("table");
98
99         _writer.println();
100     }
101
102     public void title(String JavaDoc title)
103     {
104         Defense.notNull(title, "title");
105
106         if (_title != null)
107             throw new IllegalStateException JavaDoc(DescribeMessages.setTitleOnce());
108
109         _title = title;
110     }
111
112     public void section(String JavaDoc section)
113     {
114         Defense.notNull(section, "section");
115
116         if (_title == null)
117             throw new IllegalStateException JavaDoc(DescribeMessages.mustSetTitleBeforeSection());
118
119         _section = section;
120     }
121
122     private void assertTitleSet()
123     {
124         if (_title == null)
125             throw new IllegalStateException JavaDoc(DescribeMessages.mustSetTitleBeforeProperty());
126     }
127
128     private void emitSection()
129     {
130         if (_emitDefault)
131         {
132             _emitDefault = false;
133
134             _writer.begin("div");
135             _writer.attribute("class", _styles.getHeaderClass());
136             _writer.print(_title);
137             _writer.end();
138             _writer.println();
139
140             _writer.begin("table");
141             _writer.attribute("class", _styles.getTableClass());
142             _writer.println();
143         }
144
145         if (_section != null)
146         {
147             _writer.begin("tr");
148             _writer.attribute("class", _styles.getSubheaderClass());
149             _writer.begin("th");
150             _writer.attribute("colspan", 2);
151             _writer.print(_section);
152             _writer.end("tr");
153             _writer.println();
154
155             _section = null;
156         }
157     }
158
159     private void pair(String JavaDoc key, String JavaDoc value)
160     {
161         assertTitleSet();
162         emitSection();
163
164         _writer.begin("tr");
165         _writer.begin("th");
166         _writer.print(key);
167         _writer.end();
168         _writer.begin("td");
169         _writer.print(value);
170         _writer.end("tr");
171         _writer.println();
172     }
173
174     public void property(String JavaDoc key, Object JavaDoc value)
175     {
176         Defense.notNull(key, "key");
177
178         assertTitleSet();
179         emitSection();
180
181         _writer.begin("tr");
182         _writer.begin("th");
183         _writer.print(key);
184         _writer.end();
185         _writer.begin("td");
186
187         describeNested(value);
188
189         _writer.end("tr");
190         _writer.println();
191     }
192
193     private void describeNested(Object JavaDoc value)
194     {
195         if (value == null)
196         {
197             _writer.print(NULL_VALUE);
198             return;
199         }
200
201         new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value);
202     }
203
204     public void property(String JavaDoc key, boolean value)
205     {
206         Defense.notNull(key, "key");
207
208         // toString is JDK 1.4 and above, so we'll provide our own.
209

210         pair(key, value ? "true" : "false");
211     }
212
213     public void property(String JavaDoc key, byte value)
214     {
215         Defense.notNull(key, "key");
216
217         pair(key, Byte.toString(value));
218     }
219
220     public void property(String JavaDoc key, short value)
221     {
222         Defense.notNull(key, "key");
223
224         pair(key, Short.toString(value));
225     }
226
227     public void property(String JavaDoc key, int value)
228     {
229         Defense.notNull(key, "key");
230
231         pair(key, Integer.toString(value));
232     }
233
234     public void property(String JavaDoc key, long value)
235     {
236         Defense.notNull(key, "key");
237
238         pair(key, Long.toString(value));
239     }
240
241     public void property(String JavaDoc key, float value)
242     {
243         Defense.notNull(key, "key");
244
245         pair(key, Float.toString(value));
246     }
247
248     public void property(String JavaDoc key, double value)
249     {
250         Defense.notNull(key, "key");
251
252         pair(key, Double.toString(value));
253     }
254
255     public void property(String JavaDoc key, char value)
256     {
257         Defense.notNull(key, "key");
258
259         pair(key, Character.toString(value));
260     }
261
262     public void array(String JavaDoc key, Object JavaDoc[] values)
263     {
264         Defense.notNull(key, "key");
265
266         assertTitleSet();
267
268         if (values == null || values.length == 0)
269             return;
270
271         emitSection();
272
273         for (int i = 0; i < values.length; i++)
274         {
275             _writer.begin("tr");
276             _writer.begin("th");
277
278             if (i == 0)
279                 _writer.print(key);
280
281             _writer.end();
282
283             _writer.begin("td");
284
285             describeNested(values[i]);
286
287             _writer.end("tr");
288             _writer.println();
289         }
290
291     }
292
293     public void collection(String JavaDoc key, Collection JavaDoc values)
294     {
295         Defense.notNull(key, "key");
296
297         assertTitleSet();
298
299         if (values == null || values.isEmpty())
300             return;
301
302         emitSection();
303
304         Iterator JavaDoc i = values.iterator();
305         boolean first = true;
306
307         while (i.hasNext())
308         {
309             _writer.begin("tr");
310             _writer.begin("th");
311
312             if (first)
313                 _writer.print(key);
314
315             _writer.end();
316             _writer.begin("td");
317
318             describeNested(i.next());
319
320             _writer.end("tr");
321             _writer.println();
322
323             first = false;
324         }
325     }
326 }
Popular Tags