cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jjohns...@apache.org
Subject svn commit: r474132 - in /cocoon: branches/BRANCH_2_1_X/ trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/
Date Mon, 13 Nov 2006 04:07:30 GMT
Author: jjohnston
Date: Sun Nov 12 20:07:30 2006
New Revision: 474132

URL: http://svn.apache.org/viewvc?view=rev&rev=474132
Log:
<action dev="JJ" type="add" fixes-bug="COCOON-1879">
CForms: Added @whitespace attribute to fd:field widget, to control how
leading and trailing whitespace characters are handled when reading
submitted values. Accepted values are: "trim" (default), "trim-start",
"trim-end", and "preserve".
</action>

Added:
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Whitespace.java
Modified:
    cocoon/branches/BRANCH_2_1_X/status.xml
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Field.java
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinition.java
    cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinitionBuilder.java

Modified: cocoon/branches/BRANCH_2_1_X/status.xml
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X/status.xml?view=diff&rev=474132&r1=474131&r2=474132
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/status.xml (original)
+++ cocoon/branches/BRANCH_2_1_X/status.xml Sun Nov 12 20:07:30 2006
@@ -184,6 +184,11 @@
   <release version="@version@" date="@date@">
 -->
   <release version="2.1.10" date="TBD">
+    <action dev="JJ" type="add" fixes-bug="COCOON-1879">
+      CForms: Added @whitespace attribute to fd:field widget, to control how leading
+      and trailing whitespace characters are handled when reading submitted values.
+      Accepted values are: "trim" (default), "trim-start", "trim-end", and "preserve".
+    </action>
     <action dev="VG" type="update">
       Mail: Improve MailSender interface: added setBody(Object), setBodyURL
       methods, deprecated setCharset, setBody(String), setBodyFromSrc,

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Field.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Field.java?view=diff&rev=474132&r1=474131&r2=474132
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Field.java
(original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Field.java
Sun Nov 12 20:07:30 2006
@@ -319,13 +319,7 @@
 
     protected void readFromRequest(String newEnteredValue) {
         // whitespace & empty field handling
-        if (newEnteredValue != null) {
-            // TODO make whitespace behaviour configurable !!
-            newEnteredValue = newEnteredValue.trim();
-            if (newEnteredValue.length() == 0) {
-                newEnteredValue = null;
-            }
-        }
+        newEnteredValue = applyWhitespaceTrim(newEnteredValue);
 
         // Only convert if the text value actually changed. Otherwise, keep the old value
         // and/or the old validation error (allows to keep errors when clicking on actions)
@@ -361,6 +355,27 @@
                 getForm().addWidgetUpdate(this);
             }
         }
+    }
+
+    protected String applyWhitespaceTrim(String value) {
+        if (value != null) {
+            Whitespace trim = this.definition.getWhitespaceTrim();
+            if(trim == null || trim == Whitespace.TRIM) {
+            	value = value.trim();
+            } else if(trim == Whitespace.PRESERVE) {
+                // do nothing.
+            } else if(trim == Whitespace.TRIM_START) {
+                value = StringUtils.stripStart(value, null);
+            } else if(trim == Whitespace.TRIM_END) {
+                value = StringUtils.stripEnd(value, null);
+            }
+
+            // treat empty strings as null
+            if (value.length() == 0) {
+                value = null;
+            }
+        }
+        return value;
     }
 
     /**

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinition.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinition.java?view=diff&rev=474132&r1=474131&r2=474132
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinition.java
(original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinition.java
Sun Nov 12 20:07:30 2006
@@ -27,6 +27,7 @@
 public class FieldDefinition extends AbstractDatatypeWidgetDefinition {
 
     private boolean required;
+    private Whitespace whitespaceTrim = Whitespace.TRIM;
     private SelectionList suggestionList;
 
 
@@ -48,6 +49,7 @@
         FieldDefinition other = (FieldDefinition)definition;
 
         this.required = other.required;
+        this.whitespaceTrim = other.whitespaceTrim;
 
         if (suggestionList == null) {
             suggestionList = other.getSuggestionList();
@@ -61,6 +63,15 @@
     public void setRequired(boolean required) {
         checkMutable();
         this.required = required;
+    }
+
+    public Whitespace getWhitespaceTrim() {
+        return whitespaceTrim;
+    }
+
+    public void setWhitespaceTrim(Whitespace whitespaceTrim) {
+        checkMutable();
+        this.whitespaceTrim = whitespaceTrim;
     }
 
     public SelectionList getSuggestionList() {

Modified: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinitionBuilder.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinitionBuilder.java?view=diff&rev=474132&r1=474131&r2=474132
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinitionBuilder.java
(original)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/FieldDefinitionBuilder.java
Sun Nov 12 20:07:30 2006
@@ -16,6 +16,7 @@
  */
 package org.apache.cocoon.forms.formmodel;
 
+import org.apache.cocoon.forms.FormsException;
 import org.apache.cocoon.forms.datatype.SelectionList;
 import org.apache.cocoon.forms.util.DomHelper;
 import org.w3c.dom.Element;
@@ -36,11 +37,19 @@
 
     protected void setupDefinition(Element widgetElement, FieldDefinition definition) throws
Exception {
         super.setupDefinition(widgetElement, definition);
-        
+
         // parse "@required"
         if(widgetElement.hasAttribute("required"))
             definition.setRequired(DomHelper.getAttributeAsBoolean(widgetElement, "required",
false));
-        
+
+        // parse "@whitespace"
+        Whitespace whitespace = Whitespace.getEnum(DomHelper.getAttribute(widgetElement,
"whitespace", "trim"));
+        if(whitespace == null) {
+            throw new FormsException("Unknown value for 'whitespace' attribute; valid values
are 'trim', 'trim-start', 'trim-end', and 'preserve'.",
+                    DomHelper.getLocationObject(widgetElement));
+        }
+        definition.setWhitespaceTrim(whitespace);
+
         SelectionList list = buildSelectionList(widgetElement, definition, "suggestion-list");
         if (list != null) {
             definition.setSuggestionList(list);

Added: cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Whitespace.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Whitespace.java?view=auto&rev=474132
==============================================================================
--- cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Whitespace.java
(added)
+++ cocoon/trunk/blocks/cocoon-forms/cocoon-forms-impl/src/main/java/org/apache/cocoon/forms/formmodel/Whitespace.java
Sun Nov 12 20:07:30 2006
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+package org.apache.cocoon.forms.formmodel;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.lang.enums.Enum;
+
+/**
+ * Enumeration of whitespace-handling strategies.
+ */
+public class Whitespace extends Enum {
+
+    public static final Whitespace PRESERVE = new Whitespace("preserve");
+    public static final Whitespace TRIM_START = new Whitespace("trim-start");
+    public static final Whitespace TRIM_END = new Whitespace("trim-end");
+    public static final Whitespace TRIM = new Whitespace("trim");
+
+    private Whitespace(String name) {
+        super(name);
+    }
+
+    public static Whitespace getEnum(String name) {
+        return (Whitespace) getEnum(Whitespace.class, name);
+    }
+
+    public static Map getEnumMap() {
+        return getEnumMap(Whitespace.class);
+    }
+
+    public static List getEnumList() {
+        return getEnumList(Whitespace.class);
+    }
+
+    public static Iterator iterator() {
+        return iterator(Whitespace.class);
+    }
+}



Mime
View raw message