/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ package org.apache.jasper.compiler;
/** * This class generates functions mappers for the EL expressions in the page. * Instead of a global mapper, a mapper is used for each call to EL * evaluator, thus avoiding the prefix overlapping and redefinition * issues. * * @author Kin-man Chung
*/
publicclass ELFunctionMapper { privateint currFunc = 0; private StringBuilder ds; // Contains codes to initialize the functions mappers. private StringBuilder ss; // Contains declarations of the functions mappers.
/** * Creates the functions mappers for all EL expressions in the JSP page. * * @param page The current compilation unit. * @throws JasperException EL error
*/ publicstaticvoid map(Node.Nodes page) throws JasperException {
ELFunctionMapper map = new ELFunctionMapper();
map.ds = new StringBuilder();
map.ss = new StringBuilder();
page.visit(map.new ELFunctionVisitor());
// Append the declarations to the root node
String ds = map.ds.toString(); if (ds.length() > 0) {
Node root = page.getRoot();
@SuppressWarnings("unused")
Node unused = new Node.Declaration(map.ss.toString(), null, root);
unused = new Node.Declaration( "static {\n" + ds + "}\n", null, root);
}
}
/** * A visitor for the page. The places where EL is allowed are scanned * for functions, and if found functions mappers are created.
*/ privateclass ELFunctionVisitor extends Node.Visitor {
/** * Use a global name map to facilitate reuse of function maps. * The key used is prefix:function:uri.
*/ privatefinal Map<String, String> gMap = new HashMap<>();
// Special case if there is only one function in the map
String funcMethod = null; if (functions.size() == 1) {
funcMethod = ".getMapForFunction";
} else {
ds.append(".getInstance();\n");
funcMethod = " " + decName + ".mapFunction";
}
// Setup arguments for either getMapForFunction or mapFunction for (ELNode.Function f : functions) {
FunctionInfo funcInfo = f.getFunctionInfo();
String fnQName = f.getPrefix() + ":" + f.getName(); if (funcInfo == null) { // Added via Lambda or ImportHandler. EL will expect a // function mapper even if one isn't used so just pass null
ds.append(funcMethod + "(null, null, null, null);\n");
} else {
ds.append(funcMethod + "(\"" + fnQName + "\", " +
getCanonicalName(funcInfo.getFunctionClass()) + ".class, " + '\"' + f.getMethodName() + "\", " + "new Class[] {");
String params[] = f.getParameters(); for (int k = 0; k < params.length; k++) { if (k != 0) {
ds.append(", ");
} int iArray = params[k].indexOf('['); if (iArray < 0) {
ds.append(params[k] + ".class");
} else {
String baseType = params[k].substring(0, iArray);
ds.append("java.lang.reflect.Array.newInstance(");
ds.append(baseType);
ds.append(".class,");
// Count the number of array dimension int aCount = 0; for (int jj = iArray; jj < params[k].length(); jj++ ) { if (params[k].charAt(jj) == '[') {
aCount++;
}
} if (aCount == 1) {
ds.append("0).getClass()");
} else {
ds.append("new int[" + aCount + "]).getClass()");
}
}
}
ds.append("});\n");
} // Put the current name in the global function map
gMap.put(fnQName + ':' + f.getUri(), decName);
}
el.setMapName(decName);
}
/** * Find the name of the function mapper for an EL. Reuse a * previously generated one if possible. * @param functions A List of ELNode.Function instances that * represents the functions in an EL * @return A previous generated function mapper name that can be used * by this EL; null if none found.
*/ private String matchMap(List<ELNode.Function> functions) {
String mapName = null; for (ELNode.Function f : functions) {
String temName = gMap.get(f.getPrefix() + ':' + f.getName() + ':' + f.getUri()); if (temName == null) { returnnull;
} if (mapName == null) {
mapName = temName;
} elseif (!temName.equals(mapName)) { // If not all in the previous match, then no match. returnnull;
}
} return mapName;
}
/* * @return A unique name for a function mapper.
*/ private String getMapName() { return"_jspx_fnmap_" + currFunc++;
}
/** * Convert a binary class name into a canonical one that can be used * when generating Java source code. * * @param className Binary class name * @return Canonical equivalent
*/ private String getCanonicalName(String className) throws JasperException { Class<?> clazz;
ClassLoader tccl; Thread currentThread = Thread.currentThread(); if (Constants.IS_SECURITY_ENABLED) {
PrivilegedAction<ClassLoader> pa = new PrivilegedGetTccl(currentThread);
tccl = AccessController.doPrivileged(pa);
} else {
tccl = currentThread.getContextClassLoader();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.