jaxp/src/com/sun/org/apache/xerces/internal/impl/dv/xs/DecimalDV.java

Print this page




   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
  22 
  23 import java.math.BigDecimal;
  24 import java.math.BigInteger;
  25 
  26 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  27 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  28 import com.sun.org.apache.xerces.internal.xs.datatypes.XSDecimal;

  29 
  30 /**
  31  * Represent the schema type "decimal"
  32  *
  33  * @xerces.internal
  34  *
  35  * @author Neeraj Bajaj, Sun Microsystems, inc.
  36  * @author Sandy Gao, IBM
  37  *
  38  */
  39 public class DecimalDV extends TypeValidator {
  40 

  41     public final short getAllowedFacets(){
  42         return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE  | XSSimpleTypeDecl.FACET_MINEXCLUSIVE | XSSimpleTypeDecl.FACET_TOTALDIGITS | XSSimpleTypeDecl.FACET_FRACTIONDIGITS);
  43     }
  44 

  45     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  46         try {
  47             return new XDecimal(content);
  48         } catch (NumberFormatException nfe) {
  49             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "decimal"});
  50         }
  51     }
  52 

  53     public final int compare(Object value1, Object value2){
  54         return ((XDecimal)value1).compareTo((XDecimal)value2);
  55     }
  56 

  57     public final int getTotalDigits(Object value){
  58         return ((XDecimal)value).totalDigits;
  59     }
  60 

  61     public final int getFractionDigits(Object value){
  62         return ((XDecimal)value).fracDigits;
  63     }
  64 
  65     // Avoid using the heavy-weight java.math.BigDecimal
  66     static class XDecimal implements XSDecimal {
  67         // sign: 0 for vlaue 0; 1 for positive values; -1 for negative values
  68         int sign = 1;
  69         // total digits. >= 1
  70         int totalDigits = 0;
  71         // integer digits when sign != 0
  72         int intDigits = 0;
  73         // fraction digits when sign != 0
  74         int fracDigits = 0;
  75         // the string representing the integer part
  76         String ivalue = "";
  77         // the string representing the fraction part
  78         String fvalue = "";
  79         // whether the canonical form contains decimal point
  80         boolean integer = false;
  81 
  82         XDecimal(String content) throws NumberFormatException {
  83             initD(content);
  84         }
  85         XDecimal(String content, boolean integer) throws NumberFormatException {
  86             if (integer)


 199                 throw new NumberFormatException();
 200 
 201             // no integer part, error.
 202             if (intStart == intEnd)
 203                 throw new NumberFormatException();
 204 
 205             intDigits = intEnd - actualIntStart;
 206             fracDigits = 0;
 207             totalDigits = intDigits;
 208 
 209             if (intDigits > 0) {
 210                 ivalue = content.substring(actualIntStart, intEnd);
 211             }
 212             else {
 213                 // "00", treat it as "0"
 214                 sign = 0;
 215             }
 216 
 217             integer = true;
 218         }


 219         public boolean equals(Object val) {
 220             if (val == this)
 221                 return true;
 222 
 223             if (!(val instanceof XDecimal))
 224                 return false;
 225             XDecimal oval = (XDecimal)val;
 226 
 227             if (sign != oval.sign)
 228                return false;
 229             if (sign == 0)
 230                 return true;
 231 
 232             return intDigits == oval.intDigits && fracDigits == oval.fracDigits &&
 233                    ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue);
 234         }













 235         public int compareTo(XDecimal val) {
 236             if (sign != val.sign)
 237                 return sign > val.sign ? 1 : -1;
 238             if (sign == 0)
 239                 return 0;
 240             return sign * intComp(val);
 241         }
 242         private int intComp(XDecimal val) {
 243             if (intDigits != val.intDigits)
 244                 return intDigits > val.intDigits ? 1 : -1;
 245             int ret = ivalue.compareTo(val.ivalue);
 246             if (ret != 0)
 247                 return ret > 0 ? 1 : -1;;
 248             ret = fvalue.compareTo(val.fvalue);
 249             return ret == 0 ? 0 : (ret > 0 ? 1 : -1);
 250         }

 251         private String canonical;

 252         public synchronized String toString() {
 253             if (canonical == null) {
 254                 makeCanonical();
 255             }
 256             return canonical;
 257         }
 258 
 259         private void makeCanonical() {
 260             if (sign == 0) {
 261                 if (integer)
 262                     canonical = "0";
 263                 else
 264                     canonical = "0.0";
 265                 return;
 266             }
 267             if (integer && sign > 0) {
 268                 canonical = ivalue;
 269                 return;
 270             }
 271             // for -0.1, total digits is 1, so we need 3 extra spots
 272             StringBuffer buffer = new StringBuffer(totalDigits+3);
 273             if (sign == -1)
 274                 buffer.append('-');
 275             if (intDigits != 0)
 276                 buffer.append(ivalue);
 277             else
 278                 buffer.append('0');
 279             if (!integer) {
 280                 buffer.append('.');
 281                 if (fracDigits != 0) {
 282                     buffer.append(fvalue);
 283                 }
 284                 else {
 285                     buffer.append('0');
 286                 }
 287             }
 288             canonical = buffer.toString();
 289         }
 290 

 291         public BigDecimal getBigDecimal() {
 292             if (sign == 0) {
 293                 return new BigDecimal(BigInteger.ZERO);
 294             }
 295             return new BigDecimal(toString());
 296         }
 297 

 298         public BigInteger getBigInteger() throws NumberFormatException {
 299             if (fracDigits != 0) {
 300                 throw new NumberFormatException();
 301             }
 302             if (sign == 0) {
 303                 return BigInteger.ZERO;
 304             }
 305             if (sign == 1) {
 306                 return new BigInteger(ivalue);
 307             }
 308             return new BigInteger("-" + ivalue);
 309         }
 310 

 311         public long getLong() throws NumberFormatException {
 312             if (fracDigits != 0) {
 313                 throw new NumberFormatException();
 314             }
 315             if (sign == 0) {
 316                 return 0L;
 317             }
 318             if (sign == 1) {
 319                 return Long.parseLong(ivalue);
 320             }
 321             return Long.parseLong("-" + ivalue);
 322         }
 323 

 324         public int getInt() throws NumberFormatException {
 325             if (fracDigits != 0) {
 326                 throw new NumberFormatException();
 327             }
 328             if (sign == 0) {
 329                 return 0;
 330             }
 331             if (sign == 1) {
 332                 return Integer.parseInt(ivalue);
 333             }
 334             return Integer.parseInt("-" + ivalue);
 335         }
 336 

 337         public short getShort() throws NumberFormatException {
 338             if (fracDigits != 0) {
 339                 throw new NumberFormatException();
 340             }
 341             if (sign == 0) {
 342                 return 0;
 343             }
 344             if (sign == 1) {
 345                 return Short.parseShort(ivalue);
 346             }
 347             return Short.parseShort("-" + ivalue);
 348         }
 349 

 350         public byte getByte() throws NumberFormatException {
 351             if (fracDigits != 0) {
 352                 throw new NumberFormatException();
 353             }
 354             if (sign == 0) {
 355                 return 0;
 356             }
 357             if (sign == 1) {
 358                 return Byte.parseByte(ivalue);
 359             }
 360             return Byte.parseByte("-" + ivalue);
 361         }
 362     }
 363 } // class DecimalDV


   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv.xs;
  22 
  23 import java.math.BigDecimal;
  24 import java.math.BigInteger;
  25 
  26 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  27 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  28 import com.sun.org.apache.xerces.internal.xs.datatypes.XSDecimal;
  29 import java.util.Objects;
  30 
  31 /**
  32  * Represent the schema type "decimal"
  33  *
  34  * @xerces.internal
  35  *
  36  * @author Neeraj Bajaj, Sun Microsystems, inc.
  37  * @author Sandy Gao, IBM
  38  *
  39  */
  40 public class DecimalDV extends TypeValidator {
  41 
  42     @Override
  43     public final short getAllowedFacets(){
  44         return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE  | XSSimpleTypeDecl.FACET_MINEXCLUSIVE | XSSimpleTypeDecl.FACET_TOTALDIGITS | XSSimpleTypeDecl.FACET_FRACTIONDIGITS);
  45     }
  46 
  47     @Override
  48     public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException {
  49         try {
  50             return new XDecimal(content);
  51         } catch (NumberFormatException nfe) {
  52             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "decimal"});
  53         }
  54     }
  55 
  56     @Override
  57     public final int compare(Object value1, Object value2){
  58         return ((XDecimal)value1).compareTo((XDecimal)value2);
  59     }
  60 
  61     @Override
  62     public final int getTotalDigits(Object value){
  63         return ((XDecimal)value).totalDigits;
  64     }
  65 
  66     @Override
  67     public final int getFractionDigits(Object value){
  68         return ((XDecimal)value).fracDigits;
  69     }
  70 
  71     // Avoid using the heavy-weight java.math.BigDecimal
  72     static final class XDecimal implements XSDecimal {
  73         // sign: 0 for vlaue 0; 1 for positive values; -1 for negative values
  74         int sign = 1;
  75         // total digits. >= 1
  76         int totalDigits = 0;
  77         // integer digits when sign != 0
  78         int intDigits = 0;
  79         // fraction digits when sign != 0
  80         int fracDigits = 0;
  81         // the string representing the integer part
  82         String ivalue = "";
  83         // the string representing the fraction part
  84         String fvalue = "";
  85         // whether the canonical form contains decimal point
  86         boolean integer = false;
  87 
  88         XDecimal(String content) throws NumberFormatException {
  89             initD(content);
  90         }
  91         XDecimal(String content, boolean integer) throws NumberFormatException {
  92             if (integer)


 205                 throw new NumberFormatException();
 206 
 207             // no integer part, error.
 208             if (intStart == intEnd)
 209                 throw new NumberFormatException();
 210 
 211             intDigits = intEnd - actualIntStart;
 212             fracDigits = 0;
 213             totalDigits = intDigits;
 214 
 215             if (intDigits > 0) {
 216                 ivalue = content.substring(actualIntStart, intEnd);
 217             }
 218             else {
 219                 // "00", treat it as "0"
 220                 sign = 0;
 221             }
 222 
 223             integer = true;
 224         }
 225 
 226         @Override
 227         public boolean equals(Object val) {
 228             if (val == this)
 229                 return true;
 230 
 231             if (!(val instanceof XDecimal))
 232                 return false;
 233             XDecimal oval = (XDecimal)val;
 234 
 235             if (sign != oval.sign)
 236                return false;
 237             if (sign == 0)
 238                 return true;
 239 
 240             return intDigits == oval.intDigits && fracDigits == oval.fracDigits &&
 241                    ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue);
 242         }
 243 
 244         @Override
 245         public int hashCode() {
 246             int hash = 7;
 247             hash = 17 * hash + this.sign;
 248             if (this.sign == 0) return hash;
 249             hash = 17 * hash + this.intDigits;
 250             hash = 17 * hash + this.fracDigits;
 251             hash = 17 * hash + Objects.hashCode(this.ivalue);
 252             hash = 17 * hash + Objects.hashCode(this.fvalue);
 253             return hash;
 254         }
 255 
 256         public int compareTo(XDecimal val) {
 257             if (sign != val.sign)
 258                 return sign > val.sign ? 1 : -1;
 259             if (sign == 0)
 260                 return 0;
 261             return sign * intComp(val);
 262         }
 263         private int intComp(XDecimal val) {
 264             if (intDigits != val.intDigits)
 265                 return intDigits > val.intDigits ? 1 : -1;
 266             int ret = ivalue.compareTo(val.ivalue);
 267             if (ret != 0)
 268                 return ret > 0 ? 1 : -1;;
 269             ret = fvalue.compareTo(val.fvalue);
 270             return ret == 0 ? 0 : (ret > 0 ? 1 : -1);
 271         }
 272 
 273         private String canonical;
 274         @Override
 275         public synchronized String toString() {
 276             if (canonical == null) {
 277                 makeCanonical();
 278             }
 279             return canonical;
 280         }
 281 
 282         private void makeCanonical() {
 283             if (sign == 0) {
 284                 if (integer)
 285                     canonical = "0";
 286                 else
 287                     canonical = "0.0";
 288                 return;
 289             }
 290             if (integer && sign > 0) {
 291                 canonical = ivalue;
 292                 return;
 293             }
 294             // for -0.1, total digits is 1, so we need 3 extra spots
 295             final StringBuilder buffer = new StringBuilder(totalDigits+3);
 296             if (sign == -1)
 297                 buffer.append('-');
 298             if (intDigits != 0)
 299                 buffer.append(ivalue);
 300             else
 301                 buffer.append('0');
 302             if (!integer) {
 303                 buffer.append('.');
 304                 if (fracDigits != 0) {
 305                     buffer.append(fvalue);
 306                 }
 307                 else {
 308                     buffer.append('0');
 309                 }
 310             }
 311             canonical = buffer.toString();
 312         }
 313 
 314         @Override
 315         public BigDecimal getBigDecimal() {
 316             if (sign == 0) {
 317                 return new BigDecimal(BigInteger.ZERO);
 318             }
 319             return new BigDecimal(toString());
 320         }
 321 
 322         @Override
 323         public BigInteger getBigInteger() throws NumberFormatException {
 324             if (fracDigits != 0) {
 325                 throw new NumberFormatException();
 326             }
 327             if (sign == 0) {
 328                 return BigInteger.ZERO;
 329             }
 330             if (sign == 1) {
 331                 return new BigInteger(ivalue);
 332             }
 333             return new BigInteger("-" + ivalue);
 334         }
 335 
 336         @Override
 337         public long getLong() throws NumberFormatException {
 338             if (fracDigits != 0) {
 339                 throw new NumberFormatException();
 340             }
 341             if (sign == 0) {
 342                 return 0L;
 343             }
 344             if (sign == 1) {
 345                 return Long.parseLong(ivalue);
 346             }
 347             return Long.parseLong("-" + ivalue);
 348         }
 349 
 350         @Override
 351         public int getInt() throws NumberFormatException {
 352             if (fracDigits != 0) {
 353                 throw new NumberFormatException();
 354             }
 355             if (sign == 0) {
 356                 return 0;
 357             }
 358             if (sign == 1) {
 359                 return Integer.parseInt(ivalue);
 360             }
 361             return Integer.parseInt("-" + ivalue);
 362         }
 363 
 364         @Override
 365         public short getShort() throws NumberFormatException {
 366             if (fracDigits != 0) {
 367                 throw new NumberFormatException();
 368             }
 369             if (sign == 0) {
 370                 return 0;
 371             }
 372             if (sign == 1) {
 373                 return Short.parseShort(ivalue);
 374             }
 375             return Short.parseShort("-" + ivalue);
 376         }
 377 
 378         @Override
 379         public byte getByte() throws NumberFormatException {
 380             if (fracDigits != 0) {
 381                 throw new NumberFormatException();
 382             }
 383             if (sign == 0) {
 384                 return 0;
 385             }
 386             if (sign == 1) {
 387                 return Byte.parseByte(ivalue);
 388             }
 389             return Byte.parseByte("-" + ivalue);
 390         }
 391     }
 392 } // class DecimalDV