View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Outputs "breadcrumbs" with links to parents of the current page.
56   *
57   * @jsp.tag name="breadcrumb" body-content="empty"
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       * Delimeter between links.
67       */
68      private String delimiter;
69  
70      private int startLevel = 1;
71  
72      /**
73       * Exclude current page from breadcrumb.
74       */
75      private boolean excludeCurrent;
76  
77      /**
78       * Add current page but without links.
79       */
80      private boolean nolinkCurrent;
81  
82      /**
83       * Output as link. (default: true)
84       */
85      private boolean link = true;
86  
87      private String hideProperty;
88  
89      /**
90       * Name for the property used as page title.
91       */
92      private String titleProperty;
93  
94      /**
95       * Css class for active page.
96       */
97      private String activeCss = "active";
98  
99      /**
100      * displayed between the page names, e.g. ">"
101      *
102      * @jsp.attribute required="false" rtexprvalue="true"
103      */
104     public void setDelimiter(String delimiter) {
105         this.delimiter = delimiter;
106     }
107 
108     /**
109      * At which level to start.
110      * Often you will want to omit top levels, e.g. if you split your site into multiple languages.
111      *
112      * @jsp.attribute required="false" rtexprvalue="true"
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      * Name for a page property which, if set, will make the page hidden in the breadcrumb.
123      *
124      * @jsp.attribute required="false" rtexprvalue="true"
125      */
126     public void setHideProperty(String hideProperty) {
127         this.hideProperty = hideProperty;
128     }
129 
130     /**
131      * Exclude the current (active) page from the breadcrumb. Defaults to false. If true, the current (active) page is
132      * not included in breadcrumb.
133      *
134      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
135      */
136     public void setExcludeCurrent(boolean excludeCurrent) {
137         this.excludeCurrent = excludeCurrent;
138     }
139 
140     /**
141      * Add current page but without links. Defaults to false (also current page is linked)
142      *
143      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
144      */
145     public void setNolinkCurrent(boolean nolinkCurrent) {
146         this.nolinkCurrent = nolinkCurrent;
147     }
148 
149     /**
150      * Create links to pages. Defaults to true.
151      *
152      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
153      */
154     public void setLink(boolean link) {
155         this.link = link;
156     }
157 
158     /**
159      * Name for a page property which holds the title to display in breadcrumbs. If empty, the standard title is used.
160      *
161      * @jsp.attribute required="false" rtexprvalue="true"
162      */
163     public void setTitleProperty(String titleProperty) {
164         this.titleProperty = titleProperty;
165     }
166 
167     /**
168      * Css class added to the current page link.
169      *
170      * @jsp.attribute required="false" rtexprvalue="true"
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, " &gt; "));
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      * @see javax.servlet.jsp.tagext.TagSupport#release()
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 }