📄 jspc.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.jasper.compiler.Compiler;
import org.apache.jasper.compiler.JspConfig;
import org.apache.jasper.compiler.JspRuntimeContext;
import org.apache.jasper.compiler.Localizer;
import org.apache.jasper.compiler.TagPluginManager;
import org.apache.jasper.compiler.TldLocationsCache;
import org.apache.jasper.servlet.JspCServletContext;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.util.FileUtils;
/**
* Shell for the jspc compiler. Handles all options associated with the
* command line and creates compilation contexts which it then compiles
* according to the specified options.
*
* This version can process files from a _single_ webapp at once, i.e.
* a single docbase can be specified.
*
* It can be used as an Ant task using:
* <pre>
* <taskdef classname="org.apache.jasper.JspC" name="jasper2" >
* <classpath>
* <pathelement location="${java.home}/../lib/tools.jar"/>
* <fileset dir="${ENV.CATALINA_HOME}/server/lib">
* <include name="*.jar"/>
* </fileset>
* <fileset dir="${ENV.CATALINA_HOME}/common/lib">
* <include name="*.jar"/>
* </fileset>
* <path refid="myjars"/>
* </classpath>
* </taskdef>
*
* <jasper2 verbose="0"
* package="my.package"
* uriroot="${webapps.dir}/${webapp.name}"
* webXmlFragment="${build.dir}/generated_web.xml"
* outputDir="${webapp.dir}/${webapp.name}/WEB-INF/src/my/package" />
* </pre>
*
* @author Danno Ferrin
* @author Pierre Delisle
* @author Costin Manolache
* @author Yoav Shapira
*/
public class JspC implements Options {
public static final String DEFAULT_IE_CLASS_ID =
"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
// Logger
protected static Log log = LogFactory.getLog(JspC.class);
protected static final String SWITCH_VERBOSE = "-v";
protected static final String SWITCH_HELP = "-help";
protected static final String SWITCH_OUTPUT_DIR = "-d";
protected static final String SWITCH_PACKAGE_NAME = "-p";
protected static final String SWITCH_CACHE = "-cache";
protected static final String SWITCH_CLASS_NAME = "-c";
protected static final String SWITCH_FULL_STOP = "--";
protected static final String SWITCH_COMPILE = "-compile";
protected static final String SWITCH_SOURCE = "-source";
protected static final String SWITCH_TARGET = "-target";
protected static final String SWITCH_URI_BASE = "-uribase";
protected static final String SWITCH_URI_ROOT = "-uriroot";
protected static final String SWITCH_FILE_WEBAPP = "-webapp";
protected static final String SWITCH_WEBAPP_INC = "-webinc";
protected static final String SWITCH_WEBAPP_XML = "-webxml";
protected static final String SWITCH_MAPPED = "-mapped";
protected static final String SWITCH_XPOWERED_BY = "-xpoweredBy";
protected static final String SWITCH_TRIM_SPACES = "-trimSpaces";
protected static final String SWITCH_CLASSPATH = "-classpath";
protected static final String SWITCH_DIE = "-die";
protected static final String SWITCH_POOLING = "-poolingEnabled";
protected static final String SWITCH_ENCODING = "-javaEncoding";
protected static final String SWITCH_SMAP = "-smap";
protected static final String SWITCH_DUMP_SMAP = "-dumpsmap";
protected static final String SHOW_SUCCESS ="-s";
protected static final String LIST_ERRORS = "-l";
protected static final int INC_WEBXML = 10;
protected static final int ALL_WEBXML = 20;
protected static final int DEFAULT_DIE_LEVEL = 1;
protected static final int NO_DIE_LEVEL = 0;
protected static final String[] insertBefore =
{ "</web-app>", "<servlet-mapping>", "<session-config>",
"<mime-mapping>", "<welcome-file-list>", "<error-page>", "<taglib>",
"<resource-env-ref>", "<resource-ref>", "<security-constraint>",
"<login-config>", "<security-role>", "<env-entry>", "<ejb-ref>",
"<ejb-local-ref>" };
protected static int die;
protected String classPath = null;
protected URLClassLoader loader = null;
protected boolean trimSpaces = false;
protected boolean genStringAsCharArray = false;
protected boolean xpoweredBy;
protected boolean mappedFile = false;
protected boolean poolingEnabled = true;
protected File scratchDir;
protected String ieClassId = DEFAULT_IE_CLASS_ID;
protected String targetPackage;
protected String targetClassName;
protected String uriBase;
protected String uriRoot;
protected Project project;
protected int dieLevel;
protected boolean helpNeeded = false;
protected boolean compile = false;
protected boolean smapSuppressed = true;
protected boolean smapDumped = false;
protected boolean caching = true;
protected Map cache = new HashMap();
protected String compiler = null;
protected String compilerTargetVM = "1.4";
protected String compilerSourceVM = "1.4";
protected boolean classDebugInfo = true;
/**
* Throw an exception if there's a compilation error, or swallow it.
* Default is true to preserve old behavior.
*/
protected boolean failOnError = true;
/**
* The file extensions to be handled as JSP files.
* Default list is .jsp and .jspx.
*/
protected List extensions;
/**
* The pages.
*/
protected List pages = new Vector();
/**
* Needs better documentation, this data member does.
* True by default.
*/
protected boolean errorOnUseBeanInvalidClassAttribute = true;
/**
* The java file encoding. Default
* is UTF-8. Added per bugzilla 19622.
*/
protected String javaEncoding = "UTF-8";
// Generation of web.xml fragments
protected String webxmlFile;
protected int webxmlLevel;
protected boolean addWebXmlMappings = false;
protected Writer mapout;
protected CharArrayWriter servletout;
protected CharArrayWriter mappingout;
/**
* The servlet context.
*/
protected JspCServletContext context;
/**
* The runtime context.
* Maintain a dummy JspRuntimeContext for compiling tag files.
*/
protected JspRuntimeContext rctxt;
/**
* Cache for the TLD locations
*/
protected TldLocationsCache tldLocationsCache = null;
protected JspConfig jspConfig = null;
protected TagPluginManager tagPluginManager = null;
protected boolean verbose = false;
protected boolean listErrors = false;
protected boolean showSuccess = false;
protected int argPos;
protected boolean fullstop = false;
protected String args[];
public static void main(String arg[]) {
if (arg.length == 0) {
System.out.println(Localizer.getMessage("jspc.usage"));
} else {
try {
JspC jspc = new JspC();
jspc.setArgs(arg);
if (jspc.helpNeeded) {
System.out.println(Localizer.getMessage("jspc.usage"));
} else {
jspc.execute();
}
} catch (JasperException je) {
System.err.println(je);
if (die != NO_DIE_LEVEL) {
System.exit(die);
}
}
}
}
public void setArgs(String[] arg) throws JasperException {
args = arg;
String tok;
dieLevel = NO_DIE_LEVEL;
die = dieLevel;
while ((tok = nextArg()) != null) {
if (tok.equals(SWITCH_VERBOSE)) {
verbose = true;
showSuccess = true;
listErrors = true;
} else if (tok.equals(SWITCH_OUTPUT_DIR)) {
tok = nextArg();
setOutputDir( tok );
} else if (tok.equals(SWITCH_PACKAGE_NAME)) {
targetPackage = nextArg();
} else if (tok.equals(SWITCH_COMPILE)) {
compile=true;
} else if (tok.equals(SWITCH_CLASS_NAME)) {
targetClassName = nextArg();
} else if (tok.equals(SWITCH_URI_BASE)) {
uriBase=nextArg();
} else if (tok.equals(SWITCH_URI_ROOT)) {
setUriroot( nextArg());
} else if (tok.equals(SWITCH_FILE_WEBAPP)) {
setUriroot( nextArg());
} else if ( tok.equals( SHOW_SUCCESS ) ) {
showSuccess = true;
} else if ( tok.equals( LIST_ERRORS ) ) {
listErrors = true;
} else if (tok.equals(SWITCH_WEBAPP_INC)) {
webxmlFile = nextArg();
if (webxmlFile != null) {
webxmlLevel = INC_WEBXML;
}
} else if (tok.equals(SWITCH_WEBAPP_XML)) {
webxmlFile = nextArg();
if (webxmlFile != null) {
webxmlLevel = ALL_WEBXML;
}
} else if (tok.equals(SWITCH_MAPPED)) {
mappedFile = true;
} else if (tok.equals(SWITCH_XPOWERED_BY)) {
xpoweredBy = true;
} else if (tok.equals(SWITCH_TRIM_SPACES)) {
setTrimSpaces(true);
} else if (tok.equals(SWITCH_CACHE)) {
tok = nextArg();
if ("false".equals(tok)) {
caching = false;
} else {
caching = true;
}
} else if (tok.equals(SWITCH_CLASSPATH)) {
setClassPath(nextArg());
} else if (tok.startsWith(SWITCH_DIE)) {
try {
dieLevel = Integer.parseInt(
tok.substring(SWITCH_DIE.length()));
} catch (NumberFormatException nfe) {
dieLevel = DEFAULT_DIE_LEVEL;
}
die = dieLevel;
} else if (tok.equals(SWITCH_HELP)) {
helpNeeded = true;
} else if (tok.equals(SWITCH_POOLING)) {
tok = nextArg();
if ("false".equals(tok)) {
poolingEnabled = false;
} else {
poolingEnabled = true;
}
} else if (tok.equals(SWITCH_ENCODING)) {
setJavaEncoding(nextArg());
} else if (tok.equals(SWITCH_SOURCE)) {
setCompilerSourceVM(nextArg());
} else if (tok.equals(SWITCH_TARGET)) {
setCompilerTargetVM(nextArg());
} else if (tok.equals(SWITCH_SMAP)) {
smapSuppressed = false;
} else if (tok.equals(SWITCH_DUMP_SMAP)) {
smapDumped = true;
} else {
if (tok.startsWith("-")) {
throw new JasperException("Unrecognized option: " + tok +
". Use -help for help.");
}
if (!fullstop) {
argPos--;
}
// Start treating the rest as JSP Pages
break;
}
}
// Add all extra arguments to the list of files
while( true ) {
String file = nextFile();
if( file==null ) {
break;
}
pages.add( file );
}
}
public boolean getKeepGenerated() {
// isn't this why we are running jspc?
return true;
}
public boolean getTrimSpaces() {
return trimSpaces;
}
public void setTrimSpaces(boolean ts) {
this.trimSpaces = ts;
}
public boolean isPoolingEnabled() {
return poolingEnabled;
}
public void setPoolingEnabled(boolean poolingEnabled) {
this.poolingEnabled = poolingEnabled;
}
public boolean isXpoweredBy() {
return xpoweredBy;
}
public void setXpoweredBy(boolean xpoweredBy) {
this.xpoweredBy = xpoweredBy;
}
public boolean getDisplaySourceFragment() {
return true;
}
public boolean getErrorOnUseBeanInvalidClassAttribute() {
return errorOnUseBeanInvalidClassAttribute;
}
public void setErrorOnUseBeanInvalidClassAttribute(boolean b) {
errorOnUseBeanInvalidClassAttribute = b;
}
public int getTagPoolSize() {
return Constants.MAX_POOL_SIZE;
}
/**
* Are we supporting HTML mapped servlets?
*/
public boolean getMappedFile() {
return mappedFile;
}
// Off-line compiler, no need for security manager
public Object getProtectionDomain() {
return null;
}
public boolean getSendErrorToClient() {
// implied send to System.err
return true;
}
public void setClassDebugInfo( boolean b ) {
classDebugInfo=b;
}
public boolean getClassDebugInfo() {
// compile with debug info
return classDebugInfo;
}
/**
* @see Options#isCaching()
*/
public boolean isCaching() {
return caching;
}
/**
* @see Options#isCaching()
*/
public void setCaching(boolean caching) {
this.caching = caching;
}
/**
* @see Options#getCache()
*/
public Map getCache() {
return cache;
}
/**
* Background compilation check intervals in seconds
*/
public int getCheckInterval() {
return 0;
}
/**
* Modification test interval.
*/
public int getModificationTestInterval() {
return 0;
}
/**
* Is Jasper being used in development mode?
*/
public boolean getDevelopment() {
return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -