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: 194   Methods: 8
NCLOC: 129   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
IfBean.java 70% 80% 50% 74.4%
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.io.IOException;
 18   
 19    import org.apache.hivemind.ApplicationRuntimeException;
 20    import org.apache.hivemind.HiveMind;
 21    import org.apache.tapestry.IActionListener;
 22    import org.apache.tapestry.IBinding;
 23    import org.apache.tapestry.IForm;
 24    import org.apache.tapestry.IMarkupWriter;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.Tapestry;
 27    import org.apache.tapestry.TapestryUtils;
 28    import org.apache.tapestry.form.AbstractFormComponent;
 29    import org.apache.tapestry.services.DataSqueezer;
 30   
 31    /**
 32    * @author mb
 33    */
 34    public abstract class IfBean extends AbstractFormComponent
 35    {
 36    public final static String IF_VALUE_ATTRIBUTE = "org.mb.tapestry.base.IfValue";
 37   
 38    public abstract IBinding getConditionValueBinding();
 39   
 40    public abstract boolean getCondition();
 41    public abstract boolean getVolatile();
 42    public abstract String getElement();
 43    public abstract IActionListener getListener();
 44   
 45    private boolean _rendering = false;
 46    private boolean _conditionValue;
 47   
 48  94 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 49    {
 50  94 boolean cycleRewinding = cycle.isRewinding();
 51   
 52    // form may be null if component is not located in a form
 53  94 IForm form = (IForm) cycle.getAttribute(TapestryUtils.FORM_ATTRIBUTE);
 54   
 55    // If the cycle is rewinding, but not this particular form,
 56    // then do nothing (don't even render the body).
 57  94 if (cycleRewinding && form != null && !cycleRewinding)
 58  0 return;
 59   
 60    // get the condition. work with a hidden field if necessary
 61  94 _conditionValue = evaluateCondition(cycle, form, cycleRewinding);
 62  94 _rendering = true;
 63   
 64  94 try {
 65    // call listener
 66  94 IActionListener listener = getListener();
 67  94 if (listener != null)
 68  0 listener.actionTriggered(this, cycle);
 69   
 70    // now render if condition is true
 71  94 if (_conditionValue)
 72    {
 73  42 String element = getElement();
 74   
 75  42 boolean render = !cycleRewinding && HiveMind.isNonBlank(element);
 76   
 77  42 if (render)
 78    {
 79  1 writer.begin(element);
 80  1 renderInformalParameters(writer, cycle);
 81    }
 82   
 83  42 renderBody(writer, cycle);
 84   
 85  42 if (render)
 86  1 writer.end(element);
 87    }
 88    }
 89    finally {
 90  94 _rendering = false;
 91    }
 92   
 93  94 cycle.setAttribute(IF_VALUE_ATTRIBUTE, new Boolean(_conditionValue));
 94    }
 95   
 96  94 protected boolean evaluateCondition(IRequestCycle cycle, IForm form, boolean cycleRewinding)
 97    {
 98  94 boolean condition;
 99   
 100  94 if (form == null || getVolatile()) {
 101  88 condition = getCondition();
 102    }
 103    else {
 104    // we are in a form and we care -- load/store the condition in a hidden field
 105  6 String name = form.getElementId(this);
 106   
 107  6 if (!cycleRewinding)
 108    {
 109  4 condition = getCondition();
 110  4 writeValue(form, name, condition);
 111    }
 112    else
 113    {
 114  2 condition = readValue(cycle, name);
 115    }
 116    }
 117   
 118    // write condition value if parameter is bound
 119  94 IBinding conditionValueBinding = getConditionValueBinding();
 120  94 if (conditionValueBinding != null)
 121  0 conditionValueBinding.setObject(new Boolean(condition));
 122   
 123  94 return condition;
 124    }
 125   
 126  4 private void writeValue(IForm form, String name, boolean value)
 127    {
 128  4 String externalValue;
 129   
 130  4 Object booleanValue = new Boolean(value);
 131  4 try
 132    {
 133  4 externalValue = getDataSqueezer().squeeze(booleanValue);
 134    }
 135    catch (IOException ex)
 136    {
 137  0 throw new ApplicationRuntimeException(
 138    Tapestry.format("If.unable-to-convert-value", booleanValue),
 139    this,
 140    null,
 141    ex);
 142    }
 143   
 144  4 form.addHiddenValue(name, externalValue);
 145    }
 146   
 147  2 private boolean readValue(IRequestCycle cycle, String name)
 148    {
 149  2 String submittedValue = cycle.getParameter(name);
 150   
 151  2 try
 152    {
 153  2 Object valueObject = getDataSqueezer().unsqueeze(submittedValue);
 154  2 if (!(valueObject instanceof Boolean))
 155  0 throw new ApplicationRuntimeException(
 156    Tapestry.format("If.invalid-condition-type", submittedValue));
 157   
 158  2 return ((Boolean) valueObject).booleanValue();
 159    }
 160    catch (IOException ex)
 161    {
 162  0 throw new ApplicationRuntimeException(
 163    Tapestry.format("If.unable-to-convert-string", submittedValue),
 164    this,
 165    null,
 166    ex);
 167    }
 168    }
 169   
 170    public abstract DataSqueezer getDataSqueezer();
 171   
 172   
 173  0 public boolean isDisabled()
 174    {
 175  0 return false;
 176    }
 177   
 178    /**
 179    * Returns the value of the condition
 180    * @return the condition value
 181    */
 182  0 public boolean getConditionValue()
 183    {
 184  0 if (!_rendering)
 185  0 throw Tapestry.createRenderOnlyPropertyException(this, "conditionValue");
 186   
 187  0 return _conditionValue;
 188    }
 189   
 190    // Do nothing in those methods, but make the JVM happy
 191  0 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) { }
 192  0 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) { }
 193   
 194    }