1 16 17 package org.apache.xerces.impl.dv.xs; 18 19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException; 20 import org.apache.xerces.impl.dv.ValidationContext; 21 import org.apache.xerces.xs.datatypes.XSDouble; 22 23 33 public class DoubleDV extends TypeValidator { 34 35 public short getAllowedFacets(){ 36 return ( XSSimpleTypeDecl.FACET_PATTERN | XSSimpleTypeDecl.FACET_WHITESPACE | XSSimpleTypeDecl.FACET_ENUMERATION |XSSimpleTypeDecl.FACET_MAXINCLUSIVE |XSSimpleTypeDecl.FACET_MININCLUSIVE | XSSimpleTypeDecl.FACET_MAXEXCLUSIVE | XSSimpleTypeDecl.FACET_MINEXCLUSIVE ); 37 } 39 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 41 try{ 42 return new XDouble(content); 43 } catch (NumberFormatException ex){ 44 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "double"}); 45 } 46 } 48 public int compare(Object value1, Object value2) { 50 return ((XDouble)value1).compareTo((XDouble)value2); 51 } 53 public boolean isIdentical (Object value1, Object value2) { 56 if (value2 instanceof XDouble) { 57 return ((XDouble)value1).isIdentical((XDouble)value2); 58 } 59 return false; 60 } 62 67 static boolean isPossibleFP(String val) { 68 final int length = val.length(); 69 for (int i = 0; i < length; ++i) { 70 char c = val.charAt(i); 71 if (!(c >= '0' && c <= '9' || c == '.' || 72 c == '-' || c == '+' || c == 'E' || c == 'e')) { 73 return false; 74 } 75 } 76 return true; 77 } 78 79 private static final class XDouble implements XSDouble { 80 private double value; 81 public XDouble(String s) throws NumberFormatException { 82 if (isPossibleFP(s)) { 83 value = Double.parseDouble(s); 84 } 85 else if ( s.equals("INF") ) { 86 value = Double.POSITIVE_INFINITY; 87 } 88 else if ( s.equals("-INF") ) { 89 value = Double.NEGATIVE_INFINITY; 90 } 91 else if ( s.equals("NaN" ) ) { 92 value = Double.NaN; 93 } 94 else { 95 throw new NumberFormatException (s); 96 } 97 } 98 99 public boolean equals(Object val) { 100 if (val == this) 101 return true; 102 103 if (!(val instanceof XDouble)) 104 return false; 105 XDouble oval = (XDouble)val; 106 107 if (value == oval.value) 109 return true; 110 111 if (value != value && oval.value != oval.value) 112 return true; 113 114 return false; 115 } 116 117 public boolean isIdentical (XDouble val) { 119 if (val == this) { 120 return true; 121 } 122 123 if (value == val.value) { 124 return (value != 0.0d || 125 (Double.doubleToLongBits(value) == Double.doubleToLongBits(val.value))); 126 } 127 128 if (value != value && val.value != val.value) 129 return true; 130 131 return false; 132 } 133 134 private int compareTo(XDouble val) { 135 double oval = val.value; 136 137 if (value < oval) 139 return -1; 140 if (value > oval) 142 return 1; 143 if (value == oval) 146 return 0; 147 148 150 if (value != value) { 151 if (oval != oval) 153 return 0; 154 return INDETERMINATE; 156 } 157 158 return INDETERMINATE; 160 } 161 162 private String canonical; 163 public synchronized String toString() { 164 if (canonical == null) { 165 if (value == Double.POSITIVE_INFINITY) 166 canonical = "INF"; 167 else if (value == Double.NEGATIVE_INFINITY) 168 canonical = "-INF"; 169 else if (value != value) 170 canonical = "NaN"; 171 else if (value == 0) 173 canonical = "0.0E1"; 174 else { 175 canonical = Double.toString(value); 179 if (canonical.indexOf('E') == -1) { 182 int len = canonical.length(); 183 char[] chars = new char[len+3]; 185 canonical.getChars(0, len, chars, 0); 186 int edp = chars[0] == '-' ? 2 : 1; 188 if (value >= 1 || value <= -1) { 190 int dp = canonical.indexOf('.'); 192 for (int i = dp; i > edp; i--) { 194 chars[i] = chars[i-1]; 195 } 196 chars[edp] = '.'; 197 while (chars[len-1] == '0') 199 len--; 200 if (chars[len-1] == '.') 202 len++; 203 chars[len++] = 'E'; 205 int shift = dp - edp; 207 chars[len++] = (char)(shift + '0'); 210 } 211 else { 212 int nzp = edp + 1; 214 while (chars[nzp] == '0') 216 nzp++; 217 chars[edp-1] = chars[nzp]; 219 chars[edp] = '.'; 220 for (int i = nzp+1, j = edp+1; i < len; i++, j++) 222 chars[j] = chars[i]; 223 len -= nzp - edp; 225 if (len == edp + 1) 227 chars[len++] = '0'; 228 chars[len++] = 'E'; 230 chars[len++] = '-'; 231 int shift = nzp - edp; 233 chars[len++] = (char)(shift + '0'); 236 } 237 canonical = new String (chars, 0, len); 238 } 239 } 240 } 241 return canonical; 242 } 243 public double getValue() { 244 return value; 245 } 246 } 247 } | Popular Tags |