Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 160   Methods: 8
NCLOC: 78   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 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  101
     public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent)
 42   
     {
 43  101
         IMarkupWriter wrappedWriter = null;
 44   
 
 45  101
         if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null)
 46  1
             throw new ApplicationRuntimeException(Tapestry
 47   
                     .getMessage("AbstractLinkComponent.no-nesting"), linkComponent, null, null);
 48   
 
 49  100
         cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent);
 50   
 
 51  100
         boolean hasBody = getHasBody();
 52   
 
 53  100
         boolean disabled = linkComponent.isDisabled();
 54   
 
 55  100
         if (!disabled)
 56   
         {
 57  99
             ILink l = linkComponent.getLink(cycle);
 58   
 
 59  99
             if (hasBody)
 60  95
                 writer.begin(getElement());
 61   
             else
 62  4
                 writer.beginEmpty(getElement());
 63   
 
 64  99
             writer.attribute(getUrlAttribute(), constructURL(l, linkComponent.getAnchor(), cycle));
 65   
 
 66  99
             String target = linkComponent.getTarget();
 67   
 
 68  99
             if (HiveMind.isNonBlank(target))
 69  1
                 writer.attribute(getTargetAttribute(), target);
 70   
 
 71  99
             beforeBodyRender(writer, cycle, linkComponent);
 72   
 
 73   
             // Allow the wrapped components a chance to render.
 74   
             // Along the way, they may interact with this component
 75   
             // and cause the name variable to get set.
 76   
 
 77  99
             wrappedWriter = writer.getNestedWriter();
 78   
         }
 79   
         else
 80  1
             wrappedWriter = writer;
 81   
 
 82  100
         if (hasBody)
 83  96
             linkComponent.renderBody(wrappedWriter, cycle);
 84   
 
 85  99
         if (!disabled)
 86   
         {
 87  98
             afterBodyRender(writer, cycle, linkComponent);
 88   
 
 89  98
             linkComponent.renderAdditionalAttributes(writer, cycle);
 90   
 
 91  97
             if (hasBody)
 92   
             {
 93  93
                 wrappedWriter.close();
 94   
 
 95   
                 // Close the <element> tag
 96   
 
 97  93
                 writer.end();
 98   
             }
 99   
             else
 100  4
                 writer.closeTag();
 101   
         }
 102   
 
 103  98
         cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME);
 104   
     }
 105   
 
 106   
     /**
 107   
      * Converts the EngineServiceLink into a URI or URL. This implementation simply invokes
 108   
      * {@link ILink#getURL(String, boolean)}.
 109   
      */
 110   
 
 111  97
     protected String constructURL(ILink link, String anchor, IRequestCycle cycle)
 112   
     {
 113  97
         return link.getURL(anchor, true);
 114   
     }
 115   
 
 116   
     /**
 117   
      * Invoked after the href attribute has been written but before the body of the link is rendered
 118   
      * (but only if the link is not disabled).
 119   
      * <p>
 120   
      * This implementation does nothing.
 121   
      */
 122   
 
 123  99
     protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 124   
     {
 125   
     }
 126   
 
 127   
     /**
 128   
      * Invoked after the body of the link is rendered, but before
 129   
      * {@link ILinkComponent#renderAdditionalAttributes(IMarkupWriter, IRequestCycle)}is invoked
 130   
      * (but only if the link is not disabled).
 131   
      * <p>
 132   
      * This implementation does nothing.
 133   
      */
 134   
 
 135  98
     protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link)
 136   
     {
 137   
     }
 138   
 
 139   
     /** @since 3.0 * */
 140   
 
 141  91
     protected String getElement()
 142   
     {
 143  91
         return "a";
 144   
     }
 145   
 
 146  95
     protected String getUrlAttribute()
 147   
     {
 148  95
         return "href";
 149   
     }
 150   
 
 151  1
     protected String getTargetAttribute()
 152   
     {
 153  1
         return "target";
 154   
     }
 155   
 
 156  96
     protected boolean getHasBody()
 157   
     {
 158  96
         return true;
 159   
     }
 160   
 }