KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > html > BaseTag


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

18
19 package org.apache.struts.taglib.html;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
28
29 import org.apache.struts.Globals;
30 import org.apache.struts.taglib.TagUtils;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.util.RequestUtils;
33
34 /**
35  * Renders an HTML <base> element with an href
36  * attribute pointing to the absolute location of the enclosing JSP page. This
37  * tag is only valid when nested inside a head tag body. The presence
38  * of this tag allows the browser to resolve relative URL's to images,
39  * CSS stylesheets and other resources in a manner independent of the URL
40  * used to call the ActionServlet.
41  *
42  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
43  */

44
45 public class BaseTag extends TagSupport JavaDoc {
46
47     /**
48      * The message resources for this package.
49      */

50     protected static MessageResources messages =
51         MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
52
53     /**
54      * The server name to use instead of request.getServerName().
55      */

56     protected String JavaDoc server = null;
57
58     /**
59      * The target window for this base reference.
60      */

61     protected String JavaDoc target = null;
62
63     public String JavaDoc getTarget() {
64         return (this.target);
65     }
66
67     public void setTarget(String JavaDoc target) {
68         this.target = target;
69     }
70
71     /**
72      * Process the start of this tag.
73      *
74      * @exception JspException if a JSP exception has occurred
75      */

76     public int doStartTag() throws JspException JavaDoc {
77         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
78         String JavaDoc serverName = (this.server == null) ? request.getServerName() : this.server;
79         
80         String JavaDoc baseTag =
81             renderBaseElement(
82                 request.getScheme(),
83                 serverName,
84                 request.getServerPort(),
85                 request.getRequestURI());
86
87         JspWriter JavaDoc out = pageContext.getOut();
88         try {
89             out.write(baseTag);
90         } catch (IOException JavaDoc e) {
91             pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
92             throw new JspException JavaDoc(messages.getMessage("common.io", e.toString()));
93         }
94         
95         return EVAL_BODY_INCLUDE;
96     }
97
98     /**
99      * Render a fully formed HTML &lt;base&gt; element and return it as a String.
100      * @param scheme The scheme used in the url (ie. http or https).
101      * @param serverName
102      * @param port
103      * @param uri The portion of the url from the protocol name up to the query
104      * string.
105      * @return String An HTML &lt;base&gt; element.
106      * @since Struts 1.1
107      */

108     protected String JavaDoc renderBaseElement(
109         String JavaDoc scheme,
110         String JavaDoc serverName,
111         int port,
112         String JavaDoc uri) {
113             
114         StringBuffer JavaDoc tag = new StringBuffer JavaDoc("<base HREF=\"");
115         tag.append(RequestUtils.createServerUriStringBuffer(scheme,serverName,port,uri).toString());
116
117         tag.append("\"");
118         
119         if (this.target != null) {
120             tag.append(" target=\"");
121             tag.append(this.target);
122             tag.append("\"");
123         }
124         
125         if (TagUtils.getInstance().isXhtml(this.pageContext)) {
126             tag.append(" />");
127         } else {
128             tag.append(">");
129         }
130         
131         return tag.toString();
132     }
133     
134     /**
135      * Returns the server.
136      * @return String
137      */

138     public String JavaDoc getServer() {
139         return this.server;
140     }
141
142     /**
143      * Sets the server.
144      * @param server The server to set
145      */

146     public void setServer(String JavaDoc server) {
147         this.server = server;
148     }
149
150 }
151
Popular Tags