Clover coverage report - Code Coverage for tapestry release 4.0.3
Coverage timestamp: Fri May 5 2006 21:17:42 CDT
file stats: LOC: 369   Methods: 24
NCLOC: 237   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
HTMLDescriptionReceiver.java 97.2% 99.3% 100% 99%
coverage coverage
 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;
 18    import java.util.Iterator;
 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)} 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 RootDescriptionReciever
 34    {
 35    // Emitted for null values.
 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    * (non-Javadoc)
 72    *
 73    * @see org.apache.tapestry.describe.RootDescriptionReciever#describe(java.lang.Object)
 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    // When false, a <table> was started, which must be closed.
 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    * Invoked to ensure that the section portion has been output, before any properties within the
 148    * section are output.
 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    // toString is JDK 1.4 and above, so we'll provide our own.
 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    }