1 57 58 package com.sun.org.apache.xerces.internal.impl.dv.xs; 59 60 import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException; 61 import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext; 62 63 71 public class DecimalDV extends TypeValidator { 72 73 public final short getAllowedFacets(){ 74 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); 75 } 76 77 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 78 try { 79 return new XDecimal(content); 80 } catch (NumberFormatException nfe) { 81 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "decimal"}); 82 } 83 } 84 85 public final int compare(Object value1, Object value2){ 86 return ((XDecimal)value1).compareTo((XDecimal)value2); 87 } 88 89 public final int getTotalDigits(Object value){ 90 return ((XDecimal)value).totalDigits; 91 } 92 93 public final int getFractionDigits(Object value){ 94 return ((XDecimal)value).fracDigits; 95 } 96 97 static class XDecimal { 99 int sign = 1; 101 int totalDigits = 0; 103 int intDigits = 0; 105 int fracDigits = 0; 107 String ivalue = ""; 109 String fvalue = ""; 111 boolean integer = false; 113 114 XDecimal(String content) throws NumberFormatException { 115 initD(content); 116 } 117 XDecimal(String content, boolean integer) throws NumberFormatException { 118 if (integer) 119 initI(content); 120 else 121 initD(content); 122 } 123 void initD(String content) throws NumberFormatException { 124 int len = content.length(); 125 if (len == 0) 126 throw new NumberFormatException (); 127 128 int intStart = 0, intEnd = 0, fracStart = 0, fracEnd = 0; 131 132 if (content.charAt(0) == '+') { 134 intStart = 1; 136 } 137 else if (content.charAt(0) == '-') { 138 intStart = 1; 140 sign = -1; 141 } 142 143 int actualIntStart = intStart; 145 while (actualIntStart < len && content.charAt(actualIntStart) == '0') { 146 actualIntStart++; 147 } 148 149 for (intEnd = actualIntStart; 151 intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); 152 intEnd++); 153 154 if (intEnd < len) { 156 if (content.charAt(intEnd) != '.') 158 throw new NumberFormatException (); 159 160 fracStart = intEnd + 1; 162 fracEnd = len; 163 } 164 165 if (intStart == intEnd && fracStart == fracEnd) 167 throw new NumberFormatException (); 168 169 while (fracEnd > fracStart && content.charAt(fracEnd-1) == '0') { 171 fracEnd--; 172 } 173 174 for (int fracPos = fracStart; fracPos < fracEnd; fracPos++) { 176 if (!TypeValidator.isDigit(content.charAt(fracPos))) 177 throw new NumberFormatException (); 178 } 179 180 intDigits = intEnd - actualIntStart; 181 fracDigits = fracEnd - fracStart; 182 totalDigits = intDigits + fracDigits; 183 184 if (intDigits > 0) { 185 ivalue = content.substring(actualIntStart, intEnd); 186 if (fracDigits > 0) 187 fvalue = content.substring(fracStart, fracEnd); 188 } 189 else { 190 if (fracDigits > 0) { 191 fvalue = content.substring(fracStart, fracEnd); 192 } 193 else { 194 sign = 0; 196 } 197 } 198 } 199 void initI(String content) throws NumberFormatException { 200 int len = content.length(); 201 if (len == 0) 202 throw new NumberFormatException (); 203 204 int intStart = 0, intEnd = 0; 206 207 if (content.charAt(0) == '+') { 209 intStart = 1; 211 } 212 else if (content.charAt(0) == '-') { 213 intStart = 1; 215 sign = -1; 216 } 217 218 int actualIntStart = intStart; 220 while (actualIntStart < len && content.charAt(actualIntStart) == '0') { 221 actualIntStart++; 222 } 223 224 for (intEnd = actualIntStart; 226 intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); 227 intEnd++); 228 229 if (intEnd < len) 231 throw new NumberFormatException (); 232 233 if (intStart == intEnd) 235 throw new NumberFormatException (); 236 237 intDigits = intEnd - actualIntStart; 238 fracDigits = 0; 239 totalDigits = intDigits; 240 241 if (intDigits > 0) { 242 ivalue = content.substring(actualIntStart, intEnd); 243 } 244 else { 245 sign = 0; 247 } 248 249 integer = true; 250 } 251 public boolean equals(Object val) { 252 if (val == this) 253 return true; 254 255 if (!(val instanceof XDecimal)) 256 return false; 257 XDecimal oval = (XDecimal)val; 258 259 if (sign != oval.sign) 260 return false; 261 if (sign == 0) 262 return true; 263 264 return intDigits == oval.intDigits && fracDigits == oval.fracDigits && 265 ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue); 266 } 267 public int compareTo(XDecimal val) { 268 if (sign != val.sign) 269 return sign > val.sign ? 1 : -1; 270 if (sign == 0) 271 return 0; 272 return sign * intComp(val); 273 } 274 private int intComp(XDecimal val) { 275 if (intDigits != val.intDigits) 276 return intDigits > val.intDigits ? 1 : -1; 277 int ret = ivalue.compareTo(val.ivalue); 278 if (ret != 0) 279 return ret > 0 ? 1 : -1;; 280 ret = fvalue.compareTo(val.fvalue); 281 return ret == 0 ? 0 : (ret > 0 ? 1 : -1); 282 } 283 private String canonical; 284 public synchronized String toString() { 285 if (canonical == null) { 286 makeCanonical(); 287 } 288 return canonical; 289 } 290 291 private void makeCanonical() { 292 if (sign == 0) { 293 if (integer) 294 canonical = "0"; 295 else 296 canonical = "0.0"; 297 return; 298 } 299 if (integer && sign > 0) { 300 canonical = ivalue; 301 return; 302 } 303 StringBuffer buffer = new StringBuffer (totalDigits+3); 305 if (sign == -1) 306 buffer.append('-'); 307 if (intDigits != 0) 308 buffer.append(ivalue); 309 else 310 buffer.append('0'); 311 if (!integer) { 312 buffer.append('.'); 313 if (fracDigits != 0) { 314 buffer.append(fvalue); 315 } 316 else { 317 buffer.append('0'); 318 } 319 } 320 canonical = buffer.toString(); 321 } 322 } 323 } 325 | Popular Tags |