1 17 18 package org.apache.jasper.runtime; 19 20 import java.io.IOException ; 21 import java.io.Writer ; 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import javax.el.ELContext; 29 import javax.servlet.Servlet ; 30 import javax.servlet.ServletConfig ; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.ServletRequest ; 34 import javax.servlet.ServletResponse ; 35 import javax.servlet.http.HttpSession ; 36 import javax.servlet.jsp.JspContext ; 37 import javax.servlet.jsp.JspWriter ; 38 import javax.servlet.jsp.PageContext ; 39 import javax.servlet.jsp.el.ELException ; 40 import javax.servlet.jsp.el.ExpressionEvaluator ; 41 import javax.servlet.jsp.el.VariableResolver ; 42 import javax.servlet.jsp.tagext.BodyContent ; 43 import javax.servlet.jsp.tagext.VariableInfo ; 44 45 import org.apache.jasper.compiler.Localizer; 46 import org.apache.jasper.util.Enumerator; 47 48 60 public class JspContextWrapper extends PageContext implements VariableResolver { 61 62 private PageContext invokingJspCtxt; 64 65 private transient HashMap <String , Object > pageAttributes; 66 67 private ArrayList nestedVars; 69 70 private ArrayList atBeginVars; 72 73 private ArrayList atEndVars; 75 76 private Map aliases; 77 78 private HashMap <String , Object > originalNestedVars; 79 80 public JspContextWrapper(JspContext jspContext, ArrayList nestedVars, 81 ArrayList atBeginVars, ArrayList atEndVars, Map aliases) { 82 this.invokingJspCtxt = (PageContext ) jspContext; 83 this.nestedVars = nestedVars; 84 this.atBeginVars = atBeginVars; 85 this.atEndVars = atEndVars; 86 this.pageAttributes = new HashMap <String , Object >(16); 87 this.aliases = aliases; 88 89 if (nestedVars != null) { 90 this.originalNestedVars = new HashMap <String , Object >(nestedVars.size()); 91 } 92 syncBeginTagFile(); 93 } 94 95 public void initialize(Servlet servlet, ServletRequest request, 96 ServletResponse response, String errorPageURL, 97 boolean needsSession, int bufferSize, boolean autoFlush) 98 throws IOException , IllegalStateException , IllegalArgumentException { 99 } 100 101 public Object getAttribute(String name) { 102 103 if (name == null) { 104 throw new NullPointerException (Localizer 105 .getMessage("jsp.error.attribute.null_name")); 106 } 107 108 return pageAttributes.get(name); 109 } 110 111 public Object getAttribute(String name, int scope) { 112 113 if (name == null) { 114 throw new NullPointerException (Localizer 115 .getMessage("jsp.error.attribute.null_name")); 116 } 117 118 if (scope == PAGE_SCOPE) { 119 return pageAttributes.get(name); 120 } 121 122 return invokingJspCtxt.getAttribute(name, scope); 123 } 124 125 public void setAttribute(String name, Object value) { 126 127 if (name == null) { 128 throw new NullPointerException (Localizer 129 .getMessage("jsp.error.attribute.null_name")); 130 } 131 132 if (value != null) { 133 pageAttributes.put(name, value); 134 } else { 135 removeAttribute(name, PAGE_SCOPE); 136 } 137 } 138 139 public void setAttribute(String name, Object value, int scope) { 140 141 if (name == null) { 142 throw new NullPointerException (Localizer 143 .getMessage("jsp.error.attribute.null_name")); 144 } 145 146 if (scope == PAGE_SCOPE) { 147 if (value != null) { 148 pageAttributes.put(name, value); 149 } else { 150 removeAttribute(name, PAGE_SCOPE); 151 } 152 } else { 153 invokingJspCtxt.setAttribute(name, value, scope); 154 } 155 } 156 157 public Object findAttribute(String name) { 158 159 if (name == null) { 160 throw new NullPointerException (Localizer 161 .getMessage("jsp.error.attribute.null_name")); 162 } 163 164 Object o = pageAttributes.get(name); 165 if (o == null) { 166 o = invokingJspCtxt.getAttribute(name, REQUEST_SCOPE); 167 if (o == null) { 168 if (getSession() != null) { 169 o = invokingJspCtxt.getAttribute(name, SESSION_SCOPE); 170 } 171 if (o == null) { 172 o = invokingJspCtxt.getAttribute(name, APPLICATION_SCOPE); 173 } 174 } 175 } 176 177 return o; 178 } 179 180 public void removeAttribute(String name) { 181 182 if (name == null) { 183 throw new NullPointerException (Localizer 184 .getMessage("jsp.error.attribute.null_name")); 185 } 186 187 pageAttributes.remove(name); 188 invokingJspCtxt.removeAttribute(name, REQUEST_SCOPE); 189 if (getSession() != null) { 190 invokingJspCtxt.removeAttribute(name, SESSION_SCOPE); 191 } 192 invokingJspCtxt.removeAttribute(name, APPLICATION_SCOPE); 193 } 194 195 public void removeAttribute(String name, int scope) { 196 197 if (name == null) { 198 throw new NullPointerException (Localizer 199 .getMessage("jsp.error.attribute.null_name")); 200 } 201 202 if (scope == PAGE_SCOPE) { 203 pageAttributes.remove(name); 204 } else { 205 invokingJspCtxt.removeAttribute(name, scope); 206 } 207 } 208 209 public int getAttributesScope(String name) { 210 211 if (name == null) { 212 throw new NullPointerException (Localizer 213 .getMessage("jsp.error.attribute.null_name")); 214 } 215 216 if (pageAttributes.get(name) != null) { 217 return PAGE_SCOPE; 218 } else { 219 return invokingJspCtxt.getAttributesScope(name); 220 } 221 } 222 223 public Enumeration <String > getAttributeNamesInScope(int scope) { 224 if (scope == PAGE_SCOPE) { 225 return new Enumerator(pageAttributes.keySet().iterator()); 226 } 227 228 return invokingJspCtxt.getAttributeNamesInScope(scope); 229 } 230 231 public void release() { 232 invokingJspCtxt.release(); 233 } 234 235 public JspWriter getOut() { 236 return invokingJspCtxt.getOut(); 237 } 238 239 public HttpSession getSession() { 240 return invokingJspCtxt.getSession(); 241 } 242 243 public Object getPage() { 244 return invokingJspCtxt.getPage(); 245 } 246 247 public ServletRequest getRequest() { 248 return invokingJspCtxt.getRequest(); 249 } 250 251 public ServletResponse getResponse() { 252 return invokingJspCtxt.getResponse(); 253 } 254 255 public Exception getException() { 256 return invokingJspCtxt.getException(); 257 } 258 259 public ServletConfig getServletConfig() { 260 return invokingJspCtxt.getServletConfig(); 261 } 262 263 public ServletContext getServletContext() { 264 return invokingJspCtxt.getServletContext(); 265 } 266 267 public void forward(String relativeUrlPath) throws ServletException , 268 IOException { 269 invokingJspCtxt.forward(relativeUrlPath); 270 } 271 272 public void include(String relativeUrlPath) throws ServletException , 273 IOException { 274 invokingJspCtxt.include(relativeUrlPath); 275 } 276 277 public void include(String relativeUrlPath, boolean flush) 278 throws ServletException , IOException { 279 include(relativeUrlPath, false); } 281 282 public VariableResolver getVariableResolver() { 283 return this; 284 } 285 286 public BodyContent pushBody() { 287 return invokingJspCtxt.pushBody(); 288 } 289 290 public JspWriter pushBody(Writer writer) { 291 return invokingJspCtxt.pushBody(writer); 292 } 293 294 public JspWriter popBody() { 295 return invokingJspCtxt.popBody(); 296 } 297 298 public ExpressionEvaluator getExpressionEvaluator() { 299 return invokingJspCtxt.getExpressionEvaluator(); 300 } 301 302 public void handlePageException(Exception ex) throws IOException , 303 ServletException { 304 handlePageException((Throwable ) ex); 307 } 308 309 public void handlePageException(Throwable t) throws IOException , 310 ServletException { 311 invokingJspCtxt.handlePageException(t); 312 } 313 314 317 public Object resolveVariable(String pName) throws ELException { 318 ELContext ctx = this.getELContext(); 319 return ctx.getELResolver().getValue(ctx, null, pName); 320 } 321 322 325 public void syncBeginTagFile() { 326 saveNestedVariables(); 327 } 328 329 332 public void syncBeforeInvoke() { 333 copyTagToPageScope(VariableInfo.NESTED); 334 copyTagToPageScope(VariableInfo.AT_BEGIN); 335 } 336 337 340 public void syncEndTagFile() { 341 copyTagToPageScope(VariableInfo.AT_BEGIN); 342 copyTagToPageScope(VariableInfo.AT_END); 343 restoreNestedVariables(); 344 } 345 346 353 private void copyTagToPageScope(int scope) { 354 Iterator iter = null; 355 356 switch (scope) { 357 case VariableInfo.NESTED: 358 if (nestedVars != null) { 359 iter = nestedVars.iterator(); 360 } 361 break; 362 case VariableInfo.AT_BEGIN: 363 if (atBeginVars != null) { 364 iter = atBeginVars.iterator(); 365 } 366 break; 367 case VariableInfo.AT_END: 368 if (atEndVars != null) { 369 iter = atEndVars.iterator(); 370 } 371 break; 372 } 373 374 while ((iter != null) && iter.hasNext()) { 375 String varName = (String ) iter.next(); 376 Object obj = getAttribute(varName); 377 varName = findAlias(varName); 378 if (obj != null) { 379 invokingJspCtxt.setAttribute(varName, obj); 380 } else { 381 invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE); 382 } 383 } 384 } 385 386 390 private void saveNestedVariables() { 391 if (nestedVars != null) { 392 Iterator iter = nestedVars.iterator(); 393 while (iter.hasNext()) { 394 String varName = (String ) iter.next(); 395 varName = findAlias(varName); 396 Object obj = invokingJspCtxt.getAttribute(varName); 397 if (obj != null) { 398 originalNestedVars.put(varName, obj); 399 } 400 } 401 } 402 } 403 404 407 private void restoreNestedVariables() { 408 if (nestedVars != null) { 409 Iterator iter = nestedVars.iterator(); 410 while (iter.hasNext()) { 411 String varName = (String ) iter.next(); 412 varName = findAlias(varName); 413 Object obj = originalNestedVars.get(varName); 414 if (obj != null) { 415 invokingJspCtxt.setAttribute(varName, obj); 416 } else { 417 invokingJspCtxt.removeAttribute(varName, PAGE_SCOPE); 418 } 419 } 420 } 421 } 422 423 432 private String findAlias(String varName) { 433 434 if (aliases == null) 435 return varName; 436 437 String alias = (String ) aliases.get(varName); 438 if (alias == null) { 439 return varName; 440 } 441 return alias; 442 } 443 444 446 public ELContext getELContext() { 447 449 return this.invokingJspCtxt.getELContext(); 450 451 461 } 462 } 463 | Popular Tags |