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 java.util.*;
021: import javax.servlet.jsp.tagext.FunctionInfo;
022: import org.apache.jasper.JasperException;
023:
024: /**
025: * This class generates functions mappers for the EL expressions in the page.
026: * Instead of a global mapper, a mapper is used for ecah call to EL
027: * evaluator, thus avoiding the prefix overlapping and redefinition
028: * issues.
029: *
030: * @author Kin-man Chung
031: */
032:
033: public class ELFunctionMapper {
034: private int currFunc = 0;
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: ELFunctionMapper map = new ELFunctionMapper();
048: map.ds = new StringBuffer();
049: map.ss = new StringBuffer();
050:
051: page.visit(map.new ELFunctionVisitor());
052:
053: // Append the declarations to the root node
054: String ds = map.ds.toString();
055: if (ds.length() > 0) {
056: Node root = page.getRoot();
057: new Node.Declaration(map.ss.toString(), null, root);
058: new Node.Declaration("static {\n" + ds + "}\n", null, root);
059: }
060: }
061:
062: /**
063: * A visitor for the page. The places where EL is allowed are scanned
064: * for functions, and if found functions mappers are created.
065: */
066: class ELFunctionVisitor extends Node.Visitor {
067:
068: /**
069: * Use a global name map to facilitate reuse of function maps.
070: * The key used is prefix:function:uri.
071: */
072: private HashMap<String, String> gMap = new HashMap<String, String>();
073:
074: public void visit(Node.ParamAction n) throws JasperException {
075: doMap(n.getValue());
076: visitBody(n);
077: }
078:
079: public void visit(Node.IncludeAction n) throws JasperException {
080: doMap(n.getPage());
081: visitBody(n);
082: }
083:
084: public void visit(Node.ForwardAction n) throws JasperException {
085: doMap(n.getPage());
086: visitBody(n);
087: }
088:
089: public void visit(Node.SetProperty n) throws JasperException {
090: doMap(n.getValue());
091: visitBody(n);
092: }
093:
094: public void visit(Node.UseBean n) throws JasperException {
095: doMap(n.getBeanName());
096: visitBody(n);
097: }
098:
099: public void visit(Node.PlugIn n) throws JasperException {
100: doMap(n.getHeight());
101: doMap(n.getWidth());
102: visitBody(n);
103: }
104:
105: public void visit(Node.JspElement n) throws JasperException {
106:
107: Node.JspAttribute[] attrs = n.getJspAttributes();
108: for (int i = 0; attrs != null && i < attrs.length; i++) {
109: doMap(attrs[i]);
110: }
111: doMap(n.getNameAttribute());
112: visitBody(n);
113: }
114:
115: public void visit(Node.UninterpretedTag n)
116: throws JasperException {
117:
118: Node.JspAttribute[] attrs = n.getJspAttributes();
119: for (int i = 0; attrs != null && i < attrs.length; i++) {
120: doMap(attrs[i]);
121: }
122: visitBody(n);
123: }
124:
125: public void visit(Node.CustomTag n) throws JasperException {
126: Node.JspAttribute[] attrs = n.getJspAttributes();
127: for (int i = 0; attrs != null && i < attrs.length; i++) {
128: doMap(attrs[i]);
129: }
130: visitBody(n);
131: }
132:
133: public void visit(Node.ELExpression n) throws JasperException {
134: doMap(n.getEL());
135: }
136:
137: private void doMap(Node.JspAttribute attr)
138: throws JasperException {
139: if (attr != null) {
140: doMap(attr.getEL());
141: }
142: }
143:
144: /**
145: * Creates function mappers, if needed, from ELNodes
146: */
147: private void doMap(ELNode.Nodes el) throws JasperException {
148:
149: // Only care about functions in ELNode's
150: class Fvisitor extends ELNode.Visitor {
151: ArrayList<ELNode.Function> funcs = new ArrayList<ELNode.Function>();
152: HashMap<String, String> keyMap = new HashMap<String, String>();
153:
154: public void visit(ELNode.Function n)
155: throws JasperException {
156: String key = n.getPrefix() + ":" + n.getName();
157: if (!keyMap.containsKey(key)) {
158: keyMap.put(key, "");
159: funcs.add(n);
160: }
161: }
162: }
163:
164: if (el == null) {
165: return;
166: }
167:
168: // First locate all unique functions in this EL
169: Fvisitor fv = new Fvisitor();
170: el.visit(fv);
171: ArrayList functions = fv.funcs;
172:
173: if (functions.size() == 0) {
174: return;
175: }
176:
177: // Reuse a previous map if possible
178: String decName = matchMap(functions);
179: if (decName != null) {
180: el.setMapName(decName);
181: return;
182: }
183:
184: // Generate declaration for the map statically
185: decName = getMapName();
186: ss
187: .append("static private org.apache.jasper.runtime.ProtectedFunctionMapper "
188: + decName + ";\n");
189:
190: ds.append(" " + decName + "= ");
191: ds
192: .append("org.apache.jasper.runtime.ProtectedFunctionMapper");
193:
194: // Special case if there is only one function in the map
195: String funcMethod = null;
196: if (functions.size() == 1) {
197: funcMethod = ".getMapForFunction";
198: } else {
199: ds.append(".getInstance();\n");
200: funcMethod = " " + decName + ".mapFunction";
201: }
202:
203: // Setup arguments for either getMapForFunction or mapFunction
204: for (int i = 0; i < functions.size(); i++) {
205: ELNode.Function f = (ELNode.Function) functions.get(i);
206: FunctionInfo funcInfo = f.getFunctionInfo();
207: String key = f.getPrefix() + ":" + f.getName();
208: ds.append(funcMethod + "(\"" + key + "\", "
209: + funcInfo.getFunctionClass() + ".class, "
210: + '\"' + f.getMethodName() + "\", "
211: + "new Class[] {");
212: String params[] = f.getParameters();
213: for (int k = 0; k < params.length; k++) {
214: if (k != 0) {
215: ds.append(", ");
216: }
217: int iArray = params[k].indexOf('[');
218: if (iArray < 0) {
219: ds.append(params[k] + ".class");
220: } else {
221: String baseType = params[k]
222: .substring(0, iArray);
223: ds
224: .append("java.lang.reflect.Array.newInstance(");
225: ds.append(baseType);
226: ds.append(".class,");
227:
228: // Count the number of array dimension
229: int aCount = 0;
230: for (int jj = iArray; jj < params[k].length(); jj++) {
231: if (params[k].charAt(jj) == '[') {
232: aCount++;
233: }
234: }
235: if (aCount == 1) {
236: ds.append("0).getClass()");
237: } else {
238: ds.append("new int[" + aCount
239: + "]).getClass()");
240: }
241: }
242: }
243: ds.append("});\n");
244: // Put the current name in the global function map
245: gMap.put(f.getPrefix() + ':' + f.getName() + ':'
246: + f.getUri(), decName);
247: }
248: el.setMapName(decName);
249: }
250:
251: /**
252: * Find the name of the function mapper for an EL. Reuse a
253: * previously generated one if possible.
254: * @param functions An ArrayList of ELNode.Function instances that
255: * represents the functions in an EL
256: * @return A previous generated function mapper name that can be used
257: * by this EL; null if none found.
258: */
259: private String matchMap(ArrayList functions) {
260:
261: String mapName = null;
262: for (int i = 0; i < functions.size(); i++) {
263: ELNode.Function f = (ELNode.Function) functions.get(i);
264: String temName = (String) gMap.get(f.getPrefix() + ':'
265: + f.getName() + ':' + f.getUri());
266: if (temName == null) {
267: return null;
268: }
269: if (mapName == null) {
270: mapName = temName;
271: } else if (!temName.equals(mapName)) {
272: // If not all in the previous match, then no match.
273: return null;
274: }
275: }
276: return mapName;
277: }
278:
279: /*
280: * @return An unique name for a function mapper.
281: */
282: private String getMapName() {
283: return "_jspx_fnmap_" + currFunc++;
284: }
285: }
286: }
|