📄 generator.java
字号:
}
}
}
visitBody(n);
}
}
page.visit(new ScriptingVarVisitor());
}
/**
* Generates the _jspInit() method for instantiating the tag handler pools.
* For tag file, _jspInit has to be invoked manually, and the ServletConfig
* object explicitly passed.
*/
private void generateInit() {
if (ctxt.isTagFile()) {
out.printil("private void _jspInit(ServletConfig config) {");
} else {
out.printil("public void _jspInit() {");
}
out.pushIndent();
for (int i = 0; i < tagHandlerPoolNames.size(); i++) {
out.printin((String)tagHandlerPoolNames.elementAt(i));
out.print(
" = org.apache.jasper.runtime.TagHandlerPool.getTagHandlerPool(");
if (ctxt.isTagFile()) {
out.print("config");
} else {
out.print("getServletConfig()");
}
out.println(");");
}
out.popIndent();
out.printil("}");
out.println();
}
/**
* Generates the _jspDestroy() method which is responsible for calling the
* release() method on every tag handler in any of the tag handler pools.
*/
private void generateDestroy() {
out.printil("public void _jspDestroy() {");
out.pushIndent();
for (int i = 0; i < tagHandlerPoolNames.size(); i++) {
out.printin((String)tagHandlerPoolNames.elementAt(i));
out.println(".release();");
}
out.popIndent();
out.printil("}");
out.println();
}
/**
* Generate preamble package name
* (shared by servlet and tag handler preamble generation)
*/
private void genPreamblePackage(String packageName)
throws JasperException {
if (!"".equals(packageName) && packageName != null) {
out.printil("package " + packageName + ";");
out.println();
}
}
/**
* Generate preamble imports
* (shared by servlet and tag handler preamble generation)
*/
private void genPreambleImports() throws JasperException {
Iterator iter = pageInfo.getImports().iterator();
while (iter.hasNext()) {
out.printin("import ");
out.print((String)iter.next());
out.println(";");
}
out.println();
}
/**
* Generation of static initializers in preamble.
* For example, dependant list, el function map, prefix map.
* (shared by servlet and tag handler preamble generation)
*/
private void genPreambleStaticInitializers() throws JasperException {
// Static data for getDependants()
out.printil("private static java.util.List _jspx_dependants;");
out.println();
List dependants = pageInfo.getDependants();
Iterator iter = dependants.iterator();
if (!dependants.isEmpty()) {
out.printil("static {");
out.pushIndent();
out.printin("_jspx_dependants = new java.util.ArrayList(");
out.print("" + dependants.size());
out.println(");");
while (iter.hasNext()) {
out.printin("_jspx_dependants.add(\"");
out.print((String)iter.next());
out.println("\");");
}
out.popIndent();
out.printil("}");
out.println();
}
}
/**
* Declare tag handler pools (tags of the same type and with the same
* attribute set share the same tag handler pool)
* (shared by servlet and tag handler preamble generation)
*/
private void genPreambleClassVariableDeclarations(String className)
throws JasperException {
if (isPoolingEnabled && !tagHandlerPoolNames.isEmpty()) {
for (int i = 0; i < tagHandlerPoolNames.size(); i++) {
out.printil(
"private org.apache.jasper.runtime.TagHandlerPool "
+ tagHandlerPoolNames.elementAt(i)
+ ";");
}
out.println();
}
}
/**
* Declare general-purpose methods
* (shared by servlet and tag handler preamble generation)
*/
private void genPreambleMethods() throws JasperException {
// Method used to get compile time file dependencies
out.printil("public Object getDependants() {");
out.pushIndent();
out.printil("return _jspx_dependants;");
out.popIndent();
out.printil("}");
out.println();
if (isPoolingEnabled && !tagHandlerPoolNames.isEmpty()) {
generateInit();
generateDestroy();
}
}
/**
* Generates the beginning of the static portion of the servlet.
*/
private void generatePreamble(Node.Nodes page) throws JasperException {
String servletPackageName = ctxt.getServletPackageName();
String servletClassName = ctxt.getServletClassName();
String serviceMethodName = Constants.SERVICE_METHOD_NAME;
// First the package name:
genPreamblePackage(servletPackageName);
// Generate imports
genPreambleImports();
// Generate class declaration
out.printin("public final class ");
out.print(servletClassName);
out.print(" extends ");
out.println(pageInfo.getExtends());
out.printin(
" implements org.apache.jasper.runtime.JspSourceDependent");
if (!pageInfo.isThreadSafe()) {
out.println(",");
out.printin(" SingleThreadModel");
}
out.println(" {");
out.pushIndent();
// Class body begins here
generateDeclarations(page);
// Static initializations here
genPreambleStaticInitializers();
// Class variable declarations
genPreambleClassVariableDeclarations(servletClassName);
// Constructor
// generateConstructor(className);
// Methods here
genPreambleMethods();
// Now the service method
out.printin("public void ");
out.print(serviceMethodName);
out.println(
"(HttpServletRequest request, HttpServletResponse response)");
out.println(" throws java.io.IOException, ServletException {");
out.pushIndent();
out.println();
// Local variable declarations
out.printil("JspFactory _jspxFactory = null;");
out.printil("PageContext pageContext = null;");
if (pageInfo.isSession())
out.printil("HttpSession session = null;");
if (pageInfo.isErrorPage()) {
out.printil(
"Throwable exception = org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);");
out.printil("if (exception != null) {");
out.pushIndent();
out.printil(
"response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);");
out.popIndent();
out.printil("}");
}
out.printil("ServletContext application = null;");
out.printil("ServletConfig config = null;");
out.printil("JspWriter out = null;");
out.printil("Object page = this;");
out.printil("JspWriter _jspx_out = null;");
out.printil("PageContext _jspx_page_context = null;");
out.println();
declareTemporaryScriptingVars(page);
out.println();
out.printil("try {");
out.pushIndent();
out.printil("_jspxFactory = JspFactory.getDefaultFactory();");
out.printin("response.setContentType(");
out.print(quote(pageInfo.getContentType()));
out.println(");");
if (ctxt.getOptions().isXpoweredBy()) {
out.printil("response.addHeader(\"X-Powered-By\", \"JSP/2.0\");");
}
out.printil(
"pageContext = _jspxFactory.getPageContext(this, request, response,");
out.printin("\t\t\t");
out.print(quote(pageInfo.getErrorPage()));
out.print(", " + pageInfo.isSession());
out.print(", " + pageInfo.getBuffer());
out.print(", " + pageInfo.isAutoFlush());
out.println(");");
out.printil("_jspx_page_context = pageContext;");
out.printil("application = pageContext.getServletContext();");
out.printil("config = pageContext.getServletConfig();");
if (pageInfo.isSession())
out.printil("session = pageContext.getSession();");
out.printil("out = pageContext.getOut();");
out.printil("_jspx_out = out;");
out.println();
}
/**
* Generates an XML Prolog, which includes an XML declaration and
* an XML doctype declaration.
*/
private void generateXmlProlog(Node.Nodes page) {
/*
* An XML declaration is generated under the following conditions:
*
* - 'omit-xml-declaration' attribute of <jsp:output> action is set to
* "no" or "false"
* - JSP document without a <jsp:root>
*/
String omitXmlDecl = pageInfo.getOmitXmlDecl();
if ((omitXmlDecl != null && !JspUtil.booleanValue(omitXmlDecl))
|| (omitXmlDecl == null
&& page.getRoot().isXmlSyntax()
&& !pageInfo.hasJspRoot()
&& !ctxt.isTagFile())) {
String cType = pageInfo.getContentType();
String charSet = cType.substring(cType.indexOf("charset=") + 8);
out.printil(
"out.write(\"<?xml version=\\\"1.0\\\" encoding=\\\""
+ charSet
+ "\\\"?>\\n\");");
}
/*
* Output a DOCTYPE declaration if the doctype-root-element appears.
* If doctype-public appears:
* <!DOCTYPE name PUBLIC "doctypePublic" "doctypeSystem">
* else
* <!DOCTYPE name SYSTEM "doctypeSystem" >
*/
String doctypeName = pageInfo.getDoctypeName();
if (doctypeName != null) {
String doctypePublic = pageInfo.getDoctypePublic();
String doctypeSystem = pageInfo.getDoctypeSystem();
out.printin("out.write(\"<!DOCTYPE ");
out.print(doctypeName);
if (doctypePublic == null) {
out.print(" SYSTEM \\\"");
} else {
out.print(" PUBLIC \\\"");
out.print(doctypePublic);
out.print("\\\" \\\"");
}
out.print(doctypeSystem);
out.println("\\\">\\n\");");
}
}
/*
* Generates the constructor.
* (shared by servlet and tag handler preamble generation)
*/
private void generateConstructor(String className) {
out.printil("public " + className + "() {");
out.printil("}");
out.println();
}
/**
* A visitor that generates codes for the elements in the page.
*/
class GenerateVisitor extends Node.Visitor {
/*
* Hashtable containing introspection information on tag handlers:
* <key>: tag prefix
* <value>: hashtable containing introspection on tag handlers:
* <key>: tag short name
* <value>: introspection info of tag handler for
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -