Clover coverage report - Code Coverage for tapestry release 4.0-beta-8
Coverage timestamp: Sat Sep 24 2005 11:50:34 EDT
file stats: LOC: 165   Methods: 6
NCLOC: 75   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Foreach.java 75% 89.2% 100% 85.7%
coverage coverage
 1    // Copyright 2004, 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.components;
 16   
 17    import java.util.Iterator;
 18   
 19    import org.apache.tapestry.AbstractComponent;
 20    import org.apache.tapestry.IBinding;
 21    import org.apache.tapestry.IMarkupWriter;
 22    import org.apache.tapestry.IRequestCycle;
 23    import org.apache.tapestry.Tapestry;
 24    import org.apache.tapestry.coerce.ValueConverter;
 25   
 26    /**
 27    * Repeatedly renders its wrapped contents while iterating through a list of values. [ <a
 28    * href="../../../../../ComponentReference/Foreach.html">Component Reference </a>]
 29    * <p>
 30    * While the component is rendering, the property {@link #getValue() value}(accessed as
 31    * <code>components.<i>foreach</i>.value</code> is set to each successive value from the source,
 32    * and the property {@link #getIndex() index}is set to each successive index into the source
 33    * (starting with zero).
 34    *
 35    * @author Howard Lewis Ship
 36    */
 37   
 38    public abstract class Foreach extends AbstractComponent
 39    {
 40    private Object _value;
 41   
 42    private int _index;
 43   
 44    /**
 45    * Gets the source binding and returns an {@link Iterator}representing the values identified by
 46    * the source. Returns an empty {@link Iterator}if the binding, or the binding value, is null.
 47    * <p>
 48    * Invokes {@link Tapestry#coerceToIterator(Object)}to perform the actual conversion.
 49    */
 50   
 51  85 protected Iterator getSourceData()
 52    {
 53  85 Object source = null;
 54   
 55  85 IBinding sourceBinding = getBinding("source");
 56  85 if (sourceBinding != null)
 57  85 source = sourceBinding.getObject();
 58   
 59  85 if (source == null)
 60  0 return null;
 61   
 62  85 return (Iterator) getValueConverter().coerceValue(source, Iterator.class);
 63    }
 64   
 65  85 protected void prepareForRender(IRequestCycle cycle)
 66    {
 67  85 _value = null;
 68  85 _index = 0;
 69    }
 70   
 71  85 protected void cleanupAfterRender(IRequestCycle cycle)
 72    {
 73  85 _value = null;
 74    }
 75   
 76    /**
 77    * Gets the source binding and iterates through its values. For each, it updates the value
 78    * binding and render's its wrapped elements.
 79    */
 80   
 81  85 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 82    {
 83  85 Iterator dataSource = getSourceData();
 84   
 85    // The dataSource was either not convertable, or was empty.
 86   
 87  85 if (dataSource == null)
 88  0 return;
 89   
 90  85 boolean indexBound = isParameterBound("index");
 91  85 boolean valueBound = isParameterBound("value");
 92   
 93  85 String element = getElement();
 94   
 95  85 boolean hasNext = dataSource.hasNext();
 96   
 97  85 while (hasNext)
 98    {
 99  1295 _value = dataSource.next();
 100  1295 hasNext = dataSource.hasNext();
 101   
 102  1295 if (indexBound)
 103  31 setIndexParameter(_index);
 104   
 105  1295 if (valueBound)
 106  1281 setValueParameter(_value);
 107   
 108  1295 if (element != null)
 109    {
 110  1261 writer.begin(element);
 111  1261 renderInformalParameters(writer, cycle);
 112    }
 113   
 114  1295 renderBody(writer, cycle);
 115   
 116  1295 if (element != null)
 117  1261 writer.end();
 118   
 119  1295 _index++;
 120    }
 121   
 122    }
 123   
 124    /**
 125    * Returns the most recent value extracted from the source parameter.
 126    *
 127    * @throws org.apache.tapestry.ApplicationRuntimeException
 128    * if the Foreach is not currently rendering.
 129    */
 130   
 131  19 public Object getValue()
 132    {
 133  19 if (!isRendering())
 134  0 throw Tapestry.createRenderOnlyPropertyException(this, "value");
 135   
 136  19 return _value;
 137    }
 138   
 139    /**
 140    * The index number, within the {@link #getSource() source}, of the the current value.
 141    *
 142    * @throws org.apache.tapestry.ApplicationRuntimeException
 143    * if the Foreach is not currently rendering.
 144    * @since 2.2
 145    */
 146   
 147  6 public int getIndex()
 148    {
 149  6 if (!isRendering())
 150  0 throw Tapestry.createRenderOnlyPropertyException(this, "index");
 151   
 152  6 return _index;
 153    }
 154   
 155    public abstract String getElement();
 156   
 157    /** @since 4.0 */
 158    public abstract void setIndexParameter(int value);
 159   
 160    /** @since 4.0 */
 161    public abstract void setValueParameter(Object value);
 162   
 163    /** @since 4.0 */
 164    public abstract ValueConverter getValueConverter();
 165    }