|
|||||||||||||||||||
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 | |||||||||||||||
DefaultLinkRenderer.java | 100% | 100% | 100% | 100% |
|
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 | 97 | public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent) |
42 | { | |
43 | 97 | IMarkupWriter wrappedWriter = null; |
44 | ||
45 | 97 | 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 | 96 | cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent); |
50 | ||
51 | 96 | boolean hasBody = getHasBody(); |
52 | ||
53 | 96 | boolean disabled = linkComponent.isDisabled(); |
54 | ||
55 | 96 | if (!disabled) |
56 | { | |
57 | 95 | ILink l = linkComponent.getLink(cycle); |
58 | ||
59 | 95 | if (hasBody) |
60 | 91 | writer.begin(getElement()); |
61 | else | |
62 | 4 | writer.beginEmpty(getElement()); |
63 | ||
64 | 95 | writer.attribute(getUrlAttribute(), constructURL(l, linkComponent.getAnchor(), cycle)); |
65 | ||
66 | 95 | String target = linkComponent.getTarget(); |
67 | ||
68 | 95 | if (HiveMind.isNonBlank(target)) |
69 | 1 | writer.attribute(getTargetAttribute(), target); |
70 | ||
71 | 95 | 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 | 95 | wrappedWriter = writer.getNestedWriter(); |
78 | } | |
79 | else | |
80 | 1 | wrappedWriter = writer; |
81 | ||
82 | 96 | if (hasBody) |
83 | 92 | linkComponent.renderBody(wrappedWriter, cycle); |
84 | ||
85 | 95 | if (!disabled) |
86 | { | |
87 | 94 | afterBodyRender(writer, cycle, linkComponent); |
88 | ||
89 | 94 | linkComponent.renderAdditionalAttributes(writer, cycle); |
90 | ||
91 | 93 | if (hasBody) |
92 | { | |
93 | 89 | wrappedWriter.close(); |
94 | ||
95 | // Close the <element> tag | |
96 | ||
97 | 89 | writer.end(); |
98 | } | |
99 | else | |
100 | 4 | writer.closeTag(); |
101 | } | |
102 | ||
103 | 94 | 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 | 93 | protected String constructURL(ILink link, String anchor, IRequestCycle cycle) |
112 | { | |
113 | 93 | 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 | 95 | 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 | 94 | protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link) |
136 | { | |
137 | } | |
138 | ||
139 | /** @since 3.0 * */ | |
140 | ||
141 | 87 | protected String getElement() |
142 | { | |
143 | 87 | return "a"; |
144 | } | |
145 | ||
146 | 91 | protected String getUrlAttribute() |
147 | { | |
148 | 91 | return "href"; |
149 | } | |
150 | ||
151 | 1 | protected String getTargetAttribute() |
152 | { | |
153 | 1 | return "target"; |
154 | } | |
155 | ||
156 | 92 | protected boolean getHasBody() |
157 | { | |
158 | 92 | return true; |
159 | } | |
160 | } |
|