numerous binding fixes, see changelog

This commit is contained in:
Keith Donald
2009-02-25 01:32:06 +00:00
parent c8d4d6d97c
commit 16e88dcb6f
19 changed files with 510 additions and 64 deletions

View File

@@ -36,6 +36,17 @@ public interface ConversionService {
*/
public Object executeConversion(Object source, Class targetClass) throws ConversionException;
/**
* Execute a conversion using the custom converter with the provided id.
* @param converterId the id of the custom converter, which must be registered with this conversion service and
* capable of converting to the target class
* @param source the source to convert from (may be null)
* @param targetClass the target class to convert to
* @return the converted object, an instance of the <code>targetClass</code>
* @throws ConversionException if an exception occurred during the conversion process
*/
public Object executeConversion(String converterId, Object source, Class targetClass);
/**
* Return the default conversion executor capable of converting source objects of the specified
* <code>sourceClass</code> to instances of the <code>targetClass</code>.
@@ -65,8 +76,8 @@ public interface ConversionService {
/**
* Return all conversion executors capable of converting <i>from</i> the provided <code>sourceClass</code>. For
* example, <code>getConversionExecutor(String.class)</code> would return all converters that convert from String
* to some other Object. Mainly useful for adapting a set of converters to some other environment.
* example, <code>getConversionExecutor(String.class)</code> would return all converters that convert from String to
* some other Object. Mainly useful for adapting a set of converters to some other environment.
* @param sourceClass the source class converting from
* @return the conversion executors that can convert from that source class
*/
@@ -78,4 +89,5 @@ public interface ConversionService {
* @return the class, or <code>null</code> if no alias exists
*/
public Class getClassForAlias(String alias);
}

View File

@@ -386,6 +386,15 @@ public class GenericConversionService implements ConversionService {
}
}
public Object executeConversion(String converterId, Object source, Class targetClass) throws ConversionException {
if (source != null) {
ConversionExecutor conversionExecutor = getConversionExecutor(converterId, source.getClass(), targetClass);
return conversionExecutor.execute(source);
} else {
return null;
}
}
public Class getClassForAlias(String name) throws IllegalArgumentException {
Class clazz = (Class) aliasMap.get(name);
if (clazz != null) {

View File

@@ -29,8 +29,7 @@ public class PropertyNotFoundException extends EvaluationException {
* @param cause root cause of the failure
*/
public PropertyNotFoundException(Class contextClass, String property, Throwable cause) {
super(contextClass, property, "Property '" + property + "' not found on context of class ["
+ contextClass.getName() + "]", cause);
super(contextClass, property, "Property not found", cause);
}
}

View File

@@ -48,8 +48,8 @@ public class ValueCoercionException extends EvaluationException {
* @param cause root cause of the failure
*/
public ValueCoercionException(Class contextClass, String property, Object value, Class targetClass, Throwable cause) {
super(contextClass, property, "Value [" + value + "] could not be coerced to type [" + targetClass.getName()
+ "]", cause);
super(contextClass, property,
"Value could not be converted to target class; is a suitable type converter registered?", cause);
this.value = value;
this.targetClass = targetClass;
}

View File

@@ -72,20 +72,21 @@ public class MessageContextErrors extends AbstractErrors {
}
public void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage) {
messageContext.addMessage(new MessageBuilder().error().source(field).code(errorCode).args(errorArgs)
.defaultText(defaultMessage).build());
messageContext.addMessage(new MessageBuilder().error().source(fixedField(field)).code(errorCode)
.args(errorArgs).defaultText(defaultMessage).build());
}
public void addAllErrors(Errors errors) {
Iterator it = errors.getAllErrors().iterator();
while (it.hasNext()) {
ObjectError error = (ObjectError) it.next();
MessageBuilder builder = new MessageBuilder().error().codes(error.getCodes()).args(error.getArguments())
.defaultText(error.getDefaultMessage());
if (error instanceof FieldError) {
FieldError fieldError = (FieldError) error;
rejectValue(fieldError.getField(), error.getCode(), error.getArguments(), error.getDefaultMessage());
} else {
reject(error.getCode(), error.getArguments(), error.getDefaultMessage());
builder.source(fieldError.getField());
}
messageContext.addMessage(builder.build());
}
}
@@ -103,7 +104,7 @@ public class MessageContextErrors extends AbstractErrors {
Message message = messages[i];
errors.add(new ObjectError(objectName, message.getText()));
}
return errors;
return Collections.unmodifiableList(errors);
}
public List getFieldErrors() {
@@ -116,11 +117,12 @@ public class MessageContextErrors extends AbstractErrors {
Message message = messages[i];
errors.add(new FieldError(objectName, (String) message.getSource(), message.getText()));
}
return errors;
return Collections.unmodifiableList(errors);
}
public Object getFieldValue(String field) {
// requires boundObject, and expressionParser to work
field = fixedField(field);
// requires boundObject and expressionParser to be set to work
if (mappingResults != null) {
List results = mappingResults.getResults(new PropertyErrorMappingResult(field));
if (!results.isEmpty()) {
@@ -131,13 +133,15 @@ public class MessageContextErrors extends AbstractErrors {
return parseFieldExpression(field).getValue(boundObject);
}
// internal helpers
private Expression parseFieldExpression(String field) {
return expressionParser.parseExpression(field, new FluentParserContext().evaluate(boundObject.getClass()));
}
private static MessageCriteria GLOBAL_ERROR = new MessageCriteria() {
public boolean test(Message message) {
if (message.getSource() == null && message.getSeverity().equals(Severity.ERROR)) {
if (message.getSeverity() == Severity.ERROR && message.getSource() == null) {
return true;
} else {
return false;
@@ -147,7 +151,7 @@ public class MessageContextErrors extends AbstractErrors {
private static MessageCriteria FIELD_ERROR = new MessageCriteria() {
public boolean test(Message message) {
if (message.getSource() != null && message.getSeverity().equals(Severity.ERROR)) {
if (message.getSeverity() == Severity.ERROR && message.getSource() instanceof String) {
return true;
} else {
return false;