mrglavas    2005/07/13 21:20:15

  Modified:    java/src/org/apache/xerces/impl/dv/xs DoubleDV.java
                        FloatDV.java
  Log:
  Fixing a bug with validation of float and double values. We delegate to 
Java's Float.parseFloat() and
  Double.parseDouble() methods when validating float and double values. These 
methods particularly
  in Java 5.0 allow lexical forms which are not allowed by the schema spec 
including: Infinity, -Infinity,
  +NaN, 0.2f, 0.7d and 0x1234.abcdP123. We now pre-screen the string for valid 
characters before
  passing it to the library methods.
  
  Revision  Changes    Path
  1.12      +31 -16    
xml-xerces/java/src/org/apache/xerces/impl/dv/xs/DoubleDV.java
  
  Index: DoubleDV.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/impl/dv/xs/DoubleDV.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DoubleDV.java     6 Oct 2004 14:56:47 -0000       1.11
  +++ DoubleDV.java     14 Jul 2005 04:20:14 -0000      1.12
  @@ -1,5 +1,5 @@
   /*
  - * Copyright 2001-2004 The Apache Software Foundation.
  + * Copyright 2001-2005 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.
  @@ -58,26 +58,41 @@
           }
           return false;
       }//isIdentical()
  +    
  +    /** 
  +     * Returns true if it's possible that the given
  +     * string represents a valid floating point value
  +     * (excluding NaN, INF and -INF).
  +     */
  +    static boolean isPossibleFP(String val) {
  +        final int length = val.length();
  +        for (int i = 0; i < length; ++i) {
  +            char c = val.charAt(i);
  +            if (!(c >= '0' && c <= '9' || c == '.' || 
  +                c == '-' || c == '+' || c == 'E' || c == 'e')) {
  +                return false;
  +            }
  +        }
  +        return true;
  +    }
   
       private static final class XDouble implements XSDouble {
           private double value;
           public XDouble(String s) throws NumberFormatException {
  -            try {
  +            if (isPossibleFP(s)) {
                   value = Double.parseDouble(s);
               }
  -            catch ( NumberFormatException nfe ) {
  -                if ( s.equals("INF") ) {
  -                    value = Double.POSITIVE_INFINITY;
  -                }
  -                else if ( s.equals("-INF") ) {
  -                    value = Double.NEGATIVE_INFINITY;
  -                }
  -                else if ( s.equals("NaN" ) ) {
  -                    value = Double.NaN;
  -                }
  -                else {
  -                    throw nfe;
  -                }
  +            else if ( s.equals("INF") ) {
  +                value = Double.POSITIVE_INFINITY;
  +            }
  +            else if ( s.equals("-INF") ) {
  +                value = Double.NEGATIVE_INFINITY;
  +            }
  +            else if ( s.equals("NaN" ) ) {
  +                value = Double.NaN;
  +            }
  +            else {
  +                throw new NumberFormatException(s);
               }
           }
   
  
  
  
  1.12      +14 -16    
xml-xerces/java/src/org/apache/xerces/impl/dv/xs/FloatDV.java
  
  Index: FloatDV.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xerces/java/src/org/apache/xerces/impl/dv/xs/FloatDV.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- FloatDV.java      6 Oct 2004 14:56:47 -0000       1.11
  +++ FloatDV.java      14 Jul 2005 04:20:15 -0000      1.12
  @@ -1,5 +1,5 @@
   /*
  - * Copyright 2001-2004 The Apache Software Foundation.
  + * Copyright 2001-2005 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.
  @@ -63,22 +63,20 @@
   
           private float value;
           public XFloat(String s) throws NumberFormatException {
  -            try {
  +            if (DoubleDV.isPossibleFP(s)) {
                   value = Float.parseFloat(s);
               }
  -            catch ( NumberFormatException nfe ) {
  -                if ( s.equals("INF") ) {
  -                    value = Float.POSITIVE_INFINITY;
  -                }
  -                else if ( s.equals("-INF") ) {
  -                    value = Float.NEGATIVE_INFINITY;
  -                }
  -                else if ( s.equals("NaN" ) ) {
  -                    value = Float.NaN;
  -                }
  -                else {
  -                    throw nfe;
  -                }
  +            else if ( s.equals("INF") ) {
  +                value = Float.POSITIVE_INFINITY;
  +            }
  +            else if ( s.equals("-INF") ) {
  +                value = Float.NEGATIVE_INFINITY;
  +            }
  +            else if ( s.equals("NaN") ) {
  +                value = Float.NaN;
  +            }
  +            else {
  +                throw new NumberFormatException(s);
               }
           }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to