Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 164   Methods: 8
NCLOC: 80   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultLinkRenderer.java 100% 100% 100% 100%
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  78 public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
 42    {
 43  78 IMarkupWriter wrappedWriter = null;
 44   
 45  78 if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
 46  1 throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null,
 47    null);
 48   
 49  77 cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
 50   
 51  77 boolean hasBody = getHasBody();
 52   
 53  77 boolean disabled = linkComponent.isDisabled();
 54   
 55  77 if (!disabled)
 56    {
 57  74 if (hasBody)
 58  69 writer.begin(getElement());
 59    else
 60  5 writer.beginEmpty(getElement());
 61   
 62  74 writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle));
 63   
 64  74 String target = linkComponent.getTarget();
 65   
 66  74 if (HiveMind.isNonBlank(target))
 67  3 writer.attribute(getTargetAttribute(), target);
 68   
 69  74 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  74 wrappedWriter = writer.getNestedWriter();
 76    }
 77    else
 78  3 wrappedWriter = writer;
 79   
 80  77 if (hasBody)
 81  71 linkComponent.renderBody(wrappedWriter, cycle);
 82   
 83  77 if (!disabled)
 84    {
 85  74 afterBodyRender(writer, cycle, linkComponent);
 86   
 87  74 linkComponent.renderAdditionalAttributes(writer, cycle);
 88   
 89  74 if (hasBody)
 90    {
 91  69 wrappedWriter.close();
 92   
 93    // Close the <element> tag
 94   
 95  69 writer.end();
 96    }
 97    else
 98  5 writer.closeTag();
 99    }
 100   
 101  77 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  74 protected String constructURL(ILinkComponent component, IRequestCycle cycle)
 111    {
 112  74 ILink link = component.getLink(cycle);
 113   
 114  74 String scheme = component.getScheme();
 115  74 String anchor = component.getAnchor();
 116   
 117  74 return link.getURL(scheme, null, 0, anchor, true);
 118    }
 119   
 120    /**
 121    * Invoked after the href attribute has been written but before the body of the link is rendered
 122    * (but only if the link is not disabled).
 123    * <p>
 124    * This implementation does nothing.
 125    */
 126   
 127  72 protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 128    {
 129    }
 130   
 131    /**
 132    * Invoked after the body of the link is rendered, but before
 133    * {@link ILinkComponent#renderAdditionalAttributes(IMarkupWriter, IRequestCycle)}is invoked
 134    * (but only if the link is not disabled).
 135    * <p>
 136    * This implementation does nothing.
 137    */
 138   
 139  72 protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 140    {
 141    }
 142   
 143    /** @since 3.0 * */
 144   
 145  64 protected String getElement()
 146    {
 147  64 return "a";
 148    }
 149   
 150  68 protected String getUrlAttribute()
 151    {
 152  68 return "href";
 153    }
 154   
 155  1 protected String getTargetAttribute()
 156    {
 157  1 return "target";
 158    }
 159   
 160  69 protected boolean getHasBody()
 161    {
 162  69 return true;
 163    }
 164    }