View Javadoc
1   /**
2    * This file Copyright (c) 2012-2016 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   * @jsp.tag name="breadcrumb" body-content="empty"
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       * Delimeter between links.
66       */
67      private String delimiter;
68  
69      private int startLevel = 1;
70  
71      /**
72       * Exclude current page from breadcrumb.
73       */
74      private boolean excludeCurrent;
75  
76      /**
77       * Add current page but without links.
78       */
79      private boolean nolinkCurrent;
80  
81      /**
82       * Output as link. (default: true)
83       */
84      private boolean link = true;
85  
86      private String hideProperty;
87  
88      /**
89       * Name for the property used as page title.
90       */
91      private String titleProperty;
92  
93      /**
94       * Css class for active page.
95       */
96      private String activeCss = "active";
97  
98      /**
99       * displayed between the page names, e.g. ">"
100      * @jsp.attribute required="false" rtexprvalue="true"
101      */
102     public void setDelimiter(String delimiter) {
103         this.delimiter = delimiter;
104     }
105 
106     /**
107      * At which level to start.
108      * Often you will want to omit top levels, e.g. if you split your site into multiple languages.
109      * @jsp.attribute required="false" rtexprvalue="true"
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      * Name for a page property which, if set, will make the page hidden in the breadcrumb.
120      * @jsp.attribute required="false" rtexprvalue="true"
121      */
122     public void setHideProperty(String hideProperty) {
123         this.hideProperty = hideProperty;
124     }
125 
126     /**
127      * Exclude the current (active) page from the breadcrumb. Defaults to false. If true, the current (active) page is
128      * not included in breadcrumb.
129      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
130      */
131     public void setExcludeCurrent(boolean excludeCurrent) {
132         this.excludeCurrent = excludeCurrent;
133     }
134 
135     /**
136      * Add current page but without links. Defaults to false (also current page is linked)
137      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
138      */
139     public void setNolinkCurrent(boolean nolinkCurrent) {
140         this.nolinkCurrent = nolinkCurrent;
141     }
142 
143     /**
144      * Create links to pages. Defaults to true.
145      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
146      */
147     public void setLink(boolean link) {
148         this.link = link;
149     }
150 
151     /**
152      * Name for a page property which holds the title to display in breadcrumbs. If empty, the standard title is used.
153      * @jsp.attribute required="false" rtexprvalue="true"
154      */
155     public void setTitleProperty(String titleProperty) {
156         this.titleProperty = titleProperty;
157     }
158 
159     /**
160      * Css class added to the current page link.
161      * @jsp.attribute required="false" rtexprvalue="true"
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, " &gt; "));
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      * @see javax.servlet.jsp.tagext.TagSupport#release()
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 }