Skip to content
Browse files

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.


git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1444260 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 84c2774 commit cb3d26179aa0e4ac30f9acf6c7d0b7b9506e306f Roger Lee Whitcomb committed Feb 8, 2013
View
16 wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
@@ -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));
}
+
}
View
12 wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
@@ -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;
/*
View
5 wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java
@@ -20,6 +20,9 @@
* 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 boolean isValid(String text) {
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();
}

0 comments on commit cb3d261

Please sign in to comment.
Something went wrong with that request. Please try again.