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