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 FloatDV extends TypeValidator { 72 73 public 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 ); 75 } 77 public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException { 79 try{ 80 return new XFloat(content); 81 } catch (NumberFormatException ex){ 82 throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object []{content, "float"}); 83 } 84 } 86 public int compare(Object value1, Object value2){ 88 return ((XFloat)value1).compareTo((XFloat)value2); 89 } 91 private static final class XFloat { 92 private float value; 93 public XFloat(String s) throws NumberFormatException { 94 try { 95 value = Float.parseFloat(s); 96 } 97 catch ( NumberFormatException nfe ) { 98 if ( s.equals("INF") ) { 99 value = Float.POSITIVE_INFINITY; 100 } 101 else if ( s.equals("-INF") ) { 102 value = Float.NEGATIVE_INFINITY; 103 } 104 else if ( s.equals("NaN" ) ) { 105 value = Float.NaN; 106 } 107 else { 108 throw nfe; 109 } 110 } 111 } 112 113 public boolean equals(Object val) { 114 if (val == this) 115 return true; 116 117 if (!(val instanceof XFloat)) 118 return false; 119 XFloat oval = (XFloat)val; 120 121 if (value == oval.value) 123 return true; 124 125 if (value != value && oval.value != oval.value) 126 return true; 127 128 return false; 129 } 130 131 private int compareTo(XFloat val) { 132 float oval = val.value; 133 134 if (value < oval) 136 return -1; 137 if (value > oval) 139 return 1; 140 if (value == oval) 143 return 0; 144 145 147 if (value != value) { 148 if (oval != oval) 150 return 0; 151 return INDETERMINATE; 153 } 154 155 return INDETERMINATE; 157 } 158 159 private String canonical; 160 public synchronized String toString() { 161 if (canonical == null) { 162 if (value == Float.POSITIVE_INFINITY) 163 canonical = "INF"; 164 else if (value == Float.NEGATIVE_INFINITY) 165 canonical = "-INF"; 166 else if (value != value) 167 canonical = "NaN"; 168 else if (value == 0) 170 canonical = "0.0E1"; 171 else { 172 canonical = Float.toString(value); 176 if (canonical.indexOf('E') == -1) { 179 int len = canonical.length(); 180 char[] chars = new char[len+3]; 182 canonical.getChars(0, len, chars, 0); 183 int edp = chars[0] == '-' ? 2 : 1; 185 if (value >= 1 || value <= -1) { 187 int dp = canonical.indexOf('.'); 189 for (int i = dp; i > edp; i--) { 191 chars[i] = chars[i-1]; 192 } 193 chars[edp] = '.'; 194 while (chars[len-1] == '0') 196 len--; 197 if (chars[len-1] == '.') 199 len++; 200 chars[len++] = 'E'; 202 int shift = dp - edp; 204 chars[len++] = (char)(shift + '0'); 207 } 208 else { 209 int nzp = edp + 1; 211 while (chars[nzp] == '0') 213 nzp++; 214 chars[edp-1] = chars[nzp]; 216 chars[edp] = '.'; 217 for (int i = nzp+1, j = edp+1; i < len; i++, j++) 219 chars[j] = chars[i]; 220 len -= nzp - edp; 222 if (len == edp + 1) 224 chars[len++] = '0'; 225 chars[len++] = 'E'; 227 chars[len++] = '-'; 228 int shift = nzp - edp; 230 chars[len++] = (char)(shift + '0'); 233 } 234 canonical = new String (chars, 0, len); 235 } 236 } 237 } 238 return canonical; 239 } 240 } 241 } | Popular Tags |