You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ug...@apache.org on 2006/03/07 12:02:00 UTC

svn commit: r383842 - in /cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms: datatype/EnumSelectionList.java datatype/EnumSelectionListBuilder.java samples/Contact.java samples/PreferredContact.java

Author: ugo
Date: Tue Mar  7 03:01:58 2006
New Revision: 383842

URL: http://svn.apache.org/viewcvs?rev=383842&view=rev
Log:
Added support for Commons enums to CForms [#COCOON-1793]

Added:
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java   (with props)
Modified:
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionList.java
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionListBuilder.java
    cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/Contact.java

Modified: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionList.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionList.java?rev=383842&r1=383841&r2=383842&view=diff
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionList.java (original)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionList.java Tue Mar  7 03:01:58 2006
@@ -17,11 +17,14 @@
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
+import java.util.Iterator;
 import java.util.Locale;
 
 import org.apache.cocoon.forms.FormsConstants;
 import org.apache.cocoon.xml.AttributesImpl;
 import org.apache.cocoon.xml.XMLUtils;
+import org.apache.commons.lang.enums.Enum;
+import org.apache.commons.lang.enums.EnumUtils;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 
@@ -61,6 +64,7 @@
     private Datatype datatype;
     private Class clazz;
     private boolean nullable;
+    private String nullText;
 
     /**
      * @param className
@@ -72,6 +76,11 @@
         this.clazz = Class.forName(className);
     }
 
+    public EnumSelectionList(String className, Datatype datatype, boolean nullable, String nullText) throws ClassNotFoundException {
+        this(className, datatype, nullable);
+        this.nullText = nullText;
+    }
+    
     /* (non-Javadoc)
      * @see org.apache.cocoon.forms.datatype.SelectionList#getDatatype()
      */
@@ -87,36 +96,69 @@
     throws SAXException {
         try {
             contentHandler.startElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL, XMLUtils.EMPTY_ATTRIBUTES);
-            Field fields[] = clazz.getDeclaredFields();
             // Create void element
             if (nullable) {
                 AttributesImpl voidAttrs = new AttributesImpl();
                 voidAttrs.addCDATAAttribute("value", "");
                 contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, voidAttrs);
-                contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
-            }
-            for (int i = 0 ; i < fields.length ; ++i) {
-                int mods = fields[i].getModifiers();
-                if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
-                        && Modifier.isFinal(mods) && fields[i].get(null).getClass().equals(clazz)) {
-                    String stringValue = clazz.getName() + "." + fields[i].getName();
-                    // Output this item
-                    AttributesImpl itemAttrs = new AttributesImpl();
-                    itemAttrs.addCDATAAttribute("value", stringValue);
-                    contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
+                if (this.nullText != null) {
                     contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
-                    // TODO: make i18n element optional
                     contentHandler.startElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL, XMLUtils.EMPTY_ATTRIBUTES);
-                    contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
+                    contentHandler.characters(nullText.toCharArray(), 0, nullText.length());
                     contentHandler.endElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL);
                     contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
-                    contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
+                }
+                contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
+            }
+            // Test if we have an apache enum class
+            boolean apacheEnumDone = false;
+            if (Enum.class.isAssignableFrom(clazz)) {
+                Iterator iter = EnumUtils.iterator(clazz);
+                if (iter != null) {
+                    apacheEnumDone = true;
+                    while (iter.hasNext()) {
+                        Enum element = (Enum) iter.next();
+                        String stringValue = clazz.getName() + "." + element.getName();
+                        generateItem(contentHandler, stringValue);
+                    }
                 }
             }
+            // If it's not an apache enum or we didn't manage to read the enum list, then proceed with common method.
+            if (!apacheEnumDone) {
+	            Field fields[] = clazz.getDeclaredFields();
+	            for (int i = 0 ; i < fields.length ; ++i) {
+	                int mods = fields[i].getModifiers();
+	                if (Modifier.isPublic(mods) && Modifier.isStatic(mods)
+	                        && Modifier.isFinal(mods) && fields[i].get(null).getClass().equals(clazz)) {
+	                    String stringValue = clazz.getName() + "." + fields[i].getName();
+	                    generateItem(contentHandler, stringValue);
+	                }
+	            }
+            }
             // End the selection-list
             contentHandler.endElement(FormsConstants.INSTANCE_NS, SELECTION_LIST_EL, FormsConstants.INSTANCE_PREFIX_COLON + SELECTION_LIST_EL);
         } catch (Exception e) {
             throw new SAXException("Got exception trying to get enum's values", e);
         }
+    }
+
+    /**
+     * Generates a single selection list item.
+     * @param contentHandler The content handler we are streaming sax events to.
+     * @param stringValue The string name of the item, composed by FQN and enum item.
+     * @throws SAXException
+     */
+    private void generateItem(ContentHandler contentHandler, String stringValue) throws SAXException {
+        // Output this item
+        AttributesImpl itemAttrs = new AttributesImpl();
+        itemAttrs.addCDATAAttribute("value", stringValue);
+        contentHandler.startElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL, itemAttrs);
+        contentHandler.startElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL, XMLUtils.EMPTY_ATTRIBUTES);
+        // TODO: make i18n element optional
+        contentHandler.startElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL, XMLUtils.EMPTY_ATTRIBUTES);
+        contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length());
+        contentHandler.endElement(FormsConstants.I18N_NS, TEXT_EL, FormsConstants.I18N_PREFIX_COLON + TEXT_EL);
+        contentHandler.endElement(FormsConstants.INSTANCE_NS, LABEL_EL, FormsConstants.INSTANCE_PREFIX_COLON + LABEL_EL);
+        contentHandler.endElement(FormsConstants.INSTANCE_NS, ITEM_EL, FormsConstants.INSTANCE_PREFIX_COLON + ITEM_EL);
     }
 }

Modified: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionListBuilder.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionListBuilder.java?rev=383842&r1=383841&r2=383842&view=diff
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionListBuilder.java (original)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/datatype/EnumSelectionListBuilder.java Tue Mar  7 03:01:58 2006
@@ -31,7 +31,8 @@
         throws Exception {
         String className = DomHelper.getAttribute(selectionListElement, "class");
         boolean nullable = DomHelper.getAttributeAsBoolean(selectionListElement, "nullable", true);
-        return new EnumSelectionList(className, datatype, nullable);
+        String nulltext = DomHelper.getAttribute(selectionListElement, "null-text", null);
+        return new EnumSelectionList(className, datatype, nullable, nulltext);
     }
 
 }

Modified: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/Contact.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/Contact.java?rev=383842&r1=383841&r2=383842&view=diff
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/Contact.java (original)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/Contact.java Tue Mar  7 03:01:58 2006
@@ -26,6 +26,7 @@
     private String lastName;
     private String phone;
     private String email;
+    private PreferredContact preferred;
 
     public long getId() {
         return id;
@@ -69,5 +70,13 @@
 
     public String toString() {
         return "< id = " + id + ", firstName = " + firstName + ", lastName = " + lastName + ", phone = " + phone + ", email = " + email + " >";
+    }
+
+    public PreferredContact getPreferred() {
+        return preferred;
+    }
+
+    public void setPreferred(PreferredContact preferred) {
+        this.preferred = preferred;
     }
 }

Added: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java?rev=383842&view=auto
==============================================================================
--- cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java (added)
+++ cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java Tue Mar  7 03:01:58 2006
@@ -0,0 +1,42 @@
+/*
+ * Copyright 1999-2004 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.cocoon.forms.samples;
+
+import org.apache.commons.lang.enums.Enum;
+
+/**
+ * Test apache enum class.
+ * 
+ * @author Simone Gianni <s....@thebug.it>
+ * @version $Id$
+ */
+public class PreferredContact extends Enum {
+
+    public static final PreferredContact EMAIL = new PreferredContact("EMAIL");
+    public static final PreferredContact FAX = new PreferredContact("FAX");
+    public static final PreferredContact PHONE = new PreferredContact("PHONE");
+    public static final PreferredContact PAGER = new PreferredContact("PAGER");
+    public static final PreferredContact POSTAL_MAIL = new PreferredContact("POSTAL_MAIL");
+    
+    protected PreferredContact(String name) {
+        super(name);
+    }
+    
+    public String toString() {
+        return PreferredContact.class.getName() + "." + getName();
+    }
+
+}

Propchange: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cocoon/trunk/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/samples/PreferredContact.java
------------------------------------------------------------------------------
    svn:keywords = Id