1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.util.Hashtable ; 61 import java.util.Vector ; 62 63 71 72 public class FloatDatatypeValidator extends AbstractNumericValidator { 73 74 public FloatDatatypeValidator () throws InvalidDatatypeFacetException{ 75 this( null, null, false ); } 77 78 public FloatDatatypeValidator ( DatatypeValidator base, Hashtable facets, 79 boolean derivedByList ) throws InvalidDatatypeFacetException { 80 super (base, facets, derivedByList); 82 } 83 84 public int compare( String value1, String value2) { 85 try { 86 float f1 = fValueOf(value1).floatValue(); 87 float f2 = fValueOf(value2).floatValue(); 88 return compareFloats(f1, f2); 89 } 90 catch ( NumberFormatException e ) { 91 return -1; 93 } 94 } 95 96 protected void assignAdditionalFacets(String key, Hashtable facets ) throws InvalidDatatypeFacetException{ 97 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_FLOAT_FACET, 98 DatatypeMessageProvider.MSG_NONE, new Object [] { key})); 99 } 100 101 protected int compareValues (Object value1, Object value2) { 102 103 float f1 = ((Float )value1).floatValue(); 104 float f2 = ((Float )value2).floatValue(); 105 return compareFloats(f1, f2); 106 } 107 108 protected void setMaxInclusive (String value) { 109 fMaxInclusive = fValueOf(value); 110 } 111 protected void setMinInclusive (String value) { 112 fMinInclusive = fValueOf(value); 113 114 } 115 protected void setMaxExclusive (String value) { 116 fMaxExclusive = fValueOf(value); 117 118 } 119 protected void setMinExclusive (String value) { 120 fMinExclusive = fValueOf(value); 121 122 } 123 protected void setEnumeration (Vector enumeration) throws InvalidDatatypeValueException{ 124 if ( enumeration != null ) { 125 fEnumeration = new Float [enumeration.size()]; 126 Object [] baseEnum=null; 127 for ( int i = 0; i < enumeration.size(); i++ ){ 128 fEnumeration[i] = fValueOf((String ) enumeration.elementAt(i)); 129 ((FloatDatatypeValidator)fBaseValidator).validate((String )enumeration.elementAt(i), null); 130 } 131 } 132 } 133 134 135 protected String getMaxInclusive (boolean isBase) { 136 return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMaxInclusive.toString()) 137 :((Float )fMaxInclusive).toString(); 138 } 139 protected String getMinInclusive (boolean isBase) { 140 return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMinInclusive.toString()) 141 :((Float )fMinInclusive).toString(); 142 } 143 protected String getMaxExclusive (boolean isBase) { 144 return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMaxExclusive.toString()) 145 :((Float )fMaxExclusive).toString(); 146 } 147 protected String getMinExclusive (boolean isBase) { 148 return(isBase)?(((FloatDatatypeValidator)fBaseValidator).fMinExclusive.toString()) 149 :((Float )fMinExclusive).toString(); 150 } 151 152 162 163 protected void checkContent(String content, Object state, Vector enumeration, boolean asBase) 164 throws InvalidDatatypeValueException { 165 if ( this.fBaseValidator != null ) { 167 ((FloatDatatypeValidator)fBaseValidator).checkContent(content, state, enumeration, true); 169 } 170 171 if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) { 173 if ( fRegex == null || fRegex.matches( content) == false ) 174 throw new InvalidDatatypeValueException("Value'"+content+ 175 "does not match regular expression facet" + fPattern ); 176 } 177 178 if ( asBase ) 181 return; 182 183 Float f = null; 184 try { 185 f = fValueOf(content); 186 } 187 catch ( NumberFormatException nfe ) { 188 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.NOT_FLOAT, 189 DatatypeMessageProvider.MSG_NONE, 190 new Object [] {content})); 191 } 192 193 if ( enumeration != null ) { 195 int size = enumeration.size(); 196 Float [] enumFloats = new Float [size]; 197 int i=0; 198 try { 199 for ( ; i < size; i++ ) 200 enumFloats[i] = fValueOf((String ) enumeration.elementAt(i)); 201 202 } 203 catch ( NumberFormatException nfe ) { 204 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.INVALID_ENUM_VALUE, 205 DatatypeMessageProvider.MSG_NONE, 206 new Object [] { enumeration.elementAt(i)})); 207 } 208 enumCheck(f.floatValue(), enumFloats); 209 } 210 211 boundsCheck(f); 212 213 if ( ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0 && 214 (fEnumeration != null) ) ) { 215 enumCheck(f.floatValue(), (Float [])fEnumeration); 216 } 217 } 218 219 protected int getInvalidFacetMsg (){ 220 return DatatypeMessageProvider.ILLEGAL_FLOAT_FACET; 221 } 222 223 private void enumCheck(float v, Float [] enumFloats) throws InvalidDatatypeValueException { 224 for ( int i = 0; i < enumFloats.length; i++ ) { 225 if ( v == ((Float )enumFloats[i]).floatValue() ) return; 226 } 227 throw new InvalidDatatypeValueException( 228 getErrorString(DatatypeMessageProvider.NOT_ENUM_VALUE, 229 DatatypeMessageProvider.MSG_NONE, 230 new Object [] { new Float (v)})); 231 } 232 233 234 private static Float fValueOf(String s) throws NumberFormatException { 235 Float f=null; 236 try { 237 f = Float.valueOf(s); 238 } 239 catch ( NumberFormatException nfe ) { 240 if ( s.equals("INF") ) { 241 f = new Float (Float.POSITIVE_INFINITY); 242 } 243 else if ( s.equals("-INF") ) { 244 f = new Float (Float.NEGATIVE_INFINITY); 245 } 246 else if ( s.equals("NaN" ) ) { 247 f = new Float (Float.NaN); 248 } 249 else { 250 throw nfe; 251 } 252 } 253 return f; 254 } 255 256 private int compareFloats( float f1, float f2){ 257 int f1V = Float.floatToIntBits(f1); 258 int f2V = Float.floatToIntBits(f2); 259 if ( f1 > f2 ) { 260 return 1; 261 } 262 if ( f1 < f2 ) { 263 return -1; 264 } 265 if ( f1V==f2V ) { 266 return 0; 267 } 268 return(f1V < f2V) ? -1 : 1; 270 } 271 } 272 | Popular Tags |