1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.JspTagException ; 23 import javax.servlet.jsp.PageContext ; 24 import javax.servlet.jsp.tagext.BodyTagSupport ; 25 26 import org.apache.taglibs.standard.resources.Resources; 27 28 34 35 public abstract class UrlSupport extends BodyTagSupport 36 implements ParamParent { 37 38 41 protected String value; protected String context; 44 47 private String var; private int scope; private ParamSupport.ParamManager params; 51 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 70 public void setVar(String var) { 71 this.var = var; 72 } 73 74 public void setScope(String scope) { 75 this.scope = Util.getScope(scope); 76 } 77 78 79 82 public void addParameter(String name, String value) { 84 params.addParameter(name, value); 85 } 86 87 88 91 public int doStartTag() throws JspException { 93 params = new ParamSupport.ParamManager(); 94 return EVAL_BODY_BUFFERED; 95 } 96 97 98 public int doEndTag() throws JspException { 100 String result; 102 String baseUrl = resolveUrl(value, context, pageContext); 104 result = params.aggregateParams(baseUrl); 105 106 if (!ImportSupport.isAbsoluteUrl(result)) { 108 HttpServletResponse response = 109 ((HttpServletResponse ) pageContext.getResponse()); 110 result = response.encodeURL(result); 111 } 112 113 if (var != null) 115 pageContext.setAttribute(var, result, scope); 116 else { 117 try { 118 pageContext.getOut().print(result); 119 } catch (java.io.IOException ex) { 120 throw new JspTagException (ex.toString(), ex); 121 } 122 } 123 124 return EVAL_PAGE; 125 } 126 127 public void release() { 129 init(); 130 } 131 132 135 public static String resolveUrl( 136 String url, String context, PageContext pageContext) 137 throws JspException { 138 if (ImportSupport.isAbsoluteUrl(url)) 140 return url; 141 142 HttpServletRequest request = 144 (HttpServletRequest ) 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 ( 153 Resources.getMessage("IMPORT_BAD_RELATIVE")); 154 } 155 if (context.equals("/")) { 156 return url; 160 } else { 161 return (context + url); 162 } 163 } 164 } 165 166 } 167 | Popular Tags |