1 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 ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.jsp.JspException ; 41 import javax.servlet.jsp.JspWriter ; 42 import javax.servlet.jsp.PageContext ; 43 import javax.servlet.jsp.tagext.TagSupport ; 44 import java.io.IOException ; 45 46 public class UrlTag extends TagSupport 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 _var; 53 private String _scope; 54 55 private CharBuffer _url; 56 57 60 public void setValue(Expr value) 61 { 62 _valueExpr = value; 63 } 64 65 68 public void setContext(Expr context) 69 { 70 _contextExpr = context; 71 } 72 73 76 public void setVar(String var) 77 { 78 _var = var; 79 } 80 81 84 public void setScope(String scope) 85 { 86 _scope = scope; 87 } 88 89 92 public void addParam(String name, String value) 93 { 94 String 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 100 { 101 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 102 103 String value = _valueExpr.evalString(pageContext.getELContext()); 104 String 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 115 { 116 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 117 118 String value = encodeURL(pageContext, _url); 119 120 try { 121 if (_var == null) { 122 JspWriter out = pageContext.getOut(); 123 124 out.print(value); 125 } 126 else 127 CoreSetTag.setValue(pageContext, _var, _scope, value); 128 } catch (IOException e) { 129 throw new JspException (e); 130 } 131 132 return EVAL_PAGE; 133 } 134 135 public static CharBuffer normalizeURL(PageContext pageContext, 136 String url, String context) 137 throws JspException 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 request; 152 request = (HttpServletRequest ) 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 encodeURL(PageContext pageContext, CharBuffer url) 176 { 177 String 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 ) pageContext.getResponse()).encodeURL(value); 186 192 } 193 } 194 | Popular Tags |