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.templating.jsp.taglib;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.context.MgnlContext;
38
39 import java.io.IOException;
40
41 import javax.jcr.Node;
42 import javax.jcr.RepositoryException;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.jsp.JspException;
45 import javax.servlet.jsp.JspWriter;
46 import javax.servlet.jsp.tagext.TagSupport;
47
48 import org.apache.commons.lang3.StringUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.tldgen.annotations.BodyContent;
52 import org.tldgen.annotations.Tag;
53
54
55
56
57
58
59 @Tag(name = "breadcrumb", bodyContent = BodyContent.EMPTY)
60
61 public class Breadcrumb extends TagSupport {
62
63 private static Logger log = LoggerFactory.getLogger(Breadcrumb.class);
64
65
66
67
68 private String delimiter;
69
70 private int startLevel = 1;
71
72
73
74
75 private boolean excludeCurrent;
76
77
78
79
80 private boolean nolinkCurrent;
81
82
83
84
85 private boolean link = true;
86
87 private String hideProperty;
88
89
90
91
92 private String titleProperty;
93
94
95
96
97 private String activeCss = "active";
98
99
100
101
102
103
104 public void setDelimiter(String delimiter) {
105 this.delimiter = delimiter;
106 }
107
108
109
110
111
112
113
114 public void setStartLevel(String startLevel) {
115 this.startLevel = (new Integer(startLevel)).intValue();
116 if (this.startLevel < 1) {
117 this.startLevel = 1;
118 }
119 }
120
121
122
123
124
125
126 public void setHideProperty(String hideProperty) {
127 this.hideProperty = hideProperty;
128 }
129
130
131
132
133
134
135
136 public void setExcludeCurrent(boolean excludeCurrent) {
137 this.excludeCurrent = excludeCurrent;
138 }
139
140
141
142
143
144
145 public void setNolinkCurrent(boolean nolinkCurrent) {
146 this.nolinkCurrent = nolinkCurrent;
147 }
148
149
150
151
152
153
154 public void setLink(boolean link) {
155 this.link = link;
156 }
157
158
159
160
161
162
163 public void setTitleProperty(String titleProperty) {
164 this.titleProperty = titleProperty;
165 }
166
167
168
169
170
171
172 public void setActiveCss(String activeCss) {
173 this.activeCss = activeCss;
174 }
175
176 @Override
177 public int doStartTag() throws JspException {
178 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
179 int endLevel = 0;
180
181 try {
182 Node node = MgnlContext.getJCRSession(MgnlContext.getAggregationState().getRepository()).getNode(MgnlContext.getAggregationState().getHandle());
183 endLevel = node.getDepth();
184
185 if (this.excludeCurrent) {
186 endLevel--;
187 }
188 int addedcount = 0;
189
190 JspWriter out = pageContext.getOut();
191 for (int j = this.startLevel; j <= endLevel; j++) {
192 Node ancestor = (Node) node.getAncestor(j);
193
194 if (StringUtils.isNotEmpty(hideProperty) && ancestor.getProperty(hideProperty).getBoolean()) {
195 continue;
196 }
197
198 String title = null;
199
200 if (StringUtils.isNotEmpty(titleProperty)) {
201 title = ancestor.getProperty(titleProperty).getString();
202 }
203
204 if (StringUtils.isEmpty(title)) {
205 title = ancestor.getName();
206 }
207
208 if (StringUtils.isNotEmpty(title)) {
209 if (addedcount != 0) {
210 out.print(StringUtils.defaultString(this.delimiter, " > "));
211 }
212 if (this.link && !(endLevel == j && nolinkCurrent)) {
213 out.print("<a href=\"");
214 out.print(request.getContextPath());
215 out.print(ancestor.getPath());
216 out.print(".");
217 out.print(ServerConfiguration.getInstance().getDefaultExtension());
218 if (node.getPath().equals(ancestor.getPath())) {
219 out.print("\" class=\"");
220 out.print(activeCss);
221 }
222
223 out.print("\">");
224
225 }
226 out.print(title);
227 if (this.link && !(endLevel == j && nolinkCurrent)) {
228 out.print("</a>");
229 }
230 addedcount++;
231 }
232 }
233 } catch (RepositoryException e) {
234 log.debug("Exception caught: {}", e.getMessage(), e);
235 } catch (IOException e) {
236 throw new JspException(e);
237 }
238
239 return super.doStartTag();
240 }
241
242
243
244
245 @Override
246 public void release() {
247 this.startLevel = 1;
248 this.delimiter = null;
249 this.excludeCurrent = false;
250 this.nolinkCurrent = false;
251 this.link = true;
252 this.hideProperty = null;
253 this.titleProperty = null;
254 this.activeCss = "active";
255 super.release();
256 }
257
258 }