Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
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  32 public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy adapter)
 54    {
 55  32 this(writer, adapter, new HTMLDescriptionReceiverStyles());
 56    }
 57   
 58  2718 public HTMLDescriptionReceiver(IMarkupWriter writer, DescribableStrategy strategy,
 59    HTMLDescriptionReceiverStyles styles)
 60    {
 61  2718 Defense.notNull(writer, "writer");
 62  2718 Defense.notNull(strategy, "strategy");
 63  2718 Defense.notNull(styles, "styles");
 64   
 65  2718 _writer = writer;
 66  2718 _strategy = strategy;
 67  2718 _styles = styles;
 68    }
 69   
 70    /*
 71    * (non-Javadoc)
 72    *
 73    * @see org.apache.tapestry.describe.RootDescriptionReciever#describe(java.lang.Object)
 74    */
 75  2656 public void describe(Object object)
 76    {
 77  2656 if (object == null)
 78    {
 79  2 _writer.print(NULL_VALUE);
 80  2 return;
 81    }
 82   
 83  2654 _strategy.describeObject(object, this);
 84   
 85  2654 finishUp(object);
 86    }
 87   
 88  114 public void describeAlternate(Object alternate)
 89    {
 90  114 _strategy.describeObject(alternate, this);
 91    }
 92   
 93  2898 public void finishUp()
 94    {
 95    // When false, a <table> was started, which must be closed.
 96   
 97  2898 if (!_emitDefault)
 98  352 _writer.end("table");
 99   
 100  2898 _writer.println();
 101   
 102  2898 _emitDefault = true;
 103  2898 _title = null;
 104  2898 _section = null;
 105  2898 _even = true;
 106    }
 107   
 108  2660 void finishUp(Object object)
 109    {
 110  2660 if (_emitDefault)
 111    {
 112  2546 String value = _title != null ? _title : object.toString();
 113   
 114  2546 _writer.print(value);
 115    }
 116   
 117  2660 finishUp();
 118    }
 119   
 120  374 public void title(String title)
 121    {
 122  374 Defense.notNull(title, "title");
 123   
 124  374 if (_title != null)
 125  2 throw new IllegalStateException(DescribeMessages.setTitleOnce());
 126   
 127  372 _title = title;
 128    }
 129   
 130  322 public void section(String section)
 131    {
 132  322 Defense.notNull(section, "section");
 133   
 134  322 if (_title == null)
 135  2 throw new IllegalStateException(DescribeMessages.mustSetTitleBeforeSection());
 136   
 137  320 _section = section;
 138    }
 139   
 140  1876 private void assertTitleSet()
 141    {
 142  1876 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  1834 private void emitSection()
 152    {
 153  1834 if (_emitDefault)
 154    {
 155  364 _emitDefault = false;
 156   
 157  364 _writer.begin("div");
 158  364 _writer.attribute("class", _styles.getHeaderClass());
 159  364 _writer.print(_title);
 160  364 _writer.end();
 161  364 _writer.println();
 162   
 163  364 _writer.begin("table");
 164  364 _writer.attribute("class", _styles.getTableClass());
 165  364 _writer.println();
 166   
 167  364 _even = true;
 168    }
 169   
 170  1834 if (_section != null)
 171    {
 172  214 _writer.begin("tr");
 173  214 _writer.attribute("class", _styles.getSubheaderClass());
 174  214 _writer.begin("th");
 175  214 _writer.attribute("colspan", 2);
 176  214 _writer.print(_section);
 177  214 _writer.end("tr");
 178  214 _writer.println();
 179   
 180  214 _section = null;
 181   
 182  214 _even = true;
 183    }
 184   
 185    }
 186   
 187  782 private void pair(String key, String value)
 188    {
 189  782 assertTitleSet();
 190  782 emitSection();
 191   
 192  782 _writer.begin("tr");
 193  782 writeRowClass();
 194   
 195  782 _writer.begin("th");
 196  782 _writer.print(key);
 197  782 _writer.end();
 198  782 _writer.begin("td");
 199  782 _writer.print(value);
 200  782 _writer.end("tr");
 201  782 _writer.println();
 202   
 203    }
 204   
 205  3568 private void writeRowClass()
 206    {
 207  3568 _writer.attribute("class", _even ? "even" : "odd");
 208  3568 _even = !_even;
 209    }
 210   
 211  834 public void property(String key, Object value)
 212    {
 213  834 Defense.notNull(key, "key");
 214   
 215  834 assertTitleSet();
 216  834 emitSection();
 217   
 218  834 _writer.begin("tr");
 219  834 writeRowClass();
 220   
 221  834 _writer.begin("th");
 222  834 _writer.print(key);
 223  834 _writer.end();
 224  834 _writer.begin("td");
 225   
 226  834 describeNested(value);
 227   
 228  834 _writer.end("tr");
 229  834 _writer.println();
 230    }
 231   
 232  2786 private void describeNested(Object value)
 233    {
 234  2786 if (value == null)
 235    {
 236  274 _writer.print(NULL_VALUE);
 237  274 return;
 238    }
 239   
 240  2512 new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value);
 241    }
 242   
 243  46 public void property(String key, boolean value)
 244    {
 245  46 Defense.notNull(key, "key");
 246   
 247    // toString is JDK 1.4 and above, so we'll provide our own.
 248   
 249  46 pair(key, value ? "true" : "false");
 250    }
 251   
 252  2 public void property(String key, byte value)
 253    {
 254  2 Defense.notNull(key, "key");
 255   
 256  2 pair(key, Byte.toString(value));
 257    }
 258   
 259  2 public void property(String key, short value)
 260    {
 261  2 Defense.notNull(key, "key");
 262   
 263  2 pair(key, Short.toString(value));
 264    }
 265   
 266  724 public void property(String key, int value)
 267    {
 268  724 Defense.notNull(key, "key");
 269   
 270  724 pair(key, Integer.toString(value));
 271    }
 272   
 273  2 public void property(String key, long value)
 274    {
 275  2 Defense.notNull(key, "key");
 276   
 277  2 pair(key, Long.toString(value));
 278    }
 279   
 280  2 public void property(String key, float value)
 281    {
 282  2 Defense.notNull(key, "key");
 283   
 284  2 pair(key, Float.toString(value));
 285    }
 286   
 287  2 public void property(String key, double value)
 288    {
 289  2 Defense.notNull(key, "key");
 290   
 291  2 pair(key, Double.toString(value));
 292    }
 293   
 294  2 public void property(String key, char value)
 295    {
 296  2 Defense.notNull(key, "key");
 297   
 298  2 pair(key, Character.toString(value));
 299    }
 300   
 301  118 public void array(String key, Object[] values)
 302    {
 303  118 Defense.notNull(key, "key");
 304   
 305  118 assertTitleSet();
 306   
 307  118 if (values == null || values.length == 0)
 308  38 return;
 309   
 310  80 emitSection();
 311   
 312  80 for (int i = 0; i < values.length; i++)
 313    {
 314  86 _writer.begin("tr");
 315  86 writeRowClass();
 316   
 317  86 _writer.begin("th");
 318   
 319  86 if (i == 0)
 320  80 _writer.print(key);
 321   
 322  86 _writer.end();
 323   
 324  86 _writer.begin("td");
 325   
 326  86 describeNested(values[i]);
 327   
 328  86 _writer.end("tr");
 329  86 _writer.println();
 330    }
 331   
 332    }
 333   
 334  142 public void collection(String key, Collection values)
 335    {
 336  142 Defense.notNull(key, "key");
 337   
 338  142 assertTitleSet();
 339   
 340  142 if (values == null || values.isEmpty())
 341  4 return;
 342   
 343  138 emitSection();
 344   
 345  138 Iterator i = values.iterator();
 346  138 boolean first = true;
 347   
 348  138 while (i.hasNext())
 349    {
 350  1866 _writer.begin("tr");
 351  1866 writeRowClass();
 352   
 353  1866 _writer.begin("th");
 354   
 355  1866 if (first)
 356  138 _writer.print(key);
 357   
 358  1866 _writer.end();
 359  1866 _writer.begin("td");
 360   
 361  1866 describeNested(i.next());
 362   
 363  1866 _writer.end("tr");
 364  1866 _writer.println();
 365   
 366  1866 first = false;
 367    }
 368    }
 369    }