From 8c828aa20486de635e3ac15dae5326d166ee3166 Mon Sep 17 00:00:00 2001
From: Tinny Ng <tng@apache.org>
Date: Fri, 18 May 2001 20:18:02 +0000
Subject: [PATCH] Schema: More exception messages in
 XMLBigDecimal/XMLBigInteger/DecimalDatatypeValidator.  By Pei Yong Zhang.

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@172669 13f79535-47bb-0310-9956-ffa450edef68
---
 src/util/XMLBigDecimal.cpp                    | 88 +++++++------------
 src/util/XMLBigDecimal.hpp                    |  2 -
 src/util/XMLBigInteger.cpp                    | 60 ++++---------
 src/util/XMLBigInteger.hpp                    |  2 -
 .../datatype/DecimalDatatypeValidator.cpp     | 39 ++++++--
 5 files changed, 81 insertions(+), 110 deletions(-)

diff --git a/src/util/XMLBigDecimal.cpp b/src/util/XMLBigDecimal.cpp
index 2f61557c5..71a22401a 100644
--- a/src/util/XMLBigDecimal.cpp
+++ b/src/util/XMLBigDecimal.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.4  2001/05/18 20:17:55  tng
+ * Schema: More exception messages in XMLBigDecimal/XMLBigInteger/DecimalDatatypeValidator.  By Pei Yong Zhang.
+ *
  * Revision 1.3  2001/05/18 13:22:54  tng
  * Schema: Exception messages in DatatypeValidator.  By Pei Yong Zhang.
  *
@@ -70,56 +73,52 @@
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
-#include <string.h>
-#include <iostream.h>
 #include <util/XMLBigDecimal.hpp>
 #include <util/PlatformUtils.hpp>
 #include <util/XMLString.hpp>
 #include <util/XMLUniDefs.hpp>
 #include <util/NumberFormatException.hpp>
-#include <util/RuntimeException.hpp>
 #include <util/TransService.hpp>
 #include <util/Janitor.hpp>
 
 /**
-	 * Constructs a BigDecimal from a string containing an optional minus
-	 * sign followed by a sequence of zero or more decimal digits, optionally
-	 * followed by a fraction, which consists of a decimal point followed by
-	 * zero or more decimal digits.  The string must contain at least one
-	 * digit in the integer or fractional part.  The scale of the resulting
-	 * BigDecimal will be the number of digits to the right of the decimal
-	 * point in the string, or 0 if the string contains no decimal point.
-	 * Any extraneous characters (including whitespace) will result in
-	 * a NumberFormatException.
-*/
-//
-// since parseBigDecimal and XMLBigInteger() may
-// throw exception, caller of XMLBigDecimal better
-// be ready to catch it.
+ * Constructs a BigDecimal from a string containing an optional (plus | minus)
+ * sign followed by a sequence of zero or more decimal digits, optionally
+ * followed by a fraction, which consists of a decimal point followed by
+ * zero or more decimal digits.  The string must contain at least one
+ * digit in the integer or fractional part.  The scale of the resulting
+ * BigDecimal will be the number of digits to the right of the decimal
+ * point in the string, or 0 if the string contains no decimal point.
+ * Any extraneous characters (including whitespace) will result in
+ * a NumberFormatException.
+
+ * since parseBigDecimal and XMLBigInteger() may throw exception, 
+ * caller of XMLBigDecimal need to catch it.
 //
+**/
 
 XMLBigDecimal::XMLBigDecimal(const XMLCh* const strValue)
 :fIntVal(0)
 ,fScale(0)
 {
+    if (!strValue)
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
+
     XMLCh* ret_value = new XMLCh[XMLString::stringLen(strValue)+1];
     ArrayJanitor<XMLCh> janName(ret_value);
 
     parseBigDecimal(strValue, ret_value, fScale);
     fIntVal = new XMLBigInteger(ret_value);
-
 }
 
 XMLBigDecimal::XMLBigDecimal(const XMLBigDecimal& toCopy)
 :fIntVal(0)
 ,fScale(toCopy.getScale())
 {
-
     //invoke XMLBigInteger' copy ctor
     fIntVal = new XMLBigInteger(*(toCopy.getValue()));
 }
 
-
 /***
    *
    *  Leading and trailing whitespaces are allowed, and trimmed
@@ -154,10 +153,8 @@ void XMLBigDecimal::parseBigDecimal(const XMLCh* const toConvert
     scaleValue = 0;
 
     // If no string, then its a failure
-    if ((!toConvert) ||
-        (!*toConvert))
-        ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLINT_Invalid);
+    if ((!toConvert) || (!*toConvert))
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
 
     // Scan past any whitespace. If we hit the end, then return failure
     const XMLCh* startPtr = toConvert;
@@ -165,8 +162,7 @@ void XMLBigDecimal::parseBigDecimal(const XMLCh* const toConvert
         startPtr++;
 
     if (!*startPtr)
-        ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLINT_Invalid);
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_WSString);
 
     // Start at the end and work back through any whitespace
     const XMLCh* endPtr = toConvert + XMLString::stringLen(toConvert);
@@ -181,27 +177,18 @@ void XMLBigDecimal::parseBigDecimal(const XMLCh* const toConvert
     // '+' or '-' is allowed only at the first position
     //
     if (*startPtr == chDash)
-    {
-        // copy the '-'
-        *retPtr = chDash;
+    {       
+        *retPtr = chDash;  // copy the '-'
         startPtr++;
         retPtr++;
     }
     else if (*startPtr == chPlus)
-    {
-        // skip the '+'
-        startPtr++;
-    }
+        startPtr++;        // skip the '+'
 
     // Leading zero will be taken care by BigInteger
-
     bool   dotSignFound = false;
-
     while (startPtr < endPtr)
     {
-        //
-        // '.' is allowed only once
-        //
         if (*startPtr == chPeriod)
         {
             if (dotSignFound == false)
@@ -211,15 +198,13 @@ void XMLBigDecimal::parseBigDecimal(const XMLCh* const toConvert
                 startPtr++;
                 continue;
             }
-            else
-                ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-                //ThrowXML(NumberFormatException, XMLExcepts::XMLBIGDECIMAL_MANY_DOT);
+            else  // '.' is allowed only once
+                ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_2ManyDecPoint);
         }
 
         // If not valid decimal digit, then an error
         if ((*startPtr < chDigit_0) || (*startPtr > chDigit_9))
-            ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-            //ThrowXML(NumberFormatException, XMLExcepts::XMLBIGDECIMAL_Invalid);
+            ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_Inv_chars);
 
         // copy over
         *retPtr = *startPtr;
@@ -235,16 +220,12 @@ void XMLBigDecimal::parseBigDecimal(const XMLCh* const toConvert
  * Returns -1, 0 or 1 as lValue is less than, equal to, or greater
  * than rValue.  Two BigDecimals that are equal in value but have a
  * different scale (e.g., 2.0, 2.00) are considered equal by this method.
-*/
-
+**/
 int XMLBigDecimal::compareValues(const XMLBigDecimal* const lValue
                                , const XMLBigDecimal* const rValue)
 {
-    //
-    if ((!lValue) ||
-        (!rValue) )
-        ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLBIGDECIMAL_Null_value);
+    if ((!lValue) || (!rValue) )
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_null_ptr);
 
     /* Optimization: would run fine without the next three lines */
 	int sigDiff = lValue->getSign() - rValue->getSign();
@@ -308,13 +289,6 @@ void XMLBigDecimal::reScale(unsigned int newScale)
     return;
 }
 
-void XMLBigDecimal::dumpData() const
-{
-    cout<<"scale="<<"<"<<fScale<<">"<<endl;
-    fIntVal->dumpData();
-    cout<<endl;
-}
-
 //
 // Add the decimal point as necessary
 // The caller needs to de-allocate the memory allocated by this function
diff --git a/src/util/XMLBigDecimal.hpp b/src/util/XMLBigDecimal.hpp
index 37458276d..ee52bd8f9 100644
--- a/src/util/XMLBigDecimal.hpp
+++ b/src/util/XMLBigDecimal.hpp
@@ -110,8 +110,6 @@ public:
 
     unsigned int          getTotalDigit() const;
 
-    void                  dumpData() const;
-
 	/**
 	 *  Return string representation of the decimal value.
      *  A decimal point will be included as necessary, 
diff --git a/src/util/XMLBigInteger.cpp b/src/util/XMLBigInteger.cpp
index 95cbf70e9..fb560a1bf 100644
--- a/src/util/XMLBigInteger.cpp
+++ b/src/util/XMLBigInteger.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.4  2001/05/18 20:17:57  tng
+ * Schema: More exception messages in XMLBigDecimal/XMLBigInteger/DecimalDatatypeValidator.  By Pei Yong Zhang.
+ *
  * Revision 1.3  2001/05/18 13:23:01  tng
  * Schema: Exception messages in DatatypeValidator.  By Pei Yong Zhang.
  *
@@ -70,16 +73,12 @@
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
-#include <string.h>
-#include <iostream.h>
 #include <util/XMLBigInteger.hpp>
 #include <util/XMLString.hpp>
 #include <util/NumberFormatException.hpp>
-#include <util/RuntimeException.hpp>
 #include <util/PlatformUtils.hpp>
 #include <util/TransService.hpp>
 #include <util/XMLUniDefs.hpp>
-#include <util/XMLUni.hpp>
 #include <util/Janitor.hpp>
 
 /***
@@ -112,10 +111,8 @@ void XMLBigInteger::parseBigInteger(const XMLCh* const toConvert
                                   , int&   signValue)
 {
     // If no string, then its a failure
-    if ((!toConvert) ||
-        (!*toConvert))
-        ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLINT_Invalid);
+    if ((!toConvert) || (!*toConvert))
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
 
     //
     // Note: in Java's BigInteger, it seems any leading and/or trailing
@@ -129,8 +126,7 @@ void XMLBigInteger::parseBigInteger(const XMLCh* const toConvert
         startPtr++;
 
     if (!*startPtr)
-        ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLINT_Invalid);
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_WSString);
 
     // Start at the end and work back through any whitespace
     const XMLCh* endPtr = toConvert + XMLString::stringLen(toConvert);
@@ -174,8 +170,7 @@ void XMLBigInteger::parseBigInteger(const XMLCh* const toConvert
     {
         // If not valid decimal digit, then an error
         if ((*startPtr < chDigit_0) || (*startPtr > chDigit_9))
-            ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-            //ThrowXML(NumberFormatException, XMLExcepts::XMLINT_Invalid);
+            ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_Inv_chars);
 
         // copy over
         *retPtr = *startPtr;
@@ -195,29 +190,18 @@ void XMLBigInteger::parseBigInteger(const XMLCh* const toConvert
  */
 XMLBigInteger::XMLBigInteger(const XMLCh* const strValue)
 {
+    if (!strValue)
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_emptyString);
+
     XMLCh* ret_value = new XMLCh[XMLString::stringLen(strValue)+1];
     ArrayJanitor<XMLCh> janName(ret_value);
 
-    try
-    {
-        parseBigInteger(strValue, ret_value, fSign);
-    }
-    catch (NumberFormatException)
-    {
-        throw;
-        //ThrowXML(NumberFormatException, XMLExcepts::CM_UnaryOpHadBinType);
-        //ThrowXML(NumberFormatException, XMLExcepts::XMLBIGDECIMAL_Inv_format);
-    }
+    parseBigInteger(strValue, ret_value, fSign);
 
     if (fSign == 0)
         fMagnitude = XMLString::replicate(XMLUni::fgZeroLenString);
     else
-    {
-        unsigned int strLen = XMLString::stringLen(ret_value);
-        fMagnitude = new XMLCh[strLen+1];
-        XMLString::moveChars(fMagnitude, ret_value, strLen);
-        fMagnitude[strLen]=0;
-    }
+        fMagnitude = XMLString::replicate(ret_value);
 
 }
 
@@ -229,11 +213,7 @@ XMLBigInteger::~XMLBigInteger()
 XMLBigInteger::XMLBigInteger(const XMLBigInteger& toCopy)
 {
     setSign(toCopy.getSign());
-
-    int strLen = XMLString::stringLen(toCopy.fMagnitude);
-	fMagnitude = new XMLCh[strLen+1];
-    XMLString::moveChars(fMagnitude, toCopy.fMagnitude, strLen);
-    fMagnitude[strLen]=0;
+    fMagnitude = XMLString::replicate(toCopy.fMagnitude);
 }
 
 /**
@@ -243,6 +223,9 @@ XMLBigInteger::XMLBigInteger(const XMLBigInteger& toCopy)
 int  XMLBigInteger::compareValues(const XMLBigInteger* const lValue
                                 , const XMLBigInteger* const rValue)
 {
+    if ((!lValue) || (!rValue) )
+        ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_null_ptr);
+
     int lSign = lValue->getSign();
     int rSign = rValue->getSign();
 
@@ -331,17 +314,6 @@ void XMLBigInteger::divide(const unsigned int byteToShift)
     fMagnitude = tmp;
 }
 
-void XMLBigInteger::dumpData() const
-{
-    char *p;
-    p = XMLString::transcode(fMagnitude);
-    cout<<"sign="<<"<"<<fSign<<">"<<endl;
-    cout<<"fMagnitude="<<"<"<<p<<">"<<endl;
-    cout<<endl;
-    delete[] p;
-
-}
-
 //
 // The caller needs to de-allocate the memory allocated by this function
 // return buffer ALWAYS has a leading sign
diff --git a/src/util/XMLBigInteger.hpp b/src/util/XMLBigInteger.hpp
index 376329124..7d88cc58c 100644
--- a/src/util/XMLBigInteger.hpp
+++ b/src/util/XMLBigInteger.hpp
@@ -99,8 +99,6 @@ public:
 
     int         getTotalDigit() const;
 
-    void        dumpData() const;
-
 	/**
 	 *  Return a copy of the fMagnitue.
      *  A leading sign is ALWAYS in place and the caller of this method
diff --git a/src/validators/datatype/DecimalDatatypeValidator.cpp b/src/validators/datatype/DecimalDatatypeValidator.cpp
index c51bb0e7b..4964b6a74 100644
--- a/src/validators/datatype/DecimalDatatypeValidator.cpp
+++ b/src/validators/datatype/DecimalDatatypeValidator.cpp
@@ -56,6 +56,9 @@
 
 /*
  * $Log$
+ * Revision 1.6  2001/05/18 20:18:02  tng
+ * Schema: More exception messages in XMLBigDecimal/XMLBigInteger/DecimalDatatypeValidator.  By Pei Yong Zhang.
+ *
  * Revision 1.5  2001/05/18 13:36:45  tng
  * Schema: Catch RegularExpression exception and NumberFormatException
  *
@@ -619,14 +622,40 @@ void DecimalDatatypeValidator::init(DatatypeValidator*            const baseVali
                             XMLString::binToText(fTotalDigits, value1, BUF_LEN, 10);
                             XMLString::binToText(numBase->fTotalDigits, value2, BUF_LEN, 10);
                             ThrowXML2(InvalidDatatypeFacetException
-                                 , XMLExcepts::FACET_TotDigit_FractDigit
-                                 , value2
-                                 , value1);
+                                 , XMLExcepts::FACET_totalDigit_base_totalDigit
+                                 , value1
+                                 , value2);
                         }
                     }
 
-                    // check question error: fractionDigits > base.fractionDigits ???
-                    // check question error: fractionDigits > base.totalDigits ???
+                   if (( getFacetsDefined() & DatatypeValidator::FACET_SCALE) != 0)
+                   {
+                        // check question error: fractionDigits > base.fractionDigits ???
+                        if ( (( numBase->getFacetsDefined() & DatatypeValidator::FACET_SCALE) != 0) &&
+                             ( fFractionDigits > numBase->fFractionDigits ))
+                        {
+                            XMLString::binToText(fFractionDigits, value1, BUF_LEN, 10);
+                            XMLString::binToText(numBase->fFractionDigits, value2, BUF_LEN, 10);
+                            ThrowXML2(InvalidDatatypeFacetException
+                                 , XMLExcepts::FACET_fractDigit_base_fractDigit
+                                 , value1
+                                 , value2);
+                        }
+
+                        // check question error: fractionDigits > base.totalDigits ???
+                        if ( (( numBase->getFacetsDefined() & DatatypeValidator::FACET_PRECISSION) != 0) &&
+                             ( fFractionDigits > numBase->fTotalDigits ))
+                        {
+                            XMLString::binToText(fFractionDigits, value1, BUF_LEN, 10);
+                            XMLString::binToText(numBase->fTotalDigits, value2, BUF_LEN, 10);
+                            ThrowXML2(InvalidDatatypeFacetException
+                                 , XMLExcepts::FACET_fractDigit_base_totalDigit
+                                 , value1
+                                 , value2);
+                        }
+                   }
+
+
                     // check question error: totalDigits conflicts with bounds ???
 
                     // check 4.3.5.c0 must: enumeration values from the value space of base
-- 
GitLab