Clover coverage report - Code Coverage for tapestry release 4.0-beta-2
Coverage timestamp: Sat Jul 9 2005 22:02:17 EDT
file stats: LOC: 326   Methods: 22
NCLOC: 217   Classes: 1
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
HTMLDescriptionReceiver.java 96.9% 99.2% 100% 98.9%
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)}&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 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    // Not emit default .. means a property was emitted, so a table was started and must be
 95    // finished.
 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    // toString is JDK 1.4 and above, so we'll provide our own.
 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    }