1 17 18 package org.apache.jasper.compiler; 19 20 import java.io.InputStream ; 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 import java.net.URL ; 24 25 import javax.servlet.ServletContext ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.jasper.JasperException; 30 import org.apache.jasper.xmlparser.ParserUtils; 31 import org.apache.jasper.xmlparser.TreeNode; 32 import org.xml.sax.InputSource ; 33 34 41 42 public class JspConfig { 43 44 private static final String WEB_XML = "/WEB-INF/web.xml"; 45 46 private Log log = LogFactory.getLog(JspConfig.class); 48 49 private Vector jspProperties = null; 50 private ServletContext ctxt; 51 private boolean initialized = false; 52 53 private String defaultIsXml = null; private String defaultIsELIgnored = null; private String defaultIsScriptingInvalid = null; 56 private String defaultDeferedSyntaxAllowedAsLiteral = null; 57 private String defaultTrimDirectiveWhitespaces = null; 58 private JspProperty defaultJspProperty; 59 60 public JspConfig(ServletContext ctxt) { 61 this.ctxt = ctxt; 62 } 63 64 private double getVersion(TreeNode webApp) { 65 String v = webApp.findAttribute("version"); 66 if (v != null) { 67 try { 68 return Double.parseDouble(v); 69 } catch (NumberFormatException e) { 70 } 71 } 72 return 2.3; 73 } 74 75 private void processWebDotXml(ServletContext ctxt) throws JasperException { 76 77 InputStream is = null; 78 79 try { 80 URL uri = ctxt.getResource(WEB_XML); 81 if (uri == null) { 82 return; 84 } 85 86 is = uri.openStream(); 87 InputSource ip = new InputSource (is); 88 ip.setSystemId(uri.toExternalForm()); 89 90 ParserUtils pu = new ParserUtils(); 91 TreeNode webApp = pu.parseXMLDocument(WEB_XML, ip); 92 93 if (webApp == null 94 || getVersion(webApp) < 2.4) { 95 defaultIsELIgnored = "true"; 96 return; 97 } 98 TreeNode jspConfig = webApp.findChild("jsp-config"); 99 if (jspConfig == null) { 100 return; 101 } 102 103 jspProperties = new Vector (); 104 Iterator jspPropertyList = jspConfig.findChildren("jsp-property-group"); 105 while (jspPropertyList.hasNext()) { 106 107 TreeNode element = (TreeNode) jspPropertyList.next(); 108 Iterator list = element.findChildren(); 109 110 Vector urlPatterns = new Vector (); 111 String pageEncoding = null; 112 String scriptingInvalid = null; 113 String elIgnored = null; 114 String isXml = null; 115 Vector includePrelude = new Vector (); 116 Vector includeCoda = new Vector (); 117 String deferredSyntaxAllowedAsLiteral = null; 118 String trimDirectiveWhitespaces = null; 119 120 while (list.hasNext()) { 121 122 element = (TreeNode) list.next(); 123 String tname = element.getName(); 124 125 if ("url-pattern".equals(tname)) 126 urlPatterns.addElement( element.getBody() ); 127 else if ("page-encoding".equals(tname)) 128 pageEncoding = element.getBody(); 129 else if ("is-xml".equals(tname)) 130 isXml = element.getBody(); 131 else if ("el-ignored".equals(tname)) 132 elIgnored = element.getBody(); 133 else if ("scripting-invalid".equals(tname)) 134 scriptingInvalid = element.getBody(); 135 else if ("include-prelude".equals(tname)) 136 includePrelude.addElement(element.getBody()); 137 else if ("include-coda".equals(tname)) 138 includeCoda.addElement(element.getBody()); 139 else if ("deferred-syntax-allowed-as-literal".equals(tname)) 140 deferredSyntaxAllowedAsLiteral = element.getBody(); 141 else if ("trim-directive-whitespaces".equals(tname)) 142 trimDirectiveWhitespaces = element.getBody(); 143 } 144 145 if (urlPatterns.size() == 0) { 146 continue; 147 } 148 149 for( int p = 0; p < urlPatterns.size(); p++ ) { 152 String urlPattern = (String )urlPatterns.elementAt( p ); 153 String path = null; 154 String extension = null; 155 156 if (urlPattern.indexOf('*') < 0) { 157 path = urlPattern; 159 } else { 160 int i = urlPattern.lastIndexOf('/'); 161 String file; 162 if (i >= 0) { 163 path = urlPattern.substring(0,i+1); 164 file = urlPattern.substring(i+1); 165 } else { 166 file = urlPattern; 167 } 168 169 if (file.equals("*")) { 171 extension = "*"; 172 } else if (file.startsWith("*.")) { 173 extension = file.substring(file.indexOf('.')+1); 174 } 175 176 boolean isStar = "*".equals(extension); 181 if ((path == null && (extension == null || isStar)) 182 || (path != null && !isStar)) { 183 if (log.isWarnEnabled()) { 184 log.warn(Localizer.getMessage( 185 "jsp.warning.bad.urlpattern.propertygroup", 186 urlPattern)); 187 } 188 continue; 189 } 190 } 191 192 JspProperty property = new JspProperty(isXml, 193 elIgnored, 194 scriptingInvalid, 195 pageEncoding, 196 includePrelude, 197 includeCoda, 198 deferredSyntaxAllowedAsLiteral, 199 trimDirectiveWhitespaces); 200 JspPropertyGroup propertyGroup = 201 new JspPropertyGroup(path, extension, property); 202 203 jspProperties.addElement(propertyGroup); 204 } 205 } 206 } catch (Exception ex) { 207 throw new JasperException(ex); 208 } finally { 209 if (is != null) { 210 try { 211 is.close(); 212 } catch (Throwable t) {} 213 } 214 } 215 } 216 217 private void init() throws JasperException { 218 219 if (!initialized) { 220 processWebDotXml(ctxt); 221 defaultJspProperty = new JspProperty(defaultIsXml, 222 defaultIsELIgnored, 223 defaultIsScriptingInvalid, 224 null, null, null, defaultDeferedSyntaxAllowedAsLiteral, 225 defaultTrimDirectiveWhitespaces); 226 initialized = true; 227 } 228 } 229 230 234 private JspPropertyGroup selectProperty(JspPropertyGroup prev, 235 JspPropertyGroup curr) { 236 if (prev == null) { 237 return curr; 238 } 239 if (prev.getExtension() == null) { 240 return prev; 242 } 243 if (curr.getExtension() == null) { 244 return curr; 246 } 247 String prevPath = prev.getPath(); 248 String currPath = curr.getPath(); 249 if (prevPath == null && currPath == null) { 250 return prev; 252 } 253 if (prevPath == null && currPath != null) { 254 return curr; 255 } 256 if (prevPath != null && currPath == null) { 257 return prev; 258 } 259 if (prevPath.length() >= currPath.length()) { 260 return prev; 261 } 262 return curr; 263 } 264 265 266 271 public JspProperty findJspProperty(String uri) throws JasperException { 272 273 init(); 274 275 if (jspProperties == null || uri.endsWith(".tag") 277 || uri.endsWith(".tagx")) { 278 return defaultJspProperty; 279 } 280 281 String uriPath = null; 282 int index = uri.lastIndexOf('/'); 283 if (index >=0 ) { 284 uriPath = uri.substring(0, index+1); 285 } 286 String uriExtension = null; 287 index = uri.lastIndexOf('.'); 288 if (index >=0) { 289 uriExtension = uri.substring(index+1); 290 } 291 292 Vector includePreludes = new Vector (); 293 Vector includeCodas = new Vector (); 294 295 JspPropertyGroup isXmlMatch = null; 296 JspPropertyGroup elIgnoredMatch = null; 297 JspPropertyGroup scriptingInvalidMatch = null; 298 JspPropertyGroup pageEncodingMatch = null; 299 JspPropertyGroup deferedSyntaxAllowedAsLiteralMatch = null; 300 JspPropertyGroup trimDirectiveWhitespacesMatch = null; 301 302 Iterator iter = jspProperties.iterator(); 303 while (iter.hasNext()) { 304 305 JspPropertyGroup jpg = (JspPropertyGroup) iter.next(); 306 JspProperty jp = jpg.getJspProperty(); 307 308 String extension = jpg.getExtension(); 310 String path = jpg.getPath(); 311 312 if (extension == null) { 313 if (!uri.equals(path)) { 315 continue; 317 } 318 } else { 319 if (path != null && uriPath != null && 321 ! uriPath.startsWith(path)) { 322 continue; 324 } 325 if (!extension.equals("*") && 326 !extension.equals(uriExtension)) { 327 continue; 329 } 330 } 331 if (jp.getIncludePrelude() != null) { 334 includePreludes.addAll(jp.getIncludePrelude()); 335 } 336 if (jp.getIncludeCoda() != null) { 337 includeCodas.addAll(jp.getIncludeCoda()); 338 } 339 340 if (jp.isXml() != null) { 343 isXmlMatch = selectProperty(isXmlMatch, jpg); 344 } 345 if (jp.isELIgnored() != null) { 346 elIgnoredMatch = selectProperty(elIgnoredMatch, jpg); 347 } 348 if (jp.isScriptingInvalid() != null) { 349 scriptingInvalidMatch = 350 selectProperty(scriptingInvalidMatch, jpg); 351 } 352 if (jp.getPageEncoding() != null) { 353 pageEncodingMatch = selectProperty(pageEncodingMatch, jpg); 354 } 355 if (jp.isDeferedSyntaxAllowedAsLiteral() != null) { 356 deferedSyntaxAllowedAsLiteralMatch = 357 selectProperty(deferedSyntaxAllowedAsLiteralMatch, jpg); 358 } 359 if (jp.isTrimDirectiveWhitespaces() != null) { 360 trimDirectiveWhitespacesMatch = 361 selectProperty(trimDirectiveWhitespacesMatch, jpg); 362 } 363 } 364 365 366 String isXml = defaultIsXml; 367 String isELIgnored = defaultIsELIgnored; 368 String isScriptingInvalid = defaultIsScriptingInvalid; 369 String pageEncoding = null; 370 String isDeferedSyntaxAllowedAsLiteral = defaultDeferedSyntaxAllowedAsLiteral; 371 String isTrimDirectiveWhitespaces = defaultTrimDirectiveWhitespaces; 372 373 if (isXmlMatch != null) { 374 isXml = isXmlMatch.getJspProperty().isXml(); 375 } 376 if (elIgnoredMatch != null) { 377 isELIgnored = elIgnoredMatch.getJspProperty().isELIgnored(); 378 } 379 if (scriptingInvalidMatch != null) { 380 isScriptingInvalid = 381 scriptingInvalidMatch.getJspProperty().isScriptingInvalid(); 382 } 383 if (pageEncodingMatch != null) { 384 pageEncoding = pageEncodingMatch.getJspProperty().getPageEncoding(); 385 } 386 if (deferedSyntaxAllowedAsLiteralMatch != null) { 387 isDeferedSyntaxAllowedAsLiteral = 388 deferedSyntaxAllowedAsLiteralMatch.getJspProperty().isDeferedSyntaxAllowedAsLiteral(); 389 } 390 if (trimDirectiveWhitespacesMatch != null) { 391 isTrimDirectiveWhitespaces = 392 trimDirectiveWhitespacesMatch.getJspProperty().isTrimDirectiveWhitespaces(); 393 } 394 395 return new JspProperty(isXml, isELIgnored, isScriptingInvalid, 396 pageEncoding, includePreludes, includeCodas, 397 isDeferedSyntaxAllowedAsLiteral, isTrimDirectiveWhitespaces); 398 } 399 400 404 public boolean isJspPage(String uri) throws JasperException { 405 406 init(); 407 if (jspProperties == null) { 408 return false; 409 } 410 411 String uriPath = null; 412 int index = uri.lastIndexOf('/'); 413 if (index >=0 ) { 414 uriPath = uri.substring(0, index+1); 415 } 416 String uriExtension = null; 417 index = uri.lastIndexOf('.'); 418 if (index >=0) { 419 uriExtension = uri.substring(index+1); 420 } 421 422 Iterator iter = jspProperties.iterator(); 423 while (iter.hasNext()) { 424 425 JspPropertyGroup jpg = (JspPropertyGroup) iter.next(); 426 JspProperty jp = jpg.getJspProperty(); 427 428 String extension = jpg.getExtension(); 429 String path = jpg.getPath(); 430 431 if (extension == null) { 432 if (uri.equals(path)) { 433 return true; 435 } 436 } else { 437 if ((path == null || path.equals(uriPath)) && 438 (extension.equals("*") || extension.equals(uriExtension))) { 439 return true; 441 } 442 } 443 } 444 return false; 445 } 446 447 static class JspPropertyGroup { 448 private String path; 449 private String extension; 450 private JspProperty jspProperty; 451 452 JspPropertyGroup(String path, String extension, 453 JspProperty jspProperty) { 454 this.path = path; 455 this.extension = extension; 456 this.jspProperty = jspProperty; 457 } 458 459 public String getPath() { 460 return path; 461 } 462 463 public String getExtension() { 464 return extension; 465 } 466 467 public JspProperty getJspProperty() { 468 return jspProperty; 469 } 470 } 471 472 static public class JspProperty { 473 474 private String isXml; 475 private String elIgnored; 476 private String scriptingInvalid; 477 private String pageEncoding; 478 private Vector includePrelude; 479 private Vector includeCoda; 480 private String deferedSyntaxAllowedAsLiteral; 481 private String trimDirectiveWhitespaces; 482 483 public JspProperty(String isXml, String elIgnored, 484 String scriptingInvalid, String pageEncoding, 485 Vector includePrelude, Vector includeCoda, 486 String deferedSyntaxAllowedAsLiteral, 487 String trimDirectiveWhitespaces) { 488 489 this.isXml = isXml; 490 this.elIgnored = elIgnored; 491 this.scriptingInvalid = scriptingInvalid; 492 this.pageEncoding = pageEncoding; 493 this.includePrelude = includePrelude; 494 this.includeCoda = includeCoda; 495 this.deferedSyntaxAllowedAsLiteral = deferedSyntaxAllowedAsLiteral; 496 this.trimDirectiveWhitespaces = trimDirectiveWhitespaces; 497 } 498 499 public String isXml() { 500 return isXml; 501 } 502 503 public String isELIgnored() { 504 return elIgnored; 505 } 506 507 public String isScriptingInvalid() { 508 return scriptingInvalid; 509 } 510 511 public String getPageEncoding() { 512 return pageEncoding; 513 } 514 515 public Vector getIncludePrelude() { 516 return includePrelude; 517 } 518 519 public Vector getIncludeCoda() { 520 return includeCoda; 521 } 522 523 public String isDeferedSyntaxAllowedAsLiteral() { 524 return deferedSyntaxAllowedAsLiteral; 525 } 526 527 public String isTrimDirectiveWhitespaces() { 528 return trimDirectiveWhitespaces; 529 } 530 } 531 } 532 | Popular Tags |