📄 jspparseeventlistener.java
字号:
/*
* $Header: /home/cvs/jakarta-tomcat/src/share/org/apache/jasper/compiler/JspParseEventListener.java,v 1.17.2.4 2001/03/09 23:35:25 marcsaeg Exp $
* $Revision: 1.17.2.4 $
* $Date: 2001/03/09 23:35:25 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.jasper.compiler;
import java.util.Hashtable;
import java.util.Stack;
import java.util.Vector;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.net.MalformedURLException;
import javax.servlet.jsp.tagext.TagInfo;
import javax.servlet.jsp.tagext.TagLibraryInfo;
import org.apache.jasper.JasperException;
import org.apache.jasper.Constants;
import org.apache.jasper.JspCompilationContext;
import org.apache.tomcat.logging.Logger;
/**
* JSP code generator "backend".
*
* @author Anil K. Vijendran
*/
public class JspParseEventListener extends BaseJspListener {
private static CommentGenerator commentGenerator = new JakartaCommentGenerator();
JspCompilationContext ctxt;
String jspServletBase = Constants.JSP_SERVLET_BASE;
String serviceMethodName = Constants.SERVICE_METHOD_NAME;
String servletContentType = Constants.SERVLET_CONTENT_TYPE;
String extendsClass = "";
Vector interfaces = new Vector();
Vector imports = new Vector();
String error = "";
boolean genSessionVariable = true;
boolean singleThreaded = false;
boolean autoFlush = true;
Vector generators = new Vector();
BeanRepository beanInfo;
int bufferSize = Constants.DEFAULT_BUFFER_SIZE;
// a set of boolean variables to check if there are multiple attr-val
// pairs for jsp directive.
boolean languageDir = false, extendsDir = false, sessionDir = false;
boolean bufferDir = false, threadsafeDir = false, errorpageDir = false;
boolean iserrorpageDir = false, infoDir = false, autoFlushDir = false;
boolean contentTypeDir = false;
/* support for large files */
int stringId = 0;
Vector vector = new Vector();
String dataFile;
TagLibraries libraries;
// Variables shared by all TagBeginGenerator and TagEndGenerator instances
// to keep track of nested tags and variable names
private Stack tagHandlerStack;
private Hashtable tagVarNumbers;
final void addGenerator(Generator gen) throws JasperException {
gen.init(ctxt);
generators.addElement(gen);
}
public static void setCommentGenerator(CommentGenerator generator) {
if ( null == commentGenerator) {
throw new IllegalArgumentException("null == generator");
}
commentGenerator = generator;
}
/*
* Package private since I want everyone to come in through
* org.apache.jasper.compiler.Main.
*/
JspParseEventListener(JspCompilationContext ctxt) {
super(ctxt.getReader(), ctxt.getWriter());
this.ctxt = ctxt;
this.beanInfo = new BeanRepository(ctxt.getClassLoader());
this.libraries = new TagLibraries(ctxt.getClassLoader());
// FIXME: Is this good enough? (I'm just taking the easy way out - akv)
if (ctxt.getOptions().getLargeFile())
dataFile = ctxt.getOutputDir() + File.separatorChar +
ctxt.getServletPackageName() + "_" +
ctxt.getServletClassName() + ".dat";
}
public void beginPageProcessing() throws JasperException {
for(int i = 0; i < Constants.STANDARD_IMPORTS.length; i++)
imports.addElement(Constants.STANDARD_IMPORTS[i]);
}
public void endPageProcessing() throws JasperException {
generateHeader();
writer.println();
generateAll(ServiceMethodPhase.class);
writer.println();
generateFooter();
if (ctxt.getOptions().getLargeFile())
try {
ObjectOutputStream o
= new ObjectOutputStream(new FileOutputStream(dataFile));
/*
* Serialize an array of char[]'s instead of an
* array of String's because there is a limitation
* on the size of Strings that can be serialized.
*/
char[][] tempCharArray = new char[vector.size()][];
vector.copyInto(tempCharArray);
o.writeObject(tempCharArray);
o.close();
writer.close();
} catch (IOException ex) {
throw new JasperException(Constants.getString(
"jsp.error.data.file.write"), ex);
}
ctxt.setContentType(servletContentType);
}
private Stack getTagHandlerStack() {
if (tagHandlerStack == null) {
tagHandlerStack = new Stack();
}
return tagHandlerStack;
}
private Hashtable getTagVarNumbers() {
if (tagVarNumbers == null) {
tagVarNumbers = new Hashtable();
}
return tagVarNumbers;
}
private void generateAll(Class phase) throws JasperException {
for(int i = 0; i < generators.size(); i++) {
Generator gen = (Generator) generators.elementAt(i);
if (phase.isInstance(gen)) {
gen.generate(writer, phase);
}
}
}
private void generateHeader() throws JasperException {
String servletPackageName = ctxt.getServletPackageName();
String servletClassName = ctxt.getServletClassName();
// First the package name:
if (! "".equals(servletPackageName) && servletPackageName != null) {
writer.println("package "+servletPackageName+";");
writer.println();
}
Enumeration e = imports.elements();
while (e.hasMoreElements())
writer.println("import "+(String) e.nextElement()+";");
writer.println();
generateAll(FileDeclarationPhase.class);
writer.println();
writer.print("public class "+servletClassName+ " extends ");
writer.print(extendsClass.equals("") ? jspServletBase : extendsClass);
if (singleThreaded)
interfaces.addElement("SingleThreadModel");
if (interfaces.size() != 0) {
writer.println();
writer.println(" implements ");
for(int i = 0; i < interfaces.size() - 1; i++)
writer.println(" "+interfaces.elementAt(i)+",");
writer.println(" "+interfaces.elementAt(interfaces.size()-1));
}
writer.println(" {");
writer.pushIndent();
writer.println();
generateAll(ClassDeclarationPhase.class);
writer.println();
writer.println("static {");
writer.pushIndent();
generateAll(StaticInitializerPhase.class);
writer.popIndent();
writer.println("}");
writer.println("public "+servletClassName+"( ) {");
writer.println("}");
writer.println();
writer.println("private static boolean _jspx_inited = false;");
writer.println();
writer.println("public final void _jspx_init() throws JasperException {");
writer.pushIndent();
generateAll(InitMethodPhase.class);
writer.popIndent();
writer.println("}");
writer.println();
writer.println("public void "+serviceMethodName+"("+
"HttpServletRequest request, "+
"HttpServletResponse response)");
writer.println(" throws IOException, ServletException {");
writer.pushIndent();
writer.println();
writer.println("JspFactory _jspxFactory = null;");
writer.println("PageContext pageContext = null;");
if (genSessionVariable)
writer.println("HttpSession session = null;");
if (ctxt.isErrorPage())
writer.println("Throwable exception = (Throwable) request.getAttribute(\"javax.servlet.jsp.jspException\");");
writer.println("ServletContext application = null;");
writer.println("ServletConfig config = null;");
writer.println("JspWriter out = null;");
writer.println("Object page = this;");
writer.println("String _value = null;");
writer.println("try {");
writer.pushIndent();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -