You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ki...@apache.org on 2002/06/06 00:01:34 UTC
cvs commit: jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler Collector.java Compiler.java Generator.java Node.java PageInfo.java Validator.java
kinman 2002/06/05 15:01:34
Modified: jasper2/src/share/org/apache/jasper/compiler Compiler.java
Generator.java Node.java PageInfo.java
Validator.java
Added: jasper2/src/share/org/apache/jasper/compiler Collector.java
Log:
- Generate code for a tag to a out-of-line method if it does not contain
any scripting elements or scripting variables.
- Add Collector to collect info on a page.
Revision Changes Path
1.12 +6 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java
Index: Compiler.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- Compiler.java 2 Jun 2002 21:36:45 -0000 1.11
+++ Compiler.java 5 Jun 2002 22:01:33 -0000 1.12
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v 1.11 2002/06/02 21:36:45 remm Exp $
- * $Revision: 1.11 $
- * $Date: 2002/06/02 21:36:45 $
+ * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v 1.12 2002/06/05 22:01:33 kinman Exp $
+ * $Revision: 1.12 $
+ * $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
@@ -193,6 +193,9 @@
// Dump out the page (for debugging)
// Dumper.dump(pageNodes);
+
+ // Collect page info
+ Collector.collect(this, pageNodes);
// generate servlet .java file
Generator.generate(writer, this, pageNodes);
1.17 +186 -62 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java
Index: Generator.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- Generator.java 24 May 2002 23:57:42 -0000 1.16
+++ Generator.java 5 Jun 2002 22:01:33 -0000 1.17
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v 1.16 2002/05/24 23:57:42 kinman Exp $
- * $Revision: 1.16 $
- * $Date: 2002/05/24 23:57:42 $
+ * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v 1.17 2002/06/05 22:01:33 kinman Exp $
+ * $Revision: 1.17 $
+ * $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
@@ -89,6 +89,7 @@
public class Generator {
private ServletWriter out;
+ private MethodsBuffer methodsBuffer;
private ErrorDispatcher err;
private BeanRepository beanInfo;
private JspCompilationContext ctxt;
@@ -192,10 +193,10 @@
out.print("implements SingleThreadModel");
}
out.println(" {");
+ out.pushIndent();
// Class body begins here
- out.pushIndent();
generateDeclarations(page);
out.println();
@@ -227,14 +228,6 @@
// Class fields declarations
- maxTagNesting = pageInfo.getMaxTagNesting();
- if (maxTagNesting >= 0) {
- out.printil("private static final int RELEASE_ACTION = 0;");
- out.printil("private static final int POP_AND_RELEASE_ACTION = 1;");
- out.println();
- out.println();
- }
-
// Constructor (empty so far) here
// Methods here
@@ -271,17 +264,11 @@
out.printil("JspWriter out = null;");
out.printil("Object page = this;");
- // pseudo "Finally" state stack objects
- if (maxTagNesting >= 0) {
- String depth = Integer.toString(maxTagNesting + 1);
- out.printil("int tagStackIndex = -1;");
- out.printin("int[] tagStackActions = new int[");
- out.print(depth);
- out.println("];");
- out.printin("javax.servlet.jsp.tagext.Tag[] tagStack = new javax.servlet.jsp.tagext.Tag[");
- out.print(depth);
- out.println("];");
- out.println();
+ // Number of tag object that need to be popped
+ // XXX TODO: use a better criteria
+ maxTagNesting = pageInfo.getMaxTagNesting();
+ if (maxTagNesting > 0) {
+ out.printil("JspxState _jspxState = new JspxState();");
}
out.printil("try {");
@@ -312,6 +299,25 @@
}
/**
+ * Generate codes defining the classes used in the servlet.
+ * 1. Servlet state object, used to pass servlet info round methods.
+ */
+ private void generateJspState() {
+ out.println();
+ out.printil("static final class JspxState {");
+ out.pushIndent();
+ out.printil("public int tagCount;");
+ out.println();
+ out.printil("public JspxState() {");
+ out.pushIndent();
+ out.printil("tagCount = 0;");
+ out.popIndent();
+ out.printil("}");
+ out.popIndent();
+ out.printil("}");
+ }
+
+ /**
* A visitor that generates codes for the elements in the page.
*/
class GenerateVisitor extends Node.Visitor {
@@ -329,10 +335,15 @@
private Hashtable tagVarNumbers;
private String parent;
+ private ServletWriter out;
+ private MethodsBuffer methodsBuffer;
+
/**
* Constructor.
*/
- public GenerateVisitor() {
+ public GenerateVisitor(ServletWriter out, MethodsBuffer methodsBuffer) {
+ this.out = out;
+ this.methodsBuffer = methodsBuffer;
handlerInfos = new Hashtable();
tagVarNumbers = new Hashtable();
}
@@ -347,6 +358,9 @@
*/
private String attributeValue(Node.JspAttribute attr, boolean encode) {
String v = attr.getValue();
+ if (v == null)
+ return "";
+
if (attr.isExpression()) {
if (encode) {
return "java.net.URLEncoder.encode(" + v + ")";
@@ -700,18 +714,22 @@
// First compose the runtime output string
String s0 = "<OBJECT classid=\"" + ctxt.getOptions().getIeClassId()+
"\"" + makeAttr("name", name);
- String s1, s2;
- if (width.isExpression()) {
- s1 = quote(s0 + " width=\"") + " + " + width.getValue() +
+ String s1="", s2="";
+ if (width != null) {
+ if (width.isExpression()) {
+ s1 = quote(s0 + " width=\"") + " + " + width.getValue() +
" + " + quote("\"");
- } else {
- s1 = quote(s0 + makeAttr("width", width.getValue()));
+ } else {
+ s1 = quote(s0 + makeAttr("width", width.getValue()));
+ }
}
- if (height.isExpression()) {
- s2 = quote(" height=\"") + " + " + height.getValue() +
+ if (height != null) {
+ if (height.isExpression()) {
+ s2 = quote(" height=\"") + " + " + height.getValue() +
" + " + quote("\"");
- } else {
- s2 = quote(makeAttr("height", height.getValue()));
+ } else {
+ s2 = quote(makeAttr("height", height.getValue()));
+ }
}
String s3 = quote(makeAttr("hspace", hspace) +
makeAttr("vspace", vspace) +
@@ -762,18 +780,28 @@
makeAttr("type", "application/x-java-" + type + ";" +
((jreversion==null)? "": "version=" + jreversion)) +
makeAttr("name", name);
- if (width.isExpression()) {
- s1 = quote(s0 + " width=\"") + " + " + width.getValue() +
+
+ if (width != null) {
+ if (width.isExpression()) {
+ s1 = quote(s0 + " width=\"") + " + " + width.getValue() +
" + " + quote("\"");
+ } else {
+ s1 = quote(s0 + makeAttr("width", width.getValue()));
+ }
} else {
- s1 = quote(s0 + makeAttr("width", width.getValue()));
+ s1 = quote(s0);
}
- if (height.isExpression()) {
- s2 = quote(" height=\"") + " + " + height.getValue() +
- " + " + quote("\"");
+ if (height != null) {
+ if (height.isExpression()) {
+ s2 = quote(" height=\"") + " + " + height.getValue() +
+ " + " + quote("\"");
+ } else {
+ s2 = quote(makeAttr("height", height.getValue()));
+ }
} else {
- s2 = quote(makeAttr("height", height.getValue()));
+ s2 = "";
}
+
s3 = quote(makeAttr("hspace", hspace) +
makeAttr("vspace", vspace) +
makeAttr("align", align) +
@@ -844,16 +872,93 @@
String tagEvalVar = "_jspx_eval_" + baseVar;
String tagHandlerVar = "_jspx_th_" + baseVar;
+ // If the tag contains no scripting element, generate its codes
+ // to a method.
+ ServletWriter outSave = null;
+ MethodsBuffer methodsBufferSave = null;
+ if (n.isScriptless() && varInfos == null &&
+ (tagVarInfos == null || tagVarInfos.length == 0)) {
+ // The tag handler and its body code can reside in a separate
+ // method if it is scriptless and does not have any scripting
+ // variable defined.
+ // For some reason, varInfos is null when var is not defined
+ // in TEI, but tagVarInfos is empty array when var is not
+ // defined in tld.
+
+ String tagMethod = "_jspx_meth_" + baseVar;
+
+ // Generate a call to this method
+ out.printin(tagMethod);
+ out.print("(");
+ if (parent != null) {
+ out.print(parent);
+ out.print(", ");
+ }
+ out.println("pageContext, _jspxState);");
+
+ // Set up new buffer for the method
+ outSave = out;
+ out = methodsBuffer.getOut();
+ methodsBufferSave = methodsBuffer;
+ methodsBuffer = new MethodsBuffer();
+
+ // Generate code for method declaration
+ out.println();
+ out.pushIndent();
+ out.printin("private void ");
+ out.print(tagMethod);
+ out.print("(");
+ if (parent != null) {
+ out.print("javax.servlet.jsp.tagext.Tag ");
+ out.print(parent);
+ out.print(", ");
+ }
+ out.println("PageContext pageContext, JspxState _jspxState)");
+ out.printil(" throws java.io.IOException, javax.servlet.jsp.JspException {");
+ out.pushIndent();
+
+ // Initilaize local variables used in this method.
+ out.printil("JspWriter out = pageContext.getOut();");
+ if (n.isHasUsebean()) {
+ out.println("HttpSession session = pageContext.getSession();");
+ out.println("ServletContext application = pageContext.getServletContext();");
+ }
+ if (n.isHasUsebean() || n.isHasIncludeAction() || n.isHasSetProperty()) {
+ out.println("HttpServletRequest request = pageContext.getRequest();");
+ }
+ if (n.isHasIncludeAction()) {
+ out.println("ServletResponse response = pageContext.getResponse();");
+ }
+ }
+
// Generate code for start tag, body, and end tag
generateCustomStart(n, varInfos, tagVarInfos, handlerInfo,
tagHandlerVar, tagEvalVar);
+
String tmpParent = parent;
parent = tagHandlerVar;
visitBody(n);
+
parent = tmpParent;
generateCustomEnd(n, varInfos, tagVarInfos,
handlerInfo.getTagHandlerClass(), tagHandlerVar,
tagEvalVar);
+
+ if (n.isScriptless() && varInfos == null &&
+ (tagVarInfos == null || tagVarInfos.length == 0)) {
+ // Generate end of method
+ out.popIndent();
+ out.printil("}");
+ out.popIndent();
+
+ // Append any methods that got generated in the body to the
+ // current buffer
+ out.print(methodsBuffer.toString());
+
+ // restore previous buffer
+ methodsBuffer = methodsBufferSave;
+ out = outSave;
+ }
}
private static final String SINGLE_QUOTE = "'";
@@ -984,11 +1089,6 @@
if (implementsTryCatchFinally) {
out.printil("try {");
out.pushIndent();
- } else {
- out.printil("tagStackActions[++tagStackIndex] = RELEASE_ACTION;");
- out.printin("tagStack[tagStackIndex] = ");
- out.print(tagHandlerVar);
- out.println(";");
}
out.printin("int ");
out.print(tagEvalVar);
@@ -1018,8 +1118,7 @@
out.printil("out = pageContext.pushBody();");
if (!implementsTryCatchFinally) {
- out.printil("tagStackActions[tagStackIndex]" +
- " = POP_AND_RELEASE_ACTION;");
+ out.printil("_jspxState.tagCount++;");
}
out.printin(tagHandlerVar);
out.println(".setBodyContent((javax.servlet.jsp.tagext.BodyContent) out);");
@@ -1078,7 +1177,7 @@
out.println(" != javax.servlet.jsp.tagext.Tag.EVAL_BODY_INCLUDE)");
out.pushIndent();
if (!implementsTryCatchFinally) {
- out.printil("tagStackActions[tagStackIndex] = RELEASE_ACTION;");
+ out.printil("_jspxState.tagCount--;");
}
out.printil("out = pageContext.popBody();");
out.popIndent();
@@ -1112,7 +1211,6 @@
out.popIndent();
out.printil("}");
} else {
- out.printil("tagStackIndex--;");
out.printin(tagHandlerVar);
out.println(".release();");
}
@@ -1157,13 +1255,13 @@
if (declare && tagVarInfos[i].getDeclare()) {
out.printin(tagVarInfos[i].getClassName() + " ");
}
+ out.printin(name);
+ out.print(" = (");
+ out.print(tagVarInfos[i].getClassName());
+ out.print(") pageContext.findAttribute(");
+ out.print(quote(name));
+ out.println(");");
}
- out.printin(name);
- out.print(" = (");
- out.print(tagVarInfos[i].getClassName());
- out.print(") pageContext.findAttribute(");
- out.print(quote(name));
- out.println(");");
}
}
}
@@ -1342,25 +1440,23 @@
out.popIndent();
out.printil("} catch (Throwable t) {");
out.pushIndent();
+/*
out.printil("if (out != null && out.getBufferSize() != 0)");
out.pushIndent();
out.printil("out.clearBuffer();");
out.popIndent();
+*/
out.printil("if (pageContext != null) pageContext.handlePageException(t);");
out.popIndent();
out.printil("} finally {");
out.pushIndent();
// Cleanup the tags on the stack
- if (maxTagNesting >= 0) {
- out.printil("while (tagStackIndex >= 0) {");
- out.pushIndent();
- out.printil("if (POP_AND_RELEASE_ACTION == tagStackActions[tagStackIndex])");
+ if (maxTagNesting > 0) {
+ out.printil("while (_jspxState.tagCount-- > 0) {");
out.pushIndent();
out.printil("out = pageContext.popBody();");
out.popIndent();
- out.printil("tagStack[tagStackIndex--].release();");
- out.popIndent();
out.printil("}");
}
@@ -1373,6 +1469,14 @@
out.popIndent();
out.printil("}");
+ // Append any methods that were generated
+ out.print(methodsBuffer.toString());
+
+ // generate class definition for JspxState
+ if (maxTagNesting > 0) {
+ generateJspState();
+ }
+
// Close the class definition
out.popIndent();
out.printil("}");
@@ -1383,6 +1487,7 @@
*/
Generator(ServletWriter out, Compiler compiler) {
this.out = out;
+ methodsBuffer = new MethodsBuffer();
err = compiler.getErrorDispatcher();
ctxt = compiler.getCompilationContext();
pageInfo = compiler.getPageInfo();
@@ -1401,7 +1506,7 @@
Generator gen = new Generator(out, compiler);
gen.generatePreamble(page);
- page.visit(gen.new GenerateVisitor());
+ page.visit(gen.new GenerateVisitor(out, gen.methodsBuffer));
gen.generatePostamble(page);
}
@@ -1480,6 +1585,25 @@
*/
public Class getTagHandlerClass() {
return tagHandlerClass;
+ }
+ }
+
+ private static class MethodsBuffer {
+
+ private java.io.CharArrayWriter charWriter;
+ private ServletWriter out;
+
+ MethodsBuffer() {
+ charWriter = new java.io.CharArrayWriter();
+ out = new ServletWriter(new java.io.PrintWriter(charWriter));
+ }
+
+ public ServletWriter getOut() {
+ return out;
+ }
+
+ public String toString() {
+ return charWriter.toString();
}
}
}
1.10 +40 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java
Index: Node.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- Node.java 23 May 2002 21:29:38 -0000 1.9
+++ Node.java 5 Jun 2002 22:01:33 -0000 1.10
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v 1.9 2002/05/23 21:29:38 kinman Exp $
- * $Revision: 1.9 $
- * $Date: 2002/05/23 21:29:38 $
+ * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v 1.10 2002/06/05 22:01:33 kinman Exp $
+ * $Revision: 1.10 $
+ * $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
@@ -664,6 +664,11 @@
private String shortName;
private JspAttribute[] jspAttrs;
private TagData tagData;
+ private boolean scriptless; // true if the tag and its body
+ // contians no scripting elements.
+ private boolean hasUsebean;
+ private boolean hasIncludeAction;
+ private boolean hasSetProperty;
public CustomTag(Attributes attrs, Mark start, String name,
String prefix, String shortName, Node parent) {
@@ -712,6 +717,38 @@
public TagData getTagData() {
return tagData;
+ }
+
+ public void setScriptless(boolean s) {
+ scriptless = s;
+ }
+
+ public boolean isScriptless() {
+ return scriptless;
+ }
+
+ public void setHasUsebean(boolean u) {
+ hasUsebean = u;
+ }
+
+ public boolean isHasUsebean() {
+ return hasUsebean;
+ }
+
+ public void setHasIncludeAction(boolean i) {
+ hasIncludeAction = i;
+ }
+
+ public boolean isHasIncludeAction() {
+ return hasIncludeAction;
+ }
+
+ public void setHasSetProperty(boolean s) {
+ hasSetProperty = s;
+ }
+
+ public boolean isHasSetProperty() {
+ return hasSetProperty;
}
}
1.5 +13 -3 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java
Index: PageInfo.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- PageInfo.java 18 May 2002 00:29:24 -0000 1.4
+++ PageInfo.java 5 Jun 2002 22:01:33 -0000 1.5
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java,v 1.4 2002/05/18 00:29:24 kinman Exp $
- * $Revision: 1.4 $
- * $Date: 2002/05/18 00:29:24 $
+ * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/PageInfo.java,v 1.5 2002/06/05 22:01:33 kinman Exp $
+ * $Revision: 1.5 $
+ * $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
@@ -89,6 +89,7 @@
private String errorPage = null;
private String pageEncoding = null;
private int maxTagNesting = 0;
+ private boolean scriptless = false;
PageInfo(BeanRepository beanRepository) {
this.beanRepository = beanRepository;
@@ -212,4 +213,13 @@
public void setMaxTagNesting(int maxTagNesting) {
this.maxTagNesting = maxTagNesting;
}
+
+ public void setScriptless(boolean s) {
+ scriptless = s;
+ }
+
+ public boolean isScriptless() {
+ return scriptless;
+ }
+
}
1.9 +3 -33 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Validator.java
Index: Validator.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Validator.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Validator.java 23 May 2002 21:29:38 -0000 1.8
+++ Validator.java 5 Jun 2002 22:01:33 -0000 1.9
@@ -1,7 +1,7 @@
/*
- * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Validator.java,v 1.8 2002/05/23 21:29:38 kinman Exp $
- * $Revision: 1.8 $
- * $Date: 2002/05/23 21:29:38 $
+ * $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Validator.java,v 1.9 2002/06/05 22:01:33 kinman Exp $
+ * $Revision: 1.9 $
+ * $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
@@ -571,30 +571,6 @@
}
}
- /**
- * A visitor for collection info on the page
- */
- static class CollectVisitor extends Node.Visitor {
-
- private int maxTagNesting = 0;
- private int curTagNesting = 0;
-
- public void visit(Node.CustomTag n) throws JasperException {
-
- if (curTagNesting > maxTagNesting) {
- maxTagNesting = curTagNesting;
- }
-
- curTagNesting++;
- visitBody(n);
- curTagNesting--;
- }
-
- public void updatePageInfo(PageInfo pageInfo) {
- pageInfo.setMaxTagNesting(maxTagNesting);
- }
- }
-
public static void validate(Compiler compiler,
Node.Nodes page) throws JasperException {
@@ -636,12 +612,6 @@
*/
page.visit(new TagExtraInfoVisitor(compiler));
- /*
- * Collect information about the page and update pageInfo object.
- */
- CollectVisitor collectVisitor = new CollectVisitor();
- page.visit(collectVisitor);
- collectVisitor.updatePageInfo(compiler.getPageInfo());
}
1.1 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Collector.java
Index: Collector.java
===================================================================
/*
* $Header: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/
compiler/Generator.java,v 1.16 2002/05/24 23:57:42 kinman Exp $
* $Revision: 1.1 $
* $Date: 2002/06/05 22:01:33 $
*
* ====================================================================
*
* 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 org.apache.jasper.JasperException;
/**
* Collect info about the page and nodes, and make them availabe through
* the PageInfo object.
*
* @author Kin-man Chung
*/
public class Collector {
/**
* A visitor for collection info on the page
* Info collected so far:
* Maximum tag nestings.
* Whether a page or a tag element (and its body) contains any scripting
* elements.
*/
static class CollectVisitor extends Node.Visitor {
private int maxTagNesting = 0;
private int curTagNesting = 0;
private boolean scriptingElementSeen = false;
private boolean usebeanSeen = false;
private boolean includeActionSeen = false;
private boolean setPropertySeen = false;
public void visit(Node.ParamAction n) throws JasperException {
if (n.getValue().isExpression()) {
scriptingElementSeen = true;
}
}
public void visit(Node.IncludeAction n) throws JasperException {
if (n.getPage().isExpression()) {
scriptingElementSeen = true;
}
includeActionSeen = true;
visitBody(n);
}
public void visit(Node.ForwardAction n) throws JasperException {
if (n.getPage().isExpression()) {
scriptingElementSeen = true;
}
visitBody(n);
}
public void visit(Node.SetProperty n) throws JasperException {
if (n.getValue() != null && n.getValue().isExpression()) {
scriptingElementSeen = true;
}
setPropertySeen = true;
}
public void visit(Node.UseBean n) throws JasperException {
if (n.getBeanName() != null && n.getBeanName().isExpression()) {
scriptingElementSeen = true;
}
usebeanSeen = true;
visitBody(n);
}
public void visit(Node.PlugIn n) throws JasperException {
if (n.getHeight() != null && n.getHeight().isExpression()) {
scriptingElementSeen = true;
}
if (n.getWidth() != null && n.getWidth().isExpression()) {
scriptingElementSeen = true;
}
visitBody(n);
}
public void visit(Node.CustomTag n) throws JasperException {
curTagNesting++;
if (curTagNesting > maxTagNesting) {
maxTagNesting = curTagNesting;
}
// save values collected so far
boolean scriptingElementSeenSave = scriptingElementSeen;
scriptingElementSeen = false;
boolean usebeanSeenSave = usebeanSeen;
usebeanSeen = false;
boolean includeActionSeenSave = includeActionSeen;
includeActionSeen = false;
boolean setPropertySeenSave = setPropertySeen;
setPropertySeen = false;
// Scan attribute list for expressions
Node.JspAttribute[] attrs = n.getJspAttributes();
for (int i = 0; i < attrs.length; i++) {
if (attrs[i].isExpression()) {
scriptingElementSeen = true;
break;
}
}
visitBody(n);
// Record if the tag element and its body contains any scriptlet.
n.setScriptless(! scriptingElementSeen);
n.setHasUsebean(usebeanSeen);
n.setHasIncludeAction(includeActionSeen);
n.setHasSetProperty(setPropertySeen);
// Propagate value of scriptingElementSeen up.
scriptingElementSeen = scriptingElementSeen || scriptingElementSeenSave;
usebeanSeen = usebeanSeen || usebeanSeenSave;
setPropertySeen = setPropertySeen || setPropertySeenSave;
includeActionSeen = includeActionSeen || includeActionSeenSave;
curTagNesting--;
}
public void visit(Node.Declaration n) throws JasperException {
scriptingElementSeen = true;
}
public void visit(Node.Expression n) throws JasperException {
scriptingElementSeen = true;
}
public void visit(Node.Scriptlet n) throws JasperException {
scriptingElementSeen = true;
}
public void updatePageInfo(PageInfo pageInfo) {
pageInfo.setMaxTagNesting(maxTagNesting);
pageInfo.setScriptless(! scriptingElementSeen);
}
}
public static void collect(Compiler compiler, Node.Nodes page)
throws JasperException {
CollectVisitor collectVisitor = new CollectVisitor();
page.visit(collectVisitor);
collectVisitor.updatePageInfo(compiler.getPageInfo());
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>