You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/07/04 13:40:22 UTC

svn commit: r1499726 - in /tomcat/trunk/java/javax/el: ELProcessor.java LocalStrings.properties

Author: markt
Date: Thu Jul  4 11:40:22 2013
New Revision: 1499726

URL: http://svn.apache.org/r1499726
Log:
EL 3.0
Add new class

Added:
    tomcat/trunk/java/javax/el/ELProcessor.java   (with props)
Modified:
    tomcat/trunk/java/javax/el/LocalStrings.properties

Added: tomcat/trunk/java/javax/el/ELProcessor.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/ELProcessor.java?rev=1499726&view=auto
==============================================================================
--- tomcat/trunk/java/javax/el/ELProcessor.java (added)
+++ tomcat/trunk/java/javax/el/ELProcessor.java Thu Jul  4 11:40:22 2013
@@ -0,0 +1,211 @@
+/*
+ * 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 javax.el;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+/**
+ * @since EL 3.0
+ */
+public class ELProcessor {
+
+    private final ELManager manager = new ELManager();
+    private final ELContext context = manager.getELContext();
+    private final ExpressionFactory factory = ELManager.getExpressionFactory();
+
+
+    public ELManager getELManager() {
+        return manager;
+    }
+
+
+    public Object eval(String expression) {
+        return getValue(expression, Object.class);
+    }
+
+
+    public Object getValue(String expression, Class<?> expectedType) {
+        ValueExpression ve = factory.createValueExpression(
+                context, expression, expectedType);
+        return ve.getValue(context);
+    }
+
+
+    public void setValue(String expression, Object value) {
+        ValueExpression ve = factory.createValueExpression(
+                context, expression, Object.class);
+        ve.setValue(context, value);
+    }
+
+
+    public void setVariable(String variable, String expression) {
+        if (expression == null) {
+            manager.setVariable(variable, null);
+        } else {
+            ValueExpression ve = factory.createValueExpression(
+                    context, expression, Object.class);
+            manager.setVariable(variable, ve);
+        }
+    }
+
+
+    public void defineFunction(String prefix, String function, String className,
+            String methodName) throws ClassNotFoundException,
+            NoSuchMethodException {
+
+        if (prefix == null || function == null || className == null ||
+                methodName == null) {
+            throw new NullPointerException(Util.message(
+                    context, "elProcessor.defineFunctionNullParams"));
+        }
+
+        Class<?> clazz = Class.forName(className);
+
+        if (!Modifier.isPublic(clazz.getModifiers())) {
+            throw new ClassNotFoundException(Util.message(context,
+                    "elProcessor.defineFunctionInvalidClass", className));
+        }
+
+        MethodSignature sig =
+                new MethodSignature(context, methodName, className);
+
+        Method methods[] = clazz.getMethods();
+        for (Method method : methods) {
+            if (!Modifier.isStatic(method.getModifiers())) {
+                continue;
+            }
+            if (method.getName().equals(sig.getName())) {
+                if (sig.getParamTypeNames() == null) {
+                    manager.mapFunction(prefix, function, method);
+                    return;
+                } else {
+                    Class<?>[] types = method.getParameterTypes();
+                    String[] typeNames = sig.getParamTypeNames();
+                    if (types.length == typeNames.length) {
+                        boolean match = true;
+                        for (int i = 0; i < types.length; i++) {
+                            if (!types[i].getName().equals(typeNames[i])) {
+                                match = false;
+                                break;
+                            }
+                        }
+                        if (match) {
+                            manager.mapFunction(prefix, function, method);
+                            return;
+                        }
+                    }
+                }
+            }
+        }
+
+        throw new NoSuchMethodException(Util.message(context,
+                "elProcessor.defineFunctionNoMethod", methodName, className));
+    }
+
+
+    /**
+     * @throws NullPointerException
+     *              If any of the arguments are null
+     * @throws NoSuchMethodException
+     *              If the method is not static
+     */
+    public void defineFunction(String prefix, String function, Method method)
+            throws java.lang.NoSuchMethodException {
+
+        if (prefix == null || function == null || method == null) {
+            throw new NullPointerException(Util.message(
+                    context, "elProcessor.defineFunctionNullParams"));
+        }
+
+        int modifiers = method.getModifiers();
+
+        // Check for public method as well as being static
+        if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
+            throw new NoSuchMethodException(Util.message(context,
+                    "elProcessor.defineFunctionInvalidMethod", method.getName(),
+                    method.getDeclaringClass().getName()));
+        }
+
+        manager.mapFunction(prefix, function, method);
+    }
+
+
+    public void defineBean(String name, Object bean) {
+        manager.defineBean(name, bean);
+    }
+
+
+    private static class MethodSignature {
+
+        private final String name;
+        private final String[] parameterTypeNames;
+
+        public MethodSignature(ELContext context, String methodName,
+                String className) throws NoSuchMethodException {
+
+            int paramIndex = methodName.indexOf('(');
+
+            if (paramIndex == -1) {
+                name = methodName.trim();
+                parameterTypeNames = null;
+            } else {
+                name = methodName.substring(0, paramIndex -1).trim();
+                String paramString = methodName.substring(paramIndex).trim();
+                // We know the params start with '(', check they end with ')'
+                if (!paramString.endsWith(")")) {
+                    throw new NoSuchMethodException(Util.message(context,
+                            "elProcessor.defineFunctionInvalidParameterList",
+                            paramString, methodName, className));
+                }
+                // Trim '(' and ')'
+                paramString =
+                        paramString.substring(1, paramString.length() - 1);
+                parameterTypeNames = paramString.split(",");
+                ImportHandler importHandler = context.getImportHandler();
+                for (int i = 0; i < parameterTypeNames.length; i++) {
+                    parameterTypeNames[i] = parameterTypeNames[i].trim();
+                    if (!parameterTypeNames[i].contains(".")) {
+                        Class<?> clazz = importHandler.resolveClass(
+                                parameterTypeNames[i]);
+                        if (clazz == null) {
+                            throw new NoSuchMethodException(Util.message(
+                                    context,
+                                    "elProcessor.defineFunctionInvalidParameterTypeName",
+                                    parameterTypeNames[i], methodName,
+                                    className));
+                        }
+                        parameterTypeNames[i] = clazz.getName();
+                    }
+                }
+            }
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        /**
+         * @return <code>null</code> if just the method name was specified, an
+         *         empty List if an empty parameter list was specified - i.e. ()
+         *         - otherwise an ordered list of parameter type names
+         */
+        public String[] getParamTypeNames() {
+            return parameterTypeNames;
+        }
+    }
+}

Propchange: tomcat/trunk/java/javax/el/ELProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/java/javax/el/LocalStrings.properties
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/javax/el/LocalStrings.properties?rev=1499726&r1=1499725&r2=1499726&view=diff
==============================================================================
--- tomcat/trunk/java/javax/el/LocalStrings.properties (original)
+++ tomcat/trunk/java/javax/el/LocalStrings.properties Thu Jul  4 11:40:22 2013
@@ -23,6 +23,13 @@ propertyReadError=Error reading ''{1}'' 
 propertyWriteError=Error writing ''{1}'' on type {0}
 objectNotAssignable=Unable to add an object of type [{0}] to an array of objects of type [{1}]
 
+elProcessor.defineFunctionInvalidClass=The class [{0}] is not public
+elProcessor.defineFunctionInvalidMethod=The method [{0}] on class [{1}] is not a public static method
+elProcessor.defineFunctionInvalidParameterList=The parameter list [{0}] for method [{1}] on class [{2}] is not valid
+elProcessor.defineFunctionInvalidParameterTypeName=The parameter type [{0}] for method [{1}] on class [{2}] is not valid
+elProcessor.defineFunctionNoMethod=A public static method [{0}] on class [{1}] could not be found
+elProcessor.defineFunctionNullParams=One or more of the input parameters was null
+
 importHandler.ambiguousImport=The class [{0}] could not be imported as it conflicts with [{1}] which has already been imported
 importHandler.ambiguousStaticImport=The static import [{0}] could not be processed as it conflicts with [{1}] which has already been imported
 importHandler.classNotFound=The class [{0}] could not be imported as it could not be found



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org