KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
21 import javax.servlet.jsp.tagext.FunctionInfo JavaDoc;
22 import org.apache.jasper.JasperException;
23
24 /**
25  * This class generates functions mappers for the EL expressions in the page.
26  * Instead of a global mapper, a mapper is used for ecah call to EL
27  * evaluator, thus avoiding the prefix overlapping and redefinition
28  * issues.
29  *
30  * @author Kin-man Chung
31  */

32
33 public class ELFunctionMapper {
34     static private int currFunc = 0;
35     private ErrorDispatcher err;
36     StringBuffer JavaDoc ds; // Contains codes to initialize the functions mappers.
37
StringBuffer JavaDoc ss; // Contains declarations of the functions mappers.
38

39     /**
40      * Creates the functions mappers for all EL expressions in the JSP page.
41      *
42      * @param compiler Current compiler, mainly for accessing error dispatcher.
43      * @param page The current compilation unit.
44      */

45     public static void map(Compiler JavaDoc compiler, Node.Nodes page)
46         throws JasperException {
47
48     currFunc = 0;
49     ELFunctionMapper map = new ELFunctionMapper();
50     map.err = compiler.getErrorDispatcher();
51     map.ds = new StringBuffer JavaDoc();
52     map.ss = new StringBuffer JavaDoc();
53
54     page.visit(map.new ELFunctionVisitor());
55
56     // Append the declarations to the root node
57
String JavaDoc ds = map.ds.toString();
58     if (ds.length() > 0) {
59         Node root = page.getRoot();
60         new Node.Declaration(map.ss.toString(), null, root);
61         new Node.Declaration("static {\n" + ds + "}\n", null, root);
62     }
63     }
64
65     /**
66      * A visitor for the page. The places where EL is allowed are scanned
67      * for functions, and if found functions mappers are created.
68      */

69     class ELFunctionVisitor extends Node.Visitor {
70     
71     /**
72      * Use a global name map to facilitate reuse of function maps.
73      * The key used is prefix:function:uri.
74      */

75     private HashMap gMap = new HashMap();
76
77     public void visit(Node.ParamAction n) throws JasperException {
78         doMap(n.getValue());
79         visitBody(n);
80     }
81
82     public void visit(Node.IncludeAction n) throws JasperException {
83         doMap(n.getPage());
84         visitBody(n);
85     }
86
87     public void visit(Node.ForwardAction n) throws JasperException {
88         doMap(n.getPage());
89         visitBody(n);
90     }
91
92         public void visit(Node.SetProperty n) throws JasperException {
93         doMap(n.getValue());
94         visitBody(n);
95     }
96
97         public void visit(Node.UseBean n) throws JasperException {
98         doMap(n.getBeanName());
99         visitBody(n);
100     }
101
102         public void visit(Node.PlugIn n) throws JasperException {
103         doMap(n.getHeight());
104         doMap(n.getWidth());
105         visitBody(n);
106     }
107
108         public void visit(Node.JspElement n) throws JasperException {
109
110         Node.JspAttribute[] attrs = n.getJspAttributes();
111         for (int i = 0; attrs != null && i < attrs.length; i++) {
112         doMap(attrs[i]);
113         }
114         doMap(n.getNameAttribute());
115         visitBody(n);
116     }
117
118         public void visit(Node.UninterpretedTag n) throws JasperException {
119
120         Node.JspAttribute[] attrs = n.getJspAttributes();
121         for (int i = 0; attrs != null && i < attrs.length; i++) {
122         doMap(attrs[i]);
123         }
124         visitBody(n);
125     }
126
127         public void visit(Node.CustomTag n) throws JasperException {
128         Node.JspAttribute[] attrs = n.getJspAttributes();
129         for (int i = 0; attrs != null && i < attrs.length; i++) {
130         doMap(attrs[i]);
131         }
132         visitBody(n);
133     }
134
135         public void visit(Node.ELExpression n) throws JasperException {
136         doMap(n.getEL());
137     }
138
139     private void doMap(Node.JspAttribute attr)
140         throws JasperException {
141         if (attr != null) {
142         doMap(attr.getEL());
143         }
144     }
145
146         /**
147          * Creates function mappers, if needed, from ELNodes
148          */

149     private void doMap(ELNode.Nodes el)
150         throws JasperException {
151
152             // Only care about functions in ELNode's
153
class Fvisitor extends ELNode.Visitor {
154         ArrayList funcs = new ArrayList();
155         HashMap keyMap = new HashMap();
156         public void visit(ELNode.Function n) throws JasperException {
157             String JavaDoc key = n.getPrefix() + ":" + n.getName();
158             if (! keyMap.containsKey(key)) {
159             keyMap.put(key,"");
160             funcs.add(n);
161             }
162         }
163         }
164
165         if (el == null) {
166         return;
167         }
168
169         // First locate all unique functions in this EL
170
Fvisitor fv = new Fvisitor();
171         el.visit(fv);
172         ArrayList functions = fv.funcs;
173
174         if (functions.size() == 0) {
175         return;
176         }
177
178         // Reuse a previous map if possible
179
String JavaDoc decName = matchMap(functions);
180         if (decName != null) {
181         el.setMapName(decName);
182         return;
183         }
184     
185         // Generate declaration for the map statically
186
decName = getMapName();
187         ss.append("static private org.apache.jasper.runtime.ProtectedFunctionMapper " + decName + ";\n");
188
189         ds.append(" " + decName + "= ");
190         ds.append("org.apache.jasper.runtime.ProtectedFunctionMapper");
191
192         // Special case if there is only one function in the map
193
String JavaDoc funcMethod = null;
194         if (functions.size() == 1) {
195         funcMethod = ".getMapForFunction";
196         } else {
197         ds.append(".getInstance();\n");
198         funcMethod = " " + decName + ".mapFunction";
199         }
200
201             // Setup arguments for either getMapForFunction or mapFunction
202
for (int i = 0; i < functions.size(); i++) {
203         ELNode.Function f = (ELNode.Function)functions.get(i);
204         FunctionInfo JavaDoc funcInfo = f.getFunctionInfo();
205         String JavaDoc key = f.getPrefix()+ ":" + f.getName();
206         ds.append(funcMethod + "(\"" + key + "\", " +
207             funcInfo.getFunctionClass() + ".class, " +
208             '\"' + f.getMethodName() + "\", " +
209             "new Class[] {");
210         String JavaDoc params[] = f.getParameters();
211         for (int k = 0; k < params.length; k++) {
212             if (k != 0) {
213             ds.append(", ");
214             }
215             int iArray = params[k].indexOf('[');
216             if (iArray < 0) {
217             ds.append(params[k] + ".class");
218             }
219             else {
220             String JavaDoc baseType = params[k].substring(0, iArray);
221             ds.append("java.lang.reflect.Array.newInstance(");
222             ds.append(baseType);
223             ds.append(".class,");
224
225             // Count the number of array dimension
226
int aCount = 0;
227             for (int jj = iArray; jj < params[k].length(); jj++ ) {
228                 if (params[k].charAt(jj) == '[') {
229                 aCount++;
230                 }
231             }
232             if (aCount == 1) {
233                 ds.append("0).getClass()");
234             } else {
235                 ds.append("new int[" + aCount + "]).getClass()");
236             }
237             }
238         }
239         ds.append("});\n");
240         // Put the current name in the global function map
241
gMap.put(f.getPrefix() + ':' + f.getName() + ':' + f.getUri(),
242              decName);
243         }
244         el.setMapName(decName);
245     }
246
247         /**
248          * Find the name of the function mapper for an EL. Reuse a
249          * previously generated one if possible.
250          * @param functions An ArrayList of ELNode.Function instances that
251          * represents the functions in an EL
252          * @return A previous generated function mapper name that can be used
253          * by this EL; null if none found.
254          */

255     private String JavaDoc matchMap(ArrayList functions) {
256
257         String JavaDoc mapName = null;
258         for (int i = 0; i < functions.size(); i++) {
259         ELNode.Function f = (ELNode.Function)functions.get(i);
260         String JavaDoc temName = (String JavaDoc) gMap.get(f.getPrefix() + ':' +
261                     f.getName() + ':' + f.getUri());
262         if (temName == null) {
263             return null;
264         }
265         if (mapName == null) {
266             mapName = temName;
267         } else if (!temName.equals(mapName)) {
268             // If not all in the previous match, then no match.
269
return null;
270         }
271         }
272         return mapName;
273     }
274
275         /*
276          * @return An unique name for a function mapper.
277          */

278     private String JavaDoc getMapName() {
279         return "_jspx_fnmap_" + currFunc++;
280     }
281     }
282 }
283
284
Popular Tags