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.XSFloat; 22 23 33 public class FloatDV 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 XFloat(content); 43 } catch (NumberFormatException ex){ 44 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "float"}); 45 } 46 } 48 public int compare(Object value1, Object value2){ 50 return ((XFloat)value1).compareTo((XFloat)value2); 51 } 53 public boolean isIdentical (Object value1, Object value2) { 56 if (value2 instanceof XFloat) { 57 return ((XFloat)value1).isIdentical((XFloat)value2); 58 } 59 return false; 60 } 62 private static final class XFloat implements XSFloat { 63 64 private float value; 65 public XFloat(String s) throws NumberFormatException { 66 if (DoubleDV.isPossibleFP(s)) { 67 value = Float.parseFloat(s); 68 } 69 else if ( s.equals("INF") ) { 70 value = Float.POSITIVE_INFINITY; 71 } 72 else if ( s.equals("-INF") ) { 73 value = Float.NEGATIVE_INFINITY; 74 } 75 else if ( s.equals("NaN") ) { 76 value = Float.NaN; 77 } 78 else { 79 throw new NumberFormatException (s); 80 } 81 } 82 83 public boolean equals(Object val) { 84 if (val == this) 85 return true; 86 87 if (!(val instanceof XFloat)) 88 return false; 89 XFloat oval = (XFloat)val; 90 91 if (value == oval.value) 93 return true; 94 95 if (value != value && oval.value != oval.value) 96 return true; 97 98 return false; 99 } 100 101 public boolean isIdentical (XFloat val) { 103 if (val == this) { 104 return true; 105 } 106 107 if (value == val.value) { 108 return (value != 0.0f || 109 (Float.floatToIntBits(value) == Float.floatToIntBits(val.value))); 110 } 111 112 if (value != value && val.value != val.value) 113 return true; 114 115 return false; 116 } 117 118 private int compareTo(XFloat val) { 119 float oval = val.value; 120 121 if (value < oval) 123 return -1; 124 if (value > oval) 126 return 1; 127 if (value == oval) 130 return 0; 131 132 134 if (value != value) { 135 if (oval != oval) 137 return 0; 138 return INDETERMINATE; 140 } 141 142 return INDETERMINATE; 144 } 145 146 private String canonical; 147 public synchronized String toString() { 148 if (canonical == null) { 149 if (value == Float.POSITIVE_INFINITY) 150 canonical = "INF"; 151 else if (value == Float.NEGATIVE_INFINITY) 152 canonical = "-INF"; 153 else if (value != value) 154 canonical = "NaN"; 155 else if (value == 0) 157 canonical = "0.0E1"; 158 else { 159 canonical = Float.toString(value); 163 if (canonical.indexOf('E') == -1) { 166 int len = canonical.length(); 167 char[] chars = new char[len+3]; 169 canonical.getChars(0, len, chars, 0); 170 int edp = chars[0] == '-' ? 2 : 1; 172 if (value >= 1 || value <= -1) { 174 int dp = canonical.indexOf('.'); 176 for (int i = dp; i > edp; i--) { 178 chars[i] = chars[i-1]; 179 } 180 chars[edp] = '.'; 181 while (chars[len-1] == '0') 183 len--; 184 if (chars[len-1] == '.') 186 len++; 187 chars[len++] = 'E'; 189 int shift = dp - edp; 191 chars[len++] = (char)(shift + '0'); 194 } 195 else { 196 int nzp = edp + 1; 198 while (chars[nzp] == '0') 200 nzp++; 201 chars[edp-1] = chars[nzp]; 203 chars[edp] = '.'; 204 for (int i = nzp+1, j = edp+1; i < len; i++, j++) 206 chars[j] = chars[i]; 207 len -= nzp - edp; 209 if (len == edp + 1) 211 chars[len++] = '0'; 212 chars[len++] = 'E'; 214 chars[len++] = '-'; 215 int shift = nzp - edp; 217 chars[len++] = (char)(shift + '0'); 220 } 221 canonical = new String (chars, 0, len); 222 } 223 } 224 } 225 return canonical; 226 } 227 228 public float getValue() { 229 return value; 230 } 231 } 232 } | Popular Tags |