1 57 58 package org.enhydra.apache.xerces.validators.datatype; 59 60 import java.util.Hashtable ; 61 import java.util.Vector ; 62 63 70 71 public class DoubleDatatypeValidator extends AbstractNumericValidator { 72 73 public DoubleDatatypeValidator () throws InvalidDatatypeFacetException { 74 this( null, null, false ); } 76 77 public DoubleDatatypeValidator ( DatatypeValidator base, Hashtable facets, 78 boolean derivedByList ) throws InvalidDatatypeFacetException { 79 super(base, facets, derivedByList); 80 } 81 82 public int compare( String value1, String value2) { 83 try { 84 double d1 = dValueOf(value1).doubleValue(); 85 double d2 = dValueOf(value2).doubleValue(); 86 return compareDoubles(d1, d2); 87 } 88 catch ( NumberFormatException e ) { 89 return -1; 91 } 92 93 } 94 95 protected void assignAdditionalFacets(String key, Hashtable facets ) throws InvalidDatatypeFacetException{ 96 throw new InvalidDatatypeFacetException( getErrorString(DatatypeMessageProvider.ILLEGAL_DOUBLE_FACET, 97 DatatypeMessageProvider.MSG_NONE, new Object [] { key})); 98 } 99 108 protected int compareValues (Object value1, Object value2) { 109 double d1 = ((Double )value1).doubleValue(); 110 double d2 = ((Double )value2).doubleValue(); 111 return compareDoubles(d1, d2); 112 } 113 114 protected void setMaxInclusive (String value) { 115 fMaxInclusive = dValueOf(value); 116 } 117 protected void setMinInclusive (String value) { 118 fMinInclusive = dValueOf(value); 119 120 } 121 protected void setMaxExclusive (String value) { 122 fMaxExclusive = dValueOf(value); 123 124 } 125 protected void setMinExclusive (String value) { 126 fMinExclusive = dValueOf(value); 127 128 } 129 protected void setEnumeration (Vector enumeration) throws InvalidDatatypeValueException{ 130 if ( enumeration != null ) { 131 fEnumeration = new Double [enumeration.size()]; 132 Object baseEnum=null; 133 try { 134 135 for ( int i = 0; i < enumeration.size(); i++ ) { 136 fEnumeration[i] = dValueOf((String )enumeration.elementAt(i)); 137 ((DoubleDatatypeValidator)fBaseValidator).validate((String )enumeration.elementAt(i), null); 138 139 } 140 } 141 catch ( Exception e ) { 142 throw new InvalidDatatypeValueException(e.getMessage()); 143 } 144 } 145 } 146 147 148 protected String getMaxInclusive (boolean isBase) { 149 return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMaxInclusive.toString()) 150 :((Double )fMaxInclusive).toString(); 151 } 152 protected String getMinInclusive (boolean isBase) { 153 return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMinInclusive.toString()) 154 :((Double )fMinInclusive).toString(); 155 } 156 protected String getMaxExclusive (boolean isBase) { 157 return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMaxExclusive.toString()) 158 :((Double )fMaxExclusive).toString(); 159 } 160 protected String getMinExclusive (boolean isBase) { 161 return(isBase)?(((DoubleDatatypeValidator)fBaseValidator).fMinExclusive.toString()) 162 :((Double )fMinExclusive).toString(); 163 } 164 165 175 176 protected void checkContentEnum(String content, Object state, Vector enumeration) 177 throws InvalidDatatypeValueException { 178 checkContent (content, state, enumeration, false); 179 } 180 181 protected void checkContent(String content, Object state, Vector enumeration, boolean asBase) 182 throws InvalidDatatypeValueException { 183 if ( this.fBaseValidator != null ) { 185 ((DoubleDatatypeValidator)fBaseValidator).checkContent(content, state, enumeration, true); 187 } 188 189 if ( (fFacetsDefined & DatatypeValidator.FACET_PATTERN ) != 0 ) { 191 if ( fRegex == null || fRegex.matches( content) == false ) 192 throw new InvalidDatatypeValueException("Value'"+content+ 193 "does not match regular expression facet" + fPattern ); 194 } 195 196 if ( asBase ) 199 return; 200 201 Double d = null; 202 try { 203 d = dValueOf(content); 204 } 205 catch ( NumberFormatException nfe ) { 206 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.NOT_DOUBLE, 207 DatatypeMessageProvider.MSG_NONE, 208 new Object [] { content})); 209 } 210 211 if ( enumeration != null ) { int size = enumeration.size(); 213 Double [] enumDoubles = new Double [size]; 214 int i=0; 215 try { 216 for ( ; i < size; i++ ) 217 enumDoubles[i] = dValueOf((String ) enumeration.elementAt(i)); 218 } 219 catch ( NumberFormatException nfe ) { 220 throw new InvalidDatatypeValueException( getErrorString(DatatypeMessageProvider.INVALID_ENUM_VALUE, 221 DatatypeMessageProvider.MSG_NONE, 222 new Object [] { enumeration.elementAt(i)})); 223 } 224 225 enumCheck(d.doubleValue(), enumDoubles); 226 } 227 228 boundsCheck(d); 229 230 if ( ((fFacetsDefined & DatatypeValidator.FACET_ENUMERATION ) != 0 && 231 (fEnumeration != null) ) ) 232 enumCheck(d.doubleValue(), (Double [])fEnumeration); 233 } 234 235 protected int getInvalidFacetMsg (){ 236 return DatatypeMessageProvider.ILLEGAL_DOUBLE_FACET; 237 } 238 239 private static Double dValueOf(String s) throws NumberFormatException { 243 Double d; 244 try { 245 d = Double.valueOf(s); 246 } 247 catch ( NumberFormatException nfe ) { 248 if ( s.equals("INF") ) { 249 d = new Double (Double.POSITIVE_INFINITY); 250 } 251 else if ( s.equals("-INF") ) { 252 d = new Double (Double.NEGATIVE_INFINITY); 253 } 254 else if ( s.equals("NaN" ) ) { 255 d = new Double (Double.NaN); 256 } 257 else { 258 throw nfe; 259 } 260 } 261 return d; 262 } 263 264 private void enumCheck(double v, Double [] enumDoubles) throws InvalidDatatypeValueException { 265 for ( int i = 0; i < enumDoubles.length; i++ ) { 266 if ( v == enumDoubles[i].doubleValue() ) return; 267 } 268 throw new InvalidDatatypeValueException( 269 getErrorString(DatatypeMessageProvider.NOT_ENUM_VALUE, 270 DatatypeMessageProvider.MSG_NONE, 271 new Object [] { new Double (v)})); 272 } 273 274 private int compareDoubles(double d1, double d2) { 275 long d1V = Double.doubleToLongBits(d1); 276 long d2V = Double.doubleToLongBits(d2); 277 278 if ( d1 > d2 ) { 279 return 1; 280 } 281 if ( d1 < d2 ) { 282 return -1; 283 } 284 if ( d1V == d2V ) { 285 return 0; 286 } 287 return(d1V < d2V) ? -1 : 1; 289 } 290 } 291 | Popular Tags |