You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2007/12/02 16:19:10 UTC

svn commit: r600307 - in /myfaces/commons/trunk/myfaces-commons-converters: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/myfaces/ src/main/java/org/apache/myfaces/commons/ src/main/java/org/apac...

Author: matzew
Date: Sun Dec  2 07:19:09 2007
New Revision: 600307

URL: http://svn.apache.org/viewvc?rev=600307&view=rev
Log:
adding Enum Converter

Added:
    myfaces/commons/trunk/myfaces-commons-converters/
    myfaces/commons/trunk/myfaces-commons-converters/pom.xml
    myfaces/commons/trunk/myfaces-commons-converters/src/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java
    myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/
    myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/Enum.xml
    myfaces/commons/trunk/myfaces-commons-converters/src/main/resources/
    myfaces/commons/trunk/myfaces-commons-converters/src/test/
    myfaces/commons/trunk/myfaces-commons-converters/src/test/java/
    myfaces/commons/trunk/myfaces-commons-converters/src/test/resources/

Added: myfaces/commons/trunk/myfaces-commons-converters/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-converters/pom.xml?rev=600307&view=auto
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-converters/pom.xml (added)
+++ myfaces/commons/trunk/myfaces-commons-converters/pom.xml Sun Dec  2 07:19:09 2007
@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.myfaces.commons</groupId>
+    <artifactId>commons</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+  </parent>
+  <artifactId>myfaces-converters</artifactId>
+  <packaging>jar</packaging>
+  <name>Apache MyFaces Commons Converters</name>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <inherited>true</inherited>
+        <configuration>
+          <source>1.5</source>
+          <target>1.5</target>
+        </configuration>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.myfaces.trinidadbuild</groupId>
+        <artifactId>maven-faces-plugin</artifactId>
+        <version>1.2.6-SNAPSHOT</version>
+        <configuration>
+          <taglibs>
+            <mcc>http://myfaces.apache.org/commons/converters</mcc>
+          </taglibs>
+          <packageContains>org.apache</packageContains>
+          <converterPackageContains>org.apache</converterPackageContains>
+          <validatorPackageContains>org.apache</validatorPackageContains>
+          <typePrefix>org.apache</typePrefix>
+          <packageContains>org.apache</packageContains>
+          <removeRenderers>true</removeRenderers>
+          <force>true</force>
+          <jsfVersion>1.1</jsfVersion>
+        </configuration>
+        <executions>
+          <execution>
+            <phase>validate</phase>
+            <id>generate-master-faces-config</id>
+            <goals>
+              <goal>generate-master-faces-config</goal>
+            </goals>
+            <configuration>
+              <sourcePath>.</sourcePath>
+              <sourceDirectory>src/main/maven-faces-plugin</sourceDirectory>
+            </configuration>
+          </execution>
+          <execution>
+            <id>generate-faces-config</id>
+            <goals>
+              <goal>generate-faces-config</goal>
+              <goal>generate-jsp-taglibs</goal>
+              <goal>generate-facelets-taglibs</goal>
+            </goals>
+            <configuration>
+              <localResource>${project.build.directory}/maven-faces-plugin/main/resources/META-INF/maven-faces-plugin/faces-config.xml</localResource>
+              <targetPath>META-INF/faces-config.xml</targetPath>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+      <plugin>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>validate</phase>
+            <configuration>
+              <tasks>
+                <copy todir="${project.build.directory}/maven-faces-plugin/main/resources/META-INF/maven-faces-plugin//">
+                  <fileset dir="${basedir}/src/main/maven-faces-plugin/"/>
+                </copy>
+              </tasks>
+            </configuration>
+            <goals>
+              <goal>run</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.myfaces.core</groupId>
+      <artifactId>myfaces-api</artifactId>
+      <version>1.1.5</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>commons-validator</groupId>
+      <artifactId>commons-validator</artifactId>
+      <version>1.3.1</version>
+      <scope>compile</scope>
+    </dependency>    
+    <dependency>
+      <groupId>org.apache.myfaces.commons</groupId>
+      <artifactId>myfaces-commons-utils</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+    </dependency>
+  </dependencies>
+</project>
\ No newline at end of file

Added: 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=600307&view=auto
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java (added)
+++ myfaces/commons/trunk/myfaces-commons-converters/src/main/java/org/apache/myfaces/commons/converter/EnumConverter.java Sun Dec  2 07:19:09 2007
@@ -0,0 +1,117 @@
+/*
+ * 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 javax.faces.component.StateHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+
+/**
+ * see Javadoc of <a href="http://java.sun.com/j2ee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
+ *
+ * @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.getErrorMessage(facesContext,
+                                                                       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.getErrorMessage(facesContext, 
+                                                                       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;
+    }
+    
+}

Added: myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/Enum.xml
URL: http://svn.apache.org/viewvc/myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/Enum.xml?rev=600307&view=auto
==============================================================================
--- myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/Enum.xml (added)
+++ myfaces/commons/trunk/myfaces-commons-converters/src/main/maven-faces-plugin/converters/commons/Enum.xml Sun Dec  2 07:19:09 2007
@@ -0,0 +1,34 @@
+<?xml version="1.0" ?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+	   
+-->
+<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
+              xmlns:mcc="http://myfaces.apache.org/commons/converters"
+              xmlns:mfp="http://myfaces.apache.org/maven-faces-plugin"
+              xmlns:xhtml="http://www.w3.org/1999/xhtml">
+  <converter>
+    <converter-id>org.apache.myfaces.commons.converter.Enum</converter-id>
+    <converter-class>org.apache.myfaces.commons.converter.EnumConverter</converter-class>
+    <converter-extension>
+      <mfp:tag-name>mcc:convertEnum</mfp:tag-name>
+      <mfp:tag-class>org.apache.myfaces.commons.converter.ConvertEnumTag</mfp:tag-class>
+      <mfp:long-description><![CDATA[<html:p>Converts a Java 5 Enum.</html:p>]]></mfp:long-description>
+    </converter-extension>
+  </converter>
+</faces-config>
\ No newline at end of file