1 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 32 33 public class DefaultLinkRenderer implements ILinkRenderer 34 { 35 38 39 public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer(); 40 41 public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent) 42 { 43 IMarkupWriter wrappedWriter = null; 44 45 if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) 46 throw new ApplicationRuntimeException(Tapestry 47 .getMessage("AbstractLinkComponent.no-nesting"), linkComponent, null, null); 48 49 cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent); 50 51 boolean hasBody = getHasBody(); 52 53 boolean disabled = linkComponent.isDisabled(); 54 55 if (!disabled) 56 { 57 ILink l = linkComponent.getLink(cycle); 58 59 if (hasBody) 60 writer.begin(getElement()); 61 else 62 writer.beginEmpty(getElement()); 63 64 writer.attribute(getUrlAttribute(), constructURL(l, linkComponent.getAnchor(), cycle)); 65 66 String target = linkComponent.getTarget(); 67 68 if (HiveMind.isNonBlank(target)) 69 writer.attribute(getTargetAttribute(), target); 70 71 beforeBodyRender(writer, cycle, linkComponent); 72 73 77 wrappedWriter = writer.getNestedWriter(); 78 } 79 else 80 wrappedWriter = writer; 81 82 if (hasBody) 83 linkComponent.renderBody(wrappedWriter, cycle); 84 85 if (!disabled) 86 { 87 afterBodyRender(writer, cycle, linkComponent); 88 89 linkComponent.renderAdditionalAttributes(writer, cycle); 90 91 if (hasBody) 92 { 93 wrappedWriter.close(); 94 95 97 writer.end(); 98 } 99 else 100 writer.closeTag(); 101 } 102 103 cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); 104 } 105 106 110 111 protected String constructURL(ILink link, String anchor, IRequestCycle cycle) 112 { 113 return link.getURL(anchor, true); 114 } 115 116 122 123 protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link) 124 { 125 } 126 127 134 135 protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link) 136 { 137 } 138 139 140 141 protected String getElement() 142 { 143 return "a"; 144 } 145 146 protected String getUrlAttribute() 147 { 148 return "href"; 149 } 150 151 protected String getTargetAttribute() 152 { 153 return "target"; 154 } 155 156 protected boolean getHasBody() 157 { 158 return true; 159 } 160 } | Popular Tags |