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