KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > core > UrlSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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
17 package org.apache.taglibs.standard.tag.common.core;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspTagException JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
25
26 import org.apache.taglibs.standard.resources.Resources;
27
28 /**
29  * <p>Support for tag handlers for &lt;url&gt;, the URL creation
30  * and rewriting tag in JSTL 1.0.</p>
31  *
32  * @author Shawn Bayern
33  */

34
35 public abstract class UrlSupport extends BodyTagSupport JavaDoc
36     implements ParamParent {
37
38     //*********************************************************************
39
// Protected state
40

41     protected String JavaDoc value; // 'value' attribute
42
protected String JavaDoc context; // 'context' attribute
43

44     //*********************************************************************
45
// Private state
46

47     private String JavaDoc var; // 'var' attribute
48
private int scope; // processed 'scope' attr
49
private ParamSupport.ParamManager params; // added parameters
50

51     //*********************************************************************
52
// Constructor and initialization
53

54     public UrlSupport() {
55     super();
56     init();
57     }
58
59     private void init() {
60     value = var = null;
61     params = null;
62     context = null;
63     scope = PageContext.PAGE_SCOPE;
64     }
65
66
67    //*********************************************************************
68
// Tag attributes known at translation time
69

70     public void setVar(String JavaDoc var) {
71         this.var = var;
72     }
73
74     public void setScope(String JavaDoc scope) {
75     this.scope = Util.getScope(scope);
76     }
77
78
79     //*********************************************************************
80
// Collaboration with subtags
81

82     // inherit Javadoc
83
public void addParameter(String JavaDoc name, String JavaDoc value) {
84     params.addParameter(name, value);
85     }
86
87
88     //*********************************************************************
89
// Tag logic
90

91     // resets any parameters that might be sent
92
public int doStartTag() throws JspException JavaDoc {
93     params = new ParamSupport.ParamManager();
94     return EVAL_BODY_BUFFERED;
95     }
96
97
98     // gets the right value, encodes it, and prints or stores it
99
public int doEndTag() throws JspException JavaDoc {
100     String JavaDoc result; // the eventual result
101

102     // add (already encoded) parameters
103
String JavaDoc baseUrl = resolveUrl(value, context, pageContext);
104     result = params.aggregateParams(baseUrl);
105
106     // if the URL is relative, rewrite it
107
if (!ImportSupport.isAbsoluteUrl(result)) {
108         HttpServletResponse JavaDoc response =
109                 ((HttpServletResponse JavaDoc) pageContext.getResponse());
110             result = response.encodeURL(result);
111     }
112
113     // store or print the output
114
if (var != null)
115         pageContext.setAttribute(var, result, scope);
116     else {
117         try {
118             pageContext.getOut().print(result);
119         } catch (java.io.IOException JavaDoc ex) {
120         throw new JspTagException JavaDoc(ex.toString(), ex);
121         }
122     }
123
124     return EVAL_PAGE;
125     }
126
127     // Releases any resources we may have (or inherit)
128
public void release() {
129     init();
130     }
131
132     //*********************************************************************
133
// Utility methods
134

135     public static String JavaDoc resolveUrl(
136             String JavaDoc url, String JavaDoc context, PageContext JavaDoc pageContext)
137         throws JspException JavaDoc {
138     // don't touch absolute URLs
139
if (ImportSupport.isAbsoluteUrl(url))
140         return url;
141
142     // normalize relative URLs against a context root
143
HttpServletRequest JavaDoc request =
144         (HttpServletRequest JavaDoc) pageContext.getRequest();
145     if (context == null) {
146         if (url.startsWith("/"))
147         return (request.getContextPath() + url);
148         else
149         return url;
150     } else {
151             if (!context.startsWith("/") || !url.startsWith("/")) {
152                 throw new JspTagException JavaDoc(
153                     Resources.getMessage("IMPORT_BAD_RELATIVE"));
154             }
155             if (context.equals("/")) {
156                 // Don't produce string starting with '//', many
157
// browsers interpret this as host name, not as
158
// path on same host.
159
return url;
160             } else {
161                 return (context + url);
162             }
163         }
164     }
165
166 }
167
Popular Tags