View Javadoc

1   /**
2    * This file Copyright (c) 2012 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   * @author Marcel Salathe
60   * @author Fabrizio Giustina
61   * @version $Revision $ ($Author $)
62   */
63  @Tag(name="breadcrumb", bodyContent=BodyContent.EMPTY)
64  
65  public class Breadcrumb extends TagSupport {
66  
67      /**
68       * Stable serialVersionUID.
69       */
70      private static final long serialVersionUID = 222L;
71  
72      /**
73       * Logger.
74       */
75      private static Logger log = LoggerFactory.getLogger(Breadcrumb.class);
76  
77      /**
78       * Delimeter between links.
79       */
80      private String delimiter;
81  
82      private int startLevel = 1;
83  
84      /**
85       * Exclude current page from breadcrumb.
86       */
87      private boolean excludeCurrent;
88  
89      /**
90       * Add current page but without links.
91       */
92      private boolean nolinkCurrent;
93  
94      /**
95       * Output as link. (default: true)
96       */
97      private boolean link = true;
98  
99      private String hideProperty;
100 
101     /**
102      * Name for the property used as page title.
103      */
104     private String titleProperty;
105 
106     /**
107      * Css class for active page.
108      */
109     private String activeCss = "active";
110 
111     /**
112      * displayed between the page names, e.g. ">"
113      * @jsp.attribute required="false" rtexprvalue="true"
114      */
115     public void setDelimiter(String delimiter) {
116         this.delimiter = delimiter;
117     }
118 
119     /**
120      * At which level to start.
121      * Often you will want to omit top levels, e.g. if you split your site into multiple languages.
122      * @jsp.attribute required="false" rtexprvalue="true"
123      */
124     public void setStartLevel(String startLevel) {
125         this.startLevel = (new Integer(startLevel)).intValue();
126         if (this.startLevel < 1) {
127             this.startLevel = 1;
128         }
129     }
130 
131     /**
132      * Name for a page property which, if set, will make the page hidden in the breadcrumb.
133      * @jsp.attribute required="false" rtexprvalue="true"
134      */
135     public void setHideProperty(String hideProperty) {
136         this.hideProperty = hideProperty;
137     }
138 
139     /**
140      * Exclude the current (active) page from the breadcrumb. Defaults to false. If true, the current (active) page is
141      * not included in breadcrumb.
142      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
143      */
144     public void setExcludeCurrent(boolean excludeCurrent) {
145         this.excludeCurrent = excludeCurrent;
146     }
147 
148     /**
149      * Add current page but without links. Defaults to false (also current page is linked)
150      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
151      */
152     public void setNolinkCurrent(boolean nolinkCurrent) {
153         this.nolinkCurrent = nolinkCurrent;
154     }
155 
156     /**
157      * Create links to pages. Defaults to true.
158      * @jsp.attribute required="false" rtexprvalue="true" type="boolean"
159      */
160     public void setLink(boolean link) {
161         this.link = link;
162     }
163 
164     /**
165      * Name for a page property which holds the title to display in breadcrumbs. If empty, the standard title is used.
166      * @jsp.attribute required="false" rtexprvalue="true"
167      */
168     public void setTitleProperty(String titleProperty) {
169         this.titleProperty = titleProperty;
170     }
171 
172     /**
173      * Css class added to the current page link.
174      * @jsp.attribute required="false" rtexprvalue="true"
175      */
176     public void setActiveCss(String activeCss) {
177         this.activeCss = activeCss;
178     }
179 
180     @Override
181     public int doStartTag() throws JspException {
182         HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
183         int endLevel = 0;
184 
185         try {
186             Node node = MgnlContext.getJCRSession(MgnlContext.getAggregationState().getRepository()).getNode(MgnlContext.getAggregationState().getHandle());
187             endLevel = node.getDepth();
188 
189             if (this.excludeCurrent) {
190                 endLevel--;
191             }
192             int addedcount = 0;
193 
194             JspWriter out = pageContext.getOut();
195             for (int j = this.startLevel; j <= endLevel; j++) {
196                 Node ancestor = (Node) node.getAncestor(j);
197 
198                 if (StringUtils.isNotEmpty(hideProperty) && ancestor.getProperty(hideProperty).getBoolean()) {
199                     continue;
200                 }
201 
202                 String title = null;
203 
204                 if (StringUtils.isNotEmpty(titleProperty)) {
205                     title = ancestor.getProperty(titleProperty).getString();
206                 }
207 
208                 if (StringUtils.isEmpty(title)) {
209                     title = ancestor.getName();
210                 }
211 
212                 if (StringUtils.isNotEmpty(title)) {
213                     if (addedcount != 0) {
214                         out.print(StringUtils.defaultString(this.delimiter, " &gt; "));
215                     }
216                     if (this.link && !(endLevel == j && nolinkCurrent)) {
217                         out.print("<a href=\"");
218                         out.print(request.getContextPath());
219                         out.print(ancestor.getPath());
220                         out.print(".");
221                         out.print(ServerConfiguration.getInstance().getDefaultExtension());
222                         if (node.getPath().equals(ancestor.getPath())) {
223                             out.print("\" class=\"");
224                             out.print(activeCss);
225                         }
226 
227                         out.print("\">");
228 
229                     }
230                     out.print(title);
231                     if (this.link && !(endLevel == j && nolinkCurrent)) {
232                         out.print("</a>");
233                     }
234                     addedcount++;
235                 }
236             }
237         }
238         catch (RepositoryException e) {
239             log.debug("Exception caught: " + e.getMessage(), e);
240         }
241         catch (IOException e) {
242             throw new NestableRuntimeException(e);
243         }
244 
245         return super.doStartTag();
246     }
247 
248     /**
249      * @see javax.servlet.jsp.tagext.TagSupport#release()
250      */
251     @Override
252     public void release() {
253         this.startLevel = 1;
254         this.delimiter = null;
255         this.excludeCurrent = false;
256         this.nolinkCurrent = false;
257         this.link = true;
258         this.hideProperty = null;
259         this.titleProperty = null;
260         this.activeCss = "active";
261         super.release();
262     }
263 
264 }