KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > Collector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import org.apache.jasper.JasperException;
21
22 /**
23  * Collect info about the page and nodes, and make them availabe through
24  * the PageInfo object.
25  *
26  * @author Kin-man Chung
27  * @author Mark Roth
28  */

29
30 class Collector {
31
32     /**
33      * A visitor for collecting information on the page and the body of
34      * the custom tags.
35      */

36     static class CollectVisitor extends Node.Visitor {
37
38         private boolean scriptingElementSeen = false;
39         private boolean usebeanSeen = false;
40         private boolean includeActionSeen = false;
41         private boolean paramActionSeen = false;
42         private boolean setPropertySeen = false;
43         private boolean hasScriptingVars = false;
44
45         public void visit(Node.ParamAction n) throws JasperException {
46             if (n.getValue().isExpression()) {
47                 scriptingElementSeen = true;
48             }
49             paramActionSeen = true;
50         }
51
52         public void visit(Node.IncludeAction n) throws JasperException {
53             if (n.getPage().isExpression()) {
54                 scriptingElementSeen = true;
55             }
56             includeActionSeen = true;
57             visitBody(n);
58         }
59
60         public void visit(Node.ForwardAction n) throws JasperException {
61             if (n.getPage().isExpression()) {
62                 scriptingElementSeen = true;
63             }
64             visitBody(n);
65         }
66
67         public void visit(Node.SetProperty n) throws JasperException {
68             if (n.getValue() != null && n.getValue().isExpression()) {
69                 scriptingElementSeen = true;
70             }
71             setPropertySeen = true;
72         }
73
74         public void visit(Node.UseBean n) throws JasperException {
75             if (n.getBeanName() != null && n.getBeanName().isExpression()) {
76                 scriptingElementSeen = true;
77             }
78             usebeanSeen = true;
79                 visitBody(n);
80         }
81
82         public void visit(Node.PlugIn n) throws JasperException {
83             if (n.getHeight() != null && n.getHeight().isExpression()) {
84                 scriptingElementSeen = true;
85             }
86             if (n.getWidth() != null && n.getWidth().isExpression()) {
87                 scriptingElementSeen = true;
88             }
89             visitBody(n);
90         }
91
92         public void visit(Node.CustomTag n) throws JasperException {
93             // Check to see what kinds of element we see as child elements
94
checkSeen( n.getChildInfo(), n );
95         }
96
97         /**
98          * Check all child nodes for various elements and update the given
99          * ChildInfo object accordingly. Visits body in the process.
100          */

101         private void checkSeen( Node.ChildInfo ci, Node n )
102             throws JasperException
103         {
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 || scriptingElementSeenSave;
148             usebeanSeen = usebeanSeen || usebeanSeenSave;
149             setPropertySeen = setPropertySeen || setPropertySeenSave;
150             includeActionSeen = includeActionSeen || includeActionSeenSave;
151             paramActionSeen = paramActionSeen || paramActionSeenSave;
152             hasScriptingVars = hasScriptingVars || hasScriptingVarsSave;
153         }
154
155         public void visit(Node.JspElement n) throws JasperException {
156             if (n.getNameAttribute().isExpression())
157                 scriptingElementSeen = true;
158
159             Node.JspAttribute[] attrs = n.getJspAttributes();
160             for (int i = 0; i < attrs.length; i++) {
161                 if (attrs[i].isExpression()) {
162                     scriptingElementSeen = true;
163                     break;
164                 }
165             }
166             visitBody(n);
167         }
168
169         public void visit(Node.JspBody n) throws JasperException {
170             checkSeen( n.getChildInfo(), n );
171         }
172
173         public void visit(Node.NamedAttribute n) throws JasperException {
174             checkSeen( n.getChildInfo(), n );
175         }
176
177         public void visit(Node.Declaration n) throws JasperException {
178             scriptingElementSeen = true;
179         }
180
181         public void visit(Node.Expression n) throws JasperException {
182             scriptingElementSeen = true;
183         }
184
185         public void visit(Node.Scriptlet n) throws JasperException {
186             scriptingElementSeen = true;
187         }
188
189         public void updatePageInfo(PageInfo pageInfo) {
190             pageInfo.setScriptless(! scriptingElementSeen);
191         }
192     }
193
194
195     public static void collect(Compiler JavaDoc compiler, Node.Nodes page)
196         throws JasperException {
197
198     CollectVisitor collectVisitor = new CollectVisitor();
199         page.visit(collectVisitor);
200         collectVisitor.updatePageInfo(compiler.getPageInfo());
201
202     }
203 }
204
205
Popular Tags