1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.taglibs.util;
35
36 import info.magnolia.cms.beans.config.ContentRepository;
37 import info.magnolia.cms.beans.config.ServerConfiguration;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.core.HierarchyManager;
40 import info.magnolia.cms.core.NodeData;
41 import info.magnolia.cms.taglibs.Resource;
42 import info.magnolia.context.MgnlContext;
43
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.Iterator;
47 import java.util.List;
48
49 import javax.jcr.PropertyType;
50 import javax.jcr.RepositoryException;
51 import javax.servlet.jsp.JspWriter;
52 import javax.servlet.jsp.tagext.BodyTagSupport;
53
54 import org.apache.commons.lang.StringUtils;
55 import org.apache.commons.lang.exception.NestableRuntimeException;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59
60
61
62
63
64
65
66
67
68
69
70 public class AHref extends BodyTagSupport {
71
72
73
74
75 private static final long serialVersionUID = 222L;
76
77
78
79
80 private static Logger log = LoggerFactory.getLogger(AHref.class);
81
82
83
84
85 private String preHref;
86
87
88
89
90 private String postHref;
91
92
93
94
95 private int level;
96
97
98
99
100 private String templateName;
101
102
103
104
105 private String nodeDataName;
106
107
108
109
110 private transient List attributes;
111
112
113
114
115
116
117 public void setAtomName(String name) {
118 this.setNodeDataName(name);
119 }
120
121
122
123
124
125 public void setNodeDataName(String name) {
126 this.nodeDataName = name;
127 }
128
129
130
131
132
133 public void setPreHref(String preHref) {
134 this.preHref = preHref;
135 }
136
137
138
139
140
141 public void setPostHref(String postHref) {
142 this.postHref = postHref;
143 }
144
145
146
147
148
149 public void setTemplateName(String templateName) {
150 this.templateName = templateName;
151 }
152
153
154
155
156
157 public void setLevel(int level) {
158 this.level = level;
159 }
160
161
162
163
164
165
166 public void setAttribute(String name, String value) {
167 if (attributes == null) {
168 attributes = new ArrayList();
169 }
170 String[] attributeArray = new String[]{name, value};
171 attributes.add(attributeArray);
172 }
173
174
175
176
177 public int doEndTag() {
178 if (StringUtils.isEmpty(this.templateName)) {
179 if (this.nodeDataName == null) {
180 this.writeLink(StringUtils.EMPTY);
181 return EVAL_BODY_BUFFERED;
182 }
183 Content contentNode = Resource.getLocalContentNode();
184 if (contentNode == null) {
185 contentNode = Resource.getGlobalContentNode();
186 if (contentNode == null) {
187 this.writeLink(StringUtils.EMPTY);
188 return EVAL_BODY_BUFFERED;
189 }
190 }
191
192 NodeData nodeData = contentNode.getNodeData(this.nodeDataName);
193
194 if ((nodeData == null) || !nodeData.isExist()) {
195 this.writeLink(StringUtils.EMPTY);
196 return EVAL_BODY_BUFFERED;
197 }
198 int type = nodeData.getType();
199 if (type == PropertyType.STRING) {
200 if (StringUtils.isEmpty(nodeData.getString())) {
201 this.writeLink(StringUtils.EMPTY);
202 }
203 else {
204 this.writeLink(nodeData.getString());
205 }
206 }
207 }
208 else {
209 Content startPage;
210 try {
211 startPage = Resource.getCurrentActivePage().getAncestor(this.level);
212 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
213 Content resultPage = hm.getPage(startPage.getHandle(), this.templateName);
214 this.writeLink(resultPage.getHandle());
215 }
216 catch (RepositoryException e) {
217 log.error(e.getMessage());
218 this.writeLink(StringUtils.EMPTY);
219 }
220 }
221 return EVAL_BODY_BUFFERED;
222 }
223
224
225
226
227
228 private void writeLink(String path) {
229 JspWriter out = pageContext.getOut();
230 try {
231 if (StringUtils.isNotEmpty(path)) {
232
233 out.print("<a href=\"");
234 if (this.preHref != null) {
235 out.print(this.preHref);
236 }
237 out.print(path);
238 if (MgnlContext.getHierarchyManager(ContentRepository.WEBSITE).isPage(path)) {
239 out.print(".");
240 out.print(ServerConfiguration.getInstance().getDefaultExtension());
241 }
242 if (this.postHref != null) {
243 out.print(this.postHref);
244 }
245 out.print("\"");
246 if ((attributes != null) && (attributes.size() > 0)) {
247 Iterator i = attributes.iterator();
248 while (i.hasNext()) {
249 String[] s = (String[]) i.next();
250 out.print(" ");
251 out.print(s[0]);
252 out.print("=\"");
253 out.print(s[1]);
254 out.print("\"");
255 }
256 }
257 out.print(">");
258 }
259 out.print(getBodyContent().getString());
260 if (StringUtils.isNotEmpty(path)) {
261 out.print("</a>");
262 }
263 }
264 catch (RepositoryException e) {
265 log.error(e.getMessage(), e);
266 }
267 catch (IOException e) {
268 throw new NestableRuntimeException(e);
269 }
270 attributes = null;
271 }
272
273
274
275
276 public void release() {
277 this.preHref = null;
278 this.postHref = null;
279 this.level = 0;
280 this.templateName = null;
281 this.nodeDataName = null;
282 this.attributes = null;
283 super.release();
284 }
285
286 }