1 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 ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.jsp.JspException ; 38 import javax.servlet.jsp.JspWriter ; 39 import javax.servlet.jsp.PageContext ; 40 import javax.servlet.jsp.tagext.TagSupport ; 41 import java.io.IOException ; 42 import java.io.UnsupportedEncodingException ; 43 import java.net.URLEncoder ; 44 45 public class CoreUrlTag extends TagSupport implements NameValueTag { 46 private static L10N L = new L10N(CoreUrlTag.class); 47 48 private static String []_shortEncoding; 49 50 private String _value; 51 private String _context; 52 53 private String _var; 54 private String _scope; 55 56 private CharBuffer _url; 57 58 61 public void setValue(String value) 62 { 63 _value = value; 64 } 65 66 69 public void setContext(String context) 70 { 71 _context = context; 72 } 73 74 77 public void setVar(String var) 78 { 79 _var = var; 80 } 81 82 85 public void setScope(String scope) 86 { 87 _scope = scope; 88 } 89 90 93 public void addParam(String name, String value) 94 { 95 String encoding = this.pageContext.getResponse().getCharacterEncoding(); 96 97 addParam(_url, name, value, encoding); 98 } 99 100 public int doStartTag() throws JspException 101 { 102 String value = _value; 103 String context = _context; 104 105 _url = normalizeURL(pageContext, value, context); 106 107 return EVAL_BODY_INCLUDE; 108 } 109 110 public int doEndTag() throws JspException 111 { 112 String value = encodeURL(pageContext, _url); 113 114 try { 115 if (_var == null) { 116 JspWriter out = pageContext.getOut(); 117 118 out.print(value); 119 } 120 else 121 CoreSetTag.setValue(pageContext, _var, _scope, value); 122 } catch (IOException e) { 123 throw new JspException (e); 124 } 125 126 return EVAL_PAGE; 127 } 128 129 public static CharBuffer normalizeURL(PageContext pageContext, 130 String url, String context) 131 throws JspException 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 request; 146 request = (HttpServletRequest ) 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 name, String value, 171 String 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 189 public static CharBuffer addEncodedString(CharBuffer cb, 190 String value, 191 String 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 e) { 202 throw new RuntimeException (e); 203 } 204 205 return cb; 206 } 207 } 208 209 212 public static CharBuffer addEncodedLatin1(CharBuffer cb, String 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 234 public static CharBuffer addEncodedUTF8(CharBuffer cb, String 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 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 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 encodeURL(PageContext pageContext, CharBuffer url) 279 { 280 String 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 ) pageContext.getResponse()).encodeURL(value); 289 290 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 [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 |