Coverage Report - org.apache.tapestry.describe.HTMLDescriptionReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
HTMLDescriptionReceiver
0%
0/158
0%
0/42
2.125
 
 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.ArrayList;
 18  
 import java.util.Collection;
 19  
 import java.util.Iterator;
 20  
 
 21  
 import org.apache.hivemind.util.Defense;
 22  
 import org.apache.tapestry.IMarkupWriter;
 23  
 
 24  
 /**
 25  
  * Implementation of {@link org.apache.tapestry.describe.DescriptionReceiver}
 26  
  * that produces HTML output using a {@link org.apache.tapestry.IMarkupWriter}.
 27  
  * <p>
 28  
  * TODO: Make {@link #describeAlternate(Object)} exclusive with the other
 29  
  * methods {@link #title(String)}, {@link #property(String, Object)}, etc.
 30  
  * </p>
 31  
  * 
 32  
  * @author Howard M. Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  
 public class HTMLDescriptionReceiver implements RootDescriptionReciever
 36  
 {
 37  
 
 38  
     // Emitted for null values.
 39  
 
 40  
     static final String NULL_VALUE = "<NULL>";
 41  
 
 42  
     private final IMarkupWriter _writer;
 43  
 
 44  0
     private boolean _emitDefault = true;
 45  
 
 46  
     private String _title;
 47  
 
 48  
     private String _section;
 49  
 
 50  
     private DescribableStrategy _strategy;
 51  
 
 52  
     private HTMLDescriptionReceiverStyles _styles;
 53  
 
 54  0
     private boolean _even = true;
 55  
 
 56  
     public HTMLDescriptionReceiver(IMarkupWriter writer,
 57  
             DescribableStrategy adapter)
 58  
     {
 59  0
         this(writer, adapter, new HTMLDescriptionReceiverStyles());
 60  0
     }
 61  
 
 62  
     public HTMLDescriptionReceiver(IMarkupWriter writer,
 63  
             DescribableStrategy strategy, HTMLDescriptionReceiverStyles styles)
 64  0
     {
 65  0
         Defense.notNull(writer, "writer");
 66  0
         Defense.notNull(strategy, "strategy");
 67  0
         Defense.notNull(styles, "styles");
 68  
 
 69  0
         _writer = writer;
 70  0
         _strategy = strategy;
 71  0
         _styles = styles;
 72  0
     }
 73  
 
 74  
     /*
 75  
      * (non-Javadoc)
 76  
      * 
 77  
      * @see org.apache.tapestry.describe.RootDescriptionReciever#describe(java.lang.Object)
 78  
      */
 79  
     public void describe(Object object)
 80  
     {
 81  0
         if (object == null)
 82  
         {
 83  0
             _writer.print(NULL_VALUE);
 84  0
             return;
 85  
         }
 86  
 
 87  0
         _strategy.describeObject(object, this);
 88  
 
 89  0
         finishUp(object);
 90  0
     }
 91  
 
 92  
     public void describeAlternate(Object alternate)
 93  
     {
 94  0
         _strategy.describeObject(alternate, this);
 95  0
     }
 96  
 
 97  
     public void finishUp()
 98  
     {
 99  
         // When false, a <table> was started, which must be closed.
 100  
 
 101  0
         if (!_emitDefault) _writer.end("table");
 102  
 
 103  0
         _writer.println();
 104  
 
 105  0
         _emitDefault = true;
 106  0
         _title = null;
 107  0
         _section = null;
 108  0
         _even = true;
 109  0
     }
 110  
 
 111  
     void finishUp(Object object)
 112  
     {
 113  0
         if (_emitDefault)
 114  
         {
 115  0
             String value = _title != null ? _title : object.toString();
 116  
 
 117  0
             _writer.print(value);
 118  
         }
 119  
 
 120  0
         finishUp();
 121  0
     }
 122  
 
 123  
     public void title(String title)
 124  
     {
 125  0
         Defense.notNull(title, "title");
 126  
 
 127  0
         if (_title != null)
 128  0
             throw new IllegalStateException(DescribeMessages.setTitleOnce());
 129  
 
 130  0
         _title = title;
 131  0
     }
 132  
 
 133  
     public void section(String section)
 134  
     {
 135  0
         Defense.notNull(section, "section");
 136  
 
 137  0
         if (_title == null)
 138  0
             throw new IllegalStateException(DescribeMessages
 139  
                     .mustSetTitleBeforeSection());
 140  
 
 141  0
         _section = section;
 142  0
     }
 143  
 
 144  
     private void assertTitleSet()
 145  
     {
 146  0
         if (_title == null)
 147  0
             throw new IllegalStateException(DescribeMessages
 148  
                     .mustSetTitleBeforeProperty());
 149  0
     }
 150  
 
 151  
     /**
 152  
      * Invoked to ensure that the section portion has been output, before any
 153  
      * properties within the section are output.
 154  
      */
 155  
 
 156  
     private void emitSection()
 157  
     {
 158  0
         if (_emitDefault)
 159  
         {
 160  0
             _emitDefault = false;
 161  
 
 162  0
             _writer.begin("div");
 163  0
             _writer.attribute("class", _styles.getHeaderClass());
 164  0
             _writer.print(_title);
 165  0
             _writer.end();
 166  0
             _writer.println();
 167  
 
 168  0
             _writer.begin("table");
 169  0
             _writer.attribute("class", _styles.getTableClass());
 170  0
             _writer.println();
 171  
 
 172  0
             _even = true;
 173  
         }
 174  
 
 175  0
         if (_section != null)
 176  
         {
 177  0
             _writer.begin("tr");
 178  0
             _writer.attribute("class", _styles.getSubheaderClass());
 179  0
             _writer.begin("th");
 180  0
             _writer.attribute("colspan", 2);
 181  0
             _writer.print(_section);
 182  0
             _writer.end("tr");
 183  0
             _writer.println();
 184  
 
 185  0
             _section = null;
 186  
 
 187  0
             _even = true;
 188  
         }
 189  
 
 190  0
     }
 191  
 
 192  
     private void pair(String key, String value)
 193  
     {
 194  0
         assertTitleSet();
 195  0
         emitSection();
 196  
 
 197  0
         _writer.begin("tr");
 198  0
         writeRowClass();
 199  
 
 200  0
         _writer.begin("th");
 201  0
         _writer.print(key);
 202  0
         _writer.end();
 203  0
         _writer.begin("td");
 204  0
         _writer.print(value);
 205  0
         _writer.end("tr");
 206  0
         _writer.println();
 207  
 
 208  0
     }
 209  
 
 210  
     private void writeRowClass()
 211  
     {
 212  0
         _writer.attribute("class", _even ? "even" : "odd");
 213  0
         _even = !_even;
 214  0
     }
 215  
 
 216  
     public void property(String key, Object value)
 217  
     {
 218  0
         Defense.notNull(key, "key");
 219  
 
 220  0
         assertTitleSet();
 221  0
         emitSection();
 222  
 
 223  0
         _writer.begin("tr");
 224  0
         writeRowClass();
 225  
 
 226  0
         _writer.begin("th");
 227  0
         _writer.print(key);
 228  0
         _writer.end();
 229  0
         _writer.begin("td");
 230  
 
 231  0
         describeNested(value);
 232  
 
 233  0
         _writer.end("tr");
 234  0
         _writer.println();
 235  0
     }
 236  
 
 237  
     private void describeNested(Object value)
 238  
     {
 239  0
         if (value == null)
 240  
         {
 241  0
             _writer.print(NULL_VALUE);
 242  0
             return;
 243  
         }
 244  
 
 245  0
         new HTMLDescriptionReceiver(_writer, _strategy, _styles).describe(value);
 246  0
     }
 247  
 
 248  
     public void property(String key, boolean value)
 249  
     {
 250  0
         Defense.notNull(key, "key");
 251  
 
 252  
         // toString is JDK 1.4 and above, so we'll provide our own.
 253  
 
 254  0
         pair(key, value ? "true" : "false");
 255  0
     }
 256  
 
 257  
     public void property(String key, byte value)
 258  
     {
 259  0
         Defense.notNull(key, "key");
 260  
 
 261  0
         pair(key, Byte.toString(value));
 262  0
     }
 263  
 
 264  
     public void property(String key, short value)
 265  
     {
 266  0
         Defense.notNull(key, "key");
 267  
 
 268  0
         pair(key, Short.toString(value));
 269  0
     }
 270  
 
 271  
     public void property(String key, int value)
 272  
     {
 273  0
         Defense.notNull(key, "key");
 274  
 
 275  0
         pair(key, Integer.toString(value));
 276  0
     }
 277  
 
 278  
     public void property(String key, long value)
 279  
     {
 280  0
         Defense.notNull(key, "key");
 281  
 
 282  0
         pair(key, Long.toString(value));
 283  0
     }
 284  
 
 285  
     public void property(String key, float value)
 286  
     {
 287  0
         Defense.notNull(key, "key");
 288  
 
 289  0
         pair(key, Float.toString(value));
 290  0
     }
 291  
 
 292  
     public void property(String key, double value)
 293  
     {
 294  0
         Defense.notNull(key, "key");
 295  
 
 296  0
         pair(key, Double.toString(value));
 297  0
     }
 298  
 
 299  
     public void property(String key, char value)
 300  
     {
 301  0
         Defense.notNull(key, "key");
 302  
 
 303  0
         pair(key, Character.toString(value));
 304  0
     }
 305  
 
 306  
     public void array(String key, Object[] values)
 307  
     {
 308  0
         Defense.notNull(key, "key");
 309  
 
 310  0
         assertTitleSet();
 311  
 
 312  0
         if (values == null || values.length == 0) return;
 313  
 
 314  0
         emitSection();
 315  
         
 316  0
         for(int i = 0; i < values.length; i++)
 317  
         {
 318  0
             _writer.begin("tr");
 319  0
             writeRowClass();
 320  
 
 321  0
             _writer.begin("th");
 322  
 
 323  0
             if (i == 0) _writer.print(key);
 324  
 
 325  0
             _writer.end();
 326  
 
 327  0
             _writer.begin("td");
 328  
 
 329  0
             describeNested(values[i]);
 330  
 
 331  0
             _writer.end("tr");
 332  0
             _writer.println();
 333  
         }
 334  
 
 335  0
     }
 336  
 
 337  
     public void collection(String key, Collection values)
 338  
     {
 339  0
         Defense.notNull(key, "key");
 340  
         
 341  0
         assertTitleSet();
 342  
         
 343  0
         if (values == null || values.isEmpty()) return;
 344  
         
 345  0
         emitSection();
 346  
         
 347  0
         Iterator i = new ArrayList(values).iterator();
 348  0
         boolean first = true;
 349  
         
 350  0
         while(i.hasNext())
 351  
         {
 352  0
             _writer.begin("tr");
 353  0
             writeRowClass();
 354  
             
 355  0
             _writer.begin("th");
 356  
 
 357  0
             if (first) 
 358  0
                 _writer.print(key);
 359  
             
 360  0
             _writer.end();
 361  0
             _writer.begin("td");
 362  
             
 363  0
             describeNested(i.next());
 364  
             
 365  0
             _writer.end("tr");
 366  0
             _writer.println();
 367  
             
 368  0
             first = false;
 369  
         }
 370  0
     }
 371  
 }