myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From we...@apache.org
Subject svn commit: r691186 - /myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
Date Tue, 02 Sep 2008 10:29:48 GMT
Author: weber
Date: Tue Sep  2 03:29:47 2008
New Revision: 691186

URL: http://svn.apache.org/viewvc?rev=691186&view=rev
Log:
merging changes from 1.1 branch:
r691183 | weber | 2008-09-02 12:19:30 +0200 (Di, 02 Sep 2008) | 2 lines

(MYFACES-1953) Enable use of EnumConverter tag
<https://issues.apache.org/jira/browse/MYFACES-1953>

Modified:
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java

Modified: myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
URL: http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java?rev=691186&r1=691185&r2=691186&view=diff
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
(original)
+++ myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
Tue Sep  2 03:29:47 2008
@@ -45,7 +45,8 @@
     public static final String ENUM_ID = "org.apache.myfaces.commons.converter.EnumConverter.ENUM";
     public static final String ENUM_NO_CLASS_ID = "org.apache.myfaces.commons.converter.EnumConverter.ENUM_NO_CLASS";
     public static final String ENUM_NO_ENUM_CLASS = "org.apache.myfaces.commons.converter.EnumConverter.ENUM_NO_ENUM_CLASS";
-    
+    public static final String SEPARATOR = "#";
+
     private Class targetClass;
     
     private boolean isTransient = false;
@@ -63,11 +64,24 @@
         if (facesContext == null) throw new NullPointerException("facesContext can not be
null");
         if (uiComponent == null) throw new NullPointerException("uiComponent can not be null");
         if (value == null) return "";
-        checkTargetClass(facesContext, uiComponent, value);
-        
-        for (Object enumConstant : targetClass.getEnumConstants()) {
-            if (enumConstant == value) return enumConstant.toString();
+
+      Class converterClass = null;
+      String idPrefix = "";
+      if (targetClass != null) {
+        converterClass = targetClass;
+      } else if (value.getClass().isEnum()) {
+        converterClass = value.getClass();
+        idPrefix = value.getClass().getName() + SEPARATOR;
+      }
+      if (converterClass == null) {
+        checkTargetClass(facesContext, uiComponent, value, converterClass);
+      }
+
+      for (Object enumConstant : converterClass.getEnumConstants()) {
+        if (enumConstant == value) {
+          return idPrefix + ((Enum)enumConstant).name();
         }
+      }
 
         return value.toString();
     }
@@ -78,12 +92,38 @@
         if (value == null)  return null;
         value = value.trim();
         if (value.length() == 0)  return null;
-        checkTargetClass(facesContext, uiComponent, value);
-        
+
+      Class converterClass = null;
+      String enumName = null;
+
+      if (targetClass != null) {
+        enumName = value;
+        converterClass = targetClass;
+      } else if (value.indexOf(SEPARATOR) > 0) {
+        int index = value.indexOf(SEPARATOR);
+        if (index < value.length() - 2) {
+          String className = value.substring(0, index);
+          enumName = value.substring(index +1);
+          try {
+            converterClass = Class.forName(className);
+          } catch (ClassNotFoundException e) {
+  //          LOG.error("Catched: " + e.getMessage(), e);
+          }
+        }
+        if (enumName == null || enumName.length() == 0) {
+          return null;
+        }
+      }
+
+      if (converterClass == null) {
+        checkTargetClass(facesContext, uiComponent, value, converterClass);
+      }
+
         // we know targetClass and value can't be null, so we can use Enum.valueOf
         // instead of the hokey looping called for in the javadoc
         try {
-           return Enum.valueOf(targetClass, value);    
+          //noinspection unchecked
+          return Enum.valueOf(converterClass, enumName);
         } catch (IllegalArgumentException e) {
             Object[] params = new Object[]{value, 
                                            firstConstantOfEnum(), 
@@ -95,14 +135,14 @@
         }
     }
 
-    private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object
value) {
-        if (targetClass == null) {
+    private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object
value, Class converterClass) {
+        if (converterClass == null) {
             Object[] params = new Object[]{value, MessageUtils.getLabel(facesContext, uiComponent)};
             throw new ConverterException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
                                                                        ENUM_NO_CLASS_ID,

                                                                        params));
         }
-        if (!targetClass.isEnum())
+        if (!converterClass.isEnum())
         {
             Object[] params = new Object[]{value, MessageUtils.getLabel(facesContext, uiComponent)};
             throw new ConverterException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,



Mime
View raw message