001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jasper.compiler;
018:
019: import java.util.*;
020: import javax.servlet.jsp.tagext.FunctionInfo;
021: import org.apache.jasper.JasperException;
022:
023: /**
024: * This class generates functions mappers for the EL expressions in the page.
025: * Instead of a global mapper, a mapper is used for ecah call to EL
026: * evaluator, thus avoiding the prefix overlapping and redefinition
027: * issues.
028: *
029: * @author Kin-man Chung
030: */
031:
032: public class ELFunctionMapper {
033: static private int currFunc = 0;
034: private ErrorDispatcher err;
035: StringBuffer ds; // Contains codes to initialize the functions mappers.
036: StringBuffer ss; // Contains declarations of the functions mappers.
037:
038: /**
039: * Creates the functions mappers for all EL expressions in the JSP page.
040: *
041: * @param compiler Current compiler, mainly for accessing error dispatcher.
042: * @param page The current compilation unit.
043: */
044: public static void map(Compiler compiler, Node.Nodes page)
045: throws JasperException {
046:
047: currFunc = 0;
048: ELFunctionMapper map = new ELFunctionMapper();
049: map.err = compiler.getErrorDispatcher();
050: map.ds = new StringBuffer();
051: map.ss = new StringBuffer();
052:
053: page.visit(map.new ELFunctionVisitor());
054:
055: // Append the declarations to the root node
056: String ds = map.ds.toString();
057: if (ds.length() > 0) {
058: Node root = page.getRoot();
059: new Node.Declaration(map.ss.toString(), null, root);
060: new Node.Declaration("static {\n" + ds + "}\n", null, root);
061: }
062: }
063:
064: /**
065: * A visitor for the page. The places where EL is allowed are scanned
066: * for functions, and if found functions mappers are created.
067: */
068: class ELFunctionVisitor extends Node.Visitor {
069:
070: /**
071: * Use a global name map to facilitate reuse of function maps.
072: * The key used is prefix:function:uri.
073: */
074: private HashMap gMap = new HashMap();
075:
076: public void visit(Node.ParamAction n) throws JasperException {
077: doMap(n.getValue());
078: visitBody(n);
079: }
080:
081: public void visit(Node.IncludeAction n) throws JasperException {
082: doMap(n.getPage());
083: visitBody(n);
084: }
085:
086: public void visit(Node.ForwardAction n) throws JasperException {
087: doMap(n.getPage());
088: visitBody(n);
089: }
090:
091: public void visit(Node.SetProperty n) throws JasperException {
092: doMap(n.getValue());
093: visitBody(n);
094: }
095:
096: public void visit(Node.UseBean n) throws JasperException {
097: doMap(n.getBeanName());
098: visitBody(n);
099: }
100:
101: public void visit(Node.PlugIn n) throws JasperException {
102: doMap(n.getHeight());
103: doMap(n.getWidth());
104: visitBody(n);
105: }
106:
107: public void visit(Node.JspElement n) throws JasperException {
108:
109: Node.JspAttribute[] attrs = n.getJspAttributes();
110: for (int i = 0; attrs != null && i < attrs.length; i++) {
111: doMap(attrs[i]);
112: }
113: doMap(n.getNameAttribute());
114: visitBody(n);
115: }
116:
117: public void visit(Node.UninterpretedTag n)
118: 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) throws JasperException {
150:
151: // Only care about functions in ELNode's
152: class Fvisitor extends ELNode.Visitor {
153: ArrayList funcs = new ArrayList();
154: HashMap keyMap = new HashMap();
155:
156: public void visit(ELNode.Function n)
157: throws JasperException {
158: String key = n.getPrefix() + ":" + n.getName();
159: if (!keyMap.containsKey(key)) {
160: keyMap.put(key, "");
161: funcs.add(n);
162: }
163: }
164: }
165:
166: if (el == null) {
167: return;
168: }
169:
170: // First locate all unique functions in this EL
171: Fvisitor fv = new Fvisitor();
172: el.visit(fv);
173: ArrayList functions = fv.funcs;
174:
175: if (functions.size() == 0) {
176: return;
177: }
178:
179: // Reuse a previous map if possible
180: String decName = matchMap(functions);
181: if (decName != null) {
182: el.setMapName(decName);
183: return;
184: }
185:
186: // Generate declaration for the map statically
187: decName = getMapName();
188: ss
189: .append("static private org.apache.jasper.runtime.ProtectedFunctionMapper "
190: + decName + ";\n");
191:
192: ds.append(" " + decName + "= ");
193: ds
194: .append("org.apache.jasper.runtime.ProtectedFunctionMapper");
195:
196: // Special case if there is only one function in the map
197: String funcMethod = null;
198: if (functions.size() == 1) {
199: funcMethod = ".getMapForFunction";
200: } else {
201: ds.append(".getInstance();\n");
202: funcMethod = " " + decName + ".mapFunction";
203: }
204:
205: // Setup arguments for either getMapForFunction or mapFunction
206: for (int i = 0; i < functions.size(); i++) {
207: ELNode.Function f = (ELNode.Function) functions.get(i);
208: FunctionInfo funcInfo = f.getFunctionInfo();
209: String key = f.getPrefix() + ":" + f.getName();
210: ds.append(funcMethod + "(\"" + key + "\", "
211: + funcInfo.getFunctionClass() + ".class, "
212: + '\"' + f.getMethodName() + "\", "
213: + "new Class[] {");
214: String params[] = f.getParameters();
215: for (int k = 0; k < params.length; k++) {
216: if (k != 0) {
217: ds.append(", ");
218: }
219: int iArray = params[k].indexOf('[');
220: if (iArray < 0) {
221: ds.append(params[k] + ".class");
222: } else {
223: String baseType = params[k]
224: .substring(0, iArray);
225: ds
226: .append("java.lang.reflect.Array.newInstance(");
227: ds.append(baseType);
228: ds.append(".class,");
229:
230: // Count the number of array dimension
231: int aCount = 0;
232: for (int jj = iArray; jj < params[k].length(); jj++) {
233: if (params[k].charAt(jj) == '[') {
234: aCount++;
235: }
236: }
237: if (aCount == 1) {
238: ds.append("0).getClass()");
239: } else {
240: ds.append("new int[" + aCount
241: + "]).getClass()");
242: }
243: }
244: }
245: ds.append("});\n");
246: // Put the current name in the global function map
247: gMap.put(f.getPrefix() + ':' + f.getName() + ':'
248: + f.getUri(), decName);
249: }
250: el.setMapName(decName);
251: }
252:
253: /**
254: * Find the name of the function mapper for an EL. Reuse a
255: * previously generated one if possible.
256: * @param functions An ArrayList of ELNode.Function instances that
257: * represents the functions in an EL
258: * @return A previous generated function mapper name that can be used
259: * by this EL; null if none found.
260: */
261: private String matchMap(ArrayList functions) {
262:
263: String mapName = null;
264: for (int i = 0; i < functions.size(); i++) {
265: ELNode.Function f = (ELNode.Function) functions.get(i);
266: String temName = (String) gMap.get(f.getPrefix() + ':'
267: + f.getName() + ':' + f.getUri());
268: if (temName == null) {
269: return null;
270: }
271: if (mapName == null) {
272: mapName = temName;
273: } else if (!temName.equals(mapName)) {
274: // If not all in the previous match, then no match.
275: return null;
276: }
277: }
278: return mapName;
279: }
280:
281: /*
282: * @return An unique name for a function mapper.
283: */
284: private String getMapName() {
285: return "_jspx_fnmap_" + currFunc++;
286: }
287: }
288: }
|