1 19 20 package org.apache.jasper.compiler; 21 22 import java.io.*; 23 import java.lang.reflect.Field ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.*; 27 import javax.servlet.jsp.*; 28 import javax.servlet.jsp.tagext.*; 29 import javax.xml.parsers.*; 30 import org.apache.jasper.*; 31 import org.openide.ErrorManager; 32 33 34 38 public class GetParseData { 39 40 protected JspCompilationContext ctxt; 41 42 protected Options options; 43 private CompilerHacks compHacks; 44 private int errorReportingMode; 45 46 private org.netbeans.modules.web.jsps.parserapi.Node.Nodes nbNodes; 47 private org.netbeans.modules.web.jsps.parserapi.PageInfo nbPageInfo; 48 private Throwable parseException; 49 50 51 52 public GetParseData(JspCompilationContext ctxt, int errorReportingMode) { 53 this.ctxt = ctxt; 54 this.errorReportingMode = errorReportingMode; 55 this.options = ctxt.getOptions(); 56 this.compHacks = new CompilerHacks(ctxt); 57 } 58 59 public org.netbeans.modules.web.jsps.parserapi.Node.Nodes getNbNodes() { 60 return nbNodes; 61 } 62 63 public org.netbeans.modules.web.jsps.parserapi.PageInfo getNbPageInfo() { 64 return nbPageInfo; 65 } 66 67 public Throwable getParseException() { 68 return parseException; 69 } 70 71 73 public void parse() { 74 Node.Nodes pageNodes = null; 75 PageInfo pageInfo = null; 76 String xmlView = null; 77 try { 78 80 82 Compiler comp = compHacks.getCompiler(); 84 pageInfo = comp.getPageInfo(); 85 ErrorDispatcher errDispatcher = comp.getErrorDispatcher(); 86 87 88 91 JspConfig jspConfig = options.getJspConfig(); 92 JspConfig.JspProperty jspProperty = 93 jspConfig.findJspProperty(ctxt.getJspFile()); 94 95 100 pageInfo.setELIgnored(JspUtil.booleanValue(jspProperty.isELIgnored())); 101 pageInfo.setScriptingInvalid(JspUtil.booleanValue(jspProperty.isScriptingInvalid())); 102 if (jspProperty.getIncludePrelude() != null) { 103 pageInfo.setIncludePrelude(jspProperty.getIncludePrelude()); 104 } 105 if (jspProperty.getIncludeCoda() != null) { 106 pageInfo.setIncludeCoda(jspProperty.getIncludeCoda()); 107 } 108 110 120 123 JspUtil.resetTemporaryVariableName(); 125 126 ParserController parserCtl = new ParserController(ctxt, comp); 128 pageNodes = parserCtl.parse(ctxt.getJspFile()); 129 130 137 141 xmlView = NbValidator.validate(comp, pageNodes); 144 145 149 Collector.collect(comp, pageNodes); 151 152 155 159 161 ScriptingVariabler.set(pageNodes, errDispatcher); 163 164 TagPluginManager tagPluginManager = options.getTagPluginManager(); 166 tagPluginManager.apply(pageNodes, errDispatcher, pageInfo); 167 168 ELFunctionMapper.map(comp, pageNodes); 170 171 179 185 190 196 } 199 catch (ThreadDeath t) { 200 throw t; 201 } 202 catch (Throwable t) { 203 parseException = t; 204 } 205 finally { 206 try { 208 if (pageNodes != null) { 209 nbNodes = convertNodes(pageNodes); 210 } 211 } 212 catch (JasperException e) { 213 if (parseException == null) { 214 parseException = e; 215 } 216 } 217 try { 219 if (pageInfo != null) { 220 nbPageInfo = convertPageInfo(pageInfo, xmlView, ctxt); 222 } 223 } 224 catch (JspException e) { 225 if (parseException == null) { 226 parseException = e; 227 } 228 } 229 } 230 } 232 233 234 private static org.netbeans.modules.web.jsps.parserapi.Node.Nodes convertNodes(Node.Nodes nodes) throws JasperException { 235 org.netbeans.modules.web.jsps.parserapi.Node.Nodes nbNodes = 236 NodeConverterVisitor.convertNodes(nodes); 237 return nbNodes; 238 } 239 240 private static org.netbeans.modules.web.jsps.parserapi.PageInfo convertPageInfo(PageInfo pageInfo, String xmlView, JspCompilationContext ctxt) throws JspException { 241 PageInfoImpl nbPageInfo = 242 new PageInfoImpl( 243 getTaglibsMapReflect(pageInfo, ctxt), 244 getJSPPrefixMapperReflect(pageInfo), 245 getXMLPrefixMapperReflect(pageInfo), 246 ((CompilerHacks.HackPageInfo)pageInfo).getApproxXmlPrefixMapper(), 247 pageInfo.getImports(), 248 pageInfo.getDependants(), 249 pageInfo.getIncludePrelude(), 250 pageInfo.getIncludeCoda(), 251 getPluginDclsReflect(pageInfo), 252 getPrefixesReflect(pageInfo) 253 ); 254 nbPageInfo.setLanguage( pageInfo.getLanguage()); 255 nbPageInfo.setExtends( pageInfo.getExtends()); 256 nbPageInfo.setContentType( pageInfo.getContentType()); 257 nbPageInfo.setSession( pageInfo.getSession()); 258 nbPageInfo.setBufferValue( pageInfo.getBufferValue()); 259 nbPageInfo.setAutoFlush( pageInfo.getAutoFlush()); 260 nbPageInfo.setIsThreadSafe( pageInfo.getIsThreadSafe()); 261 nbPageInfo.setIsErrorPage( pageInfo.getIsErrorPage()); 262 nbPageInfo.setErrorPage( pageInfo.getErrorPage()); 263 nbPageInfo.setScriptless( pageInfo.isScriptless()); 264 nbPageInfo.setScriptingInvalid( pageInfo.isScriptingInvalid()); 265 nbPageInfo.setELIgnored( pageInfo.isELIgnored()); 266 nbPageInfo.setOmitXmlDecl( pageInfo.getOmitXmlDecl()); 267 nbPageInfo.setIsJspPrefixHijacked( pageInfo.isJspPrefixHijacked()); 268 nbPageInfo.setDoctypeName( pageInfo.getDoctypeName()); 269 nbPageInfo.setDoctypeSystem( pageInfo.getDoctypeSystem()); 270 nbPageInfo.setDoctypePublic( pageInfo.getDoctypePublic()); 271 nbPageInfo.setHasJspRoot( pageInfo.hasJspRoot()); 272 nbPageInfo.setBeans(createBeanData(pageInfo.getBeanRepository())); 273 nbPageInfo.setXMLView(xmlView); 275 276 return nbPageInfo; 277 } 278 279 280 private static org.netbeans.modules.web.jsps.parserapi.PageInfo.BeanData[] createBeanData(BeanRepository rep) { 281 try { 282 initBeanRepositoryFields(); 283 Vector sessionBeans = (Vector)sessionBeansF.get(rep); 284 Vector pageBeans = (Vector)pageBeansF.get(rep); 285 Vector appBeans = (Vector)appBeansF.get(rep); 286 Vector requestBeans = (Vector)requestBeansF.get(rep); 287 Hashtable beanTypes = (Hashtable)beanTypesF.get(rep); 288 int size = beanTypes.size(); 289 org.netbeans.modules.web.jsps.parserapi.PageInfo.BeanData bd[] = 290 new org.netbeans.modules.web.jsps.parserapi.PageInfo.BeanData[size]; 291 Iterator it = beanTypes.keySet().iterator(); 292 int index = 0; 293 while (it.hasNext()) { 294 String id = (String )it.next(); 295 String type = (String )beanTypes.get(id); 296 int scope = PageContext.PAGE_SCOPE; 297 if (sessionBeans.contains(id)) { 298 scope = PageContext.SESSION_SCOPE; 299 } 300 if (appBeans.contains(id)) { 301 scope = PageContext.APPLICATION_SCOPE; 302 } 303 if (requestBeans.contains(id)) { 304 scope = PageContext.REQUEST_SCOPE; 305 } 306 307 bd[index] = new BeanDataImpl(id, scope, type); 308 ++index; 309 } 310 return bd; 311 } 312 catch (IllegalAccessException e) { 313 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 314 throw new RuntimeException (); 315 } 316 } 317 318 static class PageInfoImpl extends org.netbeans.modules.web.jsps.parserapi.PageInfo { 319 320 public PageInfoImpl( 321 Map taglibsMap, 322 Map jspPrefixMapper, 323 Map xmlPrefixMapper, 324 Map approxXmlPrefixMapper, 325 List imports, 326 List dependants, 327 List includePrelude, 328 List includeCoda, 329 List pluginDcls, 330 Set prefixes 331 ) { 332 super(taglibsMap, jspPrefixMapper, xmlPrefixMapper, approxXmlPrefixMapper, imports, dependants, includePrelude, 333 includeCoda, pluginDcls, prefixes); 334 } 335 336 private String xmlView; 337 338 public void setXMLView(String xmlView) { 339 this.xmlView = xmlView; 340 } 341 342 public String getXMLView() { 343 return xmlView; 344 } 345 346 public String toString() { 347 StringBuffer sb = new StringBuffer (); 348 sb.append(super.toString()); 349 sb.append(" ------- XML View (constructed from the original data structure) -----\n"); 350 if (xmlView == null) { 351 sb.append("no XML view\n"); 352 } 353 else { 354 sb.append(getXMLView()); 355 } 356 return sb.toString(); 357 } 358 359 } 360 361 static class BeanDataImpl implements org.netbeans.modules.web.jsps.parserapi.PageInfo.BeanData { 362 363 private String id; 364 private int scope; 365 private String className; 366 367 BeanDataImpl(String id, int scope, String className) { 368 this.id = id; 369 this.scope = scope; 370 this.className = className; 371 } 372 373 374 public String getId() { 375 return id; 376 } 377 378 379 public int getScope() { 380 return scope; 381 } 382 383 384 public String getClassName() { 385 return className; 386 } 387 388 } 389 390 private static Field sessionBeansF, pageBeansF, appBeansF, requestBeansF, beanTypesF; 392 393 private static void initBeanRepositoryFields() { 394 if (sessionBeansF == null) { 395 try { 396 sessionBeansF = BeanRepository.class.getDeclaredField("sessionBeans"); 397 sessionBeansF.setAccessible(true); 398 pageBeansF = BeanRepository.class.getDeclaredField("pageBeans"); 399 pageBeansF.setAccessible(true); 400 appBeansF = BeanRepository.class.getDeclaredField("appBeans"); 401 appBeansF.setAccessible(true); 402 requestBeansF = BeanRepository.class.getDeclaredField("requestBeans"); 403 requestBeansF.setAccessible(true); 404 beanTypesF = BeanRepository.class.getDeclaredField("beanTypes"); 405 beanTypesF.setAccessible(true); 406 } 407 catch (NoSuchFieldException e) { 408 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 409 } 410 } 411 } 412 413 414 private static Field pluginDclsF, prefixesF, taglibsMapF, jspPrefixMapperF, xmlPrefixMapperF; 416 417 private static void initPageInfoFields() { 418 if (pluginDclsF == null) { 419 try { 420 pluginDclsF = PageInfo.class.getDeclaredField("pluginDcls"); 421 pluginDclsF.setAccessible(true); 422 prefixesF = PageInfo.class.getDeclaredField("prefixes"); 423 prefixesF.setAccessible(true); 424 taglibsMapF = PageInfo.class.getDeclaredField("taglibsMap"); 425 taglibsMapF.setAccessible(true); 426 jspPrefixMapperF = PageInfo.class.getDeclaredField("jspPrefixMapper"); 427 jspPrefixMapperF.setAccessible(true); 428 xmlPrefixMapperF = PageInfo.class.getDeclaredField("xmlPrefixMapper"); 429 xmlPrefixMapperF.setAccessible(true); 430 } 431 catch (NoSuchFieldException e) { 432 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 433 } 434 } 435 } 436 437 private static Vector getPluginDclsReflect(PageInfo pageInfo) { 438 initPageInfoFields(); 439 try { 440 return (Vector)pluginDclsF.get(pageInfo); 441 } 442 catch (IllegalAccessException e) { 443 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 444 throw new RuntimeException (); 445 } 446 } 447 448 private static HashSet getPrefixesReflect(PageInfo pageInfo) { 449 initPageInfoFields(); 450 try { 451 return (HashSet)prefixesF.get(pageInfo); 452 } 453 catch (IllegalAccessException e) { 454 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 455 throw new RuntimeException (); 456 } 457 } 458 459 private static class TagInfoCacheRecord{ 460 long time; 461 TagInfo tagInfo; 462 463 public TagInfoCacheRecord(long time, TagInfo info){ 464 this.tagInfo = info; 465 this.time = time; 466 } 467 } 468 469 473 474 private static Hashtable tagInfoCache = new Hashtable(); 475 476 private static Map getTaglibsMapReflect(PageInfo pageInfo, JspCompilationContext ctxt) { 477 initPageInfoFields(); 478 try { 479 Map taglibs = (Map)taglibsMapF.get(pageInfo); 480 Iterator iter = taglibs.values().iterator(); 481 TagLibraryInfo libInfo; 482 TagInfo [] tagInfos; 483 while (iter.hasNext()){ 485 libInfo = (TagLibraryInfo)iter.next(); 486 tagInfos = null; 487 try { 488 ArrayList tags = new ArrayList(); 489 if (libInfo instanceof ImplicitTagLibraryInfo){ 490 Field tagFileMapF = ImplicitTagLibraryInfo.class.getDeclaredField("tagFileMap"); 492 tagFileMapF.setAccessible(true); 493 Hashtable tagFileMap = (Hashtable)tagFileMapF.get(libInfo); 494 Enumeration e = tagFileMap.keys(); 496 497 while (e.hasMoreElements()){ 498 String name = (String ) e.nextElement(); 500 String filePath = (String )tagFileMap.get(name); 501 502 URL path = ctxt.getResource(filePath); 503 File file = new File (new URI ( path.toExternalForm() )); 504 if (tagInfoCache.containsKey(path)){ 506 TagInfoCacheRecord r = (TagInfoCacheRecord)tagInfoCache.get(path); 507 if (r.time < file.lastModified()){ 509 tagInfoCache.put(path, new TagInfoCacheRecord (file.lastModified(), libInfo.getTagFile(name).getTagInfo())); 510 } 511 } 512 else { 513 tagInfoCache.put(path, new TagInfoCacheRecord (file.lastModified(), libInfo.getTagFile(name).getTagInfo())); 514 } 515 tags.add(((TagInfoCacheRecord)tagInfoCache.get(path)).tagInfo); 517 tagInfos = new TagInfo[tags.size()]; 518 for (int i = 0; i < tags.size(); i++){ 519 tagInfos[i] = (TagInfo)tags.get(i); 520 } 521 } 522 } 523 else { 524 TagFileInfo[] tagFiles = libInfo.getTagFiles(); 525 if (tagFiles != null && tagFiles.length > 0){ 526 tagInfos = new TagInfo[tagFiles.length + libInfo.getTags().length]; 527 for (int i = 0; i < tagFiles.length; i++){ 528 String [] location = ctxt.getTldLocation(libInfo.getURI()); 531 if (location != null){ 532 String path = location[0]; 533 File file = new File (path); 534 path = path + tagFiles[i].getPath(); 535 if (tagInfoCache.containsKey(path)){ 537 TagInfoCacheRecord r = (TagInfoCacheRecord)tagInfoCache.get(path); 538 if (r.time < file.lastModified()){ 540 tagInfoCache.put(path, new TagInfoCacheRecord (file.lastModified(), 541 libInfo.getTagFile(tagFiles[i].getName()).getTagInfo())); 542 } 543 } 544 else { 545 tagInfoCache.put(path, 546 new TagInfoCacheRecord (file.lastModified(), 547 libInfo.getTagFile(tagFiles[i].getName()).getTagInfo())); 548 } 549 tagInfos[i] = ((TagInfoCacheRecord)tagInfoCache.get(path)).tagInfo; 551 } 552 else 553 tagInfos[i] = libInfo.getTagFile(tagFiles[i].getName()).getTagInfo(); 554 } 555 for (int i = 0 ; i < libInfo.getTags().length; i++){ 557 tagInfos[tagFiles.length + i] = libInfo.getTags()[i]; 558 } 559 } 560 } 561 562 if (tagInfos != null && tagInfos.length > 0){ 563 Field tagInfosF = ImplicitTagLibraryInfo.class.getSuperclass().getDeclaredField("tags"); 565 tagInfosF.setAccessible(true); 566 tagInfosF.set(libInfo, tagInfos); 567 } 568 } 569 catch (java.lang.NoSuchFieldException e){ 570 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 571 } 572 catch (java.net.MalformedURLException e){ 573 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 574 } 575 catch (org.apache.jasper.JasperException e) { 576 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 577 } 578 catch (java.net.URISyntaxException e) { 579 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 580 } 581 } 582 return taglibs; 583 } 584 catch (IllegalAccessException e) { 585 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 586 throw new RuntimeException (); 587 } 588 } 589 590 private static Map getJSPPrefixMapperReflect(PageInfo pageInfo) { 591 initPageInfoFields(); 592 try { 593 return (Map)jspPrefixMapperF.get(pageInfo); 594 } 595 catch (IllegalAccessException e) { 596 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 597 throw new RuntimeException (); 598 } 599 } 600 601 private static Map getXMLPrefixMapperReflect(PageInfo pageInfo) { 602 initPageInfoFields(); 603 try { 604 return (Map)xmlPrefixMapperF.get(pageInfo); 605 } 606 catch (IllegalAccessException e) { 607 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 608 throw new RuntimeException (); 609 } 610 } 611 612 } 613 | Popular Tags |