Author: alexsmirnov
Date: 2010-11-10 17:36:12 -0500 (Wed, 10 Nov 2010)
New Revision: 20025
Added:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/application/
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/el/
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidator.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesBeanValidator.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesServiceBase.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/MessageFactory.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/NullValidator.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ObjectValidator.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/model/
branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/services/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ELTestBase.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/TestCaptureEL.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/model/
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/validator/
branches/RF-8742-1/ui/validator/ui/src/test/resources/badcsv.xml
branches/RF-8742-1/ui/validator/ui/src/test/resources/csv.xml
branches/RF-8742-1/ui/validator/ui/src/test/resources/test.html
Removed:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/application/
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/el/
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidator.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesBeanValidator.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/MessageFactory.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/NullValidator.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ObjectValidator.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/model/
branches/RF-8742-1/ui/validator/impl/src/main/resources/META-INF/services/
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ELTestBase.java
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/TestCaptureEL.java
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/model/
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/validator/
branches/RF-8742-1/ui/validator/impl/src/test/resources/badcsv.xml
branches/RF-8742-1/ui/validator/impl/src/test/resources/csv.xml
branches/RF-8742-1/ui/validator/impl/src/test/resources/test.html
Log:
CODING IN PROGRESS - issue RF-9799: CSV code review
https://jira.jboss.org/browse/RF-9799
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,42 +0,0 @@
-package org.richfaces.validator;
-
-import java.util.Map;
-
-import javax.faces.application.FacesMessage;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
-
-public class BaseFacesObjectDescriptor<T> {
-
- private final Class<? extends T> converterClass;
- private Map<String, Object> parameters = Maps.newHashMap();
- private final FacesMessage message;
-
-
- public BaseFacesObjectDescriptor(Class<? extends T> converterClass,
FacesMessage message) {
- this.converterClass = converterClass;
- this.message = message;
- }
-
- public Class<?> getImplementationClass() {
- return converterClass;
- }
-
- public Map<String, ? extends Object> getAdditionalParameters() {
- return parameters;
- }
-
- public FacesMessage getMessage() {
- return message;
- }
-
- protected void addParameter(String name, Object value) {
- parameters.put(name, value);
- }
-
- protected void makeImmutable() {
- parameters = ImmutableMap.copyOf(parameters);
- }
-
-}
\ No newline at end of file
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidator.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidator.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,110 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Set;
-
-import javax.faces.FacesException;
-import javax.faces.context.FacesContext;
-import javax.validation.ConstraintViolation;
-import javax.validation.Validator;
-import javax.validation.groups.Default;
-
-/**
- * @author asmirnov
- *
- */
-public class BeanValidator extends ObjectValidator {
-
- private static final Class<?>[] DEFAULT_PROFILE = new Class[] {Default.class};
- private final BeanValidatorFactory validatorFactory;
-
-
- public BeanValidator(BeanValidatorFactory validatorFactory) {
- this.validatorFactory = validatorFactory;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.validator.ObjectValidator#validate(java.lang.Object,
java.lang.String, java.lang.Object,
- * java.util.Locale)
- */
- @Override
- protected Collection<String> validate(FacesContext facesContext, Object base,
String property, Object value,
- Set<String> profiles) {
- Class beanType = base.getClass();
- Set<ConstraintViolation<Object>> constrains =
- getValidator(facesContext).validateValue(beanType, property, value,
getGroups(profiles));
- return extractMessages(constrains);
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.validator.ObjectValidator#validateGraph(javax.faces.context
.FacesContext, java.lang.Object,
- * java.util.Set)
- */
- @Override
- public Collection<String> validateGraph(FacesContext context, Object value,
Set<String> profiles) {
- Class<?>[] groups = getGroups(profiles);
- Set<ConstraintViolation<Object>> violations =
getValidator(context).validate(value, groups);
- Collection<String> messages = extractMessages(violations);
- if (null != parent) {
- Collection<String> parentMessages = parent.validateGraph(context,
value, profiles);
- if (null != messages) {
- if (null != parentMessages) {
- messages.addAll(parentMessages);
- }
- } else {
- messages = parentMessages;
- }
- }
- return messages;
- }
-
- private Class<?>[] getGroups(Set<String> profiles) {
- Class<?>[] groups = null;
- if (null != profiles) {
- groups = new Class<?>[profiles.size()];
- int i = 0;
- for (String group : profiles) {
- try {
- groups[i] = Class.forName(group, false,
Thread.currentThread().getContextClassLoader());
- } catch (ClassNotFoundException e) {
- try {
- groups[i] = Class.forName(group);
- } catch (ClassNotFoundException e1) {
- throw new FacesException("Bean validation group not found
" + group, e1);
- }
- }
- i++;
- }
-
- } else {
- groups = DEFAULT_PROFILE;
- }
- return groups;
- }
-
- private Collection<String>
extractMessages(Set<ConstraintViolation<Object>> violations) {
- Collection<String> messages = null;
- if (null != violations && violations.size() > 0) {
- messages = new ArrayList<String>(violations.size());
- for (ConstraintViolation<? extends Object> constraintViolation :
violations) {
- messages.add(constraintViolation.getMessage());
- }
-
- }
- return messages;
- }
-
- protected Validator getValidator(FacesContext facesContext) {
- Validator beanValidator = validatorFactory.getValidator(facesContext);
- return beanValidator;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,13 +0,0 @@
-package org.richfaces.validator;
-
-import java.lang.annotation.Annotation;
-
-import javax.faces.application.FacesMessage;
-
-public class BeanValidatorDescriptor extends BaseFacesObjectDescriptor<Annotation>
implements ValidatorDescriptor {
-
- public BeanValidatorDescriptor(Class<? extends Annotation> validatorClass,
FacesMessage message) {
- super(validatorClass, message);
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,100 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.lang.annotation.Annotation;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.faces.application.FacesMessage;
-import javax.faces.context.FacesContext;
-import javax.validation.Validator;
-import javax.validation.metadata.ConstraintDescriptor;
-import javax.validation.metadata.ElementDescriptor.ConstraintFinder;
-import javax.validation.metadata.PropertyDescriptor;
-
-import org.richfaces.el.ValueDescriptor;
-import org.richfaces.el.ValueExpressionAnalayser;
-
-/**
- * @author asmirnov
- *
- */
-public class BeanValidatorServiceImpl implements BeanValidatorService {
-
- private final ValueExpressionAnalayser analayser;
- private final BeanValidatorFactory validatorFactory;
-
- public BeanValidatorServiceImpl(ValueExpressionAnalayser analayser,
BeanValidatorFactory validatorFactory) {
- this.analayser = analayser;
- this.validatorFactory = validatorFactory;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
org.richfaces.validator.BeanValidatorService#getConstrains(javax.faces.context.FacesContext,
- * javax.el.ValueExpression, java.lang.Class<?>[])
- */
- public Collection<ValidatorDescriptor> getConstrains(FacesContext context,
ValueExpression expression,
- Class<?>... groups) {
- try {
- ValueDescriptor propertyDescriptor = analayser.getPropertyDescriptor(context,
expression);
- return processBeanAttribute(context, propertyDescriptor, groups);
- } catch (ELException e) {
- return Collections.emptySet();
- }
- }
-
- private Validator getValidator(FacesContext context) {
- return validatorFactory.getValidator(context);
- }
-
- Collection<ValidatorDescriptor> processBeanAttribute(FacesContext context,
ValueDescriptor descriptor,
- Class<?>... groups) {
- PropertyDescriptor constraintsForProperty =
-
getValidator(context).getConstraintsForClass(descriptor.getBeanType()).getConstraintsForProperty(
- descriptor.getName());
- if (null != constraintsForProperty) {
- ConstraintFinder propertyConstraints =
constraintsForProperty.findConstraints();
- if (null != groups && groups.length > 0) {
- // Filter groups, if required
- propertyConstraints =
propertyConstraints.unorderedAndMatchingGroups(groups);
- }
- Set<ConstraintDescriptor<?>> constraints = propertyConstraints //
or the requested list of groups)
- .getConstraintDescriptors();
-
- // ContextHolder is an arbitrary object, it will depend on the
implementation
- Set<ValidatorDescriptor> descriptors = new
HashSet<ValidatorDescriptor>(constraints.size());
- processConstraints(context, constraints, descriptors);
- return descriptors;
-
- } else {
- return Collections.emptySet();
- }
- }
-
- void processConstraints(FacesContext context,
Set<ConstraintDescriptor<?>> constraints,
- Collection<ValidatorDescriptor> descriptors) {
- for (ConstraintDescriptor<?> cd : constraints) {
- Annotation a = cd.getAnnotation();
- Map<String, Object> parameters = cd.getAttributes();
- // TODO if cd.isReportedAsSingleConstraint() make sure than only the root
constraint raises an error message
- // if one or several of the composing constraints are invalid)
- FacesMessage message = validatorFactory.interpolateMessage(context, cd);
- BeanValidatorDescriptor beanValidatorDescriptor = new
BeanValidatorDescriptor(a.getClass(), message);
- for (Map.Entry<String, Object> entry : parameters.entrySet()) {
- beanValidatorDescriptor.addParameter(entry.getKey(), entry.getValue());
- }
- beanValidatorDescriptor.makeImmutable();
- descriptors.add(beanValidatorDescriptor);
- processConstraints(context, cd.getComposingConstraints(), descriptors); //
process the composing constraints
- }
- }
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,74 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.util.Map;
-
-import javax.faces.application.Resource;
-import javax.faces.application.ResourceHandler;
-import javax.faces.context.FacesContext;
-
-import org.richfaces.component.util.Strings;
-
-/**
- * @author asmirnov
- *
- */
-public class ClientScriptServiceImpl implements ClientScriptService {
-
- private static final String TEXT_JAVASCRIPT = "text/javascript";
-
- private static final String ORG_RICHFACES_CSV = "org.richfaces.csv";
-
- private final Map<Class<?>, LibraryFunction> defaultMapping;
-
- public ClientScriptServiceImpl(Map<Class<?>, LibraryFunction>
defaultMapping) {
- this.defaultMapping = defaultMapping;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.validator.ClientScriptService#getScript(java.lang.Class)
- */
- public LibraryFunction getScript(FacesContext facesContext, Class<?> javaClass)
throws ScriptNotFoundException {
- if (null == facesContext || null == javaClass) {
- throw new NullPointerException();
- }
- LibraryFunction function;
- try {
- function = getScriptResource(facesContext, javaClass);
- } catch (ScriptNotFoundException e) {
- if (defaultMapping.containsKey(javaClass)) {
- function = defaultMapping.get(javaClass);
- } else {
- function = getScriptFromAnnotation(javaClass);
- }
- }
- return function;
- }
-
- private LibraryFunction getScriptFromAnnotation(Class<?> javaClass) throws
ScriptNotFoundException {
- if (javaClass.isAnnotationPresent(ClientSideScript.class)) {
- ClientSideScript clientSideScript =
javaClass.getAnnotation(ClientSideScript.class);
- return new LibraryFunctionImplementation(clientSideScript.library(),
clientSideScript.resource(), clientSideScript.function());
- } else {
- throw new ScriptNotFoundException();
- }
- }
-
- private LibraryFunction getScriptResource(FacesContext facesContext, Class<?>
javaClass)
- throws ScriptNotFoundException {
- ResourceHandler resourceHandler =
facesContext.getApplication().getResourceHandler();
- String resourceName = javaClass.getSimpleName() + ".js";
- Resource facesResource = resourceHandler.createResource(resourceName,
ORG_RICHFACES_CSV, TEXT_JAVASCRIPT);
- if (null != facesResource) {
- final String functionName =
Strings.firstToLowerCase(javaClass.getSimpleName());
- return new LibraryFunctionImplementation(ORG_RICHFACES_CSV,resourceName,
functionName);
- } else {
- throw new ScriptNotFoundException();
- }
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,67 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.io.IOException;
-import java.net.URL;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.Map;
-
-import javax.faces.FacesException;
-import javax.xml.bind.JAXB;
-
-import org.richfaces.validator.model.ClientSideScripts;
-import org.richfaces.validator.model.Component;
-
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableMap.Builder;
-import com.google.common.collect.Maps;
-
-/**
- * @author asmirnov
- *
- */
-public final class ClientServiceConfigParser {
-
- private ClientServiceConfigParser() {
- }
-
- public static Map<Class<?>, LibraryFunction> parseConfig(String name) {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (null == loader) {
- loader = ClientServiceConfigParser.class.getClassLoader();
- }
- Builder<Class<?>, LibraryFunction> resultBuilder =
ImmutableMap.builder();
- try {
- Enumeration<URL> resources = loader.getResources(name);
- while (resources.hasMoreElements()) {
- URL url = (URL) resources.nextElement();
- resultBuilder.putAll(parse(loader, url));
- }
- } catch (IOException e) {
- return Collections.emptyMap();
- }
- return resultBuilder.build();
- }
-
- static Map<Class<?>, LibraryFunction> parse(ClassLoader loader, URL url)
{
- Map<Class<?>, LibraryFunction> result = Maps.newHashMap();
- try {
- ClientSideScripts clientSideScripts = JAXB.unmarshal(url,
ClientSideScripts.class);
- for (Component component : clientSideScripts.getComponent()) {
- Class<?> componentClass = loader.loadClass(component.getType());
- LibraryFunctionImplementation function = new
LibraryFunctionImplementation(component.getLibrary(),
- component.getResource(), component.getFunction());
- result.put(componentClass, function);
- }
- } catch (ClassNotFoundException e) {
- throw new FacesException("Class for component not found",e);
- } catch (Exception e) {
- throw new FacesException("Error parsing config file "+url,e);
- }
- return result;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClonedObjectResolver.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClonedObjectResolver.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,110 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.beans.FeatureDescriptor;
-import java.util.Iterator;
-import java.util.Map;
-
-import javax.el.ELContext;
-import javax.el.ELResolver;
-import javax.faces.context.FacesContext;
-
-
-
-/**
- * @author asmirnov
- *
- */
-public class ClonedObjectResolver extends ELResolver {
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#getCommonPropertyType(javax.el.ELContext,
java.lang.Object)
- */
- @Override
- public Class<?> getCommonPropertyType(ELContext context, Object base) {
- // Do nothing
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#getFeatureDescriptors(javax.el.ELContext,
java.lang.Object)
- */
- @Override
- public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
- // do nothing
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#getType(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- @Override
- public Class<?> getType(ELContext context, Object base, Object property) {
- Object cloned = resolveCloned(context, base, property);
- if (null != cloned) {
- context.setPropertyResolved(true);
- return cloned.getClass();
- }
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#getValue(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- @Override
- public Object getValue(ELContext context, Object base, Object property) {
- Object cloned = resolveCloned(context, base, property);
- if (null != cloned) {
- context.setPropertyResolved(true);
- }
- return cloned;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#isReadOnly(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- @Override
- public boolean isReadOnly(ELContext context, Object base, Object property) {
- // TODO Auto-generated method stub
- return false;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.el.ELResolver#setValue(javax.el.ELContext, java.lang.Object,
java.lang.Object, java.lang.Object)
- */
- @Override
- public void setValue(ELContext context, Object base, Object property, Object value)
{
- // TODO Auto-generated method stub
-
- }
-
- public static Object resolveCloned(ELContext context, Object base, Object property)
{
- if (null != base || null != property) {
- FacesContext facesContext = FacesContext.getCurrentInstance();
- Map<String, Object> requestMap =
facesContext.getExternalContext().getRequestMap();
- for (String key : requestMap.keySet()) {
- if (null != key &&
key.startsWith(GraphValidatorState.STATE_ATTRIBUTE_PREFIX)) {
- GraphValidatorState state = (GraphValidatorState)
requestMap.get(key);
- if (state.isSame(base, property)) {
- return state.getCloned();
- }
- }
- }
- }
- return null;
- }
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,22 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-
-import javax.faces.application.FacesMessage;
-import javax.faces.convert.Converter;
-
-
-/**
- * @author asmirnov
- *
- */
-public class ConverterDescriptorImpl extends BaseFacesObjectDescriptor<Converter>
implements ConverterDescriptor {
-
-
- ConverterDescriptorImpl(Class<? extends Converter> converterClass,FacesMessage
message) {
- super(converterClass,message);
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,86 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIInput;
-import javax.faces.context.FacesContext;
-import javax.faces.convert.BigDecimalConverter;
-import javax.faces.convert.BigIntegerConverter;
-import javax.faces.convert.BooleanConverter;
-import javax.faces.convert.ByteConverter;
-import javax.faces.convert.CharacterConverter;
-import javax.faces.convert.Converter;
-import javax.faces.convert.DateTimeConverter;
-import javax.faces.convert.DoubleConverter;
-import javax.faces.convert.EnumConverter;
-import javax.faces.convert.FloatConverter;
-import javax.faces.convert.IntegerConverter;
-import javax.faces.convert.LongConverter;
-import javax.faces.convert.NumberConverter;
-import javax.faces.convert.ShortConverter;
-
-
-/**
- * @author asmirnov
- *
- */
-public class ConverterServiceImpl extends FacesServiceBase<Converter> implements
FacesConverterService {
-
- private static final String DEFAULT_CONVERTER_MESSAGE_ID =
UIInput.CONVERSION_MESSAGE_ID;
-
- /*
- * (non-Javadoc)
- *
- * @see
org.richfaces.validator.FacesConverterService#getConverterDescription(javax.faces.context.FacesContext,
- * javax.faces.convert.Converter)
- */
- public ConverterDescriptor getConverterDescription(FacesContext context, Converter
converter) {
- // determine converter message.
- FacesMessage message = getMessage(context, converter);
- ConverterDescriptorImpl descriptor = new
ConverterDescriptorImpl(converter.getClass(), message);
- fillParameters(descriptor, converter);
- descriptor.makeImmutable();
- return descriptor;
- }
-
- @Override
- protected String getMessageId(Converter converter) {
- String messageId;
- if (converter instanceof BigDecimalConverter) {
- messageId = BigDecimalConverter.DECIMAL_ID;
- } else if (converter instanceof BigIntegerConverter) {
- messageId = BigIntegerConverter.BIGINTEGER_ID;
- } else if (converter instanceof BooleanConverter) {
- messageId = BooleanConverter.BOOLEAN_ID;
- } else if (converter instanceof ByteConverter) {
- messageId = ByteConverter.BYTE_ID;
- } else if (converter instanceof CharacterConverter) {
- messageId = CharacterConverter.CHARACTER_ID;
- } else if (converter instanceof DateTimeConverter) {
- // TODO - distinguish Date, Time, and DateTime.
- messageId = DateTimeConverter.DATETIME_ID;
- } else if (converter instanceof DoubleConverter) {
- messageId = DoubleConverter.DOUBLE_ID;
- } else if (converter instanceof EnumConverter) {
- messageId = EnumConverter.ENUM_ID;
- } else if (converter instanceof FloatConverter) {
- messageId = FloatConverter.FLOAT_ID;
- } else if (converter instanceof IntegerConverter) {
- messageId = IntegerConverter.INTEGER_ID;
- } else if (converter instanceof LongConverter) {
- messageId = LongConverter.LONG_ID;
- } else if (converter instanceof NumberConverter) {
- // TODO - detect case ( currency, percent etc ).
- messageId = NumberConverter.NUMBER_ID;
- } else if (converter instanceof ShortConverter) {
- messageId = ShortConverter.SHORT_ID;
- } else {
- messageId = DEFAULT_CONVERTER_MESSAGE_ID;
- }
- return messageId;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,26 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.util.Collection;
-import java.util.Collections;
-
-import javax.el.ValueExpression;
-import javax.faces.context.FacesContext;
-
-/**
- * @author asmirnov
- *
- */
-public class DummyBeanValidatorService implements BeanValidatorService {
-
- /* (non-Javadoc)
- * @see
org.richfaces.validator.BeanValidatorService#getConstrains(javax.faces.context.FacesContext,
javax.el.ValueExpression, java.lang.Class<?>[])
- */
- public Collection<ValidatorDescriptor> getConstrains(FacesContext context,
ValueExpression expression,
- Class<?>... groups) {
- return Collections.emptySet();
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesBeanValidator.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,178 +0,0 @@
-/**
- * License Agreement.
- *
- * Rich Faces - Natural Ajax for Java Server Faces (JSF)
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-package org.richfaces.validator;
-
-import java.io.Serializable;
-import java.text.MessageFormat;
-import java.util.Collection;
-import java.util.Locale;
-import java.util.Set;
-
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.faces.FacesException;
-import javax.faces.application.FacesMessage;
-import javax.faces.component.EditableValueHolder;
-import javax.faces.component.UIComponent;
-import javax.faces.context.FacesContext;
-import javax.faces.validator.Validator;
-import javax.faces.validator.ValidatorException;
-
-/**
- * Implementation of the JSF validator to use with Bean Validation / Hibernate validator
- *
- * @author asmirnov
- *
- */
-public class FacesBeanValidator implements Serializable, Validator, GraphValidator {
-
- public static final String BEAN_VALIDATOR_TYPE =
"org.richfaces.BeanValidator";
- /**
- *
- */
- private static final long serialVersionUID = -264568176252121853L;
- private ValueExpression summaryExpression = null;
-
- private String summary = null;
-
- private ValueExpression profilesExpression = null;
-
- private Set<String> profiles = null;
-
- /**
- * @return the summary
- */
- public String getSummary() {
- String summaryString = null;
- if (null != summaryExpression) {
- summaryString = (String)
summaryExpression.getValue(FacesContext.getCurrentInstance().getELContext());
- } else {
- summaryString = this.summary;
- }
- return summaryString;
- }
-
- /**
- * @param summary
- * the summary to set
- */
- public void setSummary(ValueExpression summary) {
- this.summaryExpression = summary;
- }
-
- /**
- * @param summary
- * the summary to set
- */
- public void setSummary(String summary) {
- this.summary = summary;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext,
- * javax.faces.component.UIComponent, java.lang.Object)
- */
- public void validate(FacesContext context, UIComponent component, Object
convertedValue) throws ValidatorException {
- if (component instanceof EditableValueHolder) {
- // Validate input component
- EditableValueHolder input = (EditableValueHolder) component;
- try {
- ValueExpression valueExpression =
component.getValueExpression("value");
- if (null != valueExpression) {
- Collection<String> messages =
- ObjectValidator.getInstance(context).validate(context,
valueExpression, convertedValue,
- getProfiles());
- if (null != messages) {
- input.setValid(false);
- Object label = getLabel(context, component);
- Locale locale = context.getViewRoot().getLocale();
- // send all validation messages.
- for (String msg : messages) {
- //
https://jira.jboss.org/jira/browse/RF-7636 -
- // format values.
- String formattedMessage = formatMessage(msg, locale, label,
convertedValue); // create Summary message ?
- String summaryString = getSummary() != null ? getSummary() :
formattedMessage;
- summaryString = formatMessage(summaryString, locale, label,
convertedValue);
- context.addMessage(component.getClientId(context), new
FacesMessage(
- FacesMessage.SEVERITY_ERROR, summaryString,
formattedMessage));
- }
- }
- }
- } catch (ELException e) {
- throw new FacesException(e);
- }
- }
- }
-
- static String formatMessage(String msg, Locale locale, Object... messageParams) {
- if (msg.contains("{")) {
- try {
- MessageFormat messageFormat = new MessageFormat(msg, locale);
- msg = messageFormat.format(messageParams);
- } catch (IllegalArgumentException e) {
- // Do nothing, use original string unchanged.
- }
- }
- return msg;
- }
-
- static Object getLabel(FacesContext context, UIComponent component) {
- Object label = component.getAttributes().get("label");
- if (null == label || 0 == label.toString().length()) {
- label = component.getClientId(context);
- }
- return label;
- }
-
- public Collection<String> validateGraph(FacesContext context, UIComponent
component, Object value,
- Set<String> profiles) throws ValidatorException {
- ObjectValidator beanValidator = ObjectValidator.getInstance(context);
- Collection<String> messages = beanValidator.validateGraph(context, value,
profiles);
- return messages;
- }
-
- /**
- * @return the profiles
- */
- public Set<String> getProfiles() {
- Set<String> profiles;
- if (null != profilesExpression) {
- profiles = (Set<String>)
profilesExpression.getValue(FacesContext.getCurrentInstance().getELContext());
- } else {
- profiles = this.profiles;
- }
- return profiles;
- }
-
- /**
- * @param profiles
- * the profiles to set
- */
- public void setProfiles(Set<String> profiles) {
- this.profiles = profiles;
- }
-
- public void setProfiles(ValueExpression profilesExpression) {
- this.profilesExpression = profilesExpression;
- }
-}
\ No newline at end of file
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,60 +0,0 @@
-package org.richfaces.validator;
-
-import java.beans.BeanInfo;
-import java.beans.IntrospectionException;
-import java.beans.Introspector;
-import java.beans.PropertyDescriptor;
-import java.lang.reflect.InvocationTargetException;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.context.FacesContext;
-
-import com.google.common.collect.ImmutableSet;
-
-public abstract class FacesServiceBase<T> {
-
- private static final ImmutableSet<String> HIDDEN_PROPERTIES =
ImmutableSet.of("class", "transient");
-
- protected abstract String getMessageId(T component);
-
-
- protected void fillParameters(BaseFacesObjectDescriptor<T> descriptor, T
component) {
- // get bean attributes for converter, put them into parameters.
- try {
- BeanInfo beanInfo = Introspector.getBeanInfo(component.getClass());
- PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
- for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
- String name = propertyDescriptor.getName();
- if (!HIDDEN_PROPERTIES.contains(name)) {
- try {
- Object value =
propertyDescriptor.getReadMethod().invoke(component);
- if (null != value) {
- descriptor.addParameter(name, value);
- }
- } catch (IllegalArgumentException e) {
- // Ignore
- } catch (IllegalAccessException e) {
- // Ignore
- } catch (InvocationTargetException e) {
- // Ignore
- }
- }
- }
- } catch (IntrospectionException e) {
- // Ignore.
- }
- }
-
- /**
- * Creates message for converter, using current locale.
- *
- * @param context
- * @param component
- * @return
- */
- protected FacesMessage getMessage(FacesContext context, T component) {
- String messageId = getMessageId(component);
- return MessageFactory.createMessage(context, messageId);
- }
-
-}
\ No newline at end of file
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,12 +0,0 @@
-package org.richfaces.validator;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.validator.Validator;
-
-public class FacesValidatorDescriptor extends BaseFacesObjectDescriptor<Validator>
implements ValidatorDescriptor {
-
- public FacesValidatorDescriptor(Class<? extends Validator> validatorClass,
FacesMessage message) {
- super(validatorClass, message);
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,55 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIInput;
-import javax.faces.context.FacesContext;
-import javax.faces.validator.DoubleRangeValidator;
-import javax.faces.validator.LengthValidator;
-import javax.faces.validator.LongRangeValidator;
-import javax.faces.validator.RegexValidator;
-import javax.faces.validator.RequiredValidator;
-import javax.faces.validator.Validator;
-
-/**
- * @author asmirnov
- *
- */
-public class FacesValidatorServiceImpl extends FacesServiceBase<Validator>
implements FacesValidatorService {
-
- /*
- * (non-Javadoc)
- *
- * @see
org.richfaces.validator.FacesValidatorService#getValidatorDescription(javax.faces.context.FacesContext,
- * javax.faces.validator.Validator)
- */
- public ValidatorDescriptor getValidatorDescription(FacesContext context, Validator
validator) {
- FacesMessage message = getMessage(context, validator);
- FacesValidatorDescriptor descriptor = new
FacesValidatorDescriptor(validator.getClass(), message);
- fillParameters(descriptor, validator);
- descriptor.makeImmutable();
- return descriptor;
- }
-
- @Override
- protected String getMessageId(Validator component) {
- String messageId;
- if (component instanceof DoubleRangeValidator) {
- messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
- } else if (component instanceof LengthValidator) {
- messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
- } else if (component instanceof LongRangeValidator) {
- messageId = LongRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
- } else if (component instanceof RegexValidator) {
- messageId = RegexValidator.NOT_MATCHED_MESSAGE_ID;
- } else if (component instanceof RequiredValidator) {
- messageId = UIInput.REQUIRED_MESSAGE_ID;
- } else {
- messageId = UIInput.UPDATE_MESSAGE_ID;
- }
- return messageId;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,24 +0,0 @@
-package org.richfaces.validator;
-
-final class LibraryFunctionImplementation implements LibraryFunction {
- private final LibraryResource library;
- private final String functionName;
-
- LibraryFunctionImplementation(LibraryResource library, String functionName) {
- this.library = library;
- this.functionName = functionName;
- }
-
- LibraryFunctionImplementation(String library, String resource, String functionName)
{
- this.library = new LibraryResource(library, resource);
- this.functionName = functionName;
- }
-
- public LibraryResource getResource() {
- return library;
- }
-
- public String getName() {
- return functionName;
- }
-}
\ No newline at end of file
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/MessageFactory.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/MessageFactory.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/MessageFactory.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,75 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.util.Locale;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIViewRoot;
-import javax.faces.context.FacesContext;
-
-/**
- * @author asmirnov
- *
- */
-public final class MessageFactory {
-
- private MessageFactory(){}
-
- public static FacesMessage createMessage(FacesContext context, String messageId) {
- Locale locale = getCurrentLocale(context);
- String messageBundle = context.getApplication().getMessageBundle();
- FacesMessage message;
- try {
- if (null != messageBundle) {
- try {
- message = getMessageFromBundle(locale, messageBundle, messageId);
- } catch (MissingResourceException e) {
- message = getMessageFromBundle(locale, FacesMessage.FACES_MESSAGES,
messageId);
- }
- } else {
- message = getMessageFromBundle(locale, FacesMessage.FACES_MESSAGES,
messageId);
- }
- } catch (MissingResourceException e) {
- // No bundles at all, fall back message.
- return new FacesMessage("conversion error");
- }
- return message;
- }
-
- private static FacesMessage getMessageFromBundle(Locale locale, String messageBundle,
String messageId)
- throws MissingResourceException {
- ResourceBundle bundle = ResourceBundle.getBundle(messageBundle, locale,
getCurrentLoader(messageId));
- String summary = bundle.getString(messageId);
- String detail;
- try {
- detail = bundle.getString(messageId + "_detail");
- } catch (MissingResourceException e) {
- return new FacesMessage(summary);
- }
- return new FacesMessage(summary, detail);
- }
-
- public static Locale getCurrentLocale(FacesContext context) {
- UIViewRoot viewRoot = context.getViewRoot();
- Locale locale;
- if (null == viewRoot) {
- locale = Locale.getDefault();
- } else {
- locale = viewRoot.getLocale();
- }
- return locale;
- }
-
- private static ClassLoader getCurrentLoader(Object fallbackClass) {
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- if (loader == null) {
- loader = fallbackClass.getClass().getClassLoader();
- }
- return loader;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/NullValidator.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/NullValidator.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/NullValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,42 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.util.Collection;
-import java.util.Set;
-
-import javax.faces.context.FacesContext;
-
-/**
- * @author asmirnov
- *
- */
-public class NullValidator extends ObjectValidator {
-
- /*
- * (non-Javadoc)
- *
- * @see org.richfaces.validator.ObjectValidator#validate(java.lang.Object,
java.lang.String, java.lang.Object,
- * java.util.Locale)
- */
- @Override
- protected Collection<String> validate(FacesContext facesContext, Object base,
String property, Object value,
- Set<String> profiles) {
- // do nothing.
- return null;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see
org.richfaces.validator.ObjectValidator#validateGraph(javax.faces.context.FacesContext,
java.lang.Object,
- * java.util.Set)
- */
- @Override
- public Collection<String> validateGraph(FacesContext context, Object value,
Set<String> profiles) {
- // do nothing
- return null;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ObjectValidator.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ObjectValidator.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ObjectValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,420 +0,0 @@
-package org.richfaces.validator;
-
-import java.beans.FeatureDescriptor;
-import java.util.Collection;
-import java.util.EmptyStackException;
-import java.util.Iterator;
-import java.util.Locale;
-import java.util.Map;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-import java.util.Set;
-import java.util.Stack;
-
-import javax.el.ELContext;
-import javax.el.ELException;
-import javax.el.ELResolver;
-import javax.el.ValueExpression;
-import javax.faces.FacesException;
-import javax.faces.application.Application;
-import javax.faces.context.FacesContext;
-
-import org.richfaces.application.ServiceTracker;
-import org.richfaces.el.ELContextWrapper;
-
-public abstract class ObjectValidator {
-
- private static final String RESOURCE_BUNDLE_IS_NOT_REGISTERED_FOR_CURRENT_LOCALE =
- "Resource bundle is not registered for current locale";
-
- private static final String FACES_CONTEXT_IS_NULL = "Faces context is
null";
- private static final String INPUT_PARAMETERS_IS_NOT_CORRECT = "Input parameters
is not correct.";
- private static final String LOCALE_IS_NOT_SET = "Locale is not set";
- private static final String VIEW_ROOT_IS_NOT_INITIALIZED = "ViewRoot is not
initialized";
-
- protected final ObjectValidator parent;
-
- ObjectValidator() {
- this.parent = null;
- }
-
- ObjectValidator(ObjectValidator parent) {
- this.parent = parent;
- }
-
- /**
- * Return BeanValidator object from a ServletContext attribute. Create new instance
if none is defined.
- *
- * @param context
- * @return
- */
- public static ObjectValidator getInstance(FacesContext context) {
- return ServiceTracker.getService(context, ObjectValidator.class);
- }
-
- public abstract Collection<String> validateGraph(FacesContext context, Object
value, Set<String> profiles);
-
- /**
- * Perform Validation for a new value.
- *
- * @param context
- * current faces context.
- * @param target
- * {@link ValueExpression} for a value assignment.
- * @param value
- * new value for validation
- * @param profiles
- * TODO
- * @return null if no validation errors. Array of the validation messages otherwise.
- * @throws FacesException
- * if locale or context not properly initialized
- */
- public Collection<String> validate(FacesContext context, ValueExpression
target, Object value, Set<String> profiles) {
- if (null == context) {
- throw new FacesException(INPUT_PARAMETERS_IS_NOT_CORRECT);
- }
- Collection<String> validationMessages = null;
- if (null != target) {
- ELContext elContext = context.getELContext();
- ValidationResolver validationResolver =
- createValidationResolver(context, elContext.getELResolver(), profiles);
- ELContextWrapper wrappedElContext = new ELContextWrapper(elContext,
validationResolver);
- try {
- target.setValue(wrappedElContext, value);
- } catch (ELException e) {
- throw new FacesException(e);
- }
- if (!validationResolver.isValid()) {
- validationMessages = validationResolver.getValidationMessages();
- }
- if (null != parent) {
- Collection<String> parentMessages = parent.validate(context,
target, value, profiles);
- if (null != validationMessages) {
- if (null != parentMessages) {
- validationMessages.addAll(parentMessages);
- }
- } else {
- validationMessages = parentMessages;
- }
- }
-
- }
- return validationMessages;
- }
-
- /**
- * Validate bean property for a new value.
- *
- * @param facesContext
- * TODO
- * @param base
- * - bean
- * @param property
- * - bean property name.
- * @param value
- * new value.
- * @param profiles
- * TODO
- *
- * @return null for a valid value, array of the validation messages othervise.
- */
- protected abstract Collection<String> validate(FacesContext facesContext,
Object base, String property,
- Object value, Set<String> profiles);
-
- static Locale calculateLocale(FacesContext context) {
- if (null == context.getViewRoot()) {
- throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
- } else if (null == context.getViewRoot().getLocale()) {
- throw new FacesException(LOCALE_IS_NOT_SET);
- }
- Locale locale = context.getViewRoot().getLocale();
- return locale;
- }
-
- static ResourceBundle getResourceBundle(FacesContext facesContext, String name) {
- ResourceBundle bundle = null;
- if (null != facesContext) {
- Application application = facesContext.getApplication();
- try {
- bundle = application.getResourceBundle(facesContext, name);
-
- } catch (Exception e) {
- // Let one more attempt to load resource
- }
- }
- if (null == bundle) {
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
- if (null == classLoader) {
- classLoader = ObjectValidator.class.getClassLoader();
- }
- try {
- bundle = ResourceBundle.getBundle(name, calculateLocale(facesContext),
classLoader);
-
- } catch (MissingResourceException e) {
- // Do nothing, use default bundle.
- }
- }
- return bundle;
- }
-
- protected ValidationResolver createValidationResolver(FacesContext context,
ELResolver parent, Set<String> profiles) {
- return new ValidationResolver(parent, context, profiles);
- }
-
- /**
- * @author asmirnov
- *
- */
- protected static class BasePropertyPair {
- private final Object base;
- private final Object property;
-
- /**
- * @param base
- * @param property
- */
- public BasePropertyPair(Object base, Object property) {
- this.base = base;
- this.property = property;
- }
-
- /**
- * @return the base
- */
- public Object getBase() {
- return base;
- }
-
- /**
- * @return the property
- */
- public Object getProperty() {
- return property;
- }
-
- }
-
- /**
- * Class for identify validator instance by locale
- *
- * @author amarkhel
- *
- */
- protected static class ValidatorKey {
- private final Class<? extends Object> validatableClass;
- private final Locale locale;
-
- /**
- * Constructor for ValidatorKey object
- *
- * @param validatableClass
- * - class to validate
- * @param locale
- * - User locale to determine Resource bundle, used during validation
process
- */
- public ValidatorKey(Class<? extends Object> validatableClass, Locale
locale) {
- this.validatableClass = validatableClass;
- this.locale = locale;
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((this.locale == null) ? 0 :
this.locale.hashCode());
- result = prime * result + ((this.validatableClass == null) ? 0 :
this.validatableClass.hashCode());
- return result;
- }
-
- /* (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- ValidatorKey other = (ValidatorKey) obj;
- if (this.locale == null) {
- if (other.locale != null) {
- return false;
- }
- } else if (!this.locale.equals(other.locale)) {
- return false;
- }
- if (this.validatableClass == null) {
- if (other.validatableClass != null) {
- return false;
- }
- } else if (!this.validatableClass.equals(other.validatableClass)) {
- return false;
- }
- return true;
- }
-
-
-
- }
-
- /**
- * Wrapper class for a {@link ELResolver}. For a setValue method, perform validation
instead of real assignment.
- *
- * @author asmirnov
- *
- */
- final class ValidationResolver extends ELResolver {
-
- /**
- * Original resolver.
- */
- private final ELResolver parent;
-
- private boolean valid = true;
-
- private Collection<String> validationMessages = null;
-
- private Stack<BasePropertyPair> valuesStack;
-
- private Set<String> profiles;
-
- private FacesContext facesContext;
-
- private boolean clonedObject = false;
-
- /**
- * @param parent
- * @param context
- */
- public ValidationResolver(ELResolver parent, FacesContext context,
Set<String> profiles) {
- this.parent = parent;
- this.valuesStack = new Stack<BasePropertyPair>();
- this.profiles = profiles;
- this.facesContext = context;
- }
-
- public boolean isValid() {
- return valid;
- }
-
- /**
- * @param context
- * @param base
- * @return
- * @see javax.el.ELResolver#getCommonPropertyType(javax.el.ELContext,
java.lang.Object)
- */
- public Class<?> getCommonPropertyType(ELContext context, Object base) {
- return parent.getCommonPropertyType(context, base);
- }
-
- /**
- * @param context
- * @param base
- * @return
- * @see javax.el.ELResolver#getFeatureDescriptors(javax.el.ELContext,
java.lang.Object)
- */
- public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
- return parent.getFeatureDescriptors(context, base);
- }
-
- /**
- * @param context
- * @param base
- * @param property
- * @return
- * @see javax.el.ELResolver#getType(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- public Class<?> getType(ELContext context, Object base, Object property) {
- return parent.getType(context, base, property);
- }
-
- /**
- * @param context
- * @param base
- * @param property
- * @return
- * @see javax.el.ELResolver#getValue(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- public Object getValue(ELContext context, Object base, Object property) {
- Object value = ClonedObjectResolver.resolveCloned(context, base, property);
- if (null != value) {
- this.clonedObject = true;
- context.setPropertyResolved(true);
- } else {
- value = parent.getValue(context, base, property);
- }
- valuesStack.push(new BasePropertyPair(base, property));
- return value;
- }
-
- /**
- * @param context
- * @param base
- * @param property
- * @return
- * @see javax.el.ELResolver#isReadOnly(javax.el.ELContext, java.lang.Object,
java.lang.Object)
- */
- public boolean isReadOnly(ELContext context, Object base, Object property) {
- return parent.isReadOnly(context, base, property);
- }
-
- /**
- * @param context
- * @param base
- * @param property
- * @param value
- * @see javax.el.ELResolver#setValue(javax.el.ELContext, java.lang.Object,
java.lang.Object, java.lang.Object)
- */
- public void setValue(ELContext context, Object base, Object property, Object
value) {
- if (null != base && null != property) {
- // TODO - detect value object from inderect references ( e.g. data table
variables ).
- if (this.clonedObject) {
- parent.setValue(context, base, property, value);
- }
- context.setPropertyResolved(true);
- // For Arrays, Collection or Map use parent base and property.
- BasePropertyPair basePropertyPair = lookupBeanProperty(new
BasePropertyPair(base, property));
- base = basePropertyPair.getBase();
- property = basePropertyPair.getProperty();
- if (null != base && null != property) {
- //
https://jira.jboss.org/jira/browse/RF-4034
- // apache el looses locale information during value
- // resolution,
- // so we use our own
- validationMessages = validate(facesContext, base,
property.toString(), value, profiles);
- valid = null == validationMessages || 0 ==
validationMessages.size();
-
- }
- }
- }
-
- private BasePropertyPair lookupBeanProperty(BasePropertyPair pair) {
- Object base = pair.getBase();
- if (null != base && (base instanceof Collection || base instanceof
Map || base.getClass().isArray())) {
- try {
- pair = lookupBeanProperty(valuesStack.pop());
- } catch (EmptyStackException e) {
- // Do nothing, this is a first item.
- }
- }
- return pair;
- }
-
- /**
- * @return the validationMessages
- */
- public Collection<String> getValidationMessages() {
- return validationMessages;
- }
-
- }
-
-}
\ No newline at end of file
Deleted:
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,97 +0,0 @@
-/**
- *
- */
-package org.richfaces.validator;
-
-import java.lang.annotation.Annotation;
-import java.util.Locale;
-
-import javax.faces.application.FacesMessage;
-import javax.faces.component.UIInput;
-import javax.faces.context.FacesContext;
-import javax.validation.MessageInterpolator;
-import javax.validation.MessageInterpolator.Context;
-import javax.validation.Validation;
-import javax.validation.Validator;
-import javax.validation.ValidatorContext;
-import javax.validation.ValidatorFactory;
-import javax.validation.metadata.ConstraintDescriptor;
-
-/**
- * @author asmirnov
- *
- */
-public class RichFacesBeanValidatorFactory implements BeanValidatorFactory {
-
- private ValidatorFactory validatorFactory;
- private ValidatorContext validatorContext;
-
- public RichFacesBeanValidatorFactory() {
- // Enforce class to load
- ValidatorFactory.class.getName();
- // Check Factory, to avoid instantiation errors
- //
https://jira.jboss.org/jira/browse/RF-7226
- validatorFactory = Validation.buildDefaultValidatorFactory();
- validatorContext = validatorFactory.usingContext();
- MessageInterpolator jsfMessageInterpolator =
- new JsfMessageInterpolator(validatorFactory.getMessageInterpolator());
- validatorContext.messageInterpolator(jsfMessageInterpolator);
- }
-
- /* (non-Javadoc)
- * @see
org.richfaces.validator.BeanValidatorFactory#getValidator(javax.faces.context.FacesContext)
- */
- public Validator getValidator(FacesContext context) {
- return validatorContext.getValidator();
- }
-
- public FacesMessage interpolateMessage(FacesContext context,final
ConstraintDescriptor<? extends Annotation> constrain){
- if(constrain.getAttributes().containsKey("message")){
- Object object = constrain.getAttributes().get("message");
- String interpolatedMessage;
- interpolatedMessage =
validatorFactory.getMessageInterpolator().interpolate(object.toString(), new Context() {
-
- public Object getValidatedValue() {
- return "{0}";
- }
-
- public ConstraintDescriptor<?> getConstraintDescriptor() {
- return constrain;
- }
- }, MessageFactory.getCurrentLocale(context));
- return new FacesMessage(interpolatedMessage);
- } else {
- return MessageFactory.createMessage(context, UIInput.UPDATE_MESSAGE_ID);
- }
- }
-
- private static final class JsfMessageInterpolator implements MessageInterpolator {
-
- private MessageInterpolator delegate;
-
- public JsfMessageInterpolator(MessageInterpolator delegate) {
- this.delegate = delegate;
- }
-
- public String interpolate(String messageTemplate, Context context) {
-
- Locale locale =
MessageFactory.getCurrentLocale(FacesContext.getCurrentInstance());
- if (null != locale) {
- return delegate.interpolate(messageTemplate, context, locale);
- } else {
- return delegate.interpolate(messageTemplate, context);
- }
- }
-
- public String interpolate(String messageTemplate, Context context, Locale locale)
{
- Locale faceslocale =
MessageFactory.getCurrentLocale(FacesContext.getCurrentInstance());
- if (null != faceslocale) {
- return delegate.interpolate(messageTemplate, context, faceslocale);
- } else {
- return delegate.interpolate(messageTemplate, context, locale);
- }
- }
-
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ELTestBase.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ELTestBase.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ELTestBase.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,143 +0,0 @@
-package org.richfaces.el;
-
-import java.beans.FeatureDescriptor;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.el.BeanELResolver;
-import javax.el.ELContext;
-import javax.el.ELResolver;
-import javax.el.FunctionMapper;
-import javax.el.ListELResolver;
-import javax.el.MapELResolver;
-import javax.el.ValueExpression;
-import javax.el.VariableMapper;
-
-import org.jboss.el.ExpressionFactoryImpl;
-import org.junit.After;
-import org.junit.Before;
-import org.richfaces.el.model.Bean;
-import org.richfaces.el.model.Person;
-
-import com.google.common.collect.Iterators;
-
-public class ELTestBase {
-
- class DummyELResolver extends ELResolver {
-
- private final ELResolver beanResolver = new BeanELResolver();
- private final ELResolver mapResolver = new MapELResolver();
- private final ELResolver listResolver = new ListELResolver();
-
- @Override
- public Class<?> getCommonPropertyType(ELContext context, Object base) {
- return String.class;
- }
-
- @Override
- public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
- return Iterators.emptyIterator();
- }
-
- @Override
- public Class<?> getType(ELContext context, Object base, Object property) {
- if (null == base) {
- if ("bean".equals(property)) {
- return Bean.class;
- } else if ("person".equals(property)) {
- return Person.class;
- }
- } else if (base instanceof List) {
- return listResolver.getType(context, base, property);
- } else if (base instanceof Map) {
- return mapResolver.getType(context, base, property);
- }
- return beanResolver.getType(context, base, property);
- }
-
- @Override
- public Object getValue(ELContext context, Object base, Object property) {
- if (null == base) {
- if ("bean".equals(property)) {
- return bean;
- } else if ("person".equals(property)) {
- return person;
- }
- } else if (base instanceof List) {
- return listResolver.getValue(context, base, property);
- } else if (base instanceof Map) {
- return mapResolver.getValue(context, base, property);
- }
- return beanResolver.getValue(context, base, property);
- }
-
- @Override
- public boolean isReadOnly(ELContext context, Object base, Object property) {
- return true;
- }
-
- @Override
- public void setValue(ELContext context, Object base, Object property, Object
value) {
- // do nothing
-
- }
-
- }
-
- class DummyELContext extends ELContext {
-
- @Override
- public ELResolver getELResolver() {
- return elResolver;
- }
-
- @Override
- public FunctionMapper getFunctionMapper() {
- return null;
- }
-
- @Override
- public VariableMapper getVariableMapper() {
- return null;
- }
-
- }
-
- protected ExpressionFactoryImpl expressionFactory;
- protected Bean bean;
- protected ELResolver elResolver;
- protected ELContext elContext;
- protected CapturingELContext capturingELContext;
- protected Person person;
-
- @Before
- public void setUp() throws Exception {
- expressionFactory = new ExpressionFactoryImpl();
- bean = new Bean();
- person = new Person();
- bean.setString("foo");
- ArrayList<String> list = new ArrayList<String>(1);
- list.add("bar");
- bean.setList(list);
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("boo", "baz");
- bean.setMap(map);
- elResolver = new DummyELResolver();
- elContext = new DummyELContext();
- capturingELContext = new CapturingELContext(elContext);
- }
-
- @After
- public void tearDown() throws Exception {
- expressionFactory = null;
- }
-
- protected ValueExpression parse(String expressionString) {
- ValueExpression expression = expressionFactory.createValueExpression(elContext,
expressionString, String.class);
- return expression;
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/TestCaptureEL.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/TestCaptureEL.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/TestCaptureEL.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,61 +0,0 @@
-package org.richfaces.el;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
-
-import javax.el.ValueExpression;
-
-import org.jboss.test.faces.mock.MockTestRunner;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-(a)RunWith(MockTestRunner.class)
-public class TestCaptureEL extends ELTestBase {
-
- @Test
- public void testDummyResolverString() throws Exception {
- ValueExpression expression = parse("#{bean.string}");
- assertEquals("foo", expression.getValue(elContext));
- }
-
- @Test
- public void testDummyResolverList() throws Exception {
- ValueExpression expression = parse("#{bean.list[0]}");
- assertEquals("bar", expression.getValue(elContext));
- }
-
- @Test
- public void testDummyResolverMap() throws Exception {
- ValueExpression expression = parse("#{bean.map['boo']}");
- assertEquals("baz", expression.getValue(elContext));
- }
-
- @Test
- public void captureString() throws Exception {
- ValueExpression expression = parse("#{bean.string}");
- expression.getType(capturingELContext);
- ValueReference reference = capturingELContext.getReference();
- assertEquals("string", reference.getProperty());
- assertSame(bean, reference.getBase());
- reference = reference.next();
- assertNotNull(reference);
- assertEquals("bean", reference.getProperty());
- assertNull(reference.getBase());
- assertNull(reference.next());
- }
-
- @Test
- public void captureMap() throws Exception {
- ValueExpression expression = parse("#{bean.map['boo']}");
- expression.getType(capturingELContext);
- ValueReference reference = capturingELContext.getReference();
- assertEquals("boo", reference.getProperty());
- assertSame(bean.getMap(), reference.getBase());
- reference = reference.next();
- assertNotNull(reference);
- assertEquals("map", reference.getProperty());
- }
-
-}
Deleted:
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
===================================================================
---
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java 2010-11-10
21:22:24 UTC (rev 20024)
+++
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -1,57 +0,0 @@
-package org.richfaces.el;
-
-
-import static org.easymock.EasyMock.expect;
-import static org.junit.Assert.*;
-
-import javax.el.ELException;
-import javax.el.ValueExpression;
-import javax.faces.context.FacesContext;
-
-import org.jboss.test.faces.mock.FacesMock;
-import org.jboss.test.faces.mock.Mock;
-import org.jboss.test.faces.mock.MockTestRunner;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.richfaces.el.model.Bean;
-
-(a)RunWith(MockTestRunner.class)
-public class ValueExpressionAnalayserTest extends ELTestBase {
-
-
- private ValueExpressionAnalayser analayser;
-
- @Mock
- private FacesContext facesContext;
-
- @Before
- public void setUpAnalayser() throws Exception {
- analayser = new ValueExpressionAnalayserImpl();
- }
-
- @After
- public void tearDownAnalayser() throws Exception {
- analayser = null;
- }
- @Test
- public void testGetDescriptionPositive() throws Exception {
- ValueExpression expression = parse("#{bean.string}");
- expect(facesContext.getELContext()).andReturn(elContext);
- FacesMock.replay(facesContext);
- ValueDescriptor propertyDescriptor =
analayser.getPropertyDescriptor(facesContext, expression);
- assertEquals(Bean.class, propertyDescriptor.getBeanType());
- assertEquals("string", propertyDescriptor.getName());
- assertEquals(String.class,propertyDescriptor.getPropertyType());
- FacesMock.verify(facesContext);
- }
-
- @Test(expected=ELException.class)
- public void testGetDescriptionNegative() throws Exception {
- ValueExpression expression = parse("#{bean}");
- expect(facesContext.getELContext()).andReturn(elContext);
- FacesMock.replay(facesContext);
- ValueDescriptor propertyDescriptor =
analayser.getPropertyDescriptor(facesContext, expression);
- }
-}
Deleted: branches/RF-8742-1/ui/validator/impl/src/test/resources/badcsv.xml
===================================================================
--- branches/RF-8742-1/ui/validator/impl/src/test/resources/badcsv.xml 2010-11-10 21:22:24
UTC (rev 20024)
+++ branches/RF-8742-1/ui/validator/impl/src/test/resources/badcsv.xml 2010-11-10 22:36:12
UTC (rev 20025)
@@ -1,9 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<scripts>
- <component>
- <type>non.existed.Class</type>
- <library>org.richfaces</library>
- <resource>csv.js</resource>
- <function>stringConverter</function>
- </component>
-</scripts>
\ No newline at end of file
Deleted: branches/RF-8742-1/ui/validator/impl/src/test/resources/csv.xml
===================================================================
--- branches/RF-8742-1/ui/validator/impl/src/test/resources/csv.xml 2010-11-10 21:22:24
UTC (rev 20024)
+++ branches/RF-8742-1/ui/validator/impl/src/test/resources/csv.xml 2010-11-10 22:36:12
UTC (rev 20025)
@@ -1,15 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<scripts>
- <component>
- <type>java.lang.String</type>
- <library>org.richfaces</library>
- <resource>csv.js</resource>
- <function>stringConverter</function>
- </component>
- <component>
- <type>java.lang.Integer</type>
- <library>org.richfaces</library>
- <resource>csv.js</resource>
- <function>intConverter</function>
- </component>
-</scripts>
\ No newline at end of file
Deleted: branches/RF-8742-1/ui/validator/impl/src/test/resources/test.html
===================================================================
--- branches/RF-8742-1/ui/validator/impl/src/test/resources/test.html 2010-11-10 21:22:24
UTC (rev 20024)
+++ branches/RF-8742-1/ui/validator/impl/src/test/resources/test.html 2010-11-10 22:36:12
UTC (rev 20025)
@@ -1,27 +0,0 @@
-<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
-
-<html>
-<head>
- <title>Untitled</title>
- <meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
- <title>Test Page</title>
- <script type="text/javascript"
src="jquery.js"></script>
- <script type="text/javascript"
src="richfaces.js"></script>
- <script type="text/javascript"
src="length.js"></script>
-
-</head>
-
-<body style="margin : 30px">
-<form id="myForm">
-<input id="myInput" type="text" value="aaa"/>
-<input type="button" onclick="v();" value="validate
length"/>
-<input type="button"
onclick="v({'LengthValidator.MINIMUM':'CustomMessage: length of the {1}
is smaller then {0} chars', 'LengthValidator.MAXIMUM':'CustomMessage:
length of the {1} is bigger then {0} chars'});" value="validate length
(custom message)"/>
-</form>
-<script>
-RichFaces.csv.addMessage({'LengthValidator.MINIMUM':'Hello, length of the
"{1}" is smaller then {0} chars',
'LengthValidator.MAXIMUM':'Hello, length of the "{1}" is bigger then
{0} chars'});
-var v = function (customMessages) {
- RichFaces.csv.validate(null, 'myInput', {'length':{min:1, max:5,
customMessages:customMessages}}, null, null);
-};
-</script>
-</body>
-<html>
\ No newline at end of file
Copied: branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/application (from
rev 20023, branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/application)
Copied: branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/el (from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/el)
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,42 @@
+package org.richfaces.validator;
+
+import java.util.Map;
+
+import javax.faces.application.FacesMessage;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
+public class BaseFacesObjectDescriptor<T> {
+
+ private final Class<? extends T> converterClass;
+ private Map<String, Object> parameters = Maps.newHashMap();
+ private final FacesMessage message;
+
+
+ public BaseFacesObjectDescriptor(Class<? extends T> converterClass,
FacesMessage message) {
+ this.converterClass = converterClass;
+ this.message = message;
+ }
+
+ public Class<?> getImplementationClass() {
+ return converterClass;
+ }
+
+ public Map<String, ? extends Object> getAdditionalParameters() {
+ return parameters;
+ }
+
+ public FacesMessage getMessage() {
+ return message;
+ }
+
+ protected void addParameter(String name, Object value) {
+ parameters.put(name, value);
+ }
+
+ protected void makeImmutable() {
+ parameters = ImmutableMap.copyOf(parameters);
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BaseFacesObjectDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidator.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidator.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidator.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,110 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Set;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.validation.ConstraintViolation;
+import javax.validation.Validator;
+import javax.validation.groups.Default;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class BeanValidator extends ObjectValidator {
+
+ private static final Class<?>[] DEFAULT_PROFILE = new Class[] {Default.class};
+ private final BeanValidatorFactory validatorFactory;
+
+
+ public BeanValidator(BeanValidatorFactory validatorFactory) {
+ this.validatorFactory = validatorFactory;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.validator.ObjectValidator#validate(java.lang.Object,
java.lang.String, java.lang.Object,
+ * java.util.Locale)
+ */
+ @Override
+ protected Collection<String> validate(FacesContext facesContext, Object base,
String property, Object value,
+ Set<String> profiles) {
+ Class beanType = base.getClass();
+ Set<ConstraintViolation<Object>> constrains =
+ getValidator(facesContext).validateValue(beanType, property, value,
getGroups(profiles));
+ return extractMessages(constrains);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.validator.ObjectValidator#validateGraph(javax.faces.context
.FacesContext, java.lang.Object,
+ * java.util.Set)
+ */
+ @Override
+ public Collection<String> validateGraph(FacesContext context, Object value,
Set<String> profiles) {
+ Class<?>[] groups = getGroups(profiles);
+ Set<ConstraintViolation<Object>> violations =
getValidator(context).validate(value, groups);
+ Collection<String> messages = extractMessages(violations);
+ if (null != parent) {
+ Collection<String> parentMessages = parent.validateGraph(context,
value, profiles);
+ if (null != messages) {
+ if (null != parentMessages) {
+ messages.addAll(parentMessages);
+ }
+ } else {
+ messages = parentMessages;
+ }
+ }
+ return messages;
+ }
+
+ private Class<?>[] getGroups(Set<String> profiles) {
+ Class<?>[] groups = null;
+ if (null != profiles) {
+ groups = new Class<?>[profiles.size()];
+ int i = 0;
+ for (String group : profiles) {
+ try {
+ groups[i] = Class.forName(group, false,
Thread.currentThread().getContextClassLoader());
+ } catch (ClassNotFoundException e) {
+ try {
+ groups[i] = Class.forName(group);
+ } catch (ClassNotFoundException e1) {
+ throw new FacesException("Bean validation group not found
" + group, e1);
+ }
+ }
+ i++;
+ }
+
+ } else {
+ groups = DEFAULT_PROFILE;
+ }
+ return groups;
+ }
+
+ private Collection<String>
extractMessages(Set<ConstraintViolation<Object>> violations) {
+ Collection<String> messages = null;
+ if (null != violations && violations.size() > 0) {
+ messages = new ArrayList<String>(violations.size());
+ for (ConstraintViolation<? extends Object> constraintViolation :
violations) {
+ messages.add(constraintViolation.getMessage());
+ }
+
+ }
+ return messages;
+ }
+
+ protected Validator getValidator(FacesContext facesContext) {
+ Validator beanValidator = validatorFactory.getValidator(facesContext);
+ return beanValidator;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,13 @@
+package org.richfaces.validator;
+
+import java.lang.annotation.Annotation;
+
+import javax.faces.application.FacesMessage;
+
+public class BeanValidatorDescriptor extends BaseFacesObjectDescriptor<Annotation>
implements ValidatorDescriptor {
+
+ public BeanValidatorDescriptor(Class<? extends Annotation> validatorClass,
FacesMessage message) {
+ super(validatorClass, message);
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,100 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.lang.annotation.Annotation;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+import javax.validation.Validator;
+import javax.validation.metadata.ConstraintDescriptor;
+import javax.validation.metadata.ElementDescriptor.ConstraintFinder;
+import javax.validation.metadata.PropertyDescriptor;
+
+import org.richfaces.el.ValueDescriptor;
+import org.richfaces.el.ValueExpressionAnalayser;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class BeanValidatorServiceImpl implements BeanValidatorService {
+
+ private final ValueExpressionAnalayser analayser;
+ private final BeanValidatorFactory validatorFactory;
+
+ public BeanValidatorServiceImpl(ValueExpressionAnalayser analayser,
BeanValidatorFactory validatorFactory) {
+ this.analayser = analayser;
+ this.validatorFactory = validatorFactory;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.validator.BeanValidatorService#getConstrains(javax.faces.context.FacesContext,
+ * javax.el.ValueExpression, java.lang.Class<?>[])
+ */
+ public Collection<ValidatorDescriptor> getConstrains(FacesContext context,
ValueExpression expression,
+ Class<?>... groups) {
+ try {
+ ValueDescriptor propertyDescriptor = analayser.getPropertyDescriptor(context,
expression);
+ return processBeanAttribute(context, propertyDescriptor, groups);
+ } catch (ELException e) {
+ return Collections.emptySet();
+ }
+ }
+
+ private Validator getValidator(FacesContext context) {
+ return validatorFactory.getValidator(context);
+ }
+
+ Collection<ValidatorDescriptor> processBeanAttribute(FacesContext context,
ValueDescriptor descriptor,
+ Class<?>... groups) {
+ PropertyDescriptor constraintsForProperty =
+
getValidator(context).getConstraintsForClass(descriptor.getBeanType()).getConstraintsForProperty(
+ descriptor.getName());
+ if (null != constraintsForProperty) {
+ ConstraintFinder propertyConstraints =
constraintsForProperty.findConstraints();
+ if (null != groups && groups.length > 0) {
+ // Filter groups, if required
+ propertyConstraints =
propertyConstraints.unorderedAndMatchingGroups(groups);
+ }
+ Set<ConstraintDescriptor<?>> constraints = propertyConstraints //
or the requested list of groups)
+ .getConstraintDescriptors();
+
+ // ContextHolder is an arbitrary object, it will depend on the
implementation
+ Set<ValidatorDescriptor> descriptors = new
HashSet<ValidatorDescriptor>(constraints.size());
+ processConstraints(context, constraints, descriptors);
+ return descriptors;
+
+ } else {
+ return Collections.emptySet();
+ }
+ }
+
+ void processConstraints(FacesContext context,
Set<ConstraintDescriptor<?>> constraints,
+ Collection<ValidatorDescriptor> descriptors) {
+ for (ConstraintDescriptor<?> cd : constraints) {
+ Annotation a = cd.getAnnotation();
+ Map<String, Object> parameters = cd.getAttributes();
+ // TODO if cd.isReportedAsSingleConstraint() make sure than only the root
constraint raises an error message
+ // if one or several of the composing constraints are invalid)
+ FacesMessage message = validatorFactory.interpolateMessage(context, cd);
+ BeanValidatorDescriptor beanValidatorDescriptor = new
BeanValidatorDescriptor(a.getClass(), message);
+ for (Map.Entry<String, Object> entry : parameters.entrySet()) {
+ beanValidatorDescriptor.addParameter(entry.getKey(), entry.getValue());
+ }
+ beanValidatorDescriptor.makeImmutable();
+ descriptors.add(beanValidatorDescriptor);
+ processConstraints(context, cd.getComposingConstraints(), descriptors); //
process the composing constraints
+ }
+ }
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/BeanValidatorServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,74 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Map;
+
+import javax.faces.application.Resource;
+import javax.faces.application.ResourceHandler;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.component.util.Strings;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ClientScriptServiceImpl implements ClientScriptService {
+
+ private static final String TEXT_JAVASCRIPT = "text/javascript";
+
+ private static final String ORG_RICHFACES_CSV = "org.richfaces.csv";
+
+ private final Map<Class<?>, LibraryFunction> defaultMapping;
+
+ public ClientScriptServiceImpl(Map<Class<?>, LibraryFunction>
defaultMapping) {
+ this.defaultMapping = defaultMapping;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.validator.ClientScriptService#getScript(java.lang.Class)
+ */
+ public LibraryFunction getScript(FacesContext facesContext, Class<?> javaClass)
throws ScriptNotFoundException {
+ if (null == facesContext || null == javaClass) {
+ throw new NullPointerException();
+ }
+ LibraryFunction function;
+ try {
+ function = getScriptResource(facesContext, javaClass);
+ } catch (ScriptNotFoundException e) {
+ if (defaultMapping.containsKey(javaClass)) {
+ function = defaultMapping.get(javaClass);
+ } else {
+ function = getScriptFromAnnotation(javaClass);
+ }
+ }
+ return function;
+ }
+
+ private LibraryFunction getScriptFromAnnotation(Class<?> javaClass) throws
ScriptNotFoundException {
+ if (javaClass.isAnnotationPresent(ClientSideScript.class)) {
+ ClientSideScript clientSideScript =
javaClass.getAnnotation(ClientSideScript.class);
+ return new LibraryFunctionImplementation(clientSideScript.library(),
clientSideScript.resource(), clientSideScript.function());
+ } else {
+ throw new ScriptNotFoundException();
+ }
+ }
+
+ private LibraryFunction getScriptResource(FacesContext facesContext, Class<?>
javaClass)
+ throws ScriptNotFoundException {
+ ResourceHandler resourceHandler =
facesContext.getApplication().getResourceHandler();
+ String resourceName = javaClass.getSimpleName() + ".js";
+ Resource facesResource = resourceHandler.createResource(resourceName,
ORG_RICHFACES_CSV, TEXT_JAVASCRIPT);
+ if (null != facesResource) {
+ final String functionName =
Strings.firstToLowerCase(javaClass.getSimpleName());
+ return new LibraryFunctionImplementation(ORG_RICHFACES_CSV,resourceName,
functionName);
+ } else {
+ throw new ScriptNotFoundException();
+ }
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientScriptServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,67 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.Map;
+
+import javax.faces.FacesException;
+import javax.xml.bind.JAXB;
+
+import org.richfaces.validator.model.ClientSideScripts;
+import org.richfaces.validator.model.Component;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+import com.google.common.collect.Maps;
+
+/**
+ * @author asmirnov
+ *
+ */
+public final class ClientServiceConfigParser {
+
+ private ClientServiceConfigParser() {
+ }
+
+ public static Map<Class<?>, LibraryFunction> parseConfig(String name) {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (null == loader) {
+ loader = ClientServiceConfigParser.class.getClassLoader();
+ }
+ Builder<Class<?>, LibraryFunction> resultBuilder =
ImmutableMap.builder();
+ try {
+ Enumeration<URL> resources = loader.getResources(name);
+ while (resources.hasMoreElements()) {
+ URL url = (URL) resources.nextElement();
+ resultBuilder.putAll(parse(loader, url));
+ }
+ } catch (IOException e) {
+ return Collections.emptyMap();
+ }
+ return resultBuilder.build();
+ }
+
+ static Map<Class<?>, LibraryFunction> parse(ClassLoader loader, URL url)
{
+ Map<Class<?>, LibraryFunction> result = Maps.newHashMap();
+ try {
+ ClientSideScripts clientSideScripts = JAXB.unmarshal(url,
ClientSideScripts.class);
+ for (Component component : clientSideScripts.getComponent()) {
+ Class<?> componentClass = loader.loadClass(component.getType());
+ LibraryFunctionImplementation function = new
LibraryFunctionImplementation(component.getLibrary(),
+ component.getResource(), component.getFunction());
+ result.put(componentClass, function);
+ }
+ } catch (ClassNotFoundException e) {
+ throw new FacesException("Class for component not found",e);
+ } catch (Exception e) {
+ throw new FacesException("Error parsing config file "+url,e);
+ }
+ return result;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClientServiceConfigParser.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ClonedObjectResolver.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClonedObjectResolver.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,110 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.beans.FeatureDescriptor;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.el.ELContext;
+import javax.el.ELResolver;
+import javax.faces.context.FacesContext;
+
+
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ClonedObjectResolver extends ELResolver {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#getCommonPropertyType(javax.el.ELContext,
java.lang.Object)
+ */
+ @Override
+ public Class<?> getCommonPropertyType(ELContext context, Object base) {
+ // Do nothing
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#getFeatureDescriptors(javax.el.ELContext,
java.lang.Object)
+ */
+ @Override
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
+ // do nothing
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#getType(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ @Override
+ public Class<?> getType(ELContext context, Object base, Object property) {
+ Object cloned = resolveCloned(context, base, property);
+ if (null != cloned) {
+ context.setPropertyResolved(true);
+ return cloned.getClass();
+ }
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#getValue(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ @Override
+ public Object getValue(ELContext context, Object base, Object property) {
+ Object cloned = resolveCloned(context, base, property);
+ if (null != cloned) {
+ context.setPropertyResolved(true);
+ }
+ return cloned;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#isReadOnly(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ @Override
+ public boolean isReadOnly(ELContext context, Object base, Object property) {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.el.ELResolver#setValue(javax.el.ELContext, java.lang.Object,
java.lang.Object, java.lang.Object)
+ */
+ @Override
+ public void setValue(ELContext context, Object base, Object property, Object value)
{
+ // TODO Auto-generated method stub
+
+ }
+
+ public static Object resolveCloned(ELContext context, Object base, Object property)
{
+ if (null != base || null != property) {
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+ Map<String, Object> requestMap =
facesContext.getExternalContext().getRequestMap();
+ for (String key : requestMap.keySet()) {
+ if (null != key &&
key.startsWith(GraphValidatorState.STATE_ATTRIBUTE_PREFIX)) {
+ GraphValidatorState state = (GraphValidatorState)
requestMap.get(key);
+ if (state.isSame(base, property)) {
+ return state.getCloned();
+ }
+ }
+ }
+ }
+ return null;
+ }
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ClonedObjectResolver.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+
+import javax.faces.application.FacesMessage;
+import javax.faces.convert.Converter;
+
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ConverterDescriptorImpl extends BaseFacesObjectDescriptor<Converter>
implements ConverterDescriptor {
+
+
+ ConverterDescriptorImpl(Class<? extends Converter> converterClass,FacesMessage
message) {
+ super(converterClass,message);
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterDescriptorImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ConverterServiceImpl.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,86 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.BigDecimalConverter;
+import javax.faces.convert.BigIntegerConverter;
+import javax.faces.convert.BooleanConverter;
+import javax.faces.convert.ByteConverter;
+import javax.faces.convert.CharacterConverter;
+import javax.faces.convert.Converter;
+import javax.faces.convert.DateTimeConverter;
+import javax.faces.convert.DoubleConverter;
+import javax.faces.convert.EnumConverter;
+import javax.faces.convert.FloatConverter;
+import javax.faces.convert.IntegerConverter;
+import javax.faces.convert.LongConverter;
+import javax.faces.convert.NumberConverter;
+import javax.faces.convert.ShortConverter;
+
+
+/**
+ * @author asmirnov
+ *
+ */
+public class ConverterServiceImpl extends FacesServiceBase<Converter> implements
FacesConverterService {
+
+ private static final String DEFAULT_CONVERTER_MESSAGE_ID =
UIInput.CONVERSION_MESSAGE_ID;
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.validator.FacesConverterService#getConverterDescription(javax.faces.context.FacesContext,
+ * javax.faces.convert.Converter)
+ */
+ public ConverterDescriptor getConverterDescription(FacesContext context, Converter
converter) {
+ // determine converter message.
+ FacesMessage message = getMessage(context, converter);
+ ConverterDescriptorImpl descriptor = new
ConverterDescriptorImpl(converter.getClass(), message);
+ fillParameters(descriptor, converter);
+ descriptor.makeImmutable();
+ return descriptor;
+ }
+
+ @Override
+ protected String getMessageId(Converter converter) {
+ String messageId;
+ if (converter instanceof BigDecimalConverter) {
+ messageId = BigDecimalConverter.DECIMAL_ID;
+ } else if (converter instanceof BigIntegerConverter) {
+ messageId = BigIntegerConverter.BIGINTEGER_ID;
+ } else if (converter instanceof BooleanConverter) {
+ messageId = BooleanConverter.BOOLEAN_ID;
+ } else if (converter instanceof ByteConverter) {
+ messageId = ByteConverter.BYTE_ID;
+ } else if (converter instanceof CharacterConverter) {
+ messageId = CharacterConverter.CHARACTER_ID;
+ } else if (converter instanceof DateTimeConverter) {
+ // TODO - distinguish Date, Time, and DateTime.
+ messageId = DateTimeConverter.DATETIME_ID;
+ } else if (converter instanceof DoubleConverter) {
+ messageId = DoubleConverter.DOUBLE_ID;
+ } else if (converter instanceof EnumConverter) {
+ messageId = EnumConverter.ENUM_ID;
+ } else if (converter instanceof FloatConverter) {
+ messageId = FloatConverter.FLOAT_ID;
+ } else if (converter instanceof IntegerConverter) {
+ messageId = IntegerConverter.INTEGER_ID;
+ } else if (converter instanceof LongConverter) {
+ messageId = LongConverter.LONG_ID;
+ } else if (converter instanceof NumberConverter) {
+ // TODO - detect case ( currency, percent etc ).
+ messageId = NumberConverter.NUMBER_ID;
+ } else if (converter instanceof ShortConverter) {
+ messageId = ShortConverter.SHORT_ID;
+ } else {
+ messageId = DEFAULT_CONVERTER_MESSAGE_ID;
+ }
+ return messageId;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ConverterServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,26 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class DummyBeanValidatorService implements BeanValidatorService {
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.validator.BeanValidatorService#getConstrains(javax.faces.context.FacesContext,
javax.el.ValueExpression, java.lang.Class<?>[])
+ */
+ public Collection<ValidatorDescriptor> getConstrains(FacesContext context,
ValueExpression expression,
+ Class<?>... groups) {
+ return Collections.emptySet();
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/DummyBeanValidatorService.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesBeanValidator.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesBeanValidator.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesBeanValidator.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesBeanValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,178 @@
+/**
+ * License Agreement.
+ *
+ * Rich Faces - Natural Ajax for Java Server Faces (JSF)
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+package org.richfaces.validator;
+
+import java.io.Serializable;
+import java.text.MessageFormat;
+import java.util.Collection;
+import java.util.Locale;
+import java.util.Set;
+
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.FacesException;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.ValidatorException;
+
+/**
+ * Implementation of the JSF validator to use with Bean Validation / Hibernate validator
+ *
+ * @author asmirnov
+ *
+ */
+public class FacesBeanValidator implements Serializable, Validator, GraphValidator {
+
+ public static final String BEAN_VALIDATOR_TYPE =
"org.richfaces.BeanValidator";
+ /**
+ *
+ */
+ private static final long serialVersionUID = -264568176252121853L;
+ private ValueExpression summaryExpression = null;
+
+ private String summary = null;
+
+ private ValueExpression profilesExpression = null;
+
+ private Set<String> profiles = null;
+
+ /**
+ * @return the summary
+ */
+ public String getSummary() {
+ String summaryString = null;
+ if (null != summaryExpression) {
+ summaryString = (String)
summaryExpression.getValue(FacesContext.getCurrentInstance().getELContext());
+ } else {
+ summaryString = this.summary;
+ }
+ return summaryString;
+ }
+
+ /**
+ * @param summary
+ * the summary to set
+ */
+ public void setSummary(ValueExpression summary) {
+ this.summaryExpression = summary;
+ }
+
+ /**
+ * @param summary
+ * the summary to set
+ */
+ public void setSummary(String summary) {
+ this.summary = summary;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext,
+ * javax.faces.component.UIComponent, java.lang.Object)
+ */
+ public void validate(FacesContext context, UIComponent component, Object
convertedValue) throws ValidatorException {
+ if (component instanceof EditableValueHolder) {
+ // Validate input component
+ EditableValueHolder input = (EditableValueHolder) component;
+ try {
+ ValueExpression valueExpression =
component.getValueExpression("value");
+ if (null != valueExpression) {
+ Collection<String> messages =
+ ObjectValidator.getInstance(context).validate(context,
valueExpression, convertedValue,
+ getProfiles());
+ if (null != messages) {
+ input.setValid(false);
+ Object label = getLabel(context, component);
+ Locale locale = context.getViewRoot().getLocale();
+ // send all validation messages.
+ for (String msg : messages) {
+ //
https://jira.jboss.org/jira/browse/RF-7636 -
+ // format values.
+ String formattedMessage = formatMessage(msg, locale, label,
convertedValue); // create Summary message ?
+ String summaryString = getSummary() != null ? getSummary() :
formattedMessage;
+ summaryString = formatMessage(summaryString, locale, label,
convertedValue);
+ context.addMessage(component.getClientId(context), new
FacesMessage(
+ FacesMessage.SEVERITY_ERROR, summaryString,
formattedMessage));
+ }
+ }
+ }
+ } catch (ELException e) {
+ throw new FacesException(e);
+ }
+ }
+ }
+
+ static String formatMessage(String msg, Locale locale, Object... messageParams) {
+ if (msg.contains("{")) {
+ try {
+ MessageFormat messageFormat = new MessageFormat(msg, locale);
+ msg = messageFormat.format(messageParams);
+ } catch (IllegalArgumentException e) {
+ // Do nothing, use original string unchanged.
+ }
+ }
+ return msg;
+ }
+
+ static Object getLabel(FacesContext context, UIComponent component) {
+ Object label = component.getAttributes().get("label");
+ if (null == label || 0 == label.toString().length()) {
+ label = component.getClientId(context);
+ }
+ return label;
+ }
+
+ public Collection<String> validateGraph(FacesContext context, UIComponent
component, Object value,
+ Set<String> profiles) throws ValidatorException {
+ ObjectValidator beanValidator = ObjectValidator.getInstance(context);
+ Collection<String> messages = beanValidator.validateGraph(context, value,
profiles);
+ return messages;
+ }
+
+ /**
+ * @return the profiles
+ */
+ public Set<String> getProfiles() {
+ Set<String> profiles;
+ if (null != profilesExpression) {
+ profiles = (Set<String>)
profilesExpression.getValue(FacesContext.getCurrentInstance().getELContext());
+ } else {
+ profiles = this.profiles;
+ }
+ return profiles;
+ }
+
+ /**
+ * @param profiles
+ * the profiles to set
+ */
+ public void setProfiles(Set<String> profiles) {
+ this.profiles = profiles;
+ }
+
+ public void setProfiles(ValueExpression profilesExpression) {
+ this.profilesExpression = profilesExpression;
+ }
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesBeanValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesServiceBase.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesServiceBase.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesServiceBase.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesServiceBase.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,60 @@
+package org.richfaces.validator;
+
+import java.beans.BeanInfo;
+import java.beans.IntrospectionException;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.lang.reflect.InvocationTargetException;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.context.FacesContext;
+
+import com.google.common.collect.ImmutableSet;
+
+public abstract class FacesServiceBase<T> {
+
+ private static final ImmutableSet<String> HIDDEN_PROPERTIES =
ImmutableSet.of("class", "transient");
+
+ protected abstract String getMessageId(T component);
+
+
+ protected void fillParameters(BaseFacesObjectDescriptor<T> descriptor, T
component) {
+ // get bean attributes for converter, put them into parameters.
+ try {
+ BeanInfo beanInfo = Introspector.getBeanInfo(component.getClass());
+ PropertyDescriptor[] propertyDescriptors =
beanInfo.getPropertyDescriptors();
+ for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
+ String name = propertyDescriptor.getName();
+ if (!HIDDEN_PROPERTIES.contains(name)) {
+ try {
+ Object value =
propertyDescriptor.getReadMethod().invoke(component);
+ if (null != value) {
+ descriptor.addParameter(name, value);
+ }
+ } catch (IllegalArgumentException e) {
+ // Ignore
+ } catch (IllegalAccessException e) {
+ // Ignore
+ } catch (InvocationTargetException e) {
+ // Ignore
+ }
+ }
+ }
+ } catch (IntrospectionException e) {
+ // Ignore.
+ }
+ }
+
+ /**
+ * Creates message for converter, using current locale.
+ *
+ * @param context
+ * @param component
+ * @return
+ */
+ protected FacesMessage getMessage(FacesContext context, T component) {
+ String messageId = getMessageId(component);
+ return MessageFactory.createMessage(context, messageId);
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesServiceBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,12 @@
+package org.richfaces.validator;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.validator.Validator;
+
+public class FacesValidatorDescriptor extends BaseFacesObjectDescriptor<Validator>
implements ValidatorDescriptor {
+
+ public FacesValidatorDescriptor(Class<? extends Validator> validatorClass,
FacesMessage message) {
+ super(validatorClass, message);
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorDescriptor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,55 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.DoubleRangeValidator;
+import javax.faces.validator.LengthValidator;
+import javax.faces.validator.LongRangeValidator;
+import javax.faces.validator.RegexValidator;
+import javax.faces.validator.RequiredValidator;
+import javax.faces.validator.Validator;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class FacesValidatorServiceImpl extends FacesServiceBase<Validator>
implements FacesValidatorService {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.validator.FacesValidatorService#getValidatorDescription(javax.faces.context.FacesContext,
+ * javax.faces.validator.Validator)
+ */
+ public ValidatorDescriptor getValidatorDescription(FacesContext context, Validator
validator) {
+ FacesMessage message = getMessage(context, validator);
+ FacesValidatorDescriptor descriptor = new
FacesValidatorDescriptor(validator.getClass(), message);
+ fillParameters(descriptor, validator);
+ descriptor.makeImmutable();
+ return descriptor;
+ }
+
+ @Override
+ protected String getMessageId(Validator component) {
+ String messageId;
+ if (component instanceof DoubleRangeValidator) {
+ messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof LengthValidator) {
+ messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof LongRangeValidator) {
+ messageId = LongRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ } else if (component instanceof RegexValidator) {
+ messageId = RegexValidator.NOT_MATCHED_MESSAGE_ID;
+ } else if (component instanceof RequiredValidator) {
+ messageId = UIInput.REQUIRED_MESSAGE_ID;
+ } else {
+ messageId = UIInput.UPDATE_MESSAGE_ID;
+ }
+ return messageId;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,24 @@
+package org.richfaces.validator;
+
+final class LibraryFunctionImplementation implements LibraryFunction {
+ private final LibraryResource library;
+ private final String functionName;
+
+ LibraryFunctionImplementation(LibraryResource library, String functionName) {
+ this.library = library;
+ this.functionName = functionName;
+ }
+
+ LibraryFunctionImplementation(String library, String resource, String functionName)
{
+ this.library = new LibraryResource(library, resource);
+ this.functionName = functionName;
+ }
+
+ public LibraryResource getResource() {
+ return library;
+ }
+
+ public String getName() {
+ return functionName;
+ }
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/LibraryFunctionImplementation.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/MessageFactory.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/MessageFactory.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/MessageFactory.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/MessageFactory.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,75 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+/**
+ * @author asmirnov
+ *
+ */
+public final class MessageFactory {
+
+ private MessageFactory(){}
+
+ public static FacesMessage createMessage(FacesContext context, String messageId) {
+ Locale locale = getCurrentLocale(context);
+ String messageBundle = context.getApplication().getMessageBundle();
+ FacesMessage message;
+ try {
+ if (null != messageBundle) {
+ try {
+ message = getMessageFromBundle(locale, messageBundle, messageId);
+ } catch (MissingResourceException e) {
+ message = getMessageFromBundle(locale, FacesMessage.FACES_MESSAGES,
messageId);
+ }
+ } else {
+ message = getMessageFromBundle(locale, FacesMessage.FACES_MESSAGES,
messageId);
+ }
+ } catch (MissingResourceException e) {
+ // No bundles at all, fall back message.
+ return new FacesMessage("conversion error");
+ }
+ return message;
+ }
+
+ private static FacesMessage getMessageFromBundle(Locale locale, String messageBundle,
String messageId)
+ throws MissingResourceException {
+ ResourceBundle bundle = ResourceBundle.getBundle(messageBundle, locale,
getCurrentLoader(messageId));
+ String summary = bundle.getString(messageId);
+ String detail;
+ try {
+ detail = bundle.getString(messageId + "_detail");
+ } catch (MissingResourceException e) {
+ return new FacesMessage(summary);
+ }
+ return new FacesMessage(summary, detail);
+ }
+
+ public static Locale getCurrentLocale(FacesContext context) {
+ UIViewRoot viewRoot = context.getViewRoot();
+ Locale locale;
+ if (null == viewRoot) {
+ locale = Locale.getDefault();
+ } else {
+ locale = viewRoot.getLocale();
+ }
+ return locale;
+ }
+
+ private static ClassLoader getCurrentLoader(Object fallbackClass) {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null) {
+ loader = fallbackClass.getClass().getClassLoader();
+ }
+ return loader;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/MessageFactory.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/NullValidator.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/NullValidator.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/NullValidator.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/NullValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,42 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.util.Collection;
+import java.util.Set;
+
+import javax.faces.context.FacesContext;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class NullValidator extends ObjectValidator {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.richfaces.validator.ObjectValidator#validate(java.lang.Object,
java.lang.String, java.lang.Object,
+ * java.util.Locale)
+ */
+ @Override
+ protected Collection<String> validate(FacesContext facesContext, Object base,
String property, Object value,
+ Set<String> profiles) {
+ // do nothing.
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.richfaces.validator.ObjectValidator#validateGraph(javax.faces.context.FacesContext,
java.lang.Object,
+ * java.util.Set)
+ */
+ @Override
+ public Collection<String> validateGraph(FacesContext context, Object value,
Set<String> profiles) {
+ // do nothing
+ return null;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/NullValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ObjectValidator.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/ObjectValidator.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ObjectValidator.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ObjectValidator.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,420 @@
+package org.richfaces.validator;
+
+import java.beans.FeatureDescriptor;
+import java.util.Collection;
+import java.util.EmptyStackException;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Map;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.Stack;
+
+import javax.el.ELContext;
+import javax.el.ELException;
+import javax.el.ELResolver;
+import javax.el.ValueExpression;
+import javax.faces.FacesException;
+import javax.faces.application.Application;
+import javax.faces.context.FacesContext;
+
+import org.richfaces.application.ServiceTracker;
+import org.richfaces.el.ELContextWrapper;
+
+public abstract class ObjectValidator {
+
+ private static final String RESOURCE_BUNDLE_IS_NOT_REGISTERED_FOR_CURRENT_LOCALE =
+ "Resource bundle is not registered for current locale";
+
+ private static final String FACES_CONTEXT_IS_NULL = "Faces context is
null";
+ private static final String INPUT_PARAMETERS_IS_NOT_CORRECT = "Input parameters
is not correct.";
+ private static final String LOCALE_IS_NOT_SET = "Locale is not set";
+ private static final String VIEW_ROOT_IS_NOT_INITIALIZED = "ViewRoot is not
initialized";
+
+ protected final ObjectValidator parent;
+
+ ObjectValidator() {
+ this.parent = null;
+ }
+
+ ObjectValidator(ObjectValidator parent) {
+ this.parent = parent;
+ }
+
+ /**
+ * Return BeanValidator object from a ServletContext attribute. Create new instance
if none is defined.
+ *
+ * @param context
+ * @return
+ */
+ public static ObjectValidator getInstance(FacesContext context) {
+ return ServiceTracker.getService(context, ObjectValidator.class);
+ }
+
+ public abstract Collection<String> validateGraph(FacesContext context, Object
value, Set<String> profiles);
+
+ /**
+ * Perform Validation for a new value.
+ *
+ * @param context
+ * current faces context.
+ * @param target
+ * {@link ValueExpression} for a value assignment.
+ * @param value
+ * new value for validation
+ * @param profiles
+ * TODO
+ * @return null if no validation errors. Array of the validation messages otherwise.
+ * @throws FacesException
+ * if locale or context not properly initialized
+ */
+ public Collection<String> validate(FacesContext context, ValueExpression
target, Object value, Set<String> profiles) {
+ if (null == context) {
+ throw new FacesException(INPUT_PARAMETERS_IS_NOT_CORRECT);
+ }
+ Collection<String> validationMessages = null;
+ if (null != target) {
+ ELContext elContext = context.getELContext();
+ ValidationResolver validationResolver =
+ createValidationResolver(context, elContext.getELResolver(), profiles);
+ ELContextWrapper wrappedElContext = new ELContextWrapper(elContext,
validationResolver);
+ try {
+ target.setValue(wrappedElContext, value);
+ } catch (ELException e) {
+ throw new FacesException(e);
+ }
+ if (!validationResolver.isValid()) {
+ validationMessages = validationResolver.getValidationMessages();
+ }
+ if (null != parent) {
+ Collection<String> parentMessages = parent.validate(context,
target, value, profiles);
+ if (null != validationMessages) {
+ if (null != parentMessages) {
+ validationMessages.addAll(parentMessages);
+ }
+ } else {
+ validationMessages = parentMessages;
+ }
+ }
+
+ }
+ return validationMessages;
+ }
+
+ /**
+ * Validate bean property for a new value.
+ *
+ * @param facesContext
+ * TODO
+ * @param base
+ * - bean
+ * @param property
+ * - bean property name.
+ * @param value
+ * new value.
+ * @param profiles
+ * TODO
+ *
+ * @return null for a valid value, array of the validation messages othervise.
+ */
+ protected abstract Collection<String> validate(FacesContext facesContext,
Object base, String property,
+ Object value, Set<String> profiles);
+
+ static Locale calculateLocale(FacesContext context) {
+ if (null == context.getViewRoot()) {
+ throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
+ } else if (null == context.getViewRoot().getLocale()) {
+ throw new FacesException(LOCALE_IS_NOT_SET);
+ }
+ Locale locale = context.getViewRoot().getLocale();
+ return locale;
+ }
+
+ static ResourceBundle getResourceBundle(FacesContext facesContext, String name) {
+ ResourceBundle bundle = null;
+ if (null != facesContext) {
+ Application application = facesContext.getApplication();
+ try {
+ bundle = application.getResourceBundle(facesContext, name);
+
+ } catch (Exception e) {
+ // Let one more attempt to load resource
+ }
+ }
+ if (null == bundle) {
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+ if (null == classLoader) {
+ classLoader = ObjectValidator.class.getClassLoader();
+ }
+ try {
+ bundle = ResourceBundle.getBundle(name, calculateLocale(facesContext),
classLoader);
+
+ } catch (MissingResourceException e) {
+ // Do nothing, use default bundle.
+ }
+ }
+ return bundle;
+ }
+
+ protected ValidationResolver createValidationResolver(FacesContext context,
ELResolver parent, Set<String> profiles) {
+ return new ValidationResolver(parent, context, profiles);
+ }
+
+ /**
+ * @author asmirnov
+ *
+ */
+ protected static class BasePropertyPair {
+ private final Object base;
+ private final Object property;
+
+ /**
+ * @param base
+ * @param property
+ */
+ public BasePropertyPair(Object base, Object property) {
+ this.base = base;
+ this.property = property;
+ }
+
+ /**
+ * @return the base
+ */
+ public Object getBase() {
+ return base;
+ }
+
+ /**
+ * @return the property
+ */
+ public Object getProperty() {
+ return property;
+ }
+
+ }
+
+ /**
+ * Class for identify validator instance by locale
+ *
+ * @author amarkhel
+ *
+ */
+ protected static class ValidatorKey {
+ private final Class<? extends Object> validatableClass;
+ private final Locale locale;
+
+ /**
+ * Constructor for ValidatorKey object
+ *
+ * @param validatableClass
+ * - class to validate
+ * @param locale
+ * - User locale to determine Resource bundle, used during validation
process
+ */
+ public ValidatorKey(Class<? extends Object> validatableClass, Locale
locale) {
+ this.validatableClass = validatableClass;
+ this.locale = locale;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((this.locale == null) ? 0 :
this.locale.hashCode());
+ result = prime * result + ((this.validatableClass == null) ? 0 :
this.validatableClass.hashCode());
+ return result;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ ValidatorKey other = (ValidatorKey) obj;
+ if (this.locale == null) {
+ if (other.locale != null) {
+ return false;
+ }
+ } else if (!this.locale.equals(other.locale)) {
+ return false;
+ }
+ if (this.validatableClass == null) {
+ if (other.validatableClass != null) {
+ return false;
+ }
+ } else if (!this.validatableClass.equals(other.validatableClass)) {
+ return false;
+ }
+ return true;
+ }
+
+
+
+ }
+
+ /**
+ * Wrapper class for a {@link ELResolver}. For a setValue method, perform validation
instead of real assignment.
+ *
+ * @author asmirnov
+ *
+ */
+ final class ValidationResolver extends ELResolver {
+
+ /**
+ * Original resolver.
+ */
+ private final ELResolver parent;
+
+ private boolean valid = true;
+
+ private Collection<String> validationMessages = null;
+
+ private Stack<BasePropertyPair> valuesStack;
+
+ private Set<String> profiles;
+
+ private FacesContext facesContext;
+
+ private boolean clonedObject = false;
+
+ /**
+ * @param parent
+ * @param context
+ */
+ public ValidationResolver(ELResolver parent, FacesContext context,
Set<String> profiles) {
+ this.parent = parent;
+ this.valuesStack = new Stack<BasePropertyPair>();
+ this.profiles = profiles;
+ this.facesContext = context;
+ }
+
+ public boolean isValid() {
+ return valid;
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @return
+ * @see javax.el.ELResolver#getCommonPropertyType(javax.el.ELContext,
java.lang.Object)
+ */
+ public Class<?> getCommonPropertyType(ELContext context, Object base) {
+ return parent.getCommonPropertyType(context, base);
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @return
+ * @see javax.el.ELResolver#getFeatureDescriptors(javax.el.ELContext,
java.lang.Object)
+ */
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
+ return parent.getFeatureDescriptors(context, base);
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @param property
+ * @return
+ * @see javax.el.ELResolver#getType(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ public Class<?> getType(ELContext context, Object base, Object property) {
+ return parent.getType(context, base, property);
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @param property
+ * @return
+ * @see javax.el.ELResolver#getValue(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ public Object getValue(ELContext context, Object base, Object property) {
+ Object value = ClonedObjectResolver.resolveCloned(context, base, property);
+ if (null != value) {
+ this.clonedObject = true;
+ context.setPropertyResolved(true);
+ } else {
+ value = parent.getValue(context, base, property);
+ }
+ valuesStack.push(new BasePropertyPair(base, property));
+ return value;
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @param property
+ * @return
+ * @see javax.el.ELResolver#isReadOnly(javax.el.ELContext, java.lang.Object,
java.lang.Object)
+ */
+ public boolean isReadOnly(ELContext context, Object base, Object property) {
+ return parent.isReadOnly(context, base, property);
+ }
+
+ /**
+ * @param context
+ * @param base
+ * @param property
+ * @param value
+ * @see javax.el.ELResolver#setValue(javax.el.ELContext, java.lang.Object,
java.lang.Object, java.lang.Object)
+ */
+ public void setValue(ELContext context, Object base, Object property, Object
value) {
+ if (null != base && null != property) {
+ // TODO - detect value object from inderect references ( e.g. data table
variables ).
+ if (this.clonedObject) {
+ parent.setValue(context, base, property, value);
+ }
+ context.setPropertyResolved(true);
+ // For Arrays, Collection or Map use parent base and property.
+ BasePropertyPair basePropertyPair = lookupBeanProperty(new
BasePropertyPair(base, property));
+ base = basePropertyPair.getBase();
+ property = basePropertyPair.getProperty();
+ if (null != base && null != property) {
+ //
https://jira.jboss.org/jira/browse/RF-4034
+ // apache el looses locale information during value
+ // resolution,
+ // so we use our own
+ validationMessages = validate(facesContext, base,
property.toString(), value, profiles);
+ valid = null == validationMessages || 0 ==
validationMessages.size();
+
+ }
+ }
+ }
+
+ private BasePropertyPair lookupBeanProperty(BasePropertyPair pair) {
+ Object base = pair.getBase();
+ if (null != base && (base instanceof Collection || base instanceof
Map || base.getClass().isArray())) {
+ try {
+ pair = lookupBeanProperty(valuesStack.pop());
+ } catch (EmptyStackException e) {
+ // Do nothing, this is a first item.
+ }
+ }
+ return pair;
+ }
+
+ /**
+ * @return the validationMessages
+ */
+ public Collection<String> getValidationMessages() {
+ return validationMessages;
+ }
+
+ }
+
+}
\ No newline at end of file
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/ObjectValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Copied:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,97 @@
+/**
+ *
+ */
+package org.richfaces.validator;
+
+import java.lang.annotation.Annotation;
+import java.util.Locale;
+
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.validation.MessageInterpolator;
+import javax.validation.MessageInterpolator.Context;
+import javax.validation.Validation;
+import javax.validation.Validator;
+import javax.validation.ValidatorContext;
+import javax.validation.ValidatorFactory;
+import javax.validation.metadata.ConstraintDescriptor;
+
+/**
+ * @author asmirnov
+ *
+ */
+public class RichFacesBeanValidatorFactory implements BeanValidatorFactory {
+
+ private ValidatorFactory validatorFactory;
+ private ValidatorContext validatorContext;
+
+ public RichFacesBeanValidatorFactory() {
+ // Enforce class to load
+ ValidatorFactory.class.getName();
+ // Check Factory, to avoid instantiation errors
+ //
https://jira.jboss.org/jira/browse/RF-7226
+ validatorFactory = Validation.buildDefaultValidatorFactory();
+ validatorContext = validatorFactory.usingContext();
+ MessageInterpolator jsfMessageInterpolator =
+ new JsfMessageInterpolator(validatorFactory.getMessageInterpolator());
+ validatorContext.messageInterpolator(jsfMessageInterpolator);
+ }
+
+ /* (non-Javadoc)
+ * @see
org.richfaces.validator.BeanValidatorFactory#getValidator(javax.faces.context.FacesContext)
+ */
+ public Validator getValidator(FacesContext context) {
+ return validatorContext.getValidator();
+ }
+
+ public FacesMessage interpolateMessage(FacesContext context,final
ConstraintDescriptor<? extends Annotation> constrain){
+ if(constrain.getAttributes().containsKey("message")){
+ Object object = constrain.getAttributes().get("message");
+ String interpolatedMessage;
+ interpolatedMessage =
validatorFactory.getMessageInterpolator().interpolate(object.toString(), new Context() {
+
+ public Object getValidatedValue() {
+ return "{0}";
+ }
+
+ public ConstraintDescriptor<?> getConstraintDescriptor() {
+ return constrain;
+ }
+ }, MessageFactory.getCurrentLocale(context));
+ return new FacesMessage(interpolatedMessage);
+ } else {
+ return MessageFactory.createMessage(context, UIInput.UPDATE_MESSAGE_ID);
+ }
+ }
+
+ private static final class JsfMessageInterpolator implements MessageInterpolator {
+
+ private MessageInterpolator delegate;
+
+ public JsfMessageInterpolator(MessageInterpolator delegate) {
+ this.delegate = delegate;
+ }
+
+ public String interpolate(String messageTemplate, Context context) {
+
+ Locale locale =
MessageFactory.getCurrentLocale(FacesContext.getCurrentInstance());
+ if (null != locale) {
+ return delegate.interpolate(messageTemplate, context, locale);
+ } else {
+ return delegate.interpolate(messageTemplate, context);
+ }
+ }
+
+ public String interpolate(String messageTemplate, Context context, Locale locale)
{
+ Locale faceslocale =
MessageFactory.getCurrentLocale(FacesContext.getCurrentInstance());
+ if (null != faceslocale) {
+ return delegate.interpolate(messageTemplate, context, faceslocale);
+ } else {
+ return delegate.interpolate(messageTemplate, context, locale);
+ }
+ }
+
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/RichFacesBeanValidatorFactory.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: branches/RF-8742-1/ui/validator/ui/src/main/java/org/richfaces/validator/model
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/main/java/org/richfaces/validator/model)
Copied: branches/RF-8742-1/ui/validator/ui/src/main/resources/META-INF/services (from rev
20023, branches/RF-8742-1/ui/validator/impl/src/main/resources/META-INF/services)
Copied: branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ELTestBase.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ELTestBase.java)
===================================================================
--- branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ELTestBase.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ELTestBase.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,143 @@
+package org.richfaces.el;
+
+import java.beans.FeatureDescriptor;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.el.BeanELResolver;
+import javax.el.ELContext;
+import javax.el.ELResolver;
+import javax.el.FunctionMapper;
+import javax.el.ListELResolver;
+import javax.el.MapELResolver;
+import javax.el.ValueExpression;
+import javax.el.VariableMapper;
+
+import org.jboss.el.ExpressionFactoryImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.richfaces.el.model.Bean;
+import org.richfaces.el.model.Person;
+
+import com.google.common.collect.Iterators;
+
+public class ELTestBase {
+
+ class DummyELResolver extends ELResolver {
+
+ private final ELResolver beanResolver = new BeanELResolver();
+ private final ELResolver mapResolver = new MapELResolver();
+ private final ELResolver listResolver = new ListELResolver();
+
+ @Override
+ public Class<?> getCommonPropertyType(ELContext context, Object base) {
+ return String.class;
+ }
+
+ @Override
+ public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context,
Object base) {
+ return Iterators.emptyIterator();
+ }
+
+ @Override
+ public Class<?> getType(ELContext context, Object base, Object property) {
+ if (null == base) {
+ if ("bean".equals(property)) {
+ return Bean.class;
+ } else if ("person".equals(property)) {
+ return Person.class;
+ }
+ } else if (base instanceof List) {
+ return listResolver.getType(context, base, property);
+ } else if (base instanceof Map) {
+ return mapResolver.getType(context, base, property);
+ }
+ return beanResolver.getType(context, base, property);
+ }
+
+ @Override
+ public Object getValue(ELContext context, Object base, Object property) {
+ if (null == base) {
+ if ("bean".equals(property)) {
+ return bean;
+ } else if ("person".equals(property)) {
+ return person;
+ }
+ } else if (base instanceof List) {
+ return listResolver.getValue(context, base, property);
+ } else if (base instanceof Map) {
+ return mapResolver.getValue(context, base, property);
+ }
+ return beanResolver.getValue(context, base, property);
+ }
+
+ @Override
+ public boolean isReadOnly(ELContext context, Object base, Object property) {
+ return true;
+ }
+
+ @Override
+ public void setValue(ELContext context, Object base, Object property, Object
value) {
+ // do nothing
+
+ }
+
+ }
+
+ class DummyELContext extends ELContext {
+
+ @Override
+ public ELResolver getELResolver() {
+ return elResolver;
+ }
+
+ @Override
+ public FunctionMapper getFunctionMapper() {
+ return null;
+ }
+
+ @Override
+ public VariableMapper getVariableMapper() {
+ return null;
+ }
+
+ }
+
+ protected ExpressionFactoryImpl expressionFactory;
+ protected Bean bean;
+ protected ELResolver elResolver;
+ protected ELContext elContext;
+ protected CapturingELContext capturingELContext;
+ protected Person person;
+
+ @Before
+ public void setUp() throws Exception {
+ expressionFactory = new ExpressionFactoryImpl();
+ bean = new Bean();
+ person = new Person();
+ bean.setString("foo");
+ ArrayList<String> list = new ArrayList<String>(1);
+ list.add("bar");
+ bean.setList(list);
+ HashMap<String, String> map = new HashMap<String, String>();
+ map.put("boo", "baz");
+ bean.setMap(map);
+ elResolver = new DummyELResolver();
+ elContext = new DummyELContext();
+ capturingELContext = new CapturingELContext(elContext);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ expressionFactory = null;
+ }
+
+ protected ValueExpression parse(String expressionString) {
+ ValueExpression expression = expressionFactory.createValueExpression(elContext,
expressionString, String.class);
+ return expression;
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ELTestBase.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/TestCaptureEL.java (from
rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/TestCaptureEL.java)
===================================================================
--- branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/TestCaptureEL.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/TestCaptureEL.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,61 @@
+package org.richfaces.el;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+
+import javax.el.ValueExpression;
+
+import org.jboss.test.faces.mock.MockTestRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+(a)RunWith(MockTestRunner.class)
+public class TestCaptureEL extends ELTestBase {
+
+ @Test
+ public void testDummyResolverString() throws Exception {
+ ValueExpression expression = parse("#{bean.string}");
+ assertEquals("foo", expression.getValue(elContext));
+ }
+
+ @Test
+ public void testDummyResolverList() throws Exception {
+ ValueExpression expression = parse("#{bean.list[0]}");
+ assertEquals("bar", expression.getValue(elContext));
+ }
+
+ @Test
+ public void testDummyResolverMap() throws Exception {
+ ValueExpression expression = parse("#{bean.map['boo']}");
+ assertEquals("baz", expression.getValue(elContext));
+ }
+
+ @Test
+ public void captureString() throws Exception {
+ ValueExpression expression = parse("#{bean.string}");
+ expression.getType(capturingELContext);
+ ValueReference reference = capturingELContext.getReference();
+ assertEquals("string", reference.getProperty());
+ assertSame(bean, reference.getBase());
+ reference = reference.next();
+ assertNotNull(reference);
+ assertEquals("bean", reference.getProperty());
+ assertNull(reference.getBase());
+ assertNull(reference.next());
+ }
+
+ @Test
+ public void captureMap() throws Exception {
+ ValueExpression expression = parse("#{bean.map['boo']}");
+ expression.getType(capturingELContext);
+ ValueReference reference = capturingELContext.getReference();
+ assertEquals("boo", reference.getProperty());
+ assertSame(bean.getMap(), reference.getBase());
+ reference = reference.next();
+ assertNotNull(reference);
+ assertEquals("map", reference.getProperty());
+ }
+
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/TestCaptureEL.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
(from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java)
===================================================================
---
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
(rev 0)
+++
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java 2010-11-10
22:36:12 UTC (rev 20025)
@@ -0,0 +1,57 @@
+package org.richfaces.el;
+
+
+import static org.easymock.EasyMock.expect;
+import static org.junit.Assert.*;
+
+import javax.el.ELException;
+import javax.el.ValueExpression;
+import javax.faces.context.FacesContext;
+
+import org.jboss.test.faces.mock.FacesMock;
+import org.jboss.test.faces.mock.Mock;
+import org.jboss.test.faces.mock.MockTestRunner;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.richfaces.el.model.Bean;
+
+(a)RunWith(MockTestRunner.class)
+public class ValueExpressionAnalayserTest extends ELTestBase {
+
+
+ private ValueExpressionAnalayser analayser;
+
+ @Mock
+ private FacesContext facesContext;
+
+ @Before
+ public void setUpAnalayser() throws Exception {
+ analayser = new ValueExpressionAnalayserImpl();
+ }
+
+ @After
+ public void tearDownAnalayser() throws Exception {
+ analayser = null;
+ }
+ @Test
+ public void testGetDescriptionPositive() throws Exception {
+ ValueExpression expression = parse("#{bean.string}");
+ expect(facesContext.getELContext()).andReturn(elContext);
+ FacesMock.replay(facesContext);
+ ValueDescriptor propertyDescriptor =
analayser.getPropertyDescriptor(facesContext, expression);
+ assertEquals(Bean.class, propertyDescriptor.getBeanType());
+ assertEquals("string", propertyDescriptor.getName());
+ assertEquals(String.class,propertyDescriptor.getPropertyType());
+ FacesMock.verify(facesContext);
+ }
+
+ @Test(expected=ELException.class)
+ public void testGetDescriptionNegative() throws Exception {
+ ValueExpression expression = parse("#{bean}");
+ expect(facesContext.getELContext()).andReturn(elContext);
+ FacesMock.replay(facesContext);
+ ValueDescriptor propertyDescriptor =
analayser.getPropertyDescriptor(facesContext, expression);
+ }
+}
Property changes on:
branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/ValueExpressionAnalayserTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/el/model (from rev
20023, branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/el/model)
Copied: branches/RF-8742-1/ui/validator/ui/src/test/java/org/richfaces/validator (from rev
20023, branches/RF-8742-1/ui/validator/impl/src/test/java/org/richfaces/validator)
Copied: branches/RF-8742-1/ui/validator/ui/src/test/resources/badcsv.xml (from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/resources/badcsv.xml)
===================================================================
--- branches/RF-8742-1/ui/validator/ui/src/test/resources/badcsv.xml
(rev 0)
+++ branches/RF-8742-1/ui/validator/ui/src/test/resources/badcsv.xml 2010-11-10 22:36:12
UTC (rev 20025)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scripts>
+ <component>
+ <type>non.existed.Class</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>stringConverter</function>
+ </component>
+</scripts>
\ No newline at end of file
Property changes on: branches/RF-8742-1/ui/validator/ui/src/test/resources/badcsv.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: branches/RF-8742-1/ui/validator/ui/src/test/resources/csv.xml (from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/resources/csv.xml)
===================================================================
--- branches/RF-8742-1/ui/validator/ui/src/test/resources/csv.xml
(rev 0)
+++ branches/RF-8742-1/ui/validator/ui/src/test/resources/csv.xml 2010-11-10 22:36:12 UTC
(rev 20025)
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scripts>
+ <component>
+ <type>java.lang.String</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>stringConverter</function>
+ </component>
+ <component>
+ <type>java.lang.Integer</type>
+ <library>org.richfaces</library>
+ <resource>csv.js</resource>
+ <function>intConverter</function>
+ </component>
+</scripts>
\ No newline at end of file
Property changes on: branches/RF-8742-1/ui/validator/ui/src/test/resources/csv.xml
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Copied: branches/RF-8742-1/ui/validator/ui/src/test/resources/test.html (from rev 20023,
branches/RF-8742-1/ui/validator/impl/src/test/resources/test.html)
===================================================================
--- branches/RF-8742-1/ui/validator/ui/src/test/resources/test.html
(rev 0)
+++ branches/RF-8742-1/ui/validator/ui/src/test/resources/test.html 2010-11-10 22:36:12
UTC (rev 20025)
@@ -0,0 +1,27 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
+
+<html>
+<head>
+ <title>Untitled</title>
+ <meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
+ <title>Test Page</title>
+ <script type="text/javascript"
src="jquery.js"></script>
+ <script type="text/javascript"
src="richfaces.js"></script>
+ <script type="text/javascript"
src="length.js"></script>
+
+</head>
+
+<body style="margin : 30px">
+<form id="myForm">
+<input id="myInput" type="text" value="aaa"/>
+<input type="button" onclick="v();" value="validate
length"/>
+<input type="button"
onclick="v({'LengthValidator.MINIMUM':'CustomMessage: length of the {1}
is smaller then {0} chars', 'LengthValidator.MAXIMUM':'CustomMessage:
length of the {1} is bigger then {0} chars'});" value="validate length
(custom message)"/>
+</form>
+<script>
+RichFaces.csv.addMessage({'LengthValidator.MINIMUM':'Hello, length of the
"{1}" is smaller then {0} chars',
'LengthValidator.MAXIMUM':'Hello, length of the "{1}" is bigger then
{0} chars'});
+var v = function (customMessages) {
+ RichFaces.csv.validate(null, 'myInput', {'length':{min:1, max:5,
customMessages:customMessages}}, null, null);
+};
+</script>
+</body>
+<html>
\ No newline at end of file