You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2013/02/08 23:17:50 UTC

svn commit: r1444260 - in /pivot/trunk/wtk/src/org/apache/pivot/wtk/validation: DoubleValidator.java FloatValidator.java FormattedValidator.java

Author: rwhitcomb
Date: Fri Feb  8 22:17:50 2013
New Revision: 1444260

URL: http://svn.apache.org/r1444260
Log:
PIVOT-892: Floating-point validators were not recognizing scientific
notation as valid input.

>From a lot of online searching, and reading through the Java code, it
appears that scientific notation is only enabled in NumberFormat by
applying a pattern (i.e., there is no factory method that returns
a suitable NumberFormat).  And even then, exponents are only recognized
if the capital E is used (not lower-case 'e').  So, adjust for that in
both the default and locale-specific cases.

Updated a bit of documentation in the FormattedValidator class to make
things more clear.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java?rev=1444260&r1=1444259&r2=1444260&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java Fri Feb  8 22:17:50 2013
@@ -13,16 +13,30 @@
  */
 package org.apache.pivot.wtk.validation;
 
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
 import java.util.Locale;
 
 /**
  * A validator for a double value.
  */
 public class DoubleValidator extends DecimalValidator {
+    private Locale locale = null;
+
     public DoubleValidator() {
+        super(new DecimalFormat("0E0"));
     }
 
     public DoubleValidator(Locale locale) {
-        super(locale);
+        this();
+        this.locale = locale;
+        ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
+    }
+
+    @Override
+    public boolean isValid(String text) {
+        // We have to upper case because of the exponent symbol
+        return super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale));
     }
+
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java?rev=1444260&r1=1444259&r2=1444260&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java Fri Feb  8 22:17:50 2013
@@ -13,22 +13,30 @@
  */
 package org.apache.pivot.wtk.validation;
 
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
 import java.util.Locale;
 
 /**
  * A validator for a float value.
  */
 public class FloatValidator extends DecimalValidator {
+    private Locale locale = null;
+
     public FloatValidator() {
+        super(new DecimalFormat("0E0"));
     }
 
     public FloatValidator(Locale locale) {
-        super(locale);
+        this();
+        this.locale = locale;
+        ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
     }
 
     @Override
     public boolean isValid(String text) {
-        if (!super.isValid(text))
+        // We have to upper case because of the exponent symbol
+        if (!super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale)))
             return false;
 
         /*

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java?rev=1444260&r1=1444259&r2=1444260&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java Fri Feb  8 22:17:50 2013
@@ -20,6 +20,9 @@ import java.text.ParsePosition;
  * A validator for a {@link java.text.Format}'ed value.
  * <p>
  * This class is mostly intended to be a base-class for other validators.
+ * Subclasses will set a different {@link java.text.Format} object, which
+ * will be used in the {@link #isValid} method of this base class to do the
+ * validation.
  */
 public class FormattedValidator<F extends Format> implements Validator {
     protected final F format;
@@ -33,7 +36,7 @@ public class FormattedValidator<F extend
         final ParsePosition pos = new ParsePosition(0);
         Object obj = format.parseObject(text, pos);
 
-        // the text is only valid is we successfully parsed ALL of it. Don't want trailing bits of
+        // The text is only valid if we successfully parsed ALL of it. Don't want trailing bits of
         // not-valid text.
         return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == text.length();
     }