1 /* |
1 /* |
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
25 |
25 |
26 package com.sun.xml.internal.bind; |
26 package com.sun.xml.internal.bind; |
27 |
27 |
28 import java.math.BigDecimal; |
28 import java.math.BigDecimal; |
29 import java.math.BigInteger; |
29 import java.math.BigInteger; |
|
30 import java.security.AccessController; |
|
31 import java.security.PrivilegedAction; |
30 import java.util.Calendar; |
32 import java.util.Calendar; |
|
33 import java.util.Collections; |
31 import java.util.GregorianCalendar; |
34 import java.util.GregorianCalendar; |
|
35 import java.util.Map; |
32 import java.util.TimeZone; |
36 import java.util.TimeZone; |
|
37 import java.util.WeakHashMap; |
33 |
38 |
34 import javax.xml.bind.DatatypeConverter; |
39 import javax.xml.bind.DatatypeConverter; |
35 import javax.xml.bind.DatatypeConverterInterface; |
40 import javax.xml.bind.DatatypeConverterInterface; |
36 import javax.xml.datatype.DatatypeConfigurationException; |
41 import javax.xml.datatype.DatatypeConfigurationException; |
37 import javax.xml.datatype.DatatypeFactory; |
42 import javax.xml.datatype.DatatypeFactory; |
354 return new QName(uri, localPart, prefix); |
359 return new QName(uri, localPart, prefix); |
355 } |
360 } |
356 |
361 |
357 public static GregorianCalendar _parseDateTime(CharSequence s) { |
362 public static GregorianCalendar _parseDateTime(CharSequence s) { |
358 String val = WhiteSpaceProcessor.trim(s).toString(); |
363 String val = WhiteSpaceProcessor.trim(s).toString(); |
359 return datatypeFactory.newXMLGregorianCalendar(val).toGregorianCalendar(); |
364 return getDatatypeFactory().newXMLGregorianCalendar(val).toGregorianCalendar(); |
360 } |
365 } |
361 |
366 |
362 public static String _printDateTime(Calendar val) { |
367 public static String _printDateTime(Calendar val) { |
363 return CalendarFormatter.doFormat("%Y-%M-%DT%h:%m:%s%z", val); |
368 return CalendarFormatter.doFormat("%Y-%M-%DT%h:%m:%s%z", val); |
364 } |
369 } |
720 if (ch == '+' || ch == '-' || ch == '.') { |
725 if (ch == '+' || ch == '-' || ch == '.') { |
721 return true; |
726 return true; |
722 } |
727 } |
723 return false; |
728 return false; |
724 } |
729 } |
725 private static final DatatypeFactory datatypeFactory; |
730 |
726 |
731 private static final Map<ClassLoader, DatatypeFactory> DF_CACHE = Collections.synchronizedMap(new WeakHashMap<ClassLoader, DatatypeFactory>()); |
727 static { |
732 |
728 try { |
733 public static DatatypeFactory getDatatypeFactory() { |
729 datatypeFactory = DatatypeFactory.newInstance(); |
734 ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
730 } catch (DatatypeConfigurationException e) { |
735 public ClassLoader run() { |
731 throw new Error(e); |
736 return Thread.currentThread().getContextClassLoader(); |
732 } |
737 } |
|
738 }); |
|
739 DatatypeFactory df = DF_CACHE.get(tccl); |
|
740 if (df == null) { |
|
741 synchronized (DatatypeConverterImpl.class) { |
|
742 df = DF_CACHE.get(tccl); |
|
743 if (df == null) { // to prevent multiple initialization |
|
744 try { |
|
745 df = DatatypeFactory.newInstance(); |
|
746 } catch (DatatypeConfigurationException e) { |
|
747 throw new Error(Messages.FAILED_TO_INITIALE_DATATYPE_FACTORY.format(),e); |
|
748 } |
|
749 DF_CACHE.put(tccl, df); |
|
750 } |
|
751 } |
|
752 } |
|
753 return df; |
733 } |
754 } |
734 |
755 |
735 private static final class CalendarFormatter { |
756 private static final class CalendarFormatter { |
736 |
757 |
737 public static String doFormat(String format, Calendar cal) throws IllegalArgumentException { |
758 public static String doFormat(String format, Calendar cal) throws IllegalArgumentException { |
1043 return _parseInt(lexicalXSDUnsignedShort); |
1064 return _parseInt(lexicalXSDUnsignedShort); |
1044 } |
1065 } |
1045 |
1066 |
1046 @Deprecated |
1067 @Deprecated |
1047 public Calendar parseTime(String lexicalXSDTime) { |
1068 public Calendar parseTime(String lexicalXSDTime) { |
1048 return datatypeFactory.newXMLGregorianCalendar(lexicalXSDTime).toGregorianCalendar(); |
1069 return getDatatypeFactory().newXMLGregorianCalendar(lexicalXSDTime).toGregorianCalendar(); |
1049 } |
1070 } |
1050 |
1071 |
1051 @Deprecated |
1072 @Deprecated |
1052 public String printTime(Calendar val) { |
1073 public String printTime(Calendar val) { |
1053 return CalendarFormatter.doFormat("%h:%m:%s%z", val); |
1074 return CalendarFormatter.doFormat("%h:%m:%s%z", val); |
1054 } |
1075 } |
1055 |
1076 |
1056 @Deprecated |
1077 @Deprecated |
1057 public Calendar parseDate(String lexicalXSDDate) { |
1078 public Calendar parseDate(String lexicalXSDDate) { |
1058 return datatypeFactory.newXMLGregorianCalendar(lexicalXSDDate).toGregorianCalendar(); |
1079 return getDatatypeFactory().newXMLGregorianCalendar(lexicalXSDDate).toGregorianCalendar(); |
1059 } |
1080 } |
1060 |
1081 |
1061 @Deprecated |
1082 @Deprecated |
1062 public String printDate(Calendar val) { |
1083 public String printDate(Calendar val) { |
1063 return _printDate(val); |
1084 return _printDate(val); |