Source code: com/RuntimeCollective/webapps/tag/BreadcrumbTag.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/BreadcrumbTag.java,v 1.4 2003/10/02 08:36:39 matt Exp $
2 * $Revision: 1.4 $
3 * $Date: 2003/10/02 08:36:39 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.tag;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33
34 import java.io.IOException;
35 import java.io.UnsupportedEncodingException;
36 import java.net.MalformedURLException;
37 import java.net.URL;
38 import java.net.URLEncoder;
39 import java.util.StringTokenizer;
40 import javax.servlet.jsp.JspWriter;
41 import javax.servlet.jsp.JspException;
42 import javax.servlet.jsp.JspTagException;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.jsp.tagext.Tag;
45 import javax.servlet.jsp.tagext.TagSupport;
46
47 import org.apache.struts.taglib.html.BaseHandlerTag;
48 import org.apache.struts.util.RequestUtils;
49
50 /**
51 * This tag displays a breadcrumb from a list of parameters (locations).
52 * The list is passed as a comma separated list of (name,path,name,...,name).
53 * Each path can be a forward or a page reference.
54 *
55 * @version $Id: BreadcrumbTag.java,v 1.4 2003/10/02 08:36:39 matt Exp $
56 */
57 public class BreadcrumbTag extends BaseHandlerTag {
58
59 /** The locations. */
60 protected String locations= null;
61 /** Get the locations. */
62 public String getLocations() { return this.locations; }
63 /** Set the locations. */
64 public void setLocations(String locations) { this.locations = locations; }
65
66 public static String DELIMITER = " > ";
67
68 public int doStartTag() throws JspException {
69
70 StringBuffer result = new StringBuffer();
71
72 if (locations != null) {
73
74 StringTokenizer st = new StringTokenizer(locations, ",");
75 String nextName;
76 String nextPath;
77 String nextURL;
78 while (st.hasMoreTokens()) {
79
80 nextName = st.nextToken();
81
82 //RuntimeParameters.logDebug(this, "[BreadcrumbsTag:doStartTag] So far : "+result.toString()+", next name : "+nextName+", has more : "+st.hasMoreTokens());
83
84 if (st.hasMoreTokens()) {
85 // we have both a name and a path, display a link
86 nextPath = (st.nextToken()).trim();
87 //RuntimeParameters.logDebug(this, "[BreadcrumbsTag:doStartTag] nextPath is ***"+nextPath+"***");
88
89 // but process the path first
90 // try as a forward, then as a page, then as an href
91 try {
92 nextURL = RequestUtils.computeURL(pageContext, nextPath, null,
93 null, null, null, false);
94 } catch (MalformedURLException e) {
95 RuntimeParameters.logDebug(this, "Path : "+nextPath+" is not a forward");
96 nextURL = null;
97 }
98 if (nextURL == null) {
99 try {
100 nextURL = RequestUtils.computeURL(pageContext, null, null,
101 nextPath, null, null, false);
102 } catch (MalformedURLException e) {
103 RuntimeParameters.logDebug(this, "Path : "+nextPath+" is not a page");
104 nextURL = null;
105 }
106 }
107 if (nextURL == null) {
108 try {
109 nextURL = RequestUtils.computeURL(pageContext, null,
110 nextPath, null, null, null, false);
111 } catch (MalformedURLException e) {
112 RuntimeParameters.logDebug(this, "Path : "+nextPath+" is not an href");
113 throw new JspTagException("Couldn't resolve path : "+nextPath);
114 }
115 }
116
117 result.append("<a href=\"")
118 .append(nextURL.toString())
119 .append("\">")
120 .append(nextName)
121 .append("</a>");
122
123 // if there's more (and there should be)
124 // display the delimiter
125 if (st.hasMoreTokens()) {
126 result.append(DELIMITER);
127 }
128
129 } else {
130 // this is the last element in the breadcrumb, a name with no path
131 result.append(nextName);
132 }
133 }
134 }
135
136 String breadcrumb = result.toString();
137 RuntimeParameters.logDebug(this, "Breadcrumb : "+breadcrumb);
138
139 try {
140 pageContext.getOut().println(breadcrumb);
141 } catch (IOException e) {
142 throw new JspTagException("I/O Exception " + e.getMessage());
143 }
144
145 return SKIP_BODY;
146 }
147
148 public void release() {
149 super.release();
150 locations = null;
151 }
152 }