You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by tw...@locus.apache.org on 2000/01/06 01:14:58 UTC

cvs commit: xml-xerces/java/src/org/apache/xerces/validators/datatype BinaryValidator.java URIValidator.java DatatypeMessageProvider.java DecimalValidator.java StringValidator.java

twl         00/01/05 16:14:58

  Modified:    java/src/org/apache/xerces/msg DatatypeMessages.java
               java/src/org/apache/xerces/validators/datatype
                        DatatypeMessageProvider.java DecimalValidator.java
                        StringValidator.java
  Added:       java/src/org/apache/xerces/validators/datatype
                        BinaryValidator.java URIValidator.java
  Log:
  More datatype support
  
  Revision  Changes    Path
  1.2       +3 -1      xml-xerces/java/src/org/apache/xerces/msg/DatatypeMessages.java
  
  Index: DatatypeMessages.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/msg/DatatypeMessages.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DatatypeMessages.java	1999/11/09 01:12:52	1.1
  +++ DatatypeMessages.java	2000/01/06 00:14:57	1.2
  @@ -85,7 +85,9 @@
           { "IllegalIntegerFacet", "Illegal Facet for Integer type." },
           { "NotReal", "{0} is not a double." },
           { "IllegalRealFacet", "Illegal Facet for Real type." },
  -
  +        { "ScaleLargerThanPrecision", "Scale Facet must be less than or equal to Precision Facet" },
  +        { "PrecisionExceeded", "{0} has exceeded the precision Facet {1}"},
  +        { "ScaleExceeded", "{0} has execeed the scale Facet {1}"},
       };
   
       /** Returns the list resource bundle contents. */
  
  
  
  1.2       +9 -3      xml-xerces/java/src/org/apache/xerces/validators/datatype/DatatypeMessageProvider.java
  
  Index: DatatypeMessageProvider.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/datatype/DatatypeMessageProvider.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- DatatypeMessageProvider.java	1999/11/09 01:11:57	1.1
  +++ DatatypeMessageProvider.java	2000/01/06 00:14:58	1.2
  @@ -147,8 +147,11 @@
           IllegalIntegerFacet = 12,
           NotReal = 13,
           IllegalRealFacet = 14,
  -        // ...
  -        MSG_MAX_CODE = 15;
  +        ScaleLargerThanPrecision = 15,
  +        PrecisionExceeded = 16,
  +        ScaleExceeded = 17,
  +       // ...
  +        MSG_MAX_CODE = 18;
   
       //
       // Minor Codes
  @@ -171,6 +174,9 @@
           "NotInteger",                   //  11, "{0} is not an integer."
           "IllegalIntegerFacet",          //  12, "Illegal Facet for Integer type."
           "NotReal",                      //  13, "{0} is not a double."
  -        "IllegalRealFacet",             //  14, "Illegal Facet for Real type." 
  +        "IllegalRealFacet",             //  14, "Illegal Facet for Real type."
  +        "ScaleLargerThanPrecision",     //  15, "Scale Facet must be less than or equal to Precision Facet"
  +        "PrecisionExceeded",            //  16, "{0} has exceeded the precision Facet {1}"},
  +        "ScaleExceeded",                //  17, "{0} has execeed the scale Facet {1}"},
       };
   }
  
  
  
  1.3       +57 -26    xml-xerces/java/src/org/apache/xerces/validators/datatype/DecimalValidator.java
  
  Index: DecimalValidator.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/datatype/DecimalValidator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- DecimalValidator.java	1999/12/18 04:25:38	1.2
  +++ DecimalValidator.java	2000/01/06 00:14:58	1.3
  @@ -57,6 +57,8 @@
   
   package org.apache.xerces.validators.datatype;
   
  +import java.math.BigDecimal;
  +import java.math.BigInteger;
   import java.util.Enumeration;
   import java.util.Hashtable;
   import java.util.Locale;
  @@ -72,16 +74,20 @@
   
   public class DecimalValidator implements InternalDatatypeValidator {
   	
  -	double fMaxInclusive = 0;
  +	BigDecimal fMaxInclusive = null;
   	boolean fIsMaxInclusive = false;
  -	double fMaxExclusive = 0;
  +	BigDecimal fMaxExclusive = null;
   	boolean fIsMaxExclusive = false;
  -	double fMinInclusive = 0;
  +	BigDecimal fMinInclusive = null;
   	boolean fIsMinInclusive = false;
  -	double fMinExclusive = 0;
  +	BigDecimal fMinExclusive = null;
   	boolean fIsMinExclusive = false;
  -	double fEnumValues[] = null;
  +	BigDecimal fEnumValues[] = null;
   	boolean fHasEnums = false;
  +    int fPrecision = 0;
  +    boolean fIsPrecision = false;
  +    int fScale = 0;
  +    boolean fIsScale = false;
   	DecimalValidator fBaseValidator = null;
   	private DatatypeMessageProvider fMessageProvider = new DatatypeMessageProvider();
   	private Locale fLocale = null;
  @@ -99,15 +105,27 @@
        */
   
   	public void validate(String content) throws InvalidDatatypeValueException {
  -	    double d = 0;
  +	    BigDecimal d = null;
           try {
  -            d = Double.valueOf(content).doubleValue();
  +            d = new BigDecimal(content);
           } catch (NumberFormatException nfe) {
               throw new InvalidDatatypeValueException(
   				getErrorString(DatatypeMessageProvider.NotDecimal,
   							   DatatypeMessageProvider.MSG_NONE,
   							   new Object[] { content }));
           }
  +        if (fIsScale)
  +            if (d.scale() > fScale)
  +                throw new InvalidDatatypeValueException(
  +                    getErrorString(DatatypeMessageProvider.ScaleExceeded,
  +                                   DatatypeMessageProvider.MSG_NONE,
  +                                   new Object[] { content }));
  +        if (fIsPrecision)
  +            if (d.unscaledValue().toString(10).length() > fPrecision) 
  +                throw new InvalidDatatypeValueException(
  +                    getErrorString(DatatypeMessageProvider.PrecisionExceeded,
  +                                   DatatypeMessageProvider.MSG_NONE,
  +                                   new Object[] {content} ));
           boundsCheck(d);
           if (fHasEnums)
               enumCheck(d);
  @@ -125,21 +143,23 @@
   	        if (key.equals(DatatypeValidator.ENUMERATION)) 
                   continue;  // ENUM values passed as a vector & handled after bounds facets	    
       	    value = (String) facets.get(key);   
  -	        double decimalValue = 0;
  +	        BigDecimal decimalValue = null;
   	        try {
  -	            decimalValue = Double.valueOf(value).doubleValue();
  +	            decimalValue = new BigDecimal(value);
   	        } catch (NumberFormatException nfe) {
                   facetsAreConsistent = false;
   	        }
   	        if (key.equals(DatatypeValidator.MININCLUSIVE) && fIsMinInclusive) {
  -                facetsAreConsistent = fMinInclusive <= decimalValue;
  +                facetsAreConsistent = (fMinInclusive.compareTo(decimalValue) < 0);
   	        } else if (key.equals(DatatypeValidator.MINEXCLUSIVE) && fIsMinExclusive) {
  -	            facetsAreConsistent = fMinExclusive < decimalValue;
  +	            facetsAreConsistent = (fMinExclusive.compareTo(decimalValue) < 0);
   	        } else if (key.equals(DatatypeValidator.MAXINCLUSIVE) && fIsMaxInclusive) {
  -	            facetsAreConsistent = fMaxInclusive >= decimalValue;
  +	            facetsAreConsistent = (fMaxInclusive.compareTo(decimalValue) >= 0);
   	        } else if (key.equals(DatatypeValidator.MAXEXCLUSIVE) && fIsMaxExclusive) {
  -	            facetsAreConsistent = fMaxExclusive > decimalValue;
  -	        }
  +	            facetsAreConsistent = (fMaxExclusive.compareTo(decimalValue) > 0);
  +	        } else if (key.equals(DatatypeValidator.SCALE) && fIsScale && fIsPrecision){
  +                facetsAreConsistent = fScale <= fPrecision;
  +            }
   	    }
   	    return facetsAreConsistent;
   	}
  @@ -160,9 +180,9 @@
   	        if (key.equals(DatatypeValidator.ENUMERATION)) 
                   continue;  // ENUM values passed as a vector & handled after bounds facets	    
       	    value = (String) facets.get(key);   
  -	        double decimalValue = 0;
  +	        BigDecimal decimalValue = null;
   	        try {
  -	            decimalValue = Integer.parseInt(value);
  +	            decimalValue = new BigDecimal(value);
   	        } catch (NumberFormatException nfe) {
   	            throw new IllegalFacetValueException(
   					getErrorString(DatatypeMessageProvider.IllegalFacetValue,
  @@ -183,7 +203,11 @@
   	            fMaxExclusive = decimalValue;
   	        } else if (key.equals(DatatypeValidator.ENUMERATION)) {
   	        } else if (key.equals(DatatypeValidator.PRECISION)) {
  +                fIsPrecision = true;
  +                fPrecision = decimalValue.intValue();
   	        } else if (key.equals(DatatypeValidator.SCALE)) {
  +                fIsScale = true;
  +                fScale = decimalValue.intValue();
   	        } else if (key.equals(DatatypeValidator.LENGTH) ||
   	                 key.equals(DatatypeValidator.MINLENGTH) ||
                        key.equals(DatatypeValidator.MAXLENGTH) ||
  @@ -204,14 +228,21 @@
   								   new Object [] { key }));
   	    }
   	    
  +        // check for scale <= precision
  +        if (fIsScale && fIsPrecision && fScale > fPrecision)
  +          throw new IllegalFacetException(
  +            getErrorString(DatatypeMessageProvider.ScaleLargerThanPrecision,
  +                           DatatypeMessageProvider.MSG_NONE,
  +                           null));
  +        
           // check the enum values after any range constraints are in place
           Vector v = (Vector) facets.get(DatatypeValidator.ENUMERATION);    
   	    if (v != null) {
   	        fHasEnums = true;
  -	        fEnumValues = new double[v.size()];
  +	        fEnumValues = new BigDecimal[v.size()];
   	        for (int i = 0; i < v.size(); i++)
   	            try {
  -	                fEnumValues[i] = Double.valueOf((String) v.elementAt(i)).doubleValue();
  +	                fEnumValues[i] = new BigDecimal((String) v.elementAt(i));
   	                boundsCheck(fEnumValues[i]);
   	            } catch (InvalidDatatypeValueException idve) {
   	                throw new IllegalFacetValueException(
  @@ -235,37 +266,37 @@
       /*
        * check that a facet is in range, assumes that facets are compatible -- compatibility ensured by setFacets
        */
  -    private void boundsCheck(double d) throws InvalidDatatypeValueException {
  +    private void boundsCheck(BigDecimal d) throws InvalidDatatypeValueException {
           boolean minOk = false;
           boolean maxOk = false;
           if (fIsMaxInclusive)
  -            maxOk = (d <= fMaxInclusive);
  +            maxOk = (d.compareTo(fMaxInclusive) <= 0);
           else if (fIsMaxExclusive)
  -            maxOk = (d < fMaxExclusive);
  +            maxOk = (d.compareTo(fMaxExclusive) < 0);
           else 
               maxOk = (!fIsMaxInclusive && !fIsMaxExclusive);
           
           if (fIsMinInclusive)
  -            minOk = (d >= fMinInclusive);
  +            minOk = (d.compareTo(fMinInclusive) >= 0);
           else if (fIsMinExclusive) 
  -            minOk = (d > fMinInclusive);
  +            minOk = (d.compareTo(fMinInclusive) > 0);
           else 
               minOk = (!fIsMinInclusive && !fIsMinExclusive);
           if (!(minOk && maxOk))
               throw new InvalidDatatypeValueException(
   				getErrorString(DatatypeMessageProvider.OutOfBounds,
   							   DatatypeMessageProvider.MSG_NONE,
  -							   new Object [] { new Double(d) }));
  +							   new Object [] { d }));
       }
       
  -    private void enumCheck(double v) throws InvalidDatatypeValueException {
  +    private void enumCheck(BigDecimal v) throws InvalidDatatypeValueException {
           for (int i = 0; i < fEnumValues.length; i++) {
               if (v == fEnumValues[i]) return;
           }
           throw new InvalidDatatypeValueException(
   			getErrorString(DatatypeMessageProvider.NotAnEnumValue,
   						   DatatypeMessageProvider.MSG_NONE,
  -						   new Object [] { new Double(v) }));
  +						   new Object [] { v }));
       }
   
       /**
  
  
  
  1.3       +17 -1     xml-xerces/java/src/org/apache/xerces/validators/datatype/StringValidator.java
  
  Index: StringValidator.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/validators/datatype/StringValidator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StringValidator.java	1999/12/18 04:25:38	1.2
  +++ StringValidator.java	2000/01/06 00:14:58	1.3
  @@ -59,6 +59,7 @@
   
   import java.util.Hashtable;
   import java.util.Locale;
  +import java.util.Enumeration;
   
   /**
    *
  @@ -70,7 +71,7 @@
   
   public class StringValidator implements InternalDatatypeValidator {
   	
  -	Locale fLocale = null;
  +    private Locale fLocale = null;
   	
   	/**
        * validate that a string is a W3C string type
  @@ -92,6 +93,21 @@
   	}
   	
   	public void setFacets(Hashtable facets) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
  +        for (Enumeration e = facets.keys(); e.hasMoreElements();) {
  +            String key = (String) e.nextElement();
  +            if (key.equals(DatatypeValidator.LENGTH)) {
  +            } else if (key.equals(DatatypeValidator.MINLENGTH)) {
  +            } else if (key.equals(DatatypeValidator.MAXLENGTH)) {
  +            } else if (key.equals(DatatypeValidator.MAXINCLUSIVE)) {
  +            } else if (key.equals(DatatypeValidator.MAXEXCLUSIVE)) {
  +            } else if (key.equals(DatatypeValidator.MININCLUSIVE)) {
  +            } else if (key.equals(DatatypeValidator.MINEXCLUSIVE)) {
  +            } else if (key.equals(DatatypeValidator.PATTERN)) {
  +            } else if (key.equals(DatatypeValidator.ENUMERATION)) {
  +            } else {
  +                throw new IllegalFacetException();
  +            }
  +        }
   	}
   	
   	public void setFacets(int facets[]) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
  
  
  
  1.1                  xml-xerces/java/src/org/apache/xerces/validators/datatype/BinaryValidator.java
  
  Index: BinaryValidator.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xerces" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.xerces.validators.datatype;
  
  import java.util.Hashtable;
  import java.util.Locale;
  
  /**
   *
   * binaryValidator validates that XML content is a W3C binary type.
   *
   * @author Ted Leung
   * @version Revision: %M% %I% %W% %Q%
   */
  
  public class BinaryValidator implements InternalDatatypeValidator {
      private Locale fLocale;	
      /**
       * validate that a string is a W3C binary type
       *
       * validate returns true or false depending on whether the string content is an
       * instance of the W3C binary datatype
       * 
       * @param content A string containing the content to be validated
       *
       * @exception throws InvalidDatatypeException if the content is
       *  not a W3C binary type
       */
  
  	public void validate(String content) throws InvalidDatatypeValueException {
          // just say yes
  	}
  			
  	public void validate(int contentIndex) throws InvalidDatatypeValueException {
  	}
  	
  	public void setFacets(Hashtable facets) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
          for (Enumeration e = facets.keys(); e.hasMoreElements;) {
              String key = (String) e.nextElement();
              if (key.equals(DatatypeValidator.LENGTH)) {
              } else if (key.equals(DatatypeValidator.MINLENGTH)) {
              } else if (key.equals(DatatypeValidator.MAXLENGTH)) {
              } else if (key.equals(DatatypeValidator.ENCODING)) {
              } else {
                  throw new IllegalFacetException();
              }
          }	}
  	
  	public void setFacets(int facets[]) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
  	}
  
  	public void setBasetype(DatatypeValidator base) {
  	}
  
      /**
       * set the locate to be used for error messages
       */
      public void setLocale(Locale locale) {
          fLocale = locale;
      }
  }
  
  
  
  1.1                  xml-xerces/java/src/org/apache/xerces/validators/datatype/URIValidator.java
  
  Index: URIValidator.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xerces" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.apache.org.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.xerces.validators.datatype;
  
  import java.util.Hashtable;
  import java.util.Locale;
  
  /**
   *
   * URIValidator validates that XML content is a W3C uri type,
   * according to RFC 2396
   *
   * @author Ted Leung
   * @version Revision: %M% %I% %W% %Q%
   */
  
  public class URIValidator implements InternalDatatypeValidator {
  	
      private Locale fLocale = null;
  	
  	/**
       * validate that a string is a W3C uri type
       *
       * validate returns true or false depending on whether the string content is an
       * instance of the W3C uri datatype
       * 
       * @param content A string containing the content to be validated
       *
       * @exception throws InvalidDatatypeException if the content is
       *  not a W3C uri type
       */
  
  	public void validate(String content) throws InvalidDatatypeValueException {
          // just say yes
  	}
  			
  	public void validate(int contentIndex) throws InvalidDatatypeValueException {
  	}
  	
  	public void setFacets(Hashtable facets) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
  	}
  	
  	public void setFacets(int facets[]) throws UnknownFacetException, IllegalFacetException, IllegalFacetValueException {
  	}
  
  	public void setBasetype(DatatypeValidator base) {
  	}
  
      /**
       * set the locate to be used for error messages
       */
      public void setLocale(Locale locale) {
          fLocale = locale;
      }
  }