KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > UrlTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.jsp.PageContextImpl;
34 import com.caucho.jstl.NameValueTag;
35 import com.caucho.util.CharBuffer;
36 import com.caucho.util.L10N;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.JspWriter JavaDoc;
42 import javax.servlet.jsp.PageContext JavaDoc;
43 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 public class UrlTag extends TagSupport JavaDoc implements NameValueTag {
47   private static final L10N L = new L10N(ImportTag.class);
48
49   private Expr _valueExpr;
50   private Expr _contextExpr;
51
52   private String JavaDoc _var;
53   private String JavaDoc _scope;
54
55   private CharBuffer _url;
56
57   /**
58    * Sets the URL to be imported.
59    */

60   public void setValue(Expr value)
61   {
62     _valueExpr = value;
63   }
64   
65   /**
66    * Sets the external context for the import.
67    */

68   public void setContext(Expr context)
69   {
70     _contextExpr = context;
71   }
72
73   /**
74    * Sets the variable for the import.
75    */

76   public void setVar(String JavaDoc var)
77   {
78     _var = var;
79   }
80   
81   /**
82    * Sets the scope for the result variable for the output.
83    */

84   public void setScope(String JavaDoc scope)
85   {
86     _scope = scope;
87   }
88
89   /**
90    * Adds a parameter.
91    */

92   public void addParam(String JavaDoc name, String JavaDoc value)
93   {
94     String JavaDoc encoding = this.pageContext.getResponse().getCharacterEncoding();
95     
96     com.caucho.jstl.rt.CoreUrlTag.addParam(_url, name, value, encoding);
97   }
98
99   public int doStartTag() throws JspException JavaDoc
100   {
101     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
102   
103     String JavaDoc value = _valueExpr.evalString(pageContext.getELContext());
104     String JavaDoc context = null;
105
106     if (_contextExpr != null)
107       context = _contextExpr.evalString(pageContext.getELContext());
108     
109     _url = normalizeURL(pageContext, value, context);
110
111     return EVAL_BODY_INCLUDE;
112   }
113       
114   public int doEndTag() throws JspException JavaDoc
115   {
116     PageContextImpl pageContext = (PageContextImpl) this.pageContext;
117   
118     String JavaDoc value = encodeURL(pageContext, _url);
119
120     try {
121       if (_var == null) {
122         JspWriter JavaDoc out = pageContext.getOut();
123
124         out.print(value);
125       }
126       else
127         CoreSetTag.setValue(pageContext, _var, _scope, value);
128     } catch (IOException JavaDoc e) {
129       throw new JspException JavaDoc(e);
130     }
131
132     return EVAL_PAGE;
133   }
134
135   public static CharBuffer normalizeURL(PageContext JavaDoc pageContext,
136                                         String JavaDoc url, String JavaDoc context)
137     throws JspException JavaDoc
138   {
139     if (url == null)
140       url = "";
141
142     CharBuffer value = new CharBuffer();
143
144     int slash = url.indexOf('/');
145     int colon = url.indexOf(':');
146
147     if (colon > 0 && colon < slash) {
148       value.append(url);
149     }
150     else if (slash == 0) {
151       HttpServletRequest JavaDoc request;
152       request = (HttpServletRequest JavaDoc) pageContext.getRequest();
153
154       if (context != null) {
155         value.append(context);
156         value.append(url);
157       }
158       else {
159         value.append(request.getContextPath());
160         value.append(url);
161       }
162     }
163     else {
164       if (context != null) {
165         value.append(context);
166     value.append(url);
167       }
168       else
169         value.append(url);
170     }
171
172     return value;
173   }
174
175   public static String JavaDoc encodeURL(PageContext JavaDoc pageContext, CharBuffer url)
176   {
177     String JavaDoc value = url.toString();
178
179     int colon = value.indexOf(':');
180     int slash = value.indexOf('/');
181
182     if (colon < slash && slash > 0)
183       return value;
184     else
185       return ((HttpServletResponse JavaDoc) pageContext.getResponse()).encodeURL(value);
186     /*
187     if (value.startsWith("/"))
188       return ((HttpServletResponse) pageContext.getResponse()).encodeURL(value);
189     else
190       return value;
191     */

192   }
193 }
194
Popular Tags