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