public final class DatatypeConverter extends Object
The javaType binding declaration can be used to customize the binding of an XML schema datatype to a Java datatype. Customizations can involve writing a parse and print method for parsing and printing lexical representations of a XML schema datatype respectively. However, writing parse and print methods requires knowledge of the lexical representations ( XML Schema Part2: Datatypes specification ) and hence may be difficult to write.
This class makes it easier to write parse and print methods. It defines static parse and print methods that provide access to a JAXB provider's implementation of parse and print methods. These methods are invoked by custom parse and print methods. For example, the binding of xsd:dateTime to a long can be customized using parse and print methods as follows:
// Customized parse method
public long myParseCal( String dateTimeString ) {
java.util.Calendar cal = DatatypeConverter.parseDateTime(dateTimeString);
long longval = convert_calendar_to_long(cal); //application specific
return longval;
}
// Customized print method
public String myPrintCal( Long longval ) {
java.util.Calendar cal = convert_long_to_calendar(longval) ; //application specific
String dateTimeString = DatatypeConverter.printDateTime(cal);
return dateTimeString;
}
There is a static parse and print method corresponding to each parse and print method respectively in the DatatypeConverterInterface.
The static methods defined in the class can also be used to specify a parse or a print method in a javaType binding declaration.
JAXB Providers are required to call the setDatatypeConverter api at some point before the first marshal or unmarshal operation (perhaps during the call to JAXBContext.newInstance). This step is necessary to configure the converter that should be used to perform the print and parse functionality.
A print method for a XML schema datatype can output any lexical representation that is valid with respect to the XML schema datatype. If an error is encountered during conversion, then an IllegalArgumentException, or a subclass of IllegalArgumentException must be thrown by the method.
DatatypeConverterInterface,
ParseConversionEvent,
PrintConversionEvent
| Modifier and Type | Method and Description |
|---|---|
static String |
parseAnySimpleType(String
Return a string containing the lexical representation of the simple type.
|
static byte[] |
parseBase64Binary(String
Converts the string argument into an array of bytes.
|
static boolean |
parseBoolean(String
Converts the string argument into a boolean value.
|
static byte |
parseByte(String
Converts the string argument into a byte value.
|
static Calendar |
parseDate(String
Converts the string argument into a Calendar value.
|
static Calendar |
parseDateTime(String
Converts the string argument into a Calendar value.
|
static BigDecimal |
parseDecimal(String
Converts the string argument into a BigDecimal value.
|
static double |
parseDouble(String
Converts the string argument into a double value.
|
static float |
parseFloat(String
Converts the string argument into a float value.
|
static byte[] |
parseHexBinary(String
Converts the string argument into an array of bytes.
|
static int |
parseInt(String
Convert the string argument into an int value.
|
static BigInteger |
parseInteger(String
Convert the string argument into a BigInteger value.
|
static long |
parseLong(String
Converts the string argument into a long value.
|
static QName |
parseQName(String
Converts the string argument into a byte value.
|
static short |
parseShort(String
Converts the string argument into a short value.
|
static String |
parseString(String
Convert the lexical XSD string argument into a String value.
|
static Calendar |
parseTime(String
Converts the string argument into a Calendar value.
|
static long |
parseUnsignedInt(String
Converts the string argument into a long value.
|
static int |
parseUnsignedShort(String
Converts the string argument into an int value.
|
static String |
printAnySimpleType(String
Converts a string value into a string.
|
static String |
printBase64Binary(byte[] val)
Converts an array of bytes into a string.
|
static String |
printBoolean(boolean val)
Converts a boolean value into a string.
|
static String |
printByte(byte val)
Converts a byte value into a string.
|
static String |
printDate(Calendar
Converts a Calendar value into a string.
|
static String |
printDateTime(Calendar
Converts a Calendar value into a string.
|
static String |
printDecimal(BigDecimal
Converts a BigDecimal value into a string.
|
static String |
printDouble(double val)
Converts a double value into a string.
|
static String |
printFloat(float val)
Converts a float value into a string.
|
static String |
printHexBinary(byte[] val)
Converts an array of bytes into a string.
|
static String |
printInt(int val)
Converts an int value into a string.
|
static String |
printInteger(BigInteger
Converts a BigInteger value into a string.
|
static String |
printLong(long val)
Converts A long value into a string.
|
static String |
printQName(QName
Converts a QName instance into a string.
|
static String |
printShort(short val)
Converts a short value into a string.
|
static String |
printString(String
Converts the string argument into a string.
|
static String |
printTime(Calendar
Converts a Calendar value into a string.
|
static String |
printUnsignedInt(long val)
Converts a long value into a string.
|
static String |
printUnsignedShort(int val)
Converts an int value into a string.
|
static void |
setDatatypeConverter(DatatypeConverterInterface
This method is for JAXB provider use only.
|
public static void setDatatypeConverter(DatatypeConverterInterfaceconverter)
JAXB Providers are required to call this method at some point before allowing any of the JAXB client marshal or unmarshal operations to occur. This is necessary to configure the datatype converter that should be used to perform the print and parse conversions.
Calling this api repeatedly will have no effect - the DatatypeConverterInterface instance passed into the first invocation is the one that will be used from then on.
converter - an instance of a class that implements the DatatypeConverterInterface class - this parameter must not be null.
IllegalArgumentException - if the parameter is null
SecurityException - If the
SecurityManager in charge denies the access to set the datatype converter.
JAXBPermission
public static StringparseString(String lexicalXSDString)
Convert the lexical XSD string argument into a String value.
lexicalXSDString - A string containing a lexical representation of xsd:string.
public static BigIntegerparseInteger(String lexicalXSDInteger)
Convert the string argument into a BigInteger value.
lexicalXSDInteger - A string containing a lexical representation of xsd:integer.
NumberFormatException -
lexicalXSDInteger is not a valid string representation of a
BigInteger value.
public static int parseInt(StringlexicalXSDInt)
Convert the string argument into an int value.
lexicalXSDInt - A string containing a lexical representation of xsd:int.
NumberFormatException -
lexicalXSDInt is not a valid string representation of an
int value.
public static long parseLong(StringlexicalXSDLong)
Converts the string argument into a long value.
lexicalXSDLong - A string containing lexical representation of xsd:long.
NumberFormatException -
lexicalXSDLong is not a valid string representation of a
long value.
public static short parseShort(StringlexicalXSDShort)
Converts the string argument into a short value.
lexicalXSDShort - A string containing lexical representation of xsd:short.
NumberFormatException -
lexicalXSDShort is not a valid string representation of a
short value.
public static BigDecimalparseDecimal(String lexicalXSDDecimal)
Converts the string argument into a BigDecimal value.
lexicalXSDDecimal - A string containing lexical representation of xsd:decimal.
NumberFormatException -
lexicalXSDDecimal is not a valid string representation of
BigDecimal.
public static float parseFloat(StringlexicalXSDFloat)
Converts the string argument into a float value.
lexicalXSDFloat - A string containing lexical representation of xsd:float.
NumberFormatException -
lexicalXSDFloat is not a valid string representation of a
float value.
public static double parseDouble(StringlexicalXSDDouble)
Converts the string argument into a double value.
lexicalXSDDouble - A string containing lexical representation of xsd:double.
NumberFormatException -
lexicalXSDDouble is not a valid string representation of a
double value.
public static boolean parseBoolean(StringlexicalXSDBoolean)
Converts the string argument into a boolean value.
lexicalXSDBoolean - A string containing lexical representation of xsd:boolean.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:boolean.
public static byte parseByte(StringlexicalXSDByte)
Converts the string argument into a byte value.
lexicalXSDByte - A string containing lexical representation of xsd:byte.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:byte.
public static QNameparseQName(String lexicalXSDQName, NamespaceContext nsc)
Converts the string argument into a byte value.
String parameter lexicalXSDQname must conform to lexical value space specifed at XML Schema Part 2:Datatypes specification:QNames
lexicalXSDQName - A string containing lexical representation of xsd:QName.
nsc - A namespace context for interpreting a prefix within a QName.
IllegalArgumentException - if string parameter does not conform to XML Schema Part 2 specification or if namespace prefix of
lexicalXSDQname is not bound to a URI in NamespaceContext
nsc.
public static CalendarparseDateTime(String lexicalXSDDateTime)
Converts the string argument into a Calendar value.
lexicalXSDDateTime - A string containing lexical representation of xsd:datetime.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:dateTime.
public static byte[] parseBase64Binary(StringlexicalXSDBase64Binary)
Converts the string argument into an array of bytes.
lexicalXSDBase64Binary - A string containing lexical representation of xsd:base64Binary.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:base64Binary
public static byte[] parseHexBinary(StringlexicalXSDHexBinary)
Converts the string argument into an array of bytes.
lexicalXSDHexBinary - A string containing lexical representation of xsd:hexBinary.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:hexBinary.
public static long parseUnsignedInt(StringlexicalXSDUnsignedInt)
Converts the string argument into a long value.
lexicalXSDUnsignedInt - A string containing lexical representation of xsd:unsignedInt.
NumberFormatException - if string parameter can not be parsed into a
long value.
public static int parseUnsignedShort(StringlexicalXSDUnsignedShort)
Converts the string argument into an int value.
lexicalXSDUnsignedShort - A string containing lexical representation of xsd:unsignedShort.
NumberFormatException - if string parameter can not be parsed into an
int value.
public static CalendarparseTime(String lexicalXSDTime)
Converts the string argument into a Calendar value.
lexicalXSDTime - A string containing lexical representation of xsd:time.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:Time.
public static CalendarparseDate(String lexicalXSDDate)
Converts the string argument into a Calendar value.
lexicalXSDDate - A string containing lexical representation of xsd:Date.
IllegalArgumentException - if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:Date.
public static StringparseAnySimpleType(String lexicalXSDAnySimpleType)
Return a string containing the lexical representation of the simple type.
lexicalXSDAnySimpleType - A string containing lexical representation of the simple type.
public static StringprintString(String val)
Converts the string argument into a string.
val - A string value.
public static StringprintInteger(BigInteger val)
Converts a BigInteger value into a string.
val - A BigInteger value
IllegalArgumentException -
val is null.
public static StringprintInt(int val)
Converts an int value into a string.
val - An int value
public static StringprintLong(long val)
Converts A long value into a string.
val - A long value
public static StringprintShort(short val)
Converts a short value into a string.
val - A short value
public static StringprintDecimal(BigDecimal val)
Converts a BigDecimal value into a string.
val - A BigDecimal value
IllegalArgumentException -
val is null.
public static StringprintFloat(float val)
Converts a float value into a string.
val - A float value
public static StringprintDouble(double val)
Converts a double value into a string.
val - A double value
public static StringprintBoolean(boolean val)
Converts a boolean value into a string.
val - A boolean value
public static StringprintByte(byte val)
Converts a byte value into a string.
val - A byte value
public static StringprintQName(QName val, NamespaceContext nsc)
Converts a QName instance into a string.
val - A QName value
nsc - A namespace context for interpreting a prefix within a QName.
IllegalArgumentException - if
val is null or if
nsc is non-null or
nsc.getPrefix(nsprefixFromVal) is null.
public static StringprintDateTime(Calendar val)
Converts a Calendar value into a string.
val - A Calendar value
IllegalArgumentException - if
val is null.
public static StringprintBase64Binary(byte[] val)
Converts an array of bytes into a string.
val - An array of bytes
IllegalArgumentException - if
val is null.
public static StringprintHexBinary(byte[] val)
Converts an array of bytes into a string.
val - An array of bytes
IllegalArgumentException - if
val is null.
public static StringprintUnsignedInt(long val)
Converts a long value into a string.
val - A long value
public static StringprintUnsignedShort(int val)
Converts an int value into a string.
val - An int value
public static StringprintTime(Calendar val)
Converts a Calendar value into a string.
val - A Calendar value
IllegalArgumentException - if
val is null.
public static StringprintDate(Calendar val)
Converts a Calendar value into a string.
val - A Calendar value
IllegalArgumentException - if
val is null.