bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rmannibu...@apache.org
Subject svn commit: r1498793 - in /bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval: el/ el/ELFacade.java el/MessageEvaluator.java jsr303/DefaultMessageInterpolator.java
Date Tue, 02 Jul 2013 06:10:57 GMT
Author: rmannibucau
Date: Tue Jul  2 06:10:57 2013
New Revision: 1498793

URL: http://svn.apache.org/r1498793
Log:
making bval usable without all the JavaEE API the spec now needs (cdi, ejb, interceptor was
already optional while not triggered by default but el was mandatory)

Added:
    bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/
    bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/ELFacade.java
    bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/MessageEvaluator.java
Modified:
    bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java

Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/ELFacade.java
URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/ELFacade.java?rev=1498793&view=auto
==============================================================================
--- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/ELFacade.java (added)
+++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/ELFacade.java Tue Jul
 2 06:10:57 2013
@@ -0,0 +1,200 @@
+/*
+ * 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.bval.el;
+
+import javax.el.ArrayELResolver;
+import javax.el.BeanELResolver;
+import javax.el.CompositeELResolver;
+import javax.el.ELContext;
+import javax.el.ELResolver;
+import javax.el.ExpressionFactory;
+import javax.el.FunctionMapper;
+import javax.el.ListELResolver;
+import javax.el.MapELResolver;
+import javax.el.PropertyNotWritableException;
+import javax.el.ResourceBundleELResolver;
+import javax.el.ValueExpression;
+import javax.el.VariableMapper;
+import java.lang.reflect.Method;
+import java.util.Formatter;
+import java.util.HashMap;
+import java.util.Map;
+
+// ELProcessor or JavaEE 7 would be perfect too but this impl can be used in javaee 6
+public final class ELFacade implements MessageEvaluator {
+    private static final ExpressionFactory EXPRESSION_FACTORY;
+    static {
+        ExpressionFactory ef;
+        try {
+            ef = ExpressionFactory.newInstance();
+        } catch (final Exception e) {
+            ef = null;
+        }
+        EXPRESSION_FACTORY = ef;
+    }
+
+    public String interpolate(final String message, final Map<String, Object> annotationParameters,
final Object validatedValue) {
+        final ELResolver resolver = initResolver();
+        final BValELContext context = new BValELContext(resolver);
+        final VariableMapper variables = context.getVariableMapper();
+        for (final Map.Entry<String, Object> var : annotationParameters.entrySet())
{
+            variables.setVariable(var.getKey(), new ValueExpressionLiteral(var.getValue()));
+        }
+        variables.setVariable("validatedValue", new ValueExpressionLiteral(validatedValue));
+
+        try {
+            if (EXPRESSION_FACTORY != null) {
+                // #{xxx} shouldn't be evaluated
+                return EXPRESSION_FACTORY.createValueExpression(context, message.replace("#{",
"\\#{"), String.class).getValue(context).toString();
+            }
+        } catch (final Exception e) {
+            // no-op
+        }
+
+        return message;
+    }
+
+    private static ELResolver initResolver() {
+        final CompositeELResolver resolver = new CompositeELResolver();
+        resolver.add(new MapELResolver());
+        resolver.add(new ListELResolver());
+        resolver.add(new ArrayELResolver());
+        resolver.add(new ResourceBundleELResolver());
+        resolver.add(new BeanELResolver());
+        return resolver;
+    }
+
+    private static class BValELContext extends ELContext {
+        private final ELResolver resolver;
+        private final FunctionMapper functions;
+        private final VariableMapper variables;
+
+        public BValELContext(final ELResolver resolver) {
+            this.resolver = resolver;
+            this.variables = new BValVariableMapper();
+            this.functions = new BValFunctionMapper();
+        }
+
+        @Override
+        public ELResolver getELResolver() {
+            return resolver;
+        }
+
+        @Override
+        public FunctionMapper getFunctionMapper() {
+            return functions;
+        }
+
+        @Override
+        public VariableMapper getVariableMapper() {
+            return variables;
+        }
+    }
+
+    private static class BValFunctionMapper extends FunctionMapper {
+        @Override
+        public Method resolveFunction(final String prefix, final String localName) {
+            return null;
+        }
+    }
+
+    private static class BValVariableMapper extends VariableMapper {
+        private final Map<String, ValueExpression> variables = new HashMap<String,
ValueExpression>();
+
+        @Override
+        public ValueExpression resolveVariable(final String variable) {
+            if ("formatter".equals(variable)) {
+                return new ValueExpressionLiteral(new BValFormatter());
+            }
+            return variables.get(variable);
+        }
+
+        @Override
+        public ValueExpression setVariable(final String variable, final ValueExpression expression)
{
+            variables.put(variable, expression);
+            return expression;
+        }
+    }
+
+    // used to not expose all method and avoid ambiguity with format(Local, String, Object...)
in EL
+    public static class BValFormatter {
+        private final Formatter formatter = new Formatter();
+
+        public Formatter format(final String format, final Object ... args) {
+            return formatter.format(format, args);
+        }
+    }
+
+    private static final class ValueExpressionLiteral extends ValueExpression {
+        private final Object value;
+
+        public ValueExpressionLiteral(final Object value) {
+            this.value = value;
+        }
+
+        @Override
+        public Object getValue(final ELContext context) {
+            return value;
+        }
+
+        @Override
+        public void setValue(final ELContext context, final Object value) {
+            throw new PropertyNotWritableException(value.toString());
+        }
+
+        @Override
+        public boolean isReadOnly(final ELContext context) {
+            return true;
+        }
+
+        @Override
+        public Class<?> getType(final ELContext context) {
+            return (this.value != null) ? this.value.getClass() : null;
+        }
+
+        @Override
+        public Class<?> getExpectedType() {
+            return String.class;
+        }
+
+        @Override
+        public String getExpressionString() {
+            return (this.value != null) ? this.value.toString() : null;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            return (obj instanceof ValueExpressionLiteral && this
+                    .equals((ValueExpressionLiteral) obj));
+        }
+
+        public boolean equals(final ValueExpressionLiteral ve) {
+            return (ve != null && (this.value != null && ve.value != null
&& (this.value == ve.value || this.value
+                    .equals(ve.value))));
+        }
+
+        @Override
+        public int hashCode() {
+            return (this.value != null) ? this.value.hashCode() : 0;
+        }
+
+        @Override
+        public boolean isLiteralText() {
+            return true;
+        }
+    }
+}

Added: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/MessageEvaluator.java
URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/MessageEvaluator.java?rev=1498793&view=auto
==============================================================================
--- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/MessageEvaluator.java
(added)
+++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/el/MessageEvaluator.java
Tue Jul  2 06:10:57 2013
@@ -0,0 +1,25 @@
+/*
+ * 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.bval.el;
+
+import java.util.Map;
+
+// just here to ensure we can use Expression Language if at classpath
+// but not fail if not here (as before)
+public interface MessageEvaluator {
+    String interpolate(String message, Map<String, Object> annotationParameters, Object
validatedValue);
+}

Modified: bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java
URL: http://svn.apache.org/viewvc/bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java?rev=1498793&r1=1498792&r2=1498793&view=diff
==============================================================================
--- bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java
(original)
+++ bval/branches/bval-11/bval-jsr303/src/main/java/org/apache/bval/jsr303/DefaultMessageInterpolator.java
Tue Jul  2 06:10:57 2013
@@ -16,28 +16,13 @@
  */
 package org.apache.bval.jsr303;
 
+import org.apache.bval.el.MessageEvaluator;
 import org.apache.bval.jsr303.util.SecureActions;
 import org.apache.commons.lang3.ArrayUtils;
 
-import javax.el.ArrayELResolver;
-import javax.el.BeanELResolver;
-import javax.el.CompositeELResolver;
-import javax.el.ELContext;
-import javax.el.ELResolver;
-import javax.el.ExpressionFactory;
-import javax.el.FunctionMapper;
-import javax.el.ListELResolver;
-import javax.el.MapELResolver;
-import javax.el.PropertyNotWritableException;
-import javax.el.ResourceBundleELResolver;
-import javax.el.ValueExpression;
-import javax.el.VariableMapper;
 import javax.validation.MessageInterpolator;
-import java.lang.reflect.Method;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
-import java.util.Formatter;
-import java.util.HashMap;
 import java.util.Locale;
 import java.util.Map;
 import java.util.MissingResourceException;
@@ -55,17 +40,6 @@ import java.util.regex.Pattern;
  * This class is threadsafe.<br/>
  */
 public class DefaultMessageInterpolator implements MessageInterpolator {
-    private static final ExpressionFactory EXPRESSION_FACTORY;
-    static {
-        ExpressionFactory ef;
-        try {
-            ef = ExpressionFactory.newInstance();
-        } catch (final Exception e) {
-            ef = null;
-        }
-        EXPRESSION_FACTORY = ef;
-    }
-
     private static final Logger log = Logger.getLogger(DefaultMessageInterpolator.class.getName());
     private static final String DEFAULT_VALIDATION_MESSAGES = "org.apache.bval.jsr303.ValidationMessages";
     private static final String USER_VALIDATION_MESSAGES = "ValidationMessages";
@@ -83,6 +57,8 @@ public class DefaultMessageInterpolator 
     /** Builtin resource bundles hashed against their locale. */
     private final Map<Locale, ResourceBundle> defaultBundlesMap = new ConcurrentHashMap<Locale,
ResourceBundle>();
 
+    private final MessageEvaluator evaluator;
+
     /**
      * Create a new DefaultMessageInterpolator instance.
      */
@@ -109,16 +85,14 @@ public class DefaultMessageInterpolator 
 
         defaultBundlesMap.put(defaultLocale,
                 ResourceBundle.getBundle(DEFAULT_VALIDATION_MESSAGES, defaultLocale));
-    }
 
-    private static ELResolver initResolver() {
-        final CompositeELResolver resolver = new CompositeELResolver();
-        resolver.add(new MapELResolver());
-        resolver.add(new ListELResolver());
-        resolver.add(new ArrayELResolver());
-        resolver.add(new ResourceBundleELResolver());
-        resolver.add(new BeanELResolver());
-        return resolver;
+        MessageEvaluator ev = null;
+        try {
+            ev = MessageEvaluator.class.cast(getClass().getClassLoader().loadClass("org.apache.bval.el.ELFacade").newInstance());
+        } catch (final Throwable e) { // can be exception or error
+            // no-op
+        }
+        evaluator = ev;
     }
 
     /** {@inheritDoc} */
@@ -177,21 +151,9 @@ public class DefaultMessageInterpolator 
         // resolve annotation attributes (step 4)
         resolvedMessage = replaceAnnotationAttributes(resolvedMessage, annotationParameters);
 
-        final ELResolver resolver = initResolver();
-        final BValELContext context = new BValELContext(resolver);
-        final VariableMapper variables = context.getVariableMapper();
-        for (final Map.Entry<String, Object> var : annotationParameters.entrySet())
{
-            variables.setVariable(var.getKey(), new ValueExpressionLiteral(var.getValue()));
-        }
-        variables.setVariable("validatedValue", new ValueExpressionLiteral(validatedValue));
-
-        try {
-            if (EXPRESSION_FACTORY != null) {
-                final String tmp = resolvedMessage.replace("#{", "\\#{"); // shouldn't be
evaluated
-                resolvedMessage = EXPRESSION_FACTORY.createValueExpression(context, tmp,
String.class).getValue(context).toString();
-            }
-        } catch (final Exception e) {
-            // no-op
+        // EL handling
+        if (evaluator != null) {
+            resolvedMessage = evaluator.interpolate(resolvedMessage, annotationParameters,
validatedValue);
         }
 
         // curly braces need to be scaped in the original msg, so unescape them now
@@ -367,124 +329,4 @@ public class DefaultMessageInterpolator 
             return action.run();
         }
     }
-
-    private static class BValELContext extends ELContext {
-        private final ELResolver resolver;
-        private final FunctionMapper functions;
-        private final VariableMapper variables;
-
-        public BValELContext(final ELResolver resolver) {
-            this.resolver = resolver;
-            this.variables = new BValVariableMapper();
-            this.functions = new BValFunctionMapper();
-        }
-
-        @Override
-        public ELResolver getELResolver() {
-            return resolver;
-        }
-
-        @Override
-        public FunctionMapper getFunctionMapper() {
-            return functions;
-        }
-
-        @Override
-        public VariableMapper getVariableMapper() {
-            return variables;
-        }
-    }
-
-    private static class BValFunctionMapper extends FunctionMapper {
-        @Override
-        public Method resolveFunction(final String prefix, final String localName) {
-            return null;
-        }
-    }
-
-    private static class BValVariableMapper extends VariableMapper {
-        private final Map<String, ValueExpression> variables = new HashMap<String,
ValueExpression>();
-
-        @Override
-        public ValueExpression resolveVariable(final String variable) {
-            if ("formatter".equals(variable)) {
-                return new ValueExpressionLiteral(new BValFormatter());
-            }
-            return variables.get(variable);
-        }
-
-        @Override
-        public ValueExpression setVariable(final String variable, final ValueExpression expression)
{
-            variables.put(variable, expression);
-            return expression;
-        }
-    }
-
-    // used to not expose all method and avoid ambiguity with format(Local, String, Object...)
in EL
-    public static class BValFormatter {
-        private final Formatter formatter = new Formatter();
-
-        public Formatter format(final String format, final Object ... args) {
-            return formatter.format(format, args);
-        }
-    }
-
-    private static final class ValueExpressionLiteral extends ValueExpression {
-        private final Object value;
-
-        public ValueExpressionLiteral(final Object value) {
-            this.value = value;
-        }
-
-        @Override
-        public Object getValue(final ELContext context) {
-            return value;
-        }
-
-        @Override
-        public void setValue(final ELContext context, final Object value) {
-            throw new PropertyNotWritableException(value.toString());
-        }
-
-        @Override
-        public boolean isReadOnly(final ELContext context) {
-            return true;
-        }
-
-        @Override
-        public Class<?> getType(final ELContext context) {
-            return (this.value != null) ? this.value.getClass() : null;
-        }
-
-        @Override
-        public Class<?> getExpectedType() {
-            return String.class;
-        }
-
-        @Override
-        public String getExpressionString() {
-            return (this.value != null) ? this.value.toString() : null;
-        }
-
-        @Override
-        public boolean equals(Object obj) {
-            return (obj instanceof ValueExpressionLiteral && this
-                    .equals((ValueExpressionLiteral) obj));
-        }
-
-        public boolean equals(final ValueExpressionLiteral ve) {
-            return (ve != null && (this.value != null && ve.value != null
&& (this.value == ve.value || this.value
-                    .equals(ve.value))));
-        }
-
-        @Override
-        public int hashCode() {
-            return (this.value != null) ? this.value.hashCode() : 0;
-        }
-
-        @Override
-        public boolean isLiteralText() {
-            return true;
-        }
-    }
 }



Mime
View raw message