1 22 23 package org.xquark.xquery.typing; 24 25 import java.math.BigDecimal ; 26 27 import org.xquark.schema.SimpleType; 28 import org.xquark.schema.datatypes.*; 29 import org.xquark.schema.validation.ValidationContextProvider; 30 import org.xquark.xquery.parser.XQueryException; 31 32 41 public class QAtomicSerializer { 42 43 private static final String NAN = "NaN"; 44 private static final String INF = "INF"; 45 private static final String POSITIVE_INF = "+INF"; 46 private static final String NEGATIVE_INF = "-INF"; 47 private static final double MAX_DECIMAL = 1E+6; 48 private static final double MIN_DECIMAL = 1E-6; 49 private static final String ZERO = "0"; 50 private static final Double NEG_ZERO_D = new Double ("-0.0"); 51 private static final Float NEG_ZERO_F = new Float ("-0.0"); 52 53 private ValidationContextProvider context = null; 55 56 public QAtomicSerializer() { 57 } 58 59 public QAtomicSerializer(ValidationContextProvider contextProvider) { 60 context = contextProvider; 61 } 62 63 public String serialize(QName sValue) throws XQueryException { 67 throw new XQueryException("QName cannot be cast to string."); 68 } 69 public String serialize(URI sValue) throws XQueryException { 70 if (sValue == null) 71 return null; 72 return sValue.toString(); 73 } 74 75 public String serialize(SimpleType sType, String sValue) { 76 if (sValue == null) 77 return null; 78 return sType.normalizedValue(sValue); 79 } 80 81 public String serialize(SimpleType sType, DateTime sValue) throws XQueryException { 85 if (sValue == null) 86 return null; 87 String ret = sType.toXMLString(sValue, context); 88 switch (sType.getPrimitive().getType()) { 89 case PrimitiveType.DATE_TIME : 90 case PrimitiveType.TIME : 91 case PrimitiveType.DATE : 92 if (sValue.hasTimeZone() && sValue.getTimeZone() == 0) ret = ret.substring(0, ret.length() - 1); 94 break; 95 default : 96 } 97 return ret; 98 } 99 100 105 public String serialize(Long sValue) throws XQueryException { 106 if (sValue == null) 107 return null; 108 return sValue.toString(); 109 } 110 111 public String serialize(BigDecimal sValue) throws XQueryException { 112 if (sValue == null) 113 return null; 114 return removeNotSignificantDigits(sValue.toString()); 116 } 117 118 public String removeNotSignificantDigits(String decimal) { 119 int index = decimal.length(), lastZero = index; 120 char wChar; 121 do { 122 if (index == 0) 123 return decimal; 124 wChar = decimal.charAt(--index); 125 if (wChar == '0' && lastZero == index + 1) 126 lastZero = index; 127 if (wChar == 'E') 128 return decimal; } while (wChar != '.'); 130 if (lastZero == index + 1) 131 return decimal.substring(0, index); 132 else 133 return decimal.substring(0, lastZero); 134 } 135 136 public String serialize(Double sValue) throws XQueryException { 137 if (sValue == null) 138 return null; 139 if (sValue.isNaN()) 140 return NAN; 141 142 if (sValue.isInfinite()) { 143 if (sValue.doubleValue() > 0) 144 return INF; 145 else 146 return NEGATIVE_INF; 147 } else { 148 if (sValue.equals(NEG_ZERO_D)) 149 return ZERO; 150 else 151 return removeNotSignificantDigits(sValue.toString()); 152 } 153 154 } 172 173 public String serialize(Float sValue) throws XQueryException { 174 if (sValue == null) 175 return null; 176 if (sValue.isNaN()) 177 return NAN; 178 179 if (sValue.isInfinite()) { 180 if (sValue.floatValue() > 0) 181 return INF; 182 else 183 return NEGATIVE_INF; 184 } else { 185 if (sValue.equals(NEG_ZERO_F)) 186 return ZERO; 187 else 188 return removeNotSignificantDigits(sValue.toString()); 189 } 190 191 } 209 210 public String serialize(SimpleType sType, Boolean sValue) throws XQueryException { 214 if (sValue == null) 215 return null; 216 return sType.toXMLString(sValue, context); 217 } 218 219 public String serialize(SimpleType sType, ByteArray sValue) throws XQueryException { 220 if (sValue == null) 221 return null; 222 return sType.toXMLString(sValue, context); 223 } 224 225 public String serialize(SimpleType sType, byte[] sValue) throws XQueryException { 226 if (sValue == null) 227 return null; 228 return sType.toXMLString(sValue, context); 229 } 230 231 public String serialize(SimpleType sType, Duration sValue) throws XQueryException { 232 if (sValue == null) 233 return null; 234 return sType.toXMLString(sValue, context); 235 } 236 237 public String serialize(SimpleType sType, Object obj) throws XQueryException { 239 if (obj == null) 241 return null; 242 if (sType == null) { 243 return obj.toString(); 244 } 245 switch (sType.getPrimitive().getType()) { 246 case PrimitiveType.DECIMAL : 247 if (obj instanceof BigDecimal ) 249 return serialize((BigDecimal ) obj); 250 else 251 return serialize((Long ) obj); 252 case PrimitiveType.DOUBLE : 253 return serialize((Double ) obj); 254 case PrimitiveType.FLOAT : 255 return serialize((Float ) obj); 256 case PrimitiveType.QNAME : 257 return serialize((QName) obj); 258 case PrimitiveType.DATE_TIME : 259 return serialize(sType, (DateTime) obj); 260 case PrimitiveType.ANY_URI : 261 return serialize((URI) obj); 262 case PrimitiveType.STRING : 263 return serialize(sType, (String ) obj); 264 265 case PrimitiveType.DATE : 266 case PrimitiveType.TIME : 267 case PrimitiveType.BOOLEAN : 268 case PrimitiveType.BASE64_BINARY : 269 case PrimitiveType.HEX_BINARY : 270 case PrimitiveType.DURATION : 271 default : 272 return sType.toXMLString(obj, context); 273 } 274 } 275 } 276 | Popular Tags |