1 22 23 package org.xquark.schema.datatypes; 24 25 import java.text.DecimalFormat ; 26 import java.util.Locale ; 27 28 import org.xquark.schema.SchemaException; 29 import org.xquark.schema.validation.ValidationContextProvider; 30 31 32 class FloatType extends ComparableType { 33 private static final String RCSRevision = "$Revision: 1.3 $"; 34 private static final String RCSName = "$Name: $"; 35 36 private static final String NAN = "NaN"; 37 private static final String INF = "INF"; 38 private static final String POSITIVE_INF = "+INF"; 39 private static final String NEGATIVE_INF = "-INF"; 40 41 private DecimalFormat format = null; 42 43 FloatType() { 44 super("float", PrimitiveType.FLOAT); 45 format = (DecimalFormat )DecimalFormat.getInstance(Locale.US); 46 format.applyPattern("0.0######E0"); 47 } 48 49 public Object toValidType(Object data) { 50 if (data instanceof Float ) { 51 return data; 52 } else if (data instanceof Number ) { 53 return new Float (((Number )data).floatValue()); 54 } else { 55 return (Float )data; 56 } 57 } 58 59 protected Comparable toComparable(String value) throws SchemaException { 60 if (INF.equals(value)) 62 return new Float (Float.POSITIVE_INFINITY); 63 else if (POSITIVE_INF.equals(value)) 64 return new Float (Float.POSITIVE_INFINITY); 65 else if (NEGATIVE_INF.equals(value)) 66 return new Float (Float.NEGATIVE_INFINITY); 67 else if (NAN.equals(value)) 68 return new Float (Float.NaN); 69 try { 71 return new Float (value); 72 } catch (NumberFormatException nfe) { 73 super.invalidValue(value); 74 return null; 75 } 76 } 77 78 public String toXMLString(Object obj, ValidationContextProvider context) { 79 if (obj == null) return null; 80 Float data = (Float )obj; 81 if (data.isNaN()) return NAN; 82 else if (data.isInfinite()) { 83 if (data.floatValue() > 0) return INF; 84 else return NEGATIVE_INF; 85 } else { 86 return format.format(data.doubleValue()); 87 } 88 } 89 } 90 | Popular Tags |