You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2008/07/02 21:59:42 UTC

svn commit: r673478 - in /myfaces/commons/branches/jsf_11: myfaces-commons-converters/ myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/ myfaces-commons-examples/ myfaces-commons-validators/

Author: lu4242
Date: Wed Jul  2 12:59:41 2008
New Revision: 673478

URL: http://svn.apache.org/viewvc?rev=673478&view=rev
Log:
Restored EnumConverter (copied from trunk) and set code level to 1.5

Added:
    myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java   (with props)
Modified:
    myfaces/commons/branches/jsf_11/myfaces-commons-converters/pom.xml
    myfaces/commons/branches/jsf_11/myfaces-commons-examples/pom.xml
    myfaces/commons/branches/jsf_11/myfaces-commons-validators/pom.xml

Modified: myfaces/commons/branches/jsf_11/myfaces-commons-converters/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_11/myfaces-commons-converters/pom.xml?rev=673478&r1=673477&r2=673478&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_11/myfaces-commons-converters/pom.xml (original)
+++ myfaces/commons/branches/jsf_11/myfaces-commons-converters/pom.xml Wed Jul  2 12:59:41 2008
@@ -38,6 +38,14 @@
     </resources>
     <plugins>
       <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>    
+      <plugin>
         <groupId>org.apache.myfaces.buildtools</groupId>
         <artifactId>myfaces-builder-plugin</artifactId>
         <version>1.0.1-SNAPSHOT</version>

Added: myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java?rev=673478&view=auto
==============================================================================
--- myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java (added)
+++ myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java Wed Jul  2 12:59:41 2008
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.myfaces.commons.converter;
+
+import org.apache.myfaces.commons.util.MessageUtils;
+
+import javax.faces.component.StateHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.Converter;
+import javax.faces.convert.ConverterException;
+import javax.faces.application.FacesMessage;
+
+/**
+ * Converts a Java 5 Enum.
+ * 
+ * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
+ *
+ * @JSFConverter
+ *   name = "mcc:convertEnum"
+ *   tagClass = "org.apache.myfaces.commons.converter.ConvertEnumTag" 
+ *   
+ * @author Stan Silvert
+ */
+public class EnumConverter implements Converter, StateHolder {
+    
+    public static final String CONVERTER_ID = "org.apache.myfaces.commons.converter.Enum";
+    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";
+    
+    private Class targetClass;
+    
+    private boolean isTransient = false;
+    
+    /** Creates a new instance of EnumConverter */
+    public EnumConverter() {
+    }
+    
+    public EnumConverter(Class targetClass) {
+        if (!targetClass.isEnum()) throw new IllegalArgumentException("targetClass for EnumConverter must be an Enum");
+        this.targetClass = targetClass;
+    }
+
+    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) throws ConverterException {
+        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();
+        }
+
+        return value.toString();
+    }
+
+    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) throws ConverterException {
+        if (facesContext == null) throw new NullPointerException("facesContext");
+        if (uiComponent == null) throw new NullPointerException("uiComponent");
+        if (value == null)  return null;
+        value = value.trim();
+        if (value.length() == 0)  return null;
+        checkTargetClass(facesContext, uiComponent, value);
+        
+        // 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);    
+        } catch (IllegalArgumentException e) {
+            Object[] params = new Object[]{value, 
+                                           firstConstantOfEnum(), 
+                                           MessageUtils.getLabel(facesContext, uiComponent)};
+            
+            throw new ConverterException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
+                                                                       ENUM_ID,
+                                                                       params));
+        }
+    }
+
+	private void checkTargetClass(FacesContext facesContext, UIComponent uiComponent, Object value) {
+		if (targetClass == null) {
+            Object[] params = new Object[]{value, MessageUtils.getLabel(facesContext, uiComponent)};
+            throw new ConverterException(MessageUtils.getMessage(FacesMessage.SEVERITY_ERROR,
+                                                                       ENUM_NO_CLASS_ID, 
+                                                                       params));
+        }
+	}
+
+    // find the first constant value of the targetClass and return as a String
+    private String firstConstantOfEnum() {
+        Object[] enumConstants= targetClass.getEnumConstants();
+
+        if (enumConstants.length != 0 ) return enumConstants[0].toString();
+        
+        return ""; // if empty Enum
+    }
+
+    public void restoreState(FacesContext context, Object state) {
+        targetClass = (Class)state;
+    }
+
+    public Object saveState(FacesContext context) {
+        return targetClass;
+    }
+
+    public void setTransient(boolean newTransientValue) {
+        isTransient = newTransientValue;
+    }
+
+    public boolean isTransient() {
+        return isTransient;
+    }
+    
+}

Propchange: myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/commons/branches/jsf_11/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: myfaces/commons/branches/jsf_11/myfaces-commons-examples/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_11/myfaces-commons-examples/pom.xml?rev=673478&r1=673477&r2=673478&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_11/myfaces-commons-examples/pom.xml (original)
+++ myfaces/commons/branches/jsf_11/myfaces-commons-examples/pom.xml Wed Jul  2 12:59:41 2008
@@ -23,6 +23,14 @@
         </configuration>
       </plugin>
     </plugins>
+    <plugin>
+      <groupId>org.apache.maven.plugins</groupId>
+      <artifactId>maven-compiler-plugin</artifactId>
+      <configuration>
+        <source>1.5</source>
+        <target>1.5</target>
+      </configuration>
+    </plugin>      	
   </build>
   <repositories>
     <repository>

Modified: myfaces/commons/branches/jsf_11/myfaces-commons-validators/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/branches/jsf_11/myfaces-commons-validators/pom.xml?rev=673478&r1=673477&r2=673478&view=diff
==============================================================================
--- myfaces/commons/branches/jsf_11/myfaces-commons-validators/pom.xml (original)
+++ myfaces/commons/branches/jsf_11/myfaces-commons-validators/pom.xml Wed Jul  2 12:59:41 2008
@@ -37,6 +37,14 @@
     </resources>  
     <plugins>
       <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>    
+      <plugin>
         <groupId>org.apache.myfaces.buildtools</groupId>
         <artifactId>myfaces-builder-plugin</artifactId>
         <version>1.0.1-SNAPSHOT</version>