001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.rimfaxe.webserver.compiler.jsp;
057:
058: import com.rimfaxe.webserver.compiler.JspToJavaException;
059:
060: /**
061: * Collect info about the page and nodes, and make them availabe through
062: * the PageInfo object.
063: *
064: * @author Kin-man Chung
065: * @author Lars Andersen
066: */
067:
068: public class Collector {
069:
070: /**
071: * A visitor for collection info on the page
072: * Info collected so far:
073: * Maximum tag nestings.
074: * Whether a page or a tag element (and its body) contains any scripting
075: * elements.
076: */
077: static class CollectVisitor extends Node.Visitor {
078:
079: private int maxTagNesting = 0;
080: private int curTagNesting = 0;
081: private boolean scriptingElementSeen = false;
082: private boolean usebeanSeen = false;
083: private boolean includeActionSeen = false;
084: private boolean setPropertySeen = false;
085: private boolean hasScriptingVars = false;
086:
087: public void visit(Node.ParamAction n) throws JasperException,
088: JspToJavaException {
089: if (n.getValue().isExpression()) {
090: scriptingElementSeen = true;
091: }
092: }
093:
094: public void visit(Node.IncludeAction n) throws JasperException,
095: JspToJavaException {
096: if (n.getPage().isExpression()) {
097: scriptingElementSeen = true;
098: }
099: includeActionSeen = true;
100: visitBody(n);
101: }
102:
103: public void visit(Node.ForwardAction n) throws JasperException,
104: JspToJavaException {
105: if (n.getPage().isExpression()) {
106: scriptingElementSeen = true;
107: }
108: visitBody(n);
109: }
110:
111: public void visit(Node.SetProperty n) throws JasperException,
112: JspToJavaException {
113: if (n.getValue() != null && n.getValue().isExpression()) {
114: scriptingElementSeen = true;
115: }
116: setPropertySeen = true;
117: }
118:
119: public void visit(Node.UseBean n) throws JasperException,
120: JspToJavaException {
121: if (n.getBeanName() != null
122: && n.getBeanName().isExpression()) {
123: scriptingElementSeen = true;
124: }
125: usebeanSeen = true;
126: visitBody(n);
127: }
128:
129: public void visit(Node.PlugIn n) throws JasperException,
130: JspToJavaException {
131: if (n.getHeight() != null && n.getHeight().isExpression()) {
132: scriptingElementSeen = true;
133: }
134: if (n.getWidth() != null && n.getWidth().isExpression()) {
135: scriptingElementSeen = true;
136: }
137: visitBody(n);
138: }
139:
140: public void visit(Node.CustomTag n) throws JasperException,
141: JspToJavaException {
142:
143: curTagNesting++;
144: if (curTagNesting > maxTagNesting) {
145: maxTagNesting = curTagNesting;
146: }
147:
148: // save values collected so far
149: boolean scriptingElementSeenSave = scriptingElementSeen;
150: scriptingElementSeen = false;
151: boolean usebeanSeenSave = usebeanSeen;
152: usebeanSeen = false;
153: boolean includeActionSeenSave = includeActionSeen;
154: includeActionSeen = false;
155: boolean setPropertySeenSave = setPropertySeen;
156: setPropertySeen = false;
157: boolean hasScriptingVarsSave = hasScriptingVars;
158: hasScriptingVars = false;
159:
160: // Scan attribute list for expressions
161: Node.JspAttribute[] attrs = n.getJspAttributes();
162: for (int i = 0; i < attrs.length; i++) {
163: if (attrs[i].isExpression()) {
164: scriptingElementSeen = true;
165: break;
166: }
167: }
168:
169: visitBody(n);
170:
171: if (!hasScriptingVars) {
172: // For some reason, varInfos is null when var is not defined
173: // in TEI, but tagVarInfos is empty array when var is not
174: // defined in tld.
175: hasScriptingVars = n.getVariableInfos() != null
176: || (n.getTagVariableInfos() != null && n
177: .getTagVariableInfos().length > 0);
178: }
179:
180: // Record if the tag element and its body contains any scriptlet.
181: n.setScriptless(!scriptingElementSeen);
182: n.setHasUsebean(usebeanSeen);
183: n.setHasIncludeAction(includeActionSeen);
184: n.setHasSetProperty(setPropertySeen);
185: n.setHasScriptingVars(hasScriptingVars);
186:
187: // Propagate value of scriptingElementSeen up.
188: scriptingElementSeen = scriptingElementSeen
189: || scriptingElementSeenSave;
190: usebeanSeen = usebeanSeen || usebeanSeenSave;
191: setPropertySeen = setPropertySeen || setPropertySeenSave;
192: includeActionSeen = includeActionSeen
193: || includeActionSeenSave;
194: hasScriptingVars = hasScriptingVars || hasScriptingVarsSave;
195:
196: curTagNesting--;
197: }
198:
199: public void visit(Node.Declaration n) throws JasperException,
200: JspToJavaException {
201: scriptingElementSeen = true;
202: }
203:
204: public void visit(Node.Expression n) throws JasperException,
205: JspToJavaException {
206: scriptingElementSeen = true;
207: }
208:
209: public void visit(Node.Scriptlet n) throws JasperException,
210: JspToJavaException {
211: scriptingElementSeen = true;
212: }
213:
214: public void updatePageInfo(PageInfo pageInfo) {
215: pageInfo.setMaxTagNesting(maxTagNesting);
216: pageInfo.setScriptless(!scriptingElementSeen);
217: }
218: }
219:
220: public static void collect(JspC compiler, Node.Nodes page)
221: throws JasperException, JspToJavaException {
222:
223: CollectVisitor collectVisitor = new CollectVisitor();
224: page.visit(collectVisitor);
225: collectVisitor.updatePageInfo(compiler.getPageInfo());
226:
227: }
228: }
|