KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > Breadcrumb


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.util.Resource;
17
18 import java.io.IOException JavaDoc;
19
20 import javax.jcr.RepositoryException;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.lang.exception.NestableRuntimeException;
28 import org.apache.log4j.Logger;
29
30
31 /**
32  * Draws a breadcrumbs with links to parents of the current page.
33  * @author Marcel Salathe
34  * @author Fabrizio Giustina
35  * @version $Revision $ ($Author $)
36  */

37 public class Breadcrumb extends TagSupport JavaDoc {
38
39     /**
40      * Stable serialVersionUID.
41      */

42     private static final long serialVersionUID = 222L;
43
44     /**
45      * Logger.
46      */

47     private static Logger log = Logger.getLogger(Breadcrumb.class);
48
49     /**
50      * Delimeter between links.
51      */

52     private String JavaDoc delimiter;
53
54     /**
55      * Breadcrumb start level.
56      */

57     private int startLevel = 1;
58
59     /**
60      * Exclude current page from breadcrumb.
61      */

62     private boolean excludeCurrent;
63
64     /**
65      * Setter for the <code>delimeter</code> tag attribute.
66      * @param delimiter delimeter between links
67      */

68     public void setDelimiter(String JavaDoc delimiter) {
69         this.delimiter = delimiter;
70     }
71
72     /**
73      * Setter for the <code>startLevel</code> tag attribute.
74      * @param startLevel breadcrumb start level
75      */

76     public void setStartLevel(String JavaDoc startLevel) {
77         this.startLevel = (new Integer JavaDoc(startLevel)).intValue();
78         if (this.startLevel < 1) {
79             this.startLevel = 1;
80         }
81     }
82
83     /**
84      * Setter for <code>excludeCurrent</code>.
85      * @param excludeCurrent if <code>true</code> the current (active) page is not included in breadcrumb.
86      */

87     public void setExcludeCurrent(boolean excludeCurrent) {
88         this.excludeCurrent = excludeCurrent;
89     }
90
91     /**
92      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
93      */

94     public int doStartTag() throws JspException JavaDoc {
95         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
96         Content actpage = Resource.getCurrentActivePage(request);
97         int endLevel = 0;
98
99         try {
100             endLevel = actpage.getLevel();
101
102             if (this.excludeCurrent) {
103                 endLevel--;
104             }
105
106             JspWriter JavaDoc out = pageContext.getOut();
107             for (int i = this.startLevel; i <= endLevel; i++) {
108                 if (i != this.startLevel) {
109                     out.print(StringUtils.defaultString(this.delimiter, " > ")); //$NON-NLS-1$
110
}
111                 out.print("<a HREF=\""); //$NON-NLS-1$
112
out.print(request.getContextPath());
113                 out.print(actpage.getAncestor(i).getHandleWithDefaultExtension());
114                 out.print("\">"); //$NON-NLS-1$
115
out.print(actpage.getAncestor(i).getTitle());
116                 out.print("</a>"); //$NON-NLS-1$
117
}
118         }
119         catch (RepositoryException e) {
120             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
121
}
122         catch (IOException JavaDoc e) {
123             throw new NestableRuntimeException(e);
124         }
125
126         return super.doStartTag();
127     }
128
129     /**
130      * @see javax.servlet.jsp.tagext.TagSupport#release()
131      */

132     public void release() {
133         this.startLevel = 1;
134         this.delimiter = null;
135         this.excludeCurrent = false;
136         super.release();
137     }
138
139 }
140
Popular Tags