Author: rwhitcomb Date: Fri Feb 8 22:19:59 2013 New Revision: 1444261 URL: http://svn.apache.org/r1444261 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. This is a merge of revision 1444260 from trunk to branches/2.0.x. Modified: pivot/branches/2.0.x/ (props changed) pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java Propchange: pivot/branches/2.0.x/ ------------------------------------------------------------------------------ Merged /pivot/trunk:r1444260 Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java?rev=1444261&r1=1444260&r2=1444261&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java Fri Feb 8 22:19:59 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/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java?rev=1444261&r1=1444260&r2=1444261&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java Fri Feb 8 22:19:59 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/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java?rev=1444261&r1=1444260&r2=1444261&view=diff ============================================================================== --- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java (original) +++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java Fri Feb 8 22:19:59 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(); }