Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 166   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    * @deprecated As of release 4.0, replaced by {@link ForBean}
 37    */
 38   
 39    public abstract class Foreach extends AbstractComponent
 40    {
 41    private Object _value;
 42   
 43    private int _index;
 44   
 45    /**
 46    * Gets the source binding and returns an {@link Iterator}representing the values identified by
 47    * the source. Returns an empty {@link Iterator}if the binding, or the binding value, is null.
 48    * <p>
 49    * Invokes {@link Tapestry#coerceToIterator(Object)}to perform the actual conversion.
 50    */
 51   
 52  140 protected Iterator getSourceData()
 53    {
 54  140 Object source = null;
 55   
 56  140 IBinding sourceBinding = getBinding("source");
 57  140 if (sourceBinding != null)
 58  140 source = sourceBinding.getObject();
 59   
 60  140 if (source == null)
 61  0 return null;
 62   
 63  140 return (Iterator) getValueConverter().coerceValue(source, Iterator.class);
 64    }
 65   
 66  140 protected void prepareForRender(IRequestCycle cycle)
 67    {
 68  140 _value = null;
 69  140 _index = 0;
 70    }
 71   
 72  140 protected void cleanupAfterRender(IRequestCycle cycle)
 73    {
 74  140 _value = null;
 75    }
 76   
 77    /**
 78    * Gets the source binding and iterates through its values. For each, it updates the value
 79    * binding and render's its wrapped elements.
 80    */
 81   
 82  140 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 83    {
 84  140 Iterator dataSource = getSourceData();
 85   
 86    // The dataSource was either not convertable, or was empty.
 87   
 88  140 if (dataSource == null)
 89  0 return;
 90   
 91  140 boolean indexBound = isParameterBound("index");
 92  140 boolean valueBound = isParameterBound("value");
 93   
 94  140 String element = getElement();
 95   
 96  140 boolean hasNext = dataSource.hasNext();
 97   
 98  140 while (hasNext)
 99    {
 100  2118 _value = dataSource.next();
 101  2118 hasNext = dataSource.hasNext();
 102   
 103  2118 if (indexBound)
 104  48 setIndexParameter(_index);
 105   
 106  2118 if (valueBound)
 107  2090 setValueParameter(_value);
 108   
 109  2118 if (element != null)
 110    {
 111  2064 writer.begin(element);
 112  2064 renderInformalParameters(writer, cycle);
 113    }
 114   
 115  2118 renderBody(writer, cycle);
 116   
 117  2118 if (element != null)
 118  2064 writer.end();
 119   
 120  2118 _index++;
 121    }
 122   
 123    }
 124   
 125    /**
 126    * Returns the most recent value extracted from the source parameter.
 127    *
 128    * @throws org.apache.tapestry.ApplicationRuntimeException
 129    * if the Foreach is not currently rendering.
 130    */
 131   
 132  38 public Object getValue()
 133    {
 134  38 if (!isRendering())
 135  0 throw Tapestry.createRenderOnlyPropertyException(this, "value");
 136   
 137  38 return _value;
 138    }
 139   
 140    /**
 141    * The index number, within the {@link #getSource() source}, of the the current value.
 142    *
 143    * @throws org.apache.tapestry.ApplicationRuntimeException
 144    * if the Foreach is not currently rendering.
 145    * @since 2.2
 146    */
 147   
 148  12 public int getIndex()
 149    {
 150  12 if (!isRendering())
 151  0 throw Tapestry.createRenderOnlyPropertyException(this, "index");
 152   
 153  12 return _index;
 154    }
 155   
 156    public abstract String getElement();
 157   
 158    /** @since 4.0 */
 159    public abstract void setIndexParameter(int value);
 160   
 161    /** @since 4.0 */
 162    public abstract void setValueParameter(Object value);
 163   
 164    /** @since 4.0 */
 165    public abstract ValueConverter getValueConverter();
 166    }