KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > CoreUrlTag


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

61   public void setValue(String JavaDoc value)
62   {
63     _value = value;
64   }
65   
66   /**
67    * Sets the external context for the import.
68    */

69   public void setContext(String JavaDoc context)
70   {
71     _context = context;
72   }
73
74   /**
75    * Sets the variable for the import.
76    */

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

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

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

189   public static CharBuffer addEncodedString(CharBuffer cb,
190                         String JavaDoc value,
191                         String JavaDoc encoding)
192   {
193     if (encoding == null || encoding.equalsIgnoreCase("iso-8859-1"))
194       return addEncodedLatin1(cb, value);
195     else if (encoding.equalsIgnoreCase("utf8") ||
196          encoding.equalsIgnoreCase("utf-8"))
197       return addEncodedUTF8(cb, value);
198     else {
199       try {
200     cb.append(URLEncoder.encode(value, encoding));
201       } catch (UnsupportedEncodingException JavaDoc e) {
202     throw new RuntimeException JavaDoc(e);
203       }
204       
205       return cb;
206     }
207   }
208
209   /**
210    * Adds a parameter to the string, encoding with latin-1 when necessary.
211    */

212   public static CharBuffer addEncodedLatin1(CharBuffer cb, String JavaDoc value)
213   {
214     int len = value.length();
215
216     for (int i = 0; i < len; i++) {
217       char ch = value.charAt(i);
218
219       if (ch < 0x80)
220         cb.append(_shortEncoding[ch]);
221       else {
222         cb.append('%');
223         cb.append(hex(ch >> 4));
224         cb.append(hex(ch & 0xf));
225       }
226     }
227
228     return cb;
229   }
230
231   /**
232    * Adds a parameter to the string, encoding with UTF8 when necessary.
233    */

234   public static CharBuffer addEncodedUTF8(CharBuffer cb, String JavaDoc value)
235   {
236     int len = value.length();
237
238     for (int i = 0; i < len; i++) {
239       char ch = value.charAt(i);
240
241       if (ch < 0x80)
242         cb.append(_shortEncoding[ch]);
243       else if (ch < 0x800) {
244         // UTF-8
245

246         int d1 = 0xc0 + ((ch >> 6) & 0x1f);
247         int d2 = 0x80 + (ch & 0x3f);
248                          
249         cb.append('%');
250         cb.append(hex(d1 >> 4));
251         cb.append(hex(d1 & 0xf));
252         cb.append('%');
253         cb.append(hex(d2 >> 4));
254         cb.append(hex(d2 & 0xf));
255       }
256       else {
257         // UTF-8
258

259         int d1 = 0xe0 + ((ch >> 12) & 0xf);
260         int d2 = 0x80 + ((ch >> 6) & 0x3f);
261         int d3 = 0x80 + (ch & 0x3f);
262                          
263         cb.append('%');
264         cb.append(hex(d1 >> 4));
265         cb.append(hex(d1 & 0xf));
266         cb.append('%');
267         cb.append(hex(d2 >> 4));
268         cb.append(hex(d2 & 0xf));
269         cb.append('%');
270         cb.append(hex(d3 >> 4));
271         cb.append(hex(d3 & 0xf));
272       }
273     }
274
275     return cb;
276   }
277
278   public static String JavaDoc encodeURL(PageContext JavaDoc pageContext, CharBuffer url)
279   {
280     String JavaDoc value = url.toString();
281
282     int colon = value.indexOf(':');
283     int slash = value.indexOf('/');
284
285     if (colon < slash && slash > 0)
286       return value;
287     else
288       return ((HttpServletResponse JavaDoc) pageContext.getResponse()).encodeURL(value);
289
290     /*
291     if (value.startsWith("/"))
292       return ((HttpServletResponse) pageContext.getResponse()).encodeURL(value);
293     else
294       return value;
295     */

296   }
297
298
299   private static char hex(int d)
300   {
301     d = d & 0xf;
302     
303     if (d < 10)
304       return (char) ('0' + d);
305     else
306       return (char) ('A' + d - 10);
307   }
308       
309   static {
310     _shortEncoding = new String JavaDoc[128];
311
312     for (int i = 0; i < _shortEncoding.length; i++)
313       _shortEncoding[i] = "%" + hex(i / 16) + hex(i);
314
315     for (int i = 'a'; i <= 'z'; i++)
316       _shortEncoding[i] = String.valueOf((char) i);
317     
318     for (int i = 'A'; i <= 'Z'; i++)
319       _shortEncoding[i] = String.valueOf((char) i);
320     
321     for (int i = '0'; i <= '9'; i++)
322       _shortEncoding[i] = String.valueOf((char) i);
323
324     _shortEncoding[' '] = "+";
325     _shortEncoding['-'] = "-";
326     _shortEncoding['_'] = "_";
327     _shortEncoding['.'] = ".";
328     _shortEncoding['!'] = "!";
329     _shortEncoding['~'] = "~";
330     _shortEncoding['\''] = "\'";
331   }
332 }
333
Popular Tags