1 29 30 package com.caucho.jaxb; 31 32 import com.caucho.util.Base64; 33 import com.caucho.util.L10N; 34 35 import javax.xml.bind.DatatypeConverterInterface; 36 import javax.xml.namespace.NamespaceContext ; 37 import javax.xml.namespace.QName ; 38 import java.math.BigDecimal ; 39 import java.math.BigInteger ; 40 import java.text.ParseException ; 41 import java.text.SimpleDateFormat ; 42 import java.util.Calendar ; 43 import java.util.Date ; 44 45 47 public class DatatypeConverterImpl implements DatatypeConverterInterface { 48 private static final L10N L = new L10N(DatatypeConverterImpl.class); 49 50 private final SimpleDateFormat dateFormat 52 = new SimpleDateFormat ("yyyy-MM-dd"); 53 private final SimpleDateFormat timeFormat 54 = new SimpleDateFormat ("HH:mm:ss"); 55 private final SimpleDateFormat dateTimeFormat 56 = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 57 58 private final char[] hexDigits = { '0', '1', '2', '3', '4', 59 '5', '6', '7', '8', '9', 60 'A', 'B', 'C', 'D', 'E', 'F'}; 61 62 66 public String parseAnySimpleType(String lexicalXSDAnySimpleType) 67 { 68 return lexicalXSDAnySimpleType; 69 } 70 71 public byte[] parseBase64Binary(String lexicalXSDBase64Binary) 72 { 73 return Base64.decodeToByteArray(lexicalXSDBase64Binary); 74 } 75 76 public boolean parseBoolean(String lexicalXSDBoolean) 77 { 78 return Boolean.parseBoolean(lexicalXSDBoolean); 79 } 80 81 public byte parseByte(String lexicalXSDByte) 82 { 83 return Byte.parseByte(lexicalXSDByte); 84 } 85 86 public Calendar parseDate(String lexicalXSDDate) 87 throws IllegalArgumentException 88 { 89 try { 90 Date date = dateFormat.parse(lexicalXSDDate); 91 Calendar calendar = (Calendar ) dateFormat.getCalendar().clone(); 92 calendar.setTime(date); 93 94 return calendar; 95 } 96 catch (ParseException e) { 97 throw new IllegalArgumentException (e); 98 } 99 } 100 101 public Calendar parseDateTime(String lexicalXSDDateTime) 102 throws IllegalArgumentException 103 { 104 try { 105 Date date = dateTimeFormat.parse(lexicalXSDDateTime); 106 Calendar calendar = (Calendar ) dateFormat.getCalendar().clone(); 107 calendar.setTime(date); 108 109 return calendar; 110 } 111 catch (ParseException e) { 112 throw new IllegalArgumentException (e); 113 } 114 } 115 116 public BigDecimal parseDecimal(String lexicalXSDDecimal) 117 { 118 return new BigDecimal (lexicalXSDDecimal); 119 } 120 121 public double parseDouble(String lexicalXSDDouble) 122 { 123 return Double.parseDouble(lexicalXSDDouble); 124 } 125 126 public float parseFloat(String lexicalXSDFloat) 127 { 128 return Float.parseFloat(lexicalXSDFloat); 129 } 130 131 public byte[] parseHexBinary(String lexicalXSDHexBinary) 132 throws IllegalArgumentException 133 { 134 if (lexicalXSDHexBinary.length() % 2 != 0) 135 throw new IllegalArgumentException (); 136 137 byte[] buffer = new byte[lexicalXSDHexBinary.length() / 2]; 138 139 for (int i = 0; i < buffer.length; i++) { 140 buffer[i] = 141 Byte.parseByte(lexicalXSDHexBinary.substring(2 * i, 2 * i + 2), 16); 142 } 143 144 return buffer; 145 } 146 147 public int parseInt(String lexicalXSDInt) 148 { 149 return Integer.parseInt(lexicalXSDInt); 150 } 151 152 public BigInteger parseInteger(String lexicalXSDInteger) 153 { 154 return new BigInteger (lexicalXSDInteger); 155 } 156 157 public long parseLong(String lexicalXSDLong) 158 { 159 return Long.parseLong(lexicalXSDLong); 160 } 161 162 public QName parseQName(String lexicalXSDQName, NamespaceContext nsc) 163 throws IllegalArgumentException 164 { 165 String [] parts = lexicalXSDQName.split(":", 2); 166 167 if (parts.length == 1) 168 return new QName (lexicalXSDQName); 169 else { 170 String namespaceURI = nsc.getNamespaceURI(parts[0]); 171 172 if (namespaceURI == null) 173 throw new IllegalArgumentException (L.l("Unknown prefix {0}", parts[0])); 174 175 return new QName (namespaceURI, parts[1], parts[0]); 176 } 177 } 178 179 public short parseShort(String lexicalXSDShort) 180 { 181 return Short.parseShort(lexicalXSDShort); 182 } 183 184 public String parseString(String lexicalXSDString) 185 { 186 return lexicalXSDString; 187 } 188 189 public Calendar parseTime(String lexicalXSDTime) 190 throws IllegalArgumentException 191 { 192 try { 193 Date date = timeFormat.parse(lexicalXSDTime); 194 Calendar calendar = (Calendar ) dateFormat.getCalendar().clone(); 195 calendar.setTime(date); 196 197 return calendar; 198 } 199 catch (ParseException e) { 200 throw new IllegalArgumentException (e); 201 } 202 } 203 204 public long parseUnsignedInt(String lexicalXSDUnsignedInt) 205 { 206 long x = parseLong(lexicalXSDUnsignedInt); 207 208 if (x < 0) 209 throw new IllegalArgumentException (L.l("Input not unsigned")); 210 211 return x; 212 } 213 214 public int parseUnsignedShort(String lexicalXSDUnsignedShort) 215 { 216 int x = parseInt(lexicalXSDUnsignedShort); 217 218 if (x < 0) 219 throw new IllegalArgumentException (L.l("Input not unsigned")); 220 221 return x; 222 } 223 224 228 public String printAnySimpleType(String val) 229 { 230 return val; 231 } 232 233 public String printBase64Binary(byte[] val) 234 { 235 return Base64.encodeFromByteArray(val); 236 } 237 238 public String printBoolean(boolean val) 239 { 240 return String.valueOf(val); 241 } 242 243 public String printByte(byte val) 244 { 245 return String.valueOf((int) val); 246 } 247 248 public String printDate(Calendar val) 249 { 250 dateFormat.setCalendar(val); 251 return dateFormat.format(val.getTime()); 252 } 253 254 public String printDateTime(Calendar val) 255 { 256 dateTimeFormat.setCalendar(val); 257 return dateTimeFormat.format(val.getTime()); 258 } 259 260 public String printDecimal(BigDecimal val) 261 { 262 return val.toString(); 263 } 264 265 public String printDouble(double val) 266 { 267 return String.valueOf(val); 268 } 269 270 public String printFloat(float val) 271 { 272 return String.valueOf(val); 273 } 274 275 public String printHexBinary(byte[] val) 276 { 277 StringBuilder sb = new StringBuilder (); 278 279 for (int i = 0; i < val.length; i++) { 280 sb.append(hexDigits[val[i] >>> 4]); 281 sb.append(hexDigits[val[i] & 0x0f]); 282 } 283 284 return sb.toString(); 285 } 286 287 public String printInt(int val) 288 { 289 return String.valueOf(val); 290 } 291 292 public String printInteger(BigInteger val) 293 { 294 return val.toString(); 295 } 296 297 public String printLong(long val) 298 { 299 return String.valueOf(val); 300 } 301 302 public String printQName(QName val, NamespaceContext nsc) 303 { 304 return val.toString(); 306 } 307 308 public String printShort(short val) 309 { 310 return String.valueOf(val); 311 } 312 313 public String printString(String val) 314 { 315 return val; 316 } 317 318 public String printTime(Calendar val) 319 { 320 timeFormat.setCalendar(val); 321 return timeFormat.format(val.getTime()); 322 } 323 324 public String printUnsignedInt(long val) 325 { 326 return String.valueOf(val); 327 } 328 329 public String printUnsignedShort(int val) 330 { 331 return String.valueOf(val); 332 } 333 } 334 | Popular Tags |