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: 8
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultLinkRenderer.java 93.8% 100% 100% 98.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.link;
 16   
 17    import org.apache.hivemind.ApplicationRuntimeException;
 18    import org.apache.hivemind.HiveMind;
 19    import org.apache.tapestry.IMarkupWriter;
 20    import org.apache.tapestry.IRequestCycle;
 21    import org.apache.tapestry.Tapestry;
 22    import org.apache.tapestry.components.ILinkComponent;
 23    import org.apache.tapestry.engine.ILink;
 24   
 25    /**
 26    * Default implementation of {@link org.apache.tapestry.link.ILinkRenderer}, which does nothing
 27    * special. Can be used as a base class to provide additional handling.
 28    *
 29    * @author Howard Lewis Ship, David Solis
 30    * @since 3.0
 31    */
 32   
 33    public class DefaultLinkRenderer implements ILinkRenderer
 34    {
 35    /**
 36    * A shared instance used as a default for any link that doesn't explicitly override.
 37    */
 38   
 39    public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer();
 40   
 41  158 public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
 42    {
 43  158 IMarkupWriter wrappedWriter = null;
 44   
 45  158 if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
 46  2 throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null,
 47    null);
 48   
 49  156 cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
 50   
 51  156 boolean hasBody = getHasBody();
 52   
 53  156 boolean disabled = linkComponent.isDisabled() || cycle.isRewinding();
 54   
 55  156 if (!disabled)
 56    {
 57  142 if (hasBody)
 58  134 writer.begin(getElement());
 59    else
 60  8 writer.beginEmpty(getElement());
 61   
 62  142 writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle));
 63   
 64  142 String target = linkComponent.getTarget();
 65   
 66  142 if (HiveMind.isNonBlank(target))
 67  6 writer.attribute(getTargetAttribute(), target);
 68   
 69  142 beforeBodyRender(writer, cycle, linkComponent);
 70   
 71    // Allow the wrapped components a chance to render.
 72    // Along the way, they may interact with this component
 73    // and cause the name variable to get set.
 74   
 75  142 wrappedWriter = writer.getNestedWriter();
 76    }
 77    else
 78  14 wrappedWriter = writer;
 79   
 80  156 if (hasBody)
 81  144 linkComponent.renderBody(wrappedWriter, cycle);
 82   
 83  156 if (!disabled)
 84    {
 85  142 afterBodyRender(writer, cycle, linkComponent);
 86   
 87  142 linkComponent.renderAdditionalAttributes(writer, cycle);
 88   
 89  142 if (hasBody)
 90    {
 91  134 wrappedWriter.close();
 92   
 93    // Close the <element> tag
 94   
 95  134 writer.end();
 96    }
 97    else
 98  8 writer.closeTag();
 99    }
 100   
 101  156 cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 102    }
 103   
 104    /**
 105    * Converts the EngineServiceLink into a URI or URL. This implementation gets the scheme and
 106    * anchor from the component (both of which may be null), and invokes
 107    * {@link ILink#getURL(String, String, int, String, boolean)}.
 108    */
 109   
 110  142 protected String constructURL(ILinkComponent component, IRequestCycle cycle)
 111    {
 112  142 ILink link = component.getLink(cycle);
 113   
 114  142 String scheme = component.getScheme();
 115  142 Integer port = component.getPort();
 116  142 int portI = (port == null) ? 0 : port.intValue();
 117  142 String anchor = component.getAnchor();
 118   
 119  142 return link.getURL(scheme, null, portI, anchor, true);
 120    }
 121   
 122    /**
 123    * Invoked after the href attribute has been written but before the body of the link is rendered
 124    * (but only if the link is not disabled).
 125    * <p>
 126    * This implementation does nothing.
 127    */
 128   
 129  138 protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 130    {
 131    }
 132   
 133    /**
 134    * Invoked after the body of the link is rendered, but before
 135    * {@link ILinkComponent#renderAdditionalAttributes(IMarkupWriter, IRequestCycle)}is invoked
 136    * (but only if the link is not disabled).
 137    * <p>
 138    * This implementation does nothing.
 139    */
 140   
 141  138 protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 142    {
 143    }
 144   
 145    /** @since 3.0 * */
 146   
 147  128 protected String getElement()
 148    {
 149  128 return "a";
 150    }
 151   
 152  134 protected String getUrlAttribute()
 153    {
 154  134 return "href";
 155    }
 156   
 157  2 protected String getTargetAttribute()
 158    {
 159  2 return "target";
 160    }
 161   
 162  140 protected boolean getHasBody()
 163    {
 164  140 return true;
 165    }
 166    }