KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > taglib > core > html > BaseTag


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.taglib.core.html;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.struts.taglib.TagUtils;
21 import org.apache.struts.util.RequestUtils;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.SimpleTagSupport JavaDoc;
27
28 /**
29  * <p>Renders an HTML Base element. This tag extends the functionality of <code>org.apache.struts.taglib.html.BaseTag</code>.
30  * It adds ability to choose, which path to set into <code>href</code> attribute of base tag. By default it includes server name,
31  * port and context path of your application, but it may include full path to page, currently displayed in browser.
32  * Also this tag can export calculated value to some scope.
33  * </p>
34  * <p>
35  * Allowed attributes are:
36  * <ul>
37  * <li>
38  * <b>server</b> - server name to use instead of <code>request.getServerName</code>
39  * </li>
40  * <li>
41  * <b>target</b> - target window for this base reference
42  * </li>
43  * <li>
44  * <b>fullPath</b> - whether to set full path to page in <code>href</code> attribute
45  * </li>
46  * <li>
47  * <b>var</b> - name of variable to export calculated value
48  * </li>
49  * <li>
50  * <b>scope</b> - scope to export variable to
51  * </li>
52  * </ul>
53  * </p>
54  * <p><a HREF="BaseTag.java.htm"><i>View Source</i></a></p>
55  *
56  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
57  * @version $Revision: 1.8 $ $Date: 2005/09/21 13:46:01 $
58  * @jsp.tag name="base"
59  * body-content="empty"
60  * @see org.apache.struts.taglib.html.BaseTag
61  */

62 public class BaseTag extends SimpleTagSupport JavaDoc {
63
64     protected transient final Log log = LogFactory.getLog(BaseTag.class);
65
66     /**
67      * The server name to use instead of <code>request.getServerName</code>
68      */

69     protected String JavaDoc server;
70
71     /**
72      * The target window for this base reference
73      */

74     protected String JavaDoc target;
75
76     /**
77      * Whether or not to set full path to page in <code>href</code> attribute
78      */

79     protected Boolean JavaDoc fullPath = Boolean.FALSE;
80
81     /**
82      * Name of variable to export calculated value
83      */

84     protected String JavaDoc var;
85
86     /**
87      * Scope to export variable to
88      */

89     protected String JavaDoc scope;
90
91     /**
92      * Returns server name
93      *
94      * @return server name
95      * @jsp.attribute required="false"
96      * rtexprvalue="true"
97      * type="java.lang.String"
98      * description="The server name to use"
99      */

100     public String JavaDoc getServer() {
101         return server;
102     }
103
104     /**
105      * Sets server name
106      *
107      * @param server server name to set
108      */

109     public void setServer(String JavaDoc server) {
110         this.server = server;
111     }
112
113     /**
114      * Returns target window
115      *
116      * @return target window
117      * @jsp.attribute required="false"
118      * rtexprvalue="true"
119      * type="java.lang.String"
120      * description="The target window for this base reference"
121      */

122     public String JavaDoc getTarget() {
123         return target;
124     }
125
126     /**
127      * Sets target window
128      *
129      * @param target target window to set
130      */

131     public void setTarget(String JavaDoc target) {
132         this.target = target;
133     }
134
135     /**
136      * Returns whether or not to set full path to page in href attribute
137      *
138      * @return whether or not to set full path to page in href attribute
139      * @jsp.attribute required="false"
140      * rtexprvalue="true"
141      * type="java.lang.Boolean"
142      * description="Whether or not to set full path to page in href attribute"
143      */

144     public Boolean JavaDoc getFullPath() {
145         return fullPath;
146     }
147
148     /**
149      * Sets whether or not to set full path to page in href attribute
150      *
151      * @param fullPath whether or not to set full path to page in href attribute
152      */

153     public void setFullPath(Boolean JavaDoc fullPath) {
154         this.fullPath = fullPath;
155     }
156
157     /**
158      * Returns name of variable that will accept calculated href
159      *
160      * @return name of variable
161      * @jsp.attribute required="false"
162      * rtexprvalue="true"
163      * type="java.lang.String"
164      * description="Name of variable to export message"
165      */

166     public String JavaDoc getVar() {
167         return var;
168     }
169
170     /**
171      * Sets name of variable that will accept calculated href
172      *
173      * @param var name of variable to set
174      */

175     public void setVar(String JavaDoc var) {
176         this.var = var;
177     }
178
179     /**
180      * Returns variable scope
181      *
182      * @return variable scope
183      * @jsp.attribute required="false"
184      * rtexprvalue="true"
185      * type="java.lang.String"
186      * description="Scope to export variable to"
187      */

188     public String JavaDoc getScope() {
189         return scope;
190     }
191
192     /**
193      * Sets variable scope
194      *
195      * @param scope variable scope to set
196      */

197     public void setScope(String JavaDoc scope) {
198         this.scope = scope;
199     }
200
201     /**
202      * Processes the tag
203      *
204      * @throws JspException
205      */

206     public void doTag() throws JspException JavaDoc {
207
208         PageContext JavaDoc pageContext = (PageContext JavaDoc) getJspContext();
209
210         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
211
212         if ( fullPath == null ) {
213             fullPath = Boolean.FALSE;
214         }
215
216
217         if ( server == null || server.length() == 0 ) {
218             server = request.getServerName();
219         }
220
221         String JavaDoc uri = request.getContextPath();
222         if ( fullPath.booleanValue() ) {
223             uri = request.getRequestURI();
224         } else {
225             uri += "/";
226         }
227
228         StringBuffer JavaDoc tag = new StringBuffer JavaDoc("<base HREF=\"");
229         StringBuffer JavaDoc hrefValue = RequestUtils.createServerUriStringBuffer(request.getScheme(), server, request.getServerPort(), uri);
230         tag.append(hrefValue);
231         tag.append("\"");
232         if ( target != null && target.length() > 0 ) {
233             tag.append(" target=\"").append(target).append("\"");
234         }
235
236         TagUtils tagUtils = TagUtils.getInstance();
237         if ( tagUtils.isXhtml(pageContext) ) {
238             tag.append(" />");
239         } else {
240             tag.append(">");
241         }
242
243         if ( var != null ) {
244             // save value in specified scope
245
int varScope = PageContext.PAGE_SCOPE;
246             if ( scope != null ) {
247                 varScope = tagUtils.getScope(scope);
248             }
249             pageContext.setAttribute(var, hrefValue.toString(), varScope);
250
251         } else {
252             tagUtils.write(pageContext, tag.toString());
253         }
254
255     }
256
257 }
258
Popular Tags