1 19 20 package org.netbeans.modules.web.jsps.parserapi; 21 22 import java.util.*; 23 24 import org.openide.ErrorManager; 25 26 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.el.FunctionMapper ; 30 import javax.servlet.jsp.tagext.TagInfo ; 31 import javax.servlet.jsp.tagext.TagFileInfo ; 32 import javax.servlet.jsp.tagext.FunctionInfo ; 33 import javax.servlet.jsp.tagext.TagAttributeInfo ; 34 import javax.servlet.jsp.tagext.TagLibraryInfo ; 35 36 41 42 public abstract class PageInfo { 43 44 public static final String JSP_SERVLET_BASE = "org.apache.jasper.runtime.HttpJspBase"; 45 46 private List<String > imports; 47 private List<String > dependants; 48 49 private BeanData[] beans; 51 private Map<String , TagLibraryInfo > taglibsMap; 52 private Map<String , String > jspPrefixMapper; 53 private Map<String , LinkedList<String >> xmlPrefixMapper; 54 55 private Map approxXmlPrefixMapper; 56 private String defaultLanguage = "java"; 57 private String language; 58 private String defaultExtends = JSP_SERVLET_BASE; 59 private String xtends; 60 private String contentType = null; 61 private String session; 62 private boolean isSession = true; 63 private String bufferValue; 64 private int buffer = 8*1024; private String autoFlush; 66 private boolean isAutoFlush = true; 67 private String isThreadSafeValue; 68 private boolean isThreadSafe = true; 69 private String isErrorPageValue; 70 private boolean isErrorPage = false; 71 private String errorPage = null; 72 private String info; 73 74 private boolean scriptless = false; 75 private boolean scriptingInvalid = false; 76 private String isELIgnoredValue; 77 private boolean isELIgnored = false; 78 private String omitXmlDecl = null; 79 80 private String doctypeName = null; 81 private String doctypePublic = null; 82 private String doctypeSystem = null; 83 84 private boolean isJspPrefixHijacked; 85 86 private Set<String > prefixes; 88 89 private boolean hasJspRoot = false; 90 private List includePrelude; 91 private List includeCoda; 92 private List<String > pluginDcls; 94 95 public PageInfo( 96 Map<String , TagLibraryInfo > taglibsMap, 97 Map<String , String > jspPrefixMapper, 98 Map<String , LinkedList<String >> xmlPrefixMapper, 99 Map approxXmlPrefixMapper, 100 List<String > imports, 101 List<String > dependants, 102 List includePrelude, 103 List includeCoda, 104 List<String > pluginDcls, 105 Set<String > prefixes 106 ) { 107 this.taglibsMap = taglibsMap; 109 this.jspPrefixMapper = jspPrefixMapper; 110 this.xmlPrefixMapper = xmlPrefixMapper; 111 this.approxXmlPrefixMapper = approxXmlPrefixMapper; 112 this.imports = imports; 113 this.dependants = dependants; 114 this.includePrelude = includePrelude; 115 this.includeCoda = includeCoda; 116 this.pluginDcls = pluginDcls; 117 this.prefixes = prefixes; 118 } 119 120 125 public boolean isPluginDeclared(String id) { 126 if (pluginDcls.contains(id)) 127 return true; 128 pluginDcls.add(id); 129 return false; 130 } 131 132 public void addImports(List<String > imports) { 133 this.imports.addAll(imports); 134 } 135 136 public void addImport(String imp) { 137 this.imports.add(imp); 138 } 139 140 public List<String > getImports() { 141 return imports; 142 } 143 144 public void addDependant(String d) { 145 if (!dependants.contains(d)) 146 dependants.add(d); 147 } 148 149 public List<String > getDependants() { 150 return dependants; 151 } 152 153 public void setScriptless(boolean s) { 154 scriptless = s; 155 } 156 157 public boolean isScriptless() { 158 return scriptless; 159 } 160 161 public void setScriptingInvalid(boolean s) { 162 scriptingInvalid = s; 163 } 164 165 public boolean isScriptingInvalid() { 166 return scriptingInvalid; 167 } 168 169 public List getIncludePrelude() { 170 return includePrelude; 171 } 172 173 public void setIncludePrelude(Vector prelude) { 174 includePrelude = prelude; 175 } 176 177 public List getIncludeCoda() { 178 return includeCoda; 179 } 180 181 public void setIncludeCoda(Vector coda) { 182 includeCoda = coda; 183 } 184 185 public void setHasJspRoot(boolean s) { 186 hasJspRoot = s; 187 } 188 189 public boolean hasJspRoot() { 190 return hasJspRoot; 191 } 192 193 public String getOmitXmlDecl() { 194 return omitXmlDecl; 195 } 196 197 public void setOmitXmlDecl(String omit) { 198 omitXmlDecl = omit; 199 } 200 201 public String getDoctypeName() { 202 return doctypeName; 203 } 204 205 public void setDoctypeName(String doctypeName) { 206 this.doctypeName = doctypeName; 207 } 208 209 public String getDoctypeSystem() { 210 return doctypeSystem; 211 } 212 213 public void setDoctypeSystem(String doctypeSystem) { 214 this.doctypeSystem = doctypeSystem; 215 } 216 217 public String getDoctypePublic() { 218 return doctypePublic; 219 } 220 221 public void setDoctypePublic(String doctypePublic) { 222 this.doctypePublic = doctypePublic; 223 } 224 225 226 227 public void setIsJspPrefixHijacked(boolean isHijacked) { 228 isJspPrefixHijacked = isHijacked; 229 } 230 231 public boolean isJspPrefixHijacked() { 232 return isJspPrefixHijacked; 233 } 234 235 240 public void addPrefix(String prefix) { 241 prefixes.add(prefix); 242 } 243 244 252 public boolean containsPrefix(String prefix) { 253 return prefixes.contains(prefix); 254 } 255 256 262 public void addTaglib(String uri, TagLibraryInfo info) { 263 taglibsMap.put(uri, info); 264 } 265 266 271 public TagLibraryInfo getTaglib(String uri) { 272 return taglibsMap.get(uri); 273 } 274 275 280 public Collection<TagLibraryInfo > getTaglibs() { 281 return taglibsMap.values(); 282 } 283 284 292 public boolean hasTaglib(String uri) { 293 return taglibsMap.containsKey(uri); 294 } 295 296 302 public void addPrefixMapping(String prefix, String uri) { 303 jspPrefixMapper.put(prefix, uri); 304 } 305 306 313 public void pushPrefixMapping(String prefix, String uri) { 314 LinkedList<String > stack = xmlPrefixMapper.get(prefix); 315 if (stack == null) { 316 stack = new LinkedList<String >(); 317 xmlPrefixMapper.put(prefix, stack); 318 } 319 stack.addFirst(uri); 320 } 321 322 328 public void popPrefixMapping(String prefix) { 329 LinkedList<String > stack = xmlPrefixMapper.get(prefix); 330 if (stack == null || stack.size() == 0) { 331 } 333 stack.removeFirst(); 334 } 335 336 343 public String getURI(String prefix) { 344 345 String uri = null; 346 347 LinkedList<String > stack = xmlPrefixMapper.get(prefix); 348 if (stack == null || stack.size() == 0) { 349 uri = jspPrefixMapper.get(prefix); 350 } else { 351 uri = stack.getFirst(); 352 } 353 354 return uri; 355 } 356 357 358 359 360 363 public void setLanguage(String value) { 364 language = value; 365 } 366 367 public String getLanguage(boolean useDefault) { 368 return (language == null && useDefault ? defaultLanguage : language); 369 } 370 371 public String getLanguage() { 372 return getLanguage(true); 373 } 374 375 376 379 public void setExtends(String value) { 380 381 xtends = value; 382 383 388 } 391 392 403 public String getExtends(boolean useDefault) { 404 return (xtends == null && useDefault ? defaultExtends : xtends); 405 } 406 407 414 public String getExtends() { 415 return getExtends(true); 416 } 417 418 419 422 public void setContentType(String value) { 423 contentType = value; 424 } 425 426 public String getContentType() { 427 return contentType; 428 } 429 430 431 434 public void setBufferValue(String value) throws JspException { 435 if (value == null) 436 return; 437 438 if ("none".equalsIgnoreCase(value)) 439 buffer = 0; 440 else { 441 if (value == null || !value.endsWith("kb")) 442 throw new JspException (value); 443 try { 444 Integer k = new Integer (value.substring(0, value.length()-2)); 445 buffer = k.intValue() * 1024; 446 } catch (NumberFormatException e) { 447 throw new JspException (value); 448 } 449 } 450 451 bufferValue = value; 452 } 453 454 public String getBufferValue() { 455 return bufferValue; 456 } 457 458 public int getBuffer() { 459 return buffer; 460 } 461 462 463 466 public void setSession(String value) throws JspException { 467 if (value == null) 468 return; 469 470 if ("true".equalsIgnoreCase(value)) 471 isSession = true; 472 else if ("false".equalsIgnoreCase(value)) 473 isSession = false; 474 else 475 throw new JspException (value); 476 477 session = value; 478 } 479 480 public String getSession() { 481 return session; 482 } 483 484 public boolean isSession() { 485 return isSession; 486 } 487 488 489 492 public void setAutoFlush(String value) throws JspException { 493 if (value == null) 494 return; 495 496 if ("true".equalsIgnoreCase(value)) 497 isAutoFlush = true; 498 else if ("false".equalsIgnoreCase(value)) 499 isAutoFlush = false; 500 else 501 throw new JspException (value); 502 503 autoFlush = value; 504 } 505 506 public String getAutoFlush() { 507 return autoFlush; 508 } 509 510 public boolean isAutoFlush() { 511 return isAutoFlush; 512 } 513 514 515 518 public void setIsThreadSafe(String value) throws JspException { 519 if (value == null) 520 return; 521 522 if ("true".equalsIgnoreCase(value)) 523 isThreadSafe = true; 524 else if ("false".equalsIgnoreCase(value)) 525 isThreadSafe = false; 526 else 527 throw new JspException (value); 528 529 isThreadSafeValue = value; 530 } 531 532 public String getIsThreadSafe() { 533 return isThreadSafeValue; 534 } 535 536 public boolean isThreadSafe() { 537 return isThreadSafe; 538 } 539 540 541 544 public void setInfo(String value) { 545 info = value; 546 } 547 548 public String getInfo() { 549 return info; 550 } 551 552 553 556 public void setErrorPage(String value) { 557 errorPage = value; 558 } 559 560 public String getErrorPage() { 561 return errorPage; 562 } 563 564 565 568 public void setIsErrorPage(String value) throws JspException { 569 if (value == null) 570 return; 571 572 if ("true".equalsIgnoreCase(value)) 573 isErrorPage = true; 574 else if ("false".equalsIgnoreCase(value)) 575 isErrorPage = false; 576 else 577 throw new JspException (value); 578 579 isErrorPageValue = value; 580 } 581 582 public String getIsErrorPage() { 583 return isErrorPageValue; 584 } 585 586 public boolean isErrorPage() { 587 return isErrorPage; 588 } 589 590 591 594 public void setIsELIgnored(String value) throws JspException { 595 if (value == null) 596 return; 597 598 if ("true".equalsIgnoreCase(value)) 599 isELIgnored = true; 600 else if ("false".equalsIgnoreCase(value)) 601 isELIgnored = false; 602 else { 603 throw new JspException (value); 604 } 605 606 isELIgnoredValue = value; 607 } 608 609 public void setELIgnored(boolean s) { 610 isELIgnored = s; 611 } 612 613 public String getIsELIgnored() { 614 return isELIgnoredValue; 615 } 616 617 public boolean isELIgnored() { 618 return isELIgnored; 619 } 620 621 623 public Map getTagLibraries() { 624 return taglibsMap; 625 } 626 627 public Map<String , String > getJspPrefixMapper() { 628 return jspPrefixMapper; 629 } 630 631 public Map getXMLPrefixMapper() { 632 return xmlPrefixMapper; 633 } 634 635 public Map getApproxXmlPrefixMapper() { 636 return approxXmlPrefixMapper; 637 } 638 639 public BeanData[] getBeans() { 640 return beans; 641 } 642 643 public void setBeans(BeanData beans[]) { 644 this.beans = beans; 645 } 646 647 648 655 657 public String toString() { 658 StringBuilder sb = new StringBuilder (); 659 String nl = "\n"; String indent = " "; String nlIndent = nl + indent; 662 663 sb.append("----- PageInfo -----\n"); sb.append(indent).append("imports:\n").append(collectionToString(imports, indent + " ")); sb.append(indent).append("dependants:\n").append(collectionToString(dependants, indent + " ")); sb.append(indent).append("taglibsMap:\n").append(taglibsMapToString(taglibsMap, indent + " ")); sb.append(indent).append("prefixMapper:\n").append(mapToString(jspPrefixMapper, indent + " ")); sb.append(indent).append("approxXmlPrefixMapper :\n").append(mapToString(approxXmlPrefixMapper, indent + " ")); sb.append(indent).append("language : ").append(language).append('\n'); sb.append(indent).append("xtends : ").append(xtends).append('\n'); sb.append(indent).append("contentType : ").append(contentType).append('\n'); sb.append(indent).append("session : ").append(isSession).append('\n'); sb.append(indent).append("buffer : ").append(buffer).append('\n'); sb.append(indent).append("autoFlush : ").append(isAutoFlush).append('\n'); sb.append(indent).append("threadSafe : ").append(isThreadSafe).append('\n'); sb.append(indent).append("isErrorPage : ").append(isErrorPage).append('\n'); sb.append(indent).append("errorPage : ").append(errorPage).append('\n'); sb.append(indent).append("scriptless : ").append(scriptless).append('\n'); sb.append(indent).append("scriptingInvalid : ").append(scriptingInvalid).append('\n'); sb.append(indent).append("elIgnored : ").append(isELIgnored).append('\n'); sb.append(indent).append("omitXmlDecl : ").append(omitXmlDecl).append('\n'); sb.append(indent).append("isJspPrefixHijacked : ").append(isJspPrefixHijacked).append('\n'); sb.append(indent).append("doctypeName : ").append(doctypeName).append('\n'); sb.append(indent).append("doctypeSystem : ").append(doctypeSystem).append('\n'); sb.append(indent).append("doctypePublic : ").append(doctypePublic).append('\n'); sb.append(indent).append("hasJspRoot : ").append(hasJspRoot).append('\n'); sb.append(indent).append("prefixes:\n").append(collectionToString(prefixes, indent + " ")); sb.append(indent).append("includePrelude:\n").append(collectionToString(includePrelude, indent + " ")); sb.append(indent).append("includeCoda:\n").append(collectionToString(includeCoda, indent + " ")); sb.append(indent).append("pluginDcls:\n").append(collectionToString(pluginDcls, indent + " ")); sb.append(indent).append("beans:\n").append(beansToString(beans, indent + " ")); 695 return sb.toString(); 696 } 697 698 private String collectionToString(Collection c, String indent) { StringBuilder sb = new StringBuilder (); 700 Iterator it = c.iterator(); 701 while (it.hasNext()) { 702 sb.append(indent).append(it.next()).append('\n'); } 704 return sb.toString(); 705 } 706 707 private String taglibsMapToString(Map m, String indent) { 708 StringBuilder sb = new StringBuilder (); 709 Iterator it = m.keySet().iterator(); 710 while (it.hasNext()) { 711 Object key = it.next(); 712 sb.append(indent).append("tag library: ").append(key).append('\n'); sb.append(tagLibraryInfoToString((TagLibraryInfo )m.get(key), indent + " ")); } 715 return sb.toString(); 716 } 717 718 public String tagLibraryInfoToString(TagLibraryInfo info, String indent) { 719 StringBuilder sb = new StringBuilder (); 720 sb.append(indent).append("tlibversion : ").append(getFieldByReflection("tlibversion", info)).append('\n'); sb.append(indent).append("jspversion : ").append(info.getRequiredVersion()).append('\n'); sb.append(indent).append("shortname : ").append(info.getShortName()).append('\n'); sb.append(indent).append("urn : ").append(info.getReliableURN()).append('\n'); sb.append(indent).append("info : ").append(info.getInfoString()).append('\n'); sb.append(indent).append("uri : ").append(info.getURI()).append('\n'); 727 TagInfo tags[] = info.getTags(); 728 if (tags != null) { 729 for (int i = 0; i < tags.length; i++) 730 sb.append(tagInfoToString(tags[i], indent + " ")); } 732 733 TagFileInfo tagFiles[] = info.getTagFiles(); 734 if (tagFiles != null) { 735 for (int i = 0; i < tagFiles.length; i++) 736 sb.append(tagFileToString(tagFiles[i], indent + " ")); } 738 739 FunctionInfo functions[] = info.getFunctions(); 740 if (functions != null) { 741 for (int i = 0; i < functions.length; i++) 742 sb.append(functionInfoToString(functions[i], indent + " ")); } 744 745 return sb.toString(); 746 } 747 748 public String tagInfoToString(TagInfo tag, String indent) { 749 StringBuilder sb = new StringBuilder (); 750 if (tag != null) { 751 sb.append(indent).append("tag name : ").append(tag.getTagName()).append('\n'); sb.append(indent).append(" class : ").append(tag.getTagClassName()).append('\n'); sb.append(indent).append(" attribs : ["); TagAttributeInfo attrs[] = tag.getAttributes(); 755 for (int i = 0; i < attrs.length; i++) { 756 sb.append(attrs[i].getName()); 757 if (i < attrs.length - 1) { 758 sb.append(", "); } 760 } 761 sb.append("]\n"); } 763 else { 764 sb.append(indent).append("taginfo is null\n"); } 766 return sb.toString(); 767 } 768 769 public String functionInfoToString(FunctionInfo function, String indent) { 770 StringBuilder sb = new StringBuilder (); 771 if (function != null) { 772 sb.append(indent).append("function name : ").append(function.getName()).append('\n'); sb.append(indent).append(" class : ").append(function.getFunctionClass()).append('\n'); sb.append(indent).append(" signature: ").append(function.getFunctionSignature()).append('\n'); } 776 else { 777 sb.append(indent).append("functioninfo is null\n"); } 779 return sb.toString(); 780 } 781 782 public String tagFileToString(TagFileInfo tagFile, String indent) { 783 StringBuilder sb = new StringBuilder (); 784 sb.append(indent).append("tagfile path : ").append(tagFile.getPath()).append('\n'); sb.append(tagInfoToString(tagFile.getTagInfo(), indent)); 786 return sb.toString(); 787 } 788 789 private Object getFieldByReflection(String fieldName, TagLibraryInfo info) { 790 try { 791 java.lang.reflect.Field f = TagLibraryInfo .class.getDeclaredField(fieldName); 792 f.setAccessible(true); 793 return f.get(info); 794 } 795 catch (NoSuchFieldException e) { 796 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 797 } 798 catch (IllegalAccessException e) { 799 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 800 } 801 return null; 802 } 803 804 private String beansToString(BeanData beans[], String indent) { 805 StringBuilder sb = new StringBuilder (); 806 for (int i = 0; i < beans.length; i++) { 807 sb.append(indent).append("bean : ").append(beans[i].getId()).append(", "). append(scopeToString(beans[i].getScope())).append(", "). append(beans[i].getClassName()).append('\n'); } 811 return sb.toString(); 812 } 813 814 private String scopeToString(int scope) { 815 switch (scope) { 816 case PageContext.PAGE_SCOPE : return "PAGE"; case PageContext.SESSION_SCOPE : return "SESSION"; case PageContext.APPLICATION_SCOPE : return "APPLICATION"; case PageContext.REQUEST_SCOPE : return "REQUEST"; } 821 return " !!! "; 822 } 823 824 825 public interface BeanData { 826 827 828 public String getId(); 829 830 831 public int getScope(); 832 833 834 public String getClassName(); 835 836 } 838 private static String mapToString(Map m, String indent) { 840 StringBuilder sb = new StringBuilder (); 841 Iterator it = m.keySet().iterator(); 842 while (it.hasNext()) { 843 Object key = it.next(); 844 sb.append(indent).append(key).append(" -> ").append(m.get(key)).append('\n'); 845 } 846 return sb.toString(); 847 } 848 849 } 850 | Popular Tags |