1 16 17 package org.apache.xerces.impl.dv.xs; 18 19 import java.math.BigDecimal ; 20 import java.math.BigInteger ; 21 22 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 23 import org.apache.xerces.impl.dv.ValidationContext; 24 import org.apache.xerces.xs.datatypes.XSDecimal; 25 26 36 public class DecimalDV extends TypeValidator { 37 38 public final short getAllowedFacets(){ 39 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); 40 } 41 42 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 43 try { 44 return new XDecimal(content); 45 } catch (NumberFormatException nfe) { 46 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "decimal"}); 47 } 48 } 49 50 public final int compare(Object value1, Object value2){ 51 return ((XDecimal)value1).compareTo((XDecimal)value2); 52 } 53 54 public final int getTotalDigits(Object value){ 55 return ((XDecimal)value).totalDigits; 56 } 57 58 public final int getFractionDigits(Object value){ 59 return ((XDecimal)value).fracDigits; 60 } 61 62 static class XDecimal implements XSDecimal { 64 int sign = 1; 66 int totalDigits = 0; 68 int intDigits = 0; 70 int fracDigits = 0; 72 String ivalue = ""; 74 String fvalue = ""; 76 boolean integer = false; 78 79 XDecimal(String content) throws NumberFormatException { 80 initD(content); 81 } 82 XDecimal(String content, boolean integer) throws NumberFormatException { 83 if (integer) 84 initI(content); 85 else 86 initD(content); 87 } 88 void initD(String content) throws NumberFormatException { 89 int len = content.length(); 90 if (len == 0) 91 throw new NumberFormatException (); 92 93 int intStart = 0, intEnd = 0, fracStart = 0, fracEnd = 0; 96 97 if (content.charAt(0) == '+') { 99 intStart = 1; 101 } 102 else if (content.charAt(0) == '-') { 103 intStart = 1; 105 sign = -1; 106 } 107 108 int actualIntStart = intStart; 110 while (actualIntStart < len && content.charAt(actualIntStart) == '0') { 111 actualIntStart++; 112 } 113 114 for (intEnd = actualIntStart; 116 intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); 117 intEnd++); 118 119 if (intEnd < len) { 121 if (content.charAt(intEnd) != '.') 123 throw new NumberFormatException (); 124 125 fracStart = intEnd + 1; 127 fracEnd = len; 128 } 129 130 if (intStart == intEnd && fracStart == fracEnd) 132 throw new NumberFormatException (); 133 134 while (fracEnd > fracStart && content.charAt(fracEnd-1) == '0') { 136 fracEnd--; 137 } 138 139 for (int fracPos = fracStart; fracPos < fracEnd; fracPos++) { 141 if (!TypeValidator.isDigit(content.charAt(fracPos))) 142 throw new NumberFormatException (); 143 } 144 145 intDigits = intEnd - actualIntStart; 146 fracDigits = fracEnd - fracStart; 147 totalDigits = intDigits + fracDigits; 148 149 if (intDigits > 0) { 150 ivalue = content.substring(actualIntStart, intEnd); 151 if (fracDigits > 0) 152 fvalue = content.substring(fracStart, fracEnd); 153 } 154 else { 155 if (fracDigits > 0) { 156 fvalue = content.substring(fracStart, fracEnd); 157 } 158 else { 159 sign = 0; 161 } 162 } 163 } 164 void initI(String content) throws NumberFormatException { 165 int len = content.length(); 166 if (len == 0) 167 throw new NumberFormatException (); 168 169 int intStart = 0, intEnd = 0; 171 172 if (content.charAt(0) == '+') { 174 intStart = 1; 176 } 177 else if (content.charAt(0) == '-') { 178 intStart = 1; 180 sign = -1; 181 } 182 183 int actualIntStart = intStart; 185 while (actualIntStart < len && content.charAt(actualIntStart) == '0') { 186 actualIntStart++; 187 } 188 189 for (intEnd = actualIntStart; 191 intEnd < len && TypeValidator.isDigit(content.charAt(intEnd)); 192 intEnd++); 193 194 if (intEnd < len) 196 throw new NumberFormatException (); 197 198 if (intStart == intEnd) 200 throw new NumberFormatException (); 201 202 intDigits = intEnd - actualIntStart; 203 fracDigits = 0; 204 totalDigits = intDigits; 205 206 if (intDigits > 0) { 207 ivalue = content.substring(actualIntStart, intEnd); 208 } 209 else { 210 sign = 0; 212 } 213 214 integer = true; 215 } 216 public boolean equals(Object val) { 217 if (val == this) 218 return true; 219 220 if (!(val instanceof XDecimal)) 221 return false; 222 XDecimal oval = (XDecimal)val; 223 224 if (sign != oval.sign) 225 return false; 226 if (sign == 0) 227 return true; 228 229 return intDigits == oval.intDigits && fracDigits == oval.fracDigits && 230 ivalue.equals(oval.ivalue) && fvalue.equals(oval.fvalue); 231 } 232 public int compareTo(XDecimal val) { 233 if (sign != val.sign) 234 return sign > val.sign ? 1 : -1; 235 if (sign == 0) 236 return 0; 237 return sign * intComp(val); 238 } 239 private int intComp(XDecimal val) { 240 if (intDigits != val.intDigits) 241 return intDigits > val.intDigits ? 1 : -1; 242 int ret = ivalue.compareTo(val.ivalue); 243 if (ret != 0) 244 return ret > 0 ? 1 : -1;; 245 ret = fvalue.compareTo(val.fvalue); 246 return ret == 0 ? 0 : (ret > 0 ? 1 : -1); 247 } 248 private String canonical; 249 public synchronized String toString() { 250 if (canonical == null) { 251 makeCanonical(); 252 } 253 return canonical; 254 } 255 256 private void makeCanonical() { 257 if (sign == 0) { 258 if (integer) 259 canonical = "0"; 260 else 261 canonical = "0.0"; 262 return; 263 } 264 if (integer && sign > 0) { 265 canonical = ivalue; 266 return; 267 } 268 StringBuffer buffer = new StringBuffer (totalDigits+3); 270 if (sign == -1) 271 buffer.append('-'); 272 if (intDigits != 0) 273 buffer.append(ivalue); 274 else 275 buffer.append('0'); 276 if (!integer) { 277 buffer.append('.'); 278 if (fracDigits != 0) { 279 buffer.append(fvalue); 280 } 281 else { 282 buffer.append('0'); 283 } 284 } 285 canonical = buffer.toString(); 286 } 287 288 public BigDecimal getBigDecimal() { 289 if (sign == 0) { 290 return new BigDecimal (BigInteger.ZERO); 291 } 292 return new BigDecimal (toString()); 293 } 294 295 public BigInteger getBigInteger() throws NumberFormatException { 296 if (fracDigits != 0) { 297 throw new NumberFormatException (); 298 } 299 if (sign == 0) { 300 return BigInteger.ZERO; 301 } 302 if (sign == 1) { 303 return new BigInteger (ivalue); 304 } 305 return new BigInteger ("-" + ivalue); 306 } 307 308 public long getLong() throws NumberFormatException { 309 if (fracDigits != 0) { 310 throw new NumberFormatException (); 311 } 312 if (sign == 0) { 313 return 0L; 314 } 315 if (sign == 1) { 316 return Long.parseLong(ivalue); 317 } 318 return Long.parseLong("-" + ivalue); 319 } 320 321 public int getInt() throws NumberFormatException { 322 if (fracDigits != 0) { 323 throw new NumberFormatException (); 324 } 325 if (sign == 0) { 326 return 0; 327 } 328 if (sign == 1) { 329 return Integer.parseInt(ivalue); 330 } 331 return Integer.parseInt("-" + ivalue); 332 } 333 334 public short getShort() throws NumberFormatException { 335 if (fracDigits != 0) { 336 throw new NumberFormatException (); 337 } 338 if (sign == 0) { 339 return 0; 340 } 341 if (sign == 1) { 342 return Short.parseShort(ivalue); 343 } 344 return Short.parseShort("-" + ivalue); 345 } 346 347 public byte getByte() throws NumberFormatException { 348 if (fracDigits != 0) { 349 throw new NumberFormatException (); 350 } 351 if (sign == 0) { 352 return 0; 353 } 354 if (sign == 1) { 355 return Byte.parseByte(ivalue); 356 } 357 return Byte.parseByte("-" + ivalue); 358 } 359 } 360 } 362 | Popular Tags |