| 1 17 18 package org.apache.jasper; 19 20 import java.io.BufferedReader ; 21 import java.io.CharArrayWriter ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.FileReader ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.io.Writer ; 31 import java.net.MalformedURLException ; 32 import java.net.URL ; 33 import java.net.URLClassLoader ; 34 import java.util.ArrayList ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.HashMap ; 39 import java.util.Stack ; 40 import java.util.StringTokenizer ; 41 import java.util.Vector ; 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.apache.jasper.compiler.Compiler; 46 import org.apache.jasper.compiler.JspConfig; 47 import org.apache.jasper.compiler.JspRuntimeContext; 48 import org.apache.jasper.compiler.Localizer; 49 import org.apache.jasper.compiler.TagPluginManager; 50 import org.apache.jasper.compiler.TldLocationsCache; 51 import org.apache.jasper.servlet.JspCServletContext; 52 53 import org.apache.tools.ant.AntClassLoader; 54 import org.apache.tools.ant.Project; 55 import org.apache.tools.ant.util.FileUtils; 56 57 92 public class JspC implements Options { 93 94 public static final String DEFAULT_IE_CLASS_ID = 95 "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"; 96 97 protected static Log log = LogFactory.getLog(JspC.class); 99 100 protected static final String SWITCH_VERBOSE = "-v"; 101 protected static final String SWITCH_HELP = "-help"; 102 protected static final String SWITCH_OUTPUT_DIR = "-d"; 103 protected static final String SWITCH_PACKAGE_NAME = "-p"; 104 protected static final String SWITCH_CACHE = "-cache"; 105 protected static final String SWITCH_CLASS_NAME = "-c"; 106 protected static final String SWITCH_FULL_STOP = "--"; 107 protected static final String SWITCH_COMPILE = "-compile"; 108 protected static final String SWITCH_SOURCE = "-source"; 109 protected static final String SWITCH_TARGET = "-target"; 110 protected static final String SWITCH_URI_BASE = "-uribase"; 111 protected static final String SWITCH_URI_ROOT = "-uriroot"; 112 protected static final String SWITCH_FILE_WEBAPP = "-webapp"; 113 protected static final String SWITCH_WEBAPP_INC = "-webinc"; 114 protected static final String SWITCH_WEBAPP_XML = "-webxml"; 115 protected static final String SWITCH_MAPPED = "-mapped"; 116 protected static final String SWITCH_XPOWERED_BY = "-xpoweredBy"; 117 protected static final String SWITCH_TRIM_SPACES = "-trimSpaces"; 118 protected static final String SWITCH_CLASSPATH = "-classpath"; 119 protected static final String SWITCH_DIE = "-die"; 120 protected static final String SWITCH_POOLING = "-poolingEnabled"; 121 protected static final String SWITCH_ENCODING = "-javaEncoding"; 122 protected static final String SWITCH_SMAP = "-smap"; 123 protected static final String SWITCH_DUMP_SMAP = "-dumpsmap"; 124 125 protected static final String SHOW_SUCCESS ="-s"; 126 protected static final String LIST_ERRORS = "-l"; 127 protected static final int INC_WEBXML = 10; 128 protected static final int ALL_WEBXML = 20; 129 protected static final int DEFAULT_DIE_LEVEL = 1; 130 protected static final int NO_DIE_LEVEL = 0; 131 132 protected static final String [] insertBefore = 133 { "</web-app>", "<servlet-mapping>", "<session-config>", 134 "<mime-mapping>", "<welcome-file-list>", "<error-page>", "<taglib>", 135 "<resource-env-ref>", "<resource-ref>", "<security-constraint>", 136 "<login-config>", "<security-role>", "<env-entry>", "<ejb-ref>", 137 "<ejb-local-ref>" }; 138 139 protected static int die; 140 protected String classPath = null; 141 protected URLClassLoader loader = null; 142 protected boolean trimSpaces = false; 143 protected boolean genStringAsCharArray = false; 144 protected boolean xpoweredBy; 145 protected boolean mappedFile = false; 146 protected boolean poolingEnabled = true; 147 protected File scratchDir; 148 protected String ieClassId = DEFAULT_IE_CLASS_ID; 149 protected String targetPackage; 150 protected String targetClassName; 151 protected String uriBase; 152 protected String uriRoot; 153 protected Project project; 154 protected int dieLevel; 155 protected boolean helpNeeded = false; 156 protected boolean compile = false; 157 protected boolean smapSuppressed = true; 158 protected boolean smapDumped = false; 159 protected boolean caching = true; 160 protected Map cache = new HashMap (); 161 162 protected String compiler = null; 163 164 protected String compilerTargetVM = "1.4"; 165 protected String compilerSourceVM = "1.4"; 166 167 protected boolean classDebugInfo = true; 168 169 173 protected boolean failOnError = true; 174 175 179 protected List extensions; 180 181 184 protected List pages = new Vector (); 185 186 190 protected boolean errorOnUseBeanInvalidClassAttribute = true; 191 192 196 protected String javaEncoding = "UTF-8"; 197 198 protected String webxmlFile; 200 protected int webxmlLevel; 201 protected boolean addWebXmlMappings = false; 202 203 protected Writer mapout; 204 protected CharArrayWriter servletout; 205 protected CharArrayWriter mappingout; 206 207 210 protected JspCServletContext context; 211 212 216 protected JspRuntimeContext rctxt; 217 218 221 protected TldLocationsCache tldLocationsCache = null; 222 223 protected JspConfig jspConfig = null; 224 protected TagPluginManager tagPluginManager = null; 225 226 protected boolean verbose = false; 227 protected boolean listErrors = false; 228 protected boolean showSuccess = false; 229 protected int argPos; 230 protected boolean fullstop = false; 231 protected String args[]; 232 233 public static void main(String arg[]) { 234 if (arg.length == 0) { 235 System.out.println(Localizer.getMessage("jspc.usage")); 236 } else { 237 try { 238 JspC jspc = new JspC(); 239 jspc.setArgs(arg); 240 if (jspc.helpNeeded) { 241 System.out.println(Localizer.getMessage("jspc.usage")); 242 } else { 243 jspc.execute(); 244 } 245 } catch (JasperException je) { 246 System.err.println(je); 247 if (die != NO_DIE_LEVEL) { 248 System.exit(die); 249 } 250 } 251 } 252 } 253 254 public void setArgs(String [] arg) throws JasperException { 255 args = arg; 256 String tok; 257 258 dieLevel = NO_DIE_LEVEL; 259 die = dieLevel; 260 261 while ((tok = nextArg()) != null) { 262 if (tok.equals(SWITCH_VERBOSE)) { 263 verbose = true; 264 showSuccess = true; 265 listErrors = true; 266 } else if (tok.equals(SWITCH_OUTPUT_DIR)) { 267 tok = nextArg(); 268 setOutputDir( tok ); 269 } else if (tok.equals(SWITCH_PACKAGE_NAME)) { 270 targetPackage = nextArg(); 271 } else if (tok.equals(SWITCH_COMPILE)) { 272 compile=true; 273 } else if (tok.equals(SWITCH_CLASS_NAME)) { 274 targetClassName = nextArg(); 275 } else if (tok.equals(SWITCH_URI_BASE)) { 276 uriBase=nextArg(); 277 } else if (tok.equals(SWITCH_URI_ROOT)) { 278 setUriroot( nextArg()); 279 } else if (tok.equals(SWITCH_FILE_WEBAPP)) { 280 setUriroot( nextArg()); 281 } else if ( tok.equals( SHOW_SUCCESS ) ) { 282 showSuccess = true; 283 } else if ( tok.equals( LIST_ERRORS ) ) { 284 listErrors = true; 285 } else if (tok.equals(SWITCH_WEBAPP_INC)) { 286 webxmlFile = nextArg(); 287 if (webxmlFile != null) { 288 webxmlLevel = INC_WEBXML; 289 } 290 } else if (tok.equals(SWITCH_WEBAPP_XML)) { 291 webxmlFile = nextArg(); 292 if (webxmlFile != null) { 293 webxmlLevel = ALL_WEBXML; 294 } 295 } else if (tok.equals(SWITCH_MAPPED)) { 296 mappedFile = true; 297 } else if (tok.equals(SWITCH_XPOWERED_BY)) { 298 xpoweredBy = true; 299 } else if (tok.equals(SWITCH_TRIM_SPACES)) { 300 setTrimSpaces(true); 301 } else if (tok.equals(SWITCH_CACHE)) { 302 tok = nextArg(); 303 if ("false".equals(tok)) { 304 caching = false; 305 } else { 306 caching = true; 307 } 308 } else if (tok.equals(SWITCH_CLASSPATH)) { 309 setClassPath(nextArg()); 310 } else if (tok.startsWith(SWITCH_DIE)) { 311 try { 312 dieLevel = Integer.parseInt( 313 tok.substring(SWITCH_DIE.length())); 314 } catch (NumberFormatException nfe) { 315 dieLevel = DEFAULT_DIE_LEVEL; 316 } 317 die = dieLevel; 318 } else if (tok.equals(SWITCH_HELP)) { 319 helpNeeded = true; 320 } else if (tok.equals(SWITCH_POOLING)) { 321 tok = nextArg(); 322 if ("false".equals(tok)) { 323 poolingEnabled = false; 324 } else { 325 poolingEnabled = true; 326 } 327 } else if (tok.equals(SWITCH_ENCODING)) { 328 setJavaEncoding(nextArg()); 329 } else if (tok.equals(SWITCH_SOURCE)) { 330 setCompilerSourceVM(nextArg()); 331 } else if (tok.equals(SWITCH_TARGET)) { 332 setCompilerTargetVM(nextArg()); 333 } else if (tok.equals(SWITCH_SMAP)) { 334 smapSuppressed = false; 335 } else if (tok.equals(SWITCH_DUMP_SMAP)) { 336 smapDumped = true; 337 } else { 338 if (tok.startsWith("-")) { 339 throw new JasperException("Unrecognized option: " + tok + 340 ". Use -help for help."); 341 } 342 if (!fullstop) { 343 argPos--; 344 } 345 break; 347 } 348 } 349 350 while( true ) { 352 String file = nextFile(); 353 if( file==null ) { 354 break; 355 } 356 pages.add( file ); 357 } 358 } 359 360 public boolean getKeepGenerated() { 361 return true; 363 } 364 365 public boolean getTrimSpaces() { 366 return trimSpaces; 367 } 368 369 public void setTrimSpaces(boolean ts) { 370 this.trimSpaces = ts; 371 } 372 373 public boolean isPoolingEnabled() { 374 return poolingEnabled; 375 } 376 377 public void setPoolingEnabled(boolean poolingEnabled) { 378 this.poolingEnabled = poolingEnabled; 379 } 380 381 public boolean isXpoweredBy() { 382 return xpoweredBy; 383 } 384 385 public void setXpoweredBy(boolean xpoweredBy) { 386 this.xpoweredBy = xpoweredBy; 387 } 388 389 public boolean getDisplaySourceFragment() { 390 return true; 391 } 392 393 public boolean getErrorOnUseBeanInvalidClassAttribute() { 394 return errorOnUseBeanInvalidClassAttribute; 395 } 396 397 public void setErrorOnUseBeanInvalidClassAttribute(boolean b) { 398 errorOnUseBeanInvalidClassAttribute = b; 399 } 400 401 public int getTagPoolSize() { 402 return Constants.MAX_POOL_SIZE; 403 } 404 405 408 public boolean getMappedFile() { 409 return mappedFile; 410 } 411 412 public Object getProtectionDomain() { 414 return null; 415 } 416 417 public boolean getSendErrorToClient() { 418 return true; 420 } 421 422 public void setClassDebugInfo( boolean b ) { 423 classDebugInfo=b; 424 } 425 426 public boolean getClassDebugInfo() { 427 return classDebugInfo; 429 } 430 431 434 public boolean isCaching() { 435 return caching; 436 } 437 438 441 public void setCaching(boolean caching) { 442 this.caching = caching; 443 } 444 445 448 public Map getCache() { 449 return cache; 450 } 451 452 455 public int getCheckInterval() { 456 return 0; 457 } 458 459 462 public int getModificationTestInterval() { 463 return 0; 464 } 465 466 469 public boolean getDevelopment() { 470 return false; 471 } 472 473 476 public boolean isSmapSuppressed() { 477 return smapSuppressed; 478 } 479 480 483 public void setSmapSuppressed(boolean smapSuppressed) { 484 this.smapSuppressed = smapSuppressed; 485 } 486 487 488 491 public boolean isSmapDumped() { 492 return smapDumped; 493 } 494 495 498 public void setSmapDumped(boolean smapDumped) { 499 this.smapDumped = smapDumped; 500 } 501 502 503 510 public void setGenStringAsCharArray(boolean genStringAsCharArray) { 511 this.genStringAsCharArray = genStringAsCharArray; 512 } 513 514 520 public boolean genStringAsCharArray() { 521 return genStringAsCharArray; 522 } 523 524 530 public void setIeClassId(String ieClassId) { 531 this.ieClassId = ieClassId; 532 } 533 534 540 public String getIeClassId() { 541 return ieClassId; 542 } 543 544 public File getScratchDir() { 545 return scratchDir; 546 } 547 548 public Class getJspCompilerPlugin() { 549 return null; 551 } 552 553 public String getJspCompilerPath() { 554 return null; 556 } 557 558 561 public String getCompiler() { 562 return compiler; 563 } 564 565 public void setCompiler(String c) { 566 compiler=c; 567 } 568 569 572 public String getCompilerClassName() { 573 return null; 574 } 575 576 579 public String getCompilerTargetVM() { 580 return compilerTargetVM; 581 } 582 583 public void setCompilerTargetVM(String vm) { 584 compilerTargetVM = vm; 585 } 586 587 590 public String getCompilerSourceVM() { 591 return compilerSourceVM; 592 } 593 594 597 public void setCompilerSourceVM(String vm) { 598 compilerSourceVM = vm; 599 } 600 601 public TldLocationsCache getTldLocationsCache() { 602 return tldLocationsCache; 603 } 604 605 611 public String getJavaEncoding() { 612 return javaEncoding; 613 } 614 615 621 public void setJavaEncoding(String encodingName) { 622 javaEncoding = encodingName; 623 } 624 625 public boolean getFork() { 626 return false; 627 } 628 629 public String getClassPath() { 630 if( classPath != null ) 631 return classPath; 632 return System.getProperty("java.class.path"); 633 } 634 635 public void setClassPath(String s) { 636 classPath=s; 637 } 638 639 645 public List getExtensions() { 646 return extensions; 647 } 648 649 655 protected void addExtension(final String extension) { 656 if(extension != null) { 657 if(extensions == null) { 658 extensions = new Vector (); 659 } 660 661 extensions.add(extension); 662 } 663 } 664 665 670 public void setProject(final Project theProject) { 671 project = theProject; 672 } 673 674 680 public Project getProject() { 681 return project; 682 } 683 684 688 public void setUriroot( String s ) { 689 if( s==null ) { 690 uriRoot = s; 691 return; 692 } 693 try { 694 uriRoot = resolveFile(s).getCanonicalPath(); 695 } catch( Exception ex ) { 696 uriRoot = s; 697 } 698 } 699 700 709 public void setJspFiles(final String jspFiles) { 710 if(jspFiles == null) { 711 return; 712 } 713 714 StringTokenizer tok = new StringTokenizer (jspFiles, ","); 715 while (tok.hasMoreTokens()) { 716 pages.add(tok.nextToken()); 717 } 718 } 719 720 725 public void setCompile( final boolean b ) { 726 compile = b; 727 } 728 729 736 public void setVerbose( final int level ) { 737 if (level > 0) { 738 verbose = true; 739 showSuccess = true; 740 listErrors = true; 741 } 742 } 743 744 public void setValidateXml( boolean b ) { 745 org.apache.jasper.xmlparser.ParserUtils.validating=b; 746 } 747 748 public void setListErrors( boolean b ) { 749 listErrors = b; 750 } 751 752 public void setOutputDir( String s ) { 753 if( s!= null ) { 754 scratchDir = resolveFile(s).getAbsoluteFile(); 755 } else { 756 scratchDir=null; 757 } 758 } 759 760 public void setPackage( String p ) { 761 targetPackage=p; 762 } 763 764 769 public void setClassName( String p ) { 770 targetClassName=p; 771 } 772 773 776 public void setWebXmlFragment( String s ) { 777 webxmlFile=resolveFile(s).getAbsolutePath(); 778 webxmlLevel=INC_WEBXML; 779 } 780 781 784 public void setWebXml( String s ) { 785 webxmlFile=resolveFile(s).getAbsolutePath(); 786 webxmlLevel=ALL_WEBXML; 787 } 788 789 public void setAddWebXmlMappings(boolean b) { 790 addWebXmlMappings = b; 791 } 792 793 796 public void setFailOnError(final boolean b) { 797 failOnError = b; 798 } 799 800 public boolean getFailOnError() { 801 return failOnError; 802 } 803 804 807 public JspConfig getJspConfig() { 808 return jspConfig; 809 } 810 811 public TagPluginManager getTagPluginManager() { 812 return tagPluginManager; 813 } 814 815 public void generateWebMapping( String file, JspCompilationContext clctxt ) 816 throws IOException  817 { 818 String className = clctxt.getServletClassName(); 819 String packageName = clctxt.getServletPackageName(); 820 821 String thisServletName; 822 if ("".equals(packageName)) { 823 thisServletName = className; 824 } else { 825 thisServletName = packageName + '.' + className; 826 } 827 828 if (servletout != null) { 829 servletout.write("\n <servlet>\n <servlet-name>"); 830 servletout.write(thisServletName); 831 servletout.write("</servlet-name>\n <servlet-class>"); 832 servletout.write(thisServletName); 833 servletout.write("</servlet-class>\n </servlet>\n"); 834 } 835 if (mappingout != null) { 836 mappingout.write("\n <servlet-mapping>\n <servlet-name>"); 837 mappingout.write(thisServletName); 838 mappingout.write("</servlet-name>\n <url-pattern>"); 839 mappingout.write(file.replace('\\', '/')); 840 mappingout.write("</url-pattern>\n </servlet-mapping>\n"); 841 842 } 843 } 844 845 848 protected void mergeIntoWebXml() throws IOException { 849 850 File webappBase = new File (uriRoot); 851 File webXml = new File (webappBase, "WEB-INF/web.xml"); 852 File webXml2 = new File (webappBase, "WEB-INF/web2.xml"); 853 String insertStartMarker = 854 Localizer.getMessage("jspc.webinc.insertStart"); 855 String insertEndMarker = 856 Localizer.getMessage("jspc.webinc.insertEnd"); 857 858 BufferedReader reader = new BufferedReader (new FileReader (webXml)); 859 |