001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jasper.compiler;
019:
020: import org.apache.jasper.JasperException;
021:
022: /**
023: * Collect info about the page and nodes, and make them availabe through
024: * the PageInfo object.
025: *
026: * @author Kin-man Chung
027: * @author Mark Roth
028: */
029:
030: class Collector {
031:
032: /**
033: * A visitor for collecting information on the page and the body of
034: * the custom tags.
035: */
036: static class CollectVisitor extends Node.Visitor {
037:
038: private boolean scriptingElementSeen = false;
039: private boolean usebeanSeen = false;
040: private boolean includeActionSeen = false;
041: private boolean paramActionSeen = false;
042: private boolean setPropertySeen = false;
043: private boolean hasScriptingVars = false;
044:
045: public void visit(Node.ParamAction n) throws JasperException {
046: if (n.getValue().isExpression()) {
047: scriptingElementSeen = true;
048: }
049: paramActionSeen = true;
050: }
051:
052: public void visit(Node.IncludeAction n) throws JasperException {
053: if (n.getPage().isExpression()) {
054: scriptingElementSeen = true;
055: }
056: includeActionSeen = true;
057: visitBody(n);
058: }
059:
060: public void visit(Node.ForwardAction n) throws JasperException {
061: if (n.getPage().isExpression()) {
062: scriptingElementSeen = true;
063: }
064: visitBody(n);
065: }
066:
067: public void visit(Node.SetProperty n) throws JasperException {
068: if (n.getValue() != null && n.getValue().isExpression()) {
069: scriptingElementSeen = true;
070: }
071: setPropertySeen = true;
072: }
073:
074: public void visit(Node.UseBean n) throws JasperException {
075: if (n.getBeanName() != null
076: && n.getBeanName().isExpression()) {
077: scriptingElementSeen = true;
078: }
079: usebeanSeen = true;
080: visitBody(n);
081: }
082:
083: public void visit(Node.PlugIn n) throws JasperException {
084: if (n.getHeight() != null && n.getHeight().isExpression()) {
085: scriptingElementSeen = true;
086: }
087: if (n.getWidth() != null && n.getWidth().isExpression()) {
088: scriptingElementSeen = true;
089: }
090: visitBody(n);
091: }
092:
093: public void visit(Node.CustomTag n) throws JasperException {
094: // Check to see what kinds of element we see as child elements
095: checkSeen(n.getChildInfo(), n);
096: }
097:
098: /**
099: * Check all child nodes for various elements and update the given
100: * ChildInfo object accordingly. Visits body in the process.
101: */
102: private void checkSeen(Node.ChildInfo ci, Node n)
103: throws JasperException {
104: // save values collected so far
105: boolean scriptingElementSeenSave = scriptingElementSeen;
106: scriptingElementSeen = false;
107: boolean usebeanSeenSave = usebeanSeen;
108: usebeanSeen = false;
109: boolean includeActionSeenSave = includeActionSeen;
110: includeActionSeen = false;
111: boolean paramActionSeenSave = paramActionSeen;
112: paramActionSeen = false;
113: boolean setPropertySeenSave = setPropertySeen;
114: setPropertySeen = false;
115: boolean hasScriptingVarsSave = hasScriptingVars;
116: hasScriptingVars = false;
117:
118: // Scan attribute list for expressions
119: if (n instanceof Node.CustomTag) {
120: Node.CustomTag ct = (Node.CustomTag) n;
121: Node.JspAttribute[] attrs = ct.getJspAttributes();
122: for (int i = 0; attrs != null && i < attrs.length; i++) {
123: if (attrs[i].isExpression()) {
124: scriptingElementSeen = true;
125: break;
126: }
127: }
128: }
129:
130: visitBody(n);
131:
132: if ((n instanceof Node.CustomTag) && !hasScriptingVars) {
133: Node.CustomTag ct = (Node.CustomTag) n;
134: hasScriptingVars = ct.getVariableInfos().length > 0
135: || ct.getTagVariableInfos().length > 0;
136: }
137:
138: // Record if the tag element and its body contains any scriptlet.
139: ci.setScriptless(!scriptingElementSeen);
140: ci.setHasUseBean(usebeanSeen);
141: ci.setHasIncludeAction(includeActionSeen);
142: ci.setHasParamAction(paramActionSeen);
143: ci.setHasSetProperty(setPropertySeen);
144: ci.setHasScriptingVars(hasScriptingVars);
145:
146: // Propagate value of scriptingElementSeen up.
147: scriptingElementSeen = scriptingElementSeen
148: || scriptingElementSeenSave;
149: usebeanSeen = usebeanSeen || usebeanSeenSave;
150: setPropertySeen = setPropertySeen || setPropertySeenSave;
151: includeActionSeen = includeActionSeen
152: || includeActionSeenSave;
153: paramActionSeen = paramActionSeen || paramActionSeenSave;
154: hasScriptingVars = hasScriptingVars || hasScriptingVarsSave;
155: }
156:
157: public void visit(Node.JspElement n) throws JasperException {
158: if (n.getNameAttribute().isExpression())
159: scriptingElementSeen = true;
160:
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: visitBody(n);
169: }
170:
171: public void visit(Node.JspBody n) throws JasperException {
172: checkSeen(n.getChildInfo(), n);
173: }
174:
175: public void visit(Node.NamedAttribute n) throws JasperException {
176: checkSeen(n.getChildInfo(), n);
177: }
178:
179: public void visit(Node.Declaration n) throws JasperException {
180: scriptingElementSeen = true;
181: }
182:
183: public void visit(Node.Expression n) throws JasperException {
184: scriptingElementSeen = true;
185: }
186:
187: public void visit(Node.Scriptlet n) throws JasperException {
188: scriptingElementSeen = true;
189: }
190:
191: public void updatePageInfo(PageInfo pageInfo) {
192: pageInfo.setScriptless(!scriptingElementSeen);
193: }
194: }
195:
196: public static void collect(Compiler compiler, Node.Nodes page)
197: throws JasperException {
198:
199: CollectVisitor collectVisitor = new CollectVisitor();
200: page.visit(collectVisitor);
201: collectVisitor.updatePageInfo(compiler.getPageInfo());
202:
203: }
204: }
|