1 /*
2  * reserved comment block
3  * DO NOT REMOVE OR ALTER!
4  */
5 /*
6  * Copyright 2004 The Apache Software Foundation.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
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 package com.sun.org.apache.xerces.internal.impl.dv.xs;
21 
22 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
23 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
24 
25 /**
26  * Validator for <precisionDecimal> datatype (W3C Schema 1.1)
27  *
28  * @xerces.experimental
29  *
30  * @author Ankit Pasricha, IBM
31  *
32  */
33 class PrecisionDecimalDV extends TypeValidator {
34 
35     static final class XPrecisionDecimal {
36 
37         // sign: 0 for absent; 1 for positive values; -1 for negative values (except in case of INF, -INF)
38         int sign = 1;
39         // total digits. >= 1
40         int totalDigits = 0;
41         // integer digits when sign != 0
42         int intDigits = 0;
43         // fraction digits when sign != 0
44         int fracDigits = 0;
45         //precision
46         //int precision = 0;
47         // the string representing the integer part
48         String ivalue = "";
49         // the string representing the fraction part
50         String fvalue = "";
51 
52         int pvalue = 0;
53 
54 
XPrecisionDecimal(String content)55         XPrecisionDecimal(String content) throws NumberFormatException {
56             if(content.equals("NaN")) {
57                 ivalue = content;
58                 sign = 0;
59             }
60             if(content.equals("+INF") || content.equals("INF") || content.equals("-INF")) {
61                 ivalue = content.charAt(0) == '+' ? content.substring(1) : content;
62                 return;
63             }
64             initD(content);
65         }
66 
initD(String content)67         void initD(String content) throws NumberFormatException {
68             int len = content.length();
69             if (len == 0)
70                 throw new NumberFormatException();
71 
72             // these 4 variables are used to indicate where the integre/fraction
73             // parts start/end.
74             int intStart = 0, intEnd = 0, fracStart = 0, fracEnd = 0;
75 
76             // Deal with leading sign symbol if present
77             if (content.charAt(0) == '+') {
78                 // skip '+', so intStart should be 1
79                 intStart = 1;
80             }
81             else if (content.charAt(0) == '-') {
82                 intStart = 1;
83                 sign = -1;
84             }
85 
86             // skip leading zeroes in integer part
87             int actualIntStart = intStart;
88             while (actualIntStart < len && content.charAt(actualIntStart) == '0') {
89                 actualIntStart++;
90             }
91 
92             // Find the ending position of the integer part
93             for (intEnd = actualIntStart; intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); intEnd++);
94 
95             // Not reached the end yet
96             if (intEnd < len) {
97                 // the remaining part is not ".DDD" or "EDDD" or "eDDD", error
98                 if (content.charAt(intEnd) != '.' && content.charAt(intEnd) != 'E' && content.charAt(intEnd) != 'e')
99                     throw new NumberFormatException();
100 
101                 if(content.charAt(intEnd) == '.') {
102                     // fraction part starts after '.', and ends at the end of the input
103                     fracStart = intEnd + 1;
104 
105                     // find location of E or e (if present)
106                     // Find the ending position of the fracion part
107                     for (fracEnd = fracStart;
108                     fracEnd < len && TypeValidator.isDigit(content.charAt(fracEnd));
109                     fracEnd++);
110                 }
111                 else {
112                     pvalue = Integer.parseInt(content.substring(intEnd + 1, len));
113                 }
114             }
115 
116             // no integer part, no fraction part, error.
117             if (intStart == intEnd && fracStart == fracEnd)
118                 throw new NumberFormatException();
119 
120             // ignore trailing zeroes in fraction part
121             /*while (fracEnd > fracStart && content.charAt(fracEnd-1) == '0') {
122              fracEnd--;
123              }*/
124 
125             // check whether there is non-digit characters in the fraction part
126             for (int fracPos = fracStart; fracPos < fracEnd; fracPos++) {
127                 if (!TypeValidator.isDigit(content.charAt(fracPos)))
128                     throw new NumberFormatException();
129             }
130 
131             intDigits = intEnd - actualIntStart;
132             fracDigits = fracEnd - fracStart;
133 
134             if (intDigits > 0) {
135                 ivalue = content.substring(actualIntStart, intEnd);
136             }
137 
138             if (fracDigits > 0) {
139                 fvalue = content.substring(fracStart, fracEnd);
140                 if(fracEnd < len) {
141                     pvalue = Integer.parseInt(content.substring(fracEnd + 1, len));
142                 }
143             }
144             totalDigits = intDigits + fracDigits;
145         }
146 
147         // Construct a canonical String representation of this number
148         // for the purpose of deriving a hashCode value compliant with
149         // equals.
150         // The toString representation will be:
151         // NaN for NaN, INF for +infinity, -INF for -infinity, 0 for zero,
152         // and [1-9].[0-9]*[1-9]?(E[1-9][0-9]*)? for other numbers.
canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue)153         private static String canonicalToStringForHashCode(String ivalue, String fvalue, int sign, int pvalue) {
154             if ("NaN".equals(ivalue)) {
155                 return "NaN";
156             }
157             if ("INF".equals(ivalue)) {
158                 return sign < 0 ? "-INF" : "INF";
159             }
160             final StringBuilder builder = new StringBuilder();
161             final int ilen = ivalue.length();
162             final int flen0 = fvalue.length();
163             int lastNonZero;
164             for (lastNonZero = flen0; lastNonZero > 0 ; lastNonZero--) {
165                 if (fvalue.charAt(lastNonZero -1 ) != '0') break;
166             }
167             final int flen = lastNonZero;
168             int iStart;
169             int exponent = pvalue;
170             for (iStart = 0; iStart < ilen; iStart++) {
171                 if (ivalue.charAt(iStart) != '0') break;
172             }
173             int fStart = 0;
174             if (iStart < ivalue.length()) {
175                 builder.append(sign == -1 ? "-" : "");
176                 builder.append(ivalue.charAt(iStart));
177                 iStart++;
178             } else {
179                 if (flen > 0) {
180                     for (fStart = 0; fStart < flen; fStart++) {
181                         if (fvalue.charAt(fStart) != '0') break;
182                     }
183                     if (fStart < flen) {
184                         builder.append(sign == -1 ? "-" : "");
185                         builder.append(fvalue.charAt(fStart));
186                         exponent -= ++fStart;
187                     } else {
188                         return "0";
189                     }
190                 } else {
191                     return "0";
192                 }
193             }
194 
195             if (iStart < ilen || fStart < flen) {
196                 builder.append('.');
197             }
198             while (iStart < ilen) {
199                 builder.append(ivalue.charAt(iStart++));
200                 exponent++;
201             }
202             while (fStart < flen) {
203                 builder.append(fvalue.charAt(fStart++));
204             }
205             if (exponent != 0) {
206                 builder.append("E").append(exponent);
207             }
208             return builder.toString();
209         }
210 
211         @Override
equals(Object val)212         public boolean equals(Object val) {
213             if (val == this)
214                 return true;
215 
216             if (!(val instanceof XPrecisionDecimal))
217                 return false;
218             XPrecisionDecimal oval = (XPrecisionDecimal)val;
219 
220             return this.compareTo(oval) == EQUAL;
221         }
222 
223         @Override
hashCode()224         public int hashCode() {
225             // There's nothing else we can use easily, because equals could
226             // return true for widely different representation of the
227             // same number - and we don't have any canonical representation.
228             // The problem here is that we must ensure that if two numbers
229             // are equals then their hash code must also be equals.
230             // hashCode for 1.01E1 should be the same as hashCode for 0.101E2
231             // So we call cannonicalToStringForHashCode - which implements an
232             // algorithm that invents a normalized string representation
233             // for this number, and we return a hash for that.
234             return canonicalToStringForHashCode(ivalue, fvalue, sign, pvalue).hashCode();
235         }
236 
237         /**
238          * @return
239          */
compareFractionalPart(XPrecisionDecimal oval)240         private int compareFractionalPart(XPrecisionDecimal oval) {
241             if(fvalue.equals(oval.fvalue))
242                 return EQUAL;
243 
244             StringBuffer temp1 = new StringBuffer(fvalue);
245             StringBuffer temp2 = new StringBuffer(oval.fvalue);
246 
247             truncateTrailingZeros(temp1, temp2);
248             return temp1.toString().compareTo(temp2.toString());
249         }
250 
truncateTrailingZeros(StringBuffer fValue, StringBuffer otherFValue)251         private void truncateTrailingZeros(StringBuffer fValue, StringBuffer otherFValue) {
252             for(int i = fValue.length() - 1;i >= 0; i--)
253                 if(fValue.charAt(i) == '0')
254                     fValue.deleteCharAt(i);
255                 else
256                     break;
257 
258             for(int i = otherFValue.length() - 1;i >= 0; i--)
259                 if(otherFValue.charAt(i) == '0')
260                     otherFValue.deleteCharAt(i);
261                 else
262                     break;
263         }
264 
compareTo(XPrecisionDecimal val)265         public int compareTo(XPrecisionDecimal val) {
266 
267             // seen NaN
268             if(sign == 0)
269                 return INDETERMINATE;
270 
271             //INF is greater than everything and equal to itself
272             if(ivalue.equals("INF") || val.ivalue.equals("INF")) {
273                 if(ivalue.equals(val.ivalue))
274                     return EQUAL;
275                 else if(ivalue.equals("INF"))
276                     return GREATER_THAN;
277                 return LESS_THAN;
278             }
279 
280             //-INF is smaller than everything and equal itself
281             if(ivalue.equals("-INF") || val.ivalue.equals("-INF")) {
282                 if(ivalue.equals(val.ivalue))
283                     return EQUAL;
284                 else if(ivalue.equals("-INF"))
285                     return LESS_THAN;
286                 return GREATER_THAN;
287             }
288 
289             if (sign != val.sign)
290                 return sign > val.sign ? GREATER_THAN : LESS_THAN;
291 
292             return sign * compare(val);
293         }
294 
295         // To enable comparison - the exponent part of the decimal will be limited
296         // to the max value of int.
compare(XPrecisionDecimal val)297         private int compare(XPrecisionDecimal val) {
298 
299             if(pvalue != 0 || val.pvalue != 0) {
300                 if(pvalue == val.pvalue)
301                     return intComp(val);
302                 else {
303 
304                     if(intDigits + pvalue != val.intDigits + val.pvalue)
305                         return intDigits + pvalue > val.intDigits + val.pvalue ? GREATER_THAN : LESS_THAN;
306 
307                     //otherwise the 2 combined values are the same
308                     if(pvalue > val.pvalue) {
309                         int expDiff = pvalue - val.pvalue;
310                         StringBuffer buffer = new StringBuffer(ivalue);
311                         StringBuffer fbuffer = new StringBuffer(fvalue);
312                         for(int i = 0;i < expDiff; i++) {
313                             if(i < fracDigits) {
314                                 buffer.append(fvalue.charAt(i));
315                                 fbuffer.deleteCharAt(i);
316                             }
317                             else
318                                 buffer.append('0');
319                         }
320                         return compareDecimal(buffer.toString(), val.ivalue, fbuffer.toString(), val.fvalue);
321                     }
322                     else {
323                         int expDiff = val.pvalue - pvalue;
324                         StringBuffer buffer = new StringBuffer(val.ivalue);
325                         StringBuffer fbuffer = new StringBuffer(val.fvalue);
326                         for(int i = 0;i < expDiff; i++) {
327                             if(i < val.fracDigits) {
328                                 buffer.append(val.fvalue.charAt(i));
329                                 fbuffer.deleteCharAt(i);
330                             }
331                             else
332                                 buffer.append('0');
333                         }
334                         return compareDecimal(ivalue, buffer.toString(), fvalue, fbuffer.toString());
335                     }
336                 }
337             }
338             else {
339                 return intComp(val);
340             }
341         }
342 
343         /**
344          * @param val
345          * @return
346          */
intComp(XPrecisionDecimal val)347         private int intComp(XPrecisionDecimal val) {
348             if (intDigits != val.intDigits)
349                 return intDigits > val.intDigits ? GREATER_THAN : LESS_THAN;
350 
351             return compareDecimal(ivalue, val.ivalue, fvalue, val.fvalue);
352         }
353 
354         /**
355          * @param val
356          * @return
357          */
compareDecimal(String iValue, String fValue, String otherIValue, String otherFValue)358         private int compareDecimal(String iValue, String fValue, String otherIValue, String otherFValue) {
359             int ret = iValue.compareTo(otherIValue);
360             if (ret != 0)
361                 return ret > 0 ? GREATER_THAN : LESS_THAN;
362 
363             if(fValue.equals(otherFValue))
364                 return EQUAL;
365 
366             StringBuffer temp1=new StringBuffer(fValue);
367             StringBuffer temp2=new StringBuffer(otherFValue);
368 
369             truncateTrailingZeros(temp1, temp2);
370             ret = temp1.toString().compareTo(temp2.toString());
371             return ret == 0 ? EQUAL : (ret > 0 ? GREATER_THAN : LESS_THAN);
372         }
373 
374         private String canonical;
375 
376         @Override
toString()377         public synchronized String toString() {
378             if (canonical == null) {
379                 makeCanonical();
380             }
381             return canonical;
382         }
383 
makeCanonical()384         private void makeCanonical() {
385             // REVISIT: to be determined by working group
386             canonical = "TBD by Working Group";
387         }
388 
389         /**
390          * @param decimal
391          * @return
392          */
isIdentical(XPrecisionDecimal decimal)393         public boolean isIdentical(XPrecisionDecimal decimal) {
394             if(ivalue.equals(decimal.ivalue) && (ivalue.equals("INF") || ivalue.equals("-INF") || ivalue.equals("NaN")))
395                 return true;
396 
397             if(sign == decimal.sign && intDigits == decimal.intDigits && fracDigits == decimal.fracDigits && pvalue == decimal.pvalue
398                     && ivalue.equals(decimal.ivalue) && fvalue.equals(decimal.fvalue))
399                 return true;
400             return false;
401         }
402 
403     }
404     /* (non-Javadoc)
405      * @see com.sun.org.apache.xerces.internal.impl.dv.xs.TypeValidator#getAllowedFacets()
406      */
407     @Override
getAllowedFacets()408     public short getAllowedFacets() {
409         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);
410     }
411 
412     /* (non-Javadoc)
413      * @see com.sun.org.apache.xerces.internal.impl.dv.xs.TypeValidator#getActualValue(java.lang.String, com.sun.org.apache.xerces.internal.impl.dv.ValidationContext)
414      */
415     @Override
getActualValue(String content, ValidationContext context)416     public Object getActualValue(String content, ValidationContext context)
417     throws InvalidDatatypeValueException {
418         try {
419             return new XPrecisionDecimal(content);
420         } catch (NumberFormatException nfe) {
421             throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "precisionDecimal"});
422         }
423     }
424 
425     @Override
compare(Object value1, Object value2)426     public int compare(Object value1, Object value2) {
427         return ((XPrecisionDecimal)value1).compareTo((XPrecisionDecimal)value2);
428     }
429 
430     @Override
getFractionDigits(Object value)431     public int getFractionDigits(Object value) {
432         return ((XPrecisionDecimal)value).fracDigits;
433     }
434 
435     @Override
getTotalDigits(Object value)436     public int getTotalDigits(Object value) {
437         return ((XPrecisionDecimal)value).totalDigits;
438     }
439 
440     @Override
isIdentical(Object value1, Object value2)441     public boolean isIdentical(Object value1, Object value2) {
442         if(!(value2 instanceof XPrecisionDecimal) || !(value1 instanceof XPrecisionDecimal))
443             return false;
444         return ((XPrecisionDecimal)value1).isIdentical((XPrecisionDecimal)value2);
445     }
446 }
447