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