1 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 33 public class HTMLDescriptionReceiver implements DescriptionReceiver 34 { 35 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 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 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 alternate) 82 { 83 _strategy.describeObject(alternate, this); 84 } 85 86 void finishUp(Object object) 87 { 88 if (_emitDefault) 89 { 90 String value = _title != null ? _title : object.toString(); 91 92 _writer.print(value); 93 } 94 else 97 _writer.end("table"); 98 99 _writer.println(); 100 } 101 102 public void title(String title) 103 { 104 Defense.notNull(title, "title"); 105 106 if (_title != null) 107 throw new IllegalStateException (DescribeMessages.setTitleOnce()); 108 109 _title = title; 110 } 111 112 public void section(String section) 113 { 114 Defense.notNull(section, "section"); 115 116 if (_title == null) 117 throw new IllegalStateException (DescribeMessages.mustSetTitleBeforeSection()); 118 119 _section = section; 120 } 121 122 private void assertTitleSet() 123 { 124 if (_title == null) 125 throw new IllegalStateException (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 key, String 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 key, Object 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 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 key, boolean value) 205 { 206 Defense.notNull(key, "key"); 207 208 210 pair(key, value ? "true" : "false"); 211 } 212 213 public void property(String key, byte value) 214 { 215 Defense.notNull(key, "key"); 216 217 pair(key, Byte.toString(value)); 218 } 219 220 public void property(String key, short value) 221 { 222 Defense.notNull(key, "key"); 223 224 pair(key, Short.toString(value)); 225 } 226 227 public void property(String key, int value) 228 { 229 Defense.notNull(key, "key"); 230 231 pair(key, Integer.toString(value)); 232 } 233 234 public void property(String key, long value) 235 { 236 Defense.notNull(key, "key"); 237 238 pair(key, Long.toString(value)); 239 } 240 241 public void property(String key, float value) 242 { 243 Defense.notNull(key, "key"); 244 245 pair(key, Float.toString(value)); 246 } 247 248 public void property(String key, double value) 249 { 250 Defense.notNull(key, "key"); 251 252 pair(key, Double.toString(value)); 253 } 254 255 public void property(String key, char value) 256 { 257 Defense.notNull(key, "key"); 258 259 pair(key, Character.toString(value)); 260 } 261 262 public void array(String key, Object [] 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 key, Collection 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 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 |