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