Author: abelevich
Date: 2008-03-20 10:26:15 -0400 (Thu, 20 Mar 2008)
New Revision: 7002
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/util/InputUtils.java
Log:
add converters processing methods
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/util/InputUtils.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/util/InputUtils.java 2008-03-20
14:25:28 UTC (rev 7001)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/util/InputUtils.java 2008-03-20
14:26:15 UTC (rev 7002)
@@ -23,8 +23,15 @@
import java.io.Serializable;
+import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
+import javax.faces.component.ValueHolder;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+import org.ajax4jsf.Messages;
+
/**
* @author Maksim Kaszynski
* @author Manfred Geiler
@@ -52,4 +59,60 @@
return isTrue(component.getAttributes().get("readonly"));
}
+ public static Object getConvertedValue(FacesContext context, UIComponent component,
Object submittedValue) throws ConverterException {
+
+ String newValue = (String) submittedValue;
+
+ ValueExpression valueExpression = component.getValueExpression("value");
+ Converter converter = null;
+
+ if (component instanceof ValueHolder) {
+ converter = ((ValueHolder)component).getConverter();
+ }
+
+ if (converter == null && valueExpression != null) {
+ Class converterType = valueExpression.getType(context.getELContext());
+ if (converterType == null || converterType == Object.class || converterType ==
String.class) {
+ return newValue;
+ } else {
+ converter = context.getApplication().createConverter(converterType);
+ if (converter == null) {
+ throw new ConverterException(Messages.getMessage(Messages.NO_CONVERTER_FOUND_ERROR,
converterType.getName()));
+ }
+ }
+ } else if (converter == null) {
+ return newValue;
+ }
+
+ return converter.getAsObject(context, component, newValue);
+ }
+
+ public static String getConvertedStringValue(FacesContext context, UIComponent
component, Object value) {
+
+ Converter converter = null;
+
+ if (component instanceof ValueHolder) {
+ converter = ((ValueHolder)component).getConverter();
+ }
+
+ if (converter == null) {
+ if (value == null) {
+ return "";
+ } else if (value instanceof String) {
+ return (String) value;
+ }
+
+ Class converterType = value.getClass();
+ if (converterType != null ) {
+ converter = context.getApplication().createConverter(converterType);
+ }
+
+ if (converter == null) {
+ return value.toString();
+ }
+ }
+ return converter.getAsString(context, component, value);
+
+ }
+
}
Show replies by date