286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/Apackage com.sun.org.apache.xerces.internal.impl.dv.xs;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
286N/Aimport com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
286N/A
286N/A/**
286N/A * Validator for <precisionDecimal> datatype (W3C Schema 1.1)
286N/A *
286N/A * @xerces.experimental
286N/A *
286N/A * @author Ankit Pasricha, IBM
286N/A *
286N/A */
286N/Aclass PrecisionDecimalDV extends TypeValidator {
286N/A
570N/A static final class XPrecisionDecimal {
286N/A
286N/A // sign: 0 for absent; 1 for positive values; -1 for negative values (except in case of INF, -INF)
286N/A int sign = 1;
286N/A // total digits. >= 1
286N/A int totalDigits = 0;
286N/A // integer digits when sign != 0
286N/A int intDigits = 0;
286N/A // fraction digits when sign != 0
286N/A int fracDigits = 0;
286N/A //precision
286N/A //int precision = 0;
286N/A // the string representing the integer part
286N/A String ivalue = "";
286N/A // the string representing the fraction part
286N/A String fvalue = "";
286N/A
286N/A int pvalue = 0;
286N/A
286N/A
286N/A XPrecisionDecimal(String content) throws NumberFormatException {
286N/A if(content.equals("NaN")) {
286N/A ivalue = content;
286N/A sign = 0;
286N/A }
286N/A if(content.equals("+INF") || content.equals("INF") || content.equals("-INF")) {
286N/A ivalue = content.charAt(0) == '+' ? content.substring(1) : content;
286N/A return;
286N/A }
286N/A initD(content);
286N/A }
286N/A
286N/A void initD(String content) throws NumberFormatException {
286N/A int len = content.length();
286N/A if (len == 0)
286N/A throw new NumberFormatException();
286N/A
286N/A // these 4 variables are used to indicate where the integre/fraction
286N/A // parts start/end.
286N/A int intStart = 0, intEnd = 0, fracStart = 0, fracEnd = 0;
286N/A
286N/A // Deal with leading sign symbol if present
286N/A if (content.charAt(0) == '+') {
286N/A // skip '+', so intStart should be 1
286N/A intStart = 1;
286N/A }
286N/A else if (content.charAt(0) == '-') {
286N/A intStart = 1;
286N/A sign = -1;
286N/A }
286N/A
286N/A // skip leading zeroes in integer part
286N/A int actualIntStart = intStart;
286N/A while (actualIntStart < len && content.charAt(actualIntStart) == '0') {
286N/A actualIntStart++;
286N/A }
286N/A
286N/A // Find the ending position of the integer part
286N/A for (intEnd = actualIntStart; intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); intEnd++);
286N/A
286N/A // Not reached the end yet
286N/A if (intEnd < len) {
286N/A // the remaining part is not ".DDD" or "EDDD" or "eDDD", error
286N/A if (content.charAt(intEnd) != '.' && content.charAt(intEnd) != 'E' && content.charAt(intEnd) != 'e')
286N/A throw new NumberFormatException();
286N/A
286N/A if(content.charAt(intEnd) == '.') {
286N/A // fraction part starts after '.', and ends at the end of the input
286N/A fracStart = intEnd + 1;
286N/A
286N/A // find location of E or e (if present)
286N/A // Find the ending position of the fracion part
286N/A for (fracEnd = fracStart;
286N/A fracEnd < len && TypeValidator.isDigit(content.charAt(fracEnd));
286N/A fracEnd++);
286N/A }
286N/A else {
286N/A pvalue = Integer.parseInt(content.substring(intEnd + 1, len));
286N/A }
286N/A }
286N/A
286N/A // no integer part, no fraction part, error.
286N/A if (intStart == intEnd && fracStart == fracEnd)
286N/A throw new NumberFormatException();
286N/A
286N/A // ignore trailing zeroes in fraction part
286N/A /*while (fracEnd > fracStart && content.charAt(fracEnd-1) == '0') {
286N/A fracEnd--;
286N/A }*/
286N/A
286N/A // check whether there is non-digit characters in the fraction part
286N/A for (int fracPos = fracStart; fracPos < fracEnd; fracPos++) {
286N/A if (!TypeValidator.isDigit(content.charAt(fracPos)))
286N/A throw new NumberFormatException();
286N/A }
286N/A
286N/A intDigits = intEnd - actualIntStart;
286N/A fracDigits = fracEnd - fracStart;
286N/A
286N/A if (intDigits > 0) {
286N/A ivalue = content.substring(actualIntStart, intEnd);
286N/A }
286N/A
286N/A if (fracDigits > 0) {
286N/A fvalue = content.substring(fracStart, fracEnd);
286N/A if(fracEnd < len) {
286N/A pvalue = Integer.parseInt(content.substring(fracEnd + 1, len));
286N/A }
286N/A }
286N/A totalDigits = intDigits + fracDigits;
286N/A }
286N/A
570N/A // Construct a canonical String representation of this number
570N/A // for the purpose of deriving a hashCode value compliant with
570N/A // equals.
570N/A // The toString representation will be:
570N/A // NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero,
570N/A // and [1-9].[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers.
570N/A private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) {
570N/A if ("NaN".equals(ivalue)) {
570N/A return "NaN";
570N/A }
570N/A if ("INF".equals(ivalue)) {
570N/A return sign < 0 ? "-INF" : "INF";
570N/A }
570N/A final StringBuilder builder = new StringBuilder();
570N/A final int ilen = ivalue.length();
570N/A final int flen0 = fvalue.length();
570N/A int lastNonZero;
570N/A for (lastNonZero = flen0; lastNonZero > 0 ; lastNonZero--) {
570N/A if (fvalue.charAt(lastNonZero -1 ) != '0') break;
570N/A }
570N/A final int flen = lastNonZero;
570N/A int iStart;
570N/A int exponent = pvalue;
570N/A for (iStart = 0; iStart < ilen; iStart++) {
570N/A if (ivalue.charAt(iStart) != '0') break;
570N/A }
570N/A int fStart = 0;
570N/A if (iStart < ivalue.length()) {
570N/A builder.append(sign == -1 ? "-" : "");
570N/A builder.append(ivalue.charAt(iStart));
570N/A iStart++;
570N/A } else {
570N/A if (flen > 0) {
570N/A for (fStart = 0; fStart < flen; fStart++) {
570N/A if (fvalue.charAt(fStart) != '0') break;
570N/A }
570N/A if (fStart < flen) {
570N/A builder.append(sign == -1 ? "-" : "");
570N/A builder.append(fvalue.charAt(fStart));
570N/A exponent -= ++fStart;
570N/A } else {
570N/A return "0";
570N/A }
570N/A } else {
570N/A return "0";
570N/A }
570N/A }
286N/A
570N/A if (iStart < ilen || fStart < flen) {
570N/A builder.append('.');
570N/A }
570N/A while (iStart < ilen) {
570N/A builder.append(ivalue.charAt(iStart++));
570N/A exponent++;
570N/A }
570N/A while (fStart < flen) {
570N/A builder.append(fvalue.charAt(fStart++));
570N/A }
570N/A if (exponent != 0) {
570N/A builder.append("E").append(exponent);
570N/A }
570N/A return builder.toString();
570N/A }
570N/A
570N/A @Override
286N/A public boolean equals(Object val) {
286N/A if (val == this)
286N/A return true;
286N/A
286N/A if (!(val instanceof XPrecisionDecimal))
286N/A return false;
286N/A XPrecisionDecimal oval = (XPrecisionDecimal)val;
286N/A
286N/A return this.compareTo(oval) == EQUAL;
286N/A }
286N/A
570N/A @Override
570N/A public int hashCode() {
570N/A // There's nothing else we can use easily, because equals could
570N/A // return true for widely different representation of the
570N/A // same number - and we don't have any canonical representation.
570N/A // The problem here is that we must ensure that if two numbers
570N/A // are equals then their hash code must also be equals.
570N/A // hashCode for 1.01E1 should be the same as hashCode for 0.101E2
570N/A // So we call cannonicalToStringForHashCode - which implements an
570N/A // algorithm that invents a normalized string representation
570N/A // for this number, and we return a hash for that.
570N/A return canonicalToStringForHashCode(ivalue, fvalue, sign, pvalue).hashCode();
570N/A }
570N/A
286N/A /**
286N/A * @return
286N/A */
286N/A private int compareFractionalPart(XPrecisionDecimal oval) {
286N/A if(fvalue.equals(oval.fvalue))
286N/A return EQUAL;
286N/A
286N/A StringBuffer temp1 = new StringBuffer(fvalue);
286N/A StringBuffer temp2 = new StringBuffer(oval.fvalue);
286N/A
286N/A truncateTrailingZeros(temp1, temp2);
286N/A return temp1.toString().compareTo(temp2.toString());
286N/A }
286N/A
286N/A private void truncateTrailingZeros(StringBuffer fValue, StringBuffer otherFValue) {
286N/A for(int i = fValue.length() - 1;i >= 0; i--)
286N/A if(fValue.charAt(i) == '0')
286N/A fValue.deleteCharAt(i);
286N/A else
286N/A break;
286N/A
286N/A for(int i = otherFValue.length() - 1;i >= 0; i--)
286N/A if(otherFValue.charAt(i) == '0')
286N/A otherFValue.deleteCharAt(i);
286N/A else
286N/A break;
286N/A }
286N/A
286N/A public int compareTo(XPrecisionDecimal val) {
286N/A
286N/A // seen NaN
286N/A if(sign == 0)
286N/A return INDETERMINATE;
286N/A
286N/A //INF is greater than everything and equal to itself
286N/A if(ivalue.equals("INF") || val.ivalue.equals("INF")) {
286N/A if(ivalue.equals(val.ivalue))
286N/A return EQUAL;
286N/A else if(ivalue.equals("INF"))
286N/A return GREATER_THAN;
286N/A return LESS_THAN;
286N/A }
286N/A
286N/A //-INF is smaller than everything and equal itself
286N/A if(ivalue.equals("-INF") || val.ivalue.equals("-INF")) {
286N/A if(ivalue.equals(val.ivalue))
286N/A return EQUAL;
286N/A else if(ivalue.equals("-INF"))
286N/A return LESS_THAN;
286N/A return GREATER_THAN;
286N/A }
286N/A
286N/A if (sign != val.sign)
286N/A return sign > val.sign ? GREATER_THAN : LESS_THAN;
286N/A
286N/A return sign * compare(val);
286N/A }
286N/A
286N/A // To enable comparison - the exponent part of the decimal will be limited
286N/A // to the max value of int.
286N/A private int compare(XPrecisionDecimal val) {
286N/A
286N/A if(pvalue != 0 || val.pvalue != 0) {
286N/A if(pvalue == val.pvalue)
286N/A return intComp(val);
286N/A else {
286N/A
286N/A if(intDigits + pvalue != val.intDigits + val.pvalue)
286N/A return intDigits + pvalue > val.intDigits + val.pvalue ? GREATER_THAN : LESS_THAN;
286N/A
286N/A //otherwise the 2 combined values are the same
286N/A if(pvalue > val.pvalue) {
286N/A int expDiff = pvalue - val.pvalue;
286N/A StringBuffer buffer = new StringBuffer(ivalue);
286N/A StringBuffer fbuffer = new StringBuffer(fvalue);
286N/A for(int i = 0;i < expDiff; i++) {
286N/A if(i < fracDigits) {
286N/A buffer.append(fvalue.charAt(i));
286N/A fbuffer.deleteCharAt(i);
286N/A }
286N/A else
286N/A buffer.append('0');
286N/A }
286N/A return compareDecimal(buffer.toString(), val.ivalue, fbuffer.toString(), val.fvalue);
286N/A }
286N/A else {
286N/A int expDiff = val.pvalue - pvalue;
286N/A StringBuffer buffer = new StringBuffer(val.ivalue);
286N/A StringBuffer fbuffer = new StringBuffer(val.fvalue);
286N/A for(int i = 0;i < expDiff; i++) {
286N/A if(i < val.fracDigits) {
286N/A buffer.append(val.fvalue.charAt(i));
286N/A fbuffer.deleteCharAt(i);
286N/A }
286N/A else
286N/A buffer.append('0');
286N/A }
286N/A return compareDecimal(ivalue, buffer.toString(), fvalue, fbuffer.toString());
286N/A }
286N/A }
286N/A }
286N/A else {
286N/A return intComp(val);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * @param val
286N/A * @return
286N/A */
286N/A private int intComp(XPrecisionDecimal val) {
286N/A if (intDigits != val.intDigits)
286N/A return intDigits > val.intDigits ? GREATER_THAN : LESS_THAN;
286N/A
286N/A return compareDecimal(ivalue, val.ivalue, fvalue, val.fvalue);
286N/A }
286N/A
286N/A /**
286N/A * @param val
286N/A * @return
286N/A */
286N/A private int compareDecimal(String iValue, String fValue, String otherIValue, String otherFValue) {
286N/A int ret = iValue.compareTo(otherIValue);
286N/A if (ret != 0)
286N/A return ret > 0 ? GREATER_THAN : LESS_THAN;
286N/A
286N/A if(fValue.equals(otherFValue))
286N/A return EQUAL;
286N/A
286N/A StringBuffer temp1=new StringBuffer(fValue);
286N/A StringBuffer temp2=new StringBuffer(otherFValue);
286N/A
286N/A truncateTrailingZeros(temp1, temp2);
286N/A ret = temp1.toString().compareTo(temp2.toString());
286N/A return ret == 0 ? EQUAL : (ret > 0 ? GREATER_THAN : LESS_THAN);
286N/A }
286N/A
286N/A private String canonical;
286N/A
570N/A @Override
286N/A public synchronized String toString() {
286N/A if (canonical == null) {
286N/A makeCanonical();
286N/A }
286N/A return canonical;
286N/A }
286N/A
286N/A private void makeCanonical() {
286N/A // REVISIT: to be determined by working group
286N/A canonical = "TBD by Working Group";
286N/A }
286N/A
286N/A /**
286N/A * @param decimal
286N/A * @return
286N/A */
286N/A public boolean isIdentical(XPrecisionDecimal decimal) {
286N/A if(ivalue.equals(decimal.ivalue) && (ivalue.equals("INF") || ivalue.equals("-INF") || ivalue.equals("NaN")))
286N/A return true;
286N/A
286N/A if(sign == decimal.sign && intDigits == decimal.intDigits && fracDigits == decimal.fracDigits && pvalue == decimal.pvalue
286N/A && ivalue.equals(decimal.ivalue) && fvalue.equals(decimal.fvalue))
286N/A return true;
286N/A return false;
286N/A }
286N/A
286N/A }
286N/A /* (non-Javadoc)
286N/A * @see com.sun.org.apache.xerces.internal.impl.dv.xs.TypeValidator#getAllowedFacets()
286N/A */
570N/A @Override
286N/A public short getAllowedFacets() {
286N/A 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);
286N/A }
286N/A
286N/A /* (non-Javadoc)
286N/A * @see com.sun.org.apache.xerces.internal.impl.dv.xs.TypeValidator#getActualValue(java.lang.String, com.sun.org.apache.xerces.internal.impl.dv.ValidationContext)
286N/A */
570N/A @Override
286N/A public Object getActualValue(String content, ValidationContext context)
286N/A throws InvalidDatatypeValueException {
286N/A try {
286N/A return new XPrecisionDecimal(content);
286N/A } catch (NumberFormatException nfe) {
286N/A throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "precisionDecimal"});
286N/A }
286N/A }
286N/A
570N/A @Override
286N/A public int compare(Object value1, Object value2) {
286N/A return ((XPrecisionDecimal)value1).compareTo((XPrecisionDecimal)value2);
286N/A }
286N/A
570N/A @Override
286N/A public int getFractionDigits(Object value) {
286N/A return ((XPrecisionDecimal)value).fracDigits;
286N/A }
286N/A
570N/A @Override
286N/A public int getTotalDigits(Object value) {
286N/A return ((XPrecisionDecimal)value).totalDigits;
286N/A }
286N/A
570N/A @Override
286N/A public boolean isIdentical(Object value1, Object value2) {
286N/A if(!(value2 instanceof XPrecisionDecimal) || !(value1 instanceof XPrecisionDecimal))
286N/A return false;
286N/A return ((XPrecisionDecimal)value1).isIdentical((XPrecisionDecimal)value2);
286N/A }
286N/A}