FileDocCategorySizeDatePackage
DatatypeConverter.javaAPI DocJava SE 6 API22360Tue Jun 10 00:27:04 BST 2008javax.xml.bind

DatatypeConverter

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 {@link DatatypeConverterInterface 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 {@link #setDatatypeConverter(DatatypeConverterInterface) 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.

author
  • Sekhar Vajjhala, Sun Microsystems, Inc.
  • Joe Fialli, Sun Microsystems Inc.
  • Kohsuke Kawaguchi, Sun Microsystems, Inc.
  • Ryan Shoemaker,Sun Microsystems Inc.
version
$Revision$
see
DatatypeConverterInterface
see
ParseConversionEvent
see
PrintConversionEvent
since
JAXB1.0

Fields Summary
private static DatatypeConverterInterface
theConverter
Constructors Summary
private DatatypeConverter()

        
      
        // private constructor
    
Methods Summary
public static java.lang.StringparseAnySimpleType(java.lang.String lexicalXSDAnySimpleType)

Return a string containing the lexical representation of the simple type.

param
lexicalXSDAnySimpleType A string containing lexical representation of the simple type.
return
A string containing the lexical representation of the simple type.

        return theConverter.parseAnySimpleType( lexicalXSDAnySimpleType );
    
public static byte[]parseBase64Binary(java.lang.String lexicalXSDBase64Binary)

Converts the string argument into an array of bytes.

param
lexicalXSDBase64Binary A string containing lexical representation of xsd:base64Binary.
return
An array of bytes represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:base64Binary

        return theConverter.parseBase64Binary( lexicalXSDBase64Binary );
    
public static booleanparseBoolean(java.lang.String lexicalXSDBoolean)

Converts the string argument into a boolean value.

param
lexicalXSDBoolean A string containing lexical representation of xsd:boolean.
return
A boolean value represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:boolean.

        return theConverter.parseBoolean( lexicalXSDBoolean );
    
public static byteparseByte(java.lang.String lexicalXSDByte)

Converts the string argument into a byte value.

param
lexicalXSDByte A string containing lexical representation of xsd:byte.
return
A byte value represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:byte.

 
        return theConverter.parseByte( lexicalXSDByte );
    
public static java.util.CalendarparseDate(java.lang.String lexicalXSDDate)

Converts the string argument into a Calendar value.

param
lexicalXSDDate A string containing lexical representation of xsd:Date.
return
A Calendar value represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:Date.

        return theConverter.parseDate( lexicalXSDDate );
    
public static java.util.CalendarparseDateTime(java.lang.String lexicalXSDDateTime)

Converts the string argument into a Calendar value.

param
lexicalXSDDateTime A string containing lexical representation of xsd:datetime.
return
A Calendar object represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:dateTime.

        return theConverter.parseDateTime( lexicalXSDDateTime );
    
public static java.math.BigDecimalparseDecimal(java.lang.String lexicalXSDDecimal)

Converts the string argument into a BigDecimal value.

param
lexicalXSDDecimal A string containing lexical representation of xsd:decimal.
return
A BigDecimal value represented by the string argument.
throws
NumberFormatException lexicalXSDDecimal is not a valid string representation of {@link java.math.BigDecimal}.

        return theConverter.parseDecimal( lexicalXSDDecimal );
    
public static doubleparseDouble(java.lang.String lexicalXSDDouble)

Converts the string argument into a double value.

param
lexicalXSDDouble A string containing lexical representation of xsd:double.
return
A double value represented by the string argument.
throws
NumberFormatException lexicalXSDDouble is not a valid string representation of a double value.

 
        return theConverter.parseDouble( lexicalXSDDouble );
    
public static floatparseFloat(java.lang.String lexicalXSDFloat)

Converts the string argument into a float value.

param
lexicalXSDFloat A string containing lexical representation of xsd:float.
return
A float value represented by the string argument.
throws
NumberFormatException lexicalXSDFloat is not a valid string representation of a float value.

        return theConverter.parseFloat( lexicalXSDFloat );
    
public static byte[]parseHexBinary(java.lang.String lexicalXSDHexBinary)

Converts the string argument into an array of bytes.

param
lexicalXSDHexBinary A string containing lexical representation of xsd:hexBinary.
return
An array of bytes represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:hexBinary.

        return theConverter.parseHexBinary( lexicalXSDHexBinary );
    
public static intparseInt(java.lang.String lexicalXSDInt)

Convert the string argument into an int value.

param
lexicalXSDInt A string containing a lexical representation of xsd:int.
return
A int value represented by the string argument.
throws
NumberFormatException lexicalXSDInt is not a valid string representation of an int value.

        return theConverter.parseInt( lexicalXSDInt );
    
public static java.math.BigIntegerparseInteger(java.lang.String lexicalXSDInteger)

Convert the string argument into a BigInteger value.

param
lexicalXSDInteger A string containing a lexical representation of xsd:integer.
return
A BigInteger value represented by the string argument.
throws
NumberFormatException lexicalXSDInteger is not a valid string representation of a {@link java.math.BigInteger} value.

        return theConverter.parseInteger( lexicalXSDInteger );
    
public static longparseLong(java.lang.String lexicalXSDLong)

Converts the string argument into a long value.

param
lexicalXSDLong A string containing lexical representation of xsd:long.
return
A long value represented by the string argument.
throws
NumberFormatException lexicalXSDLong is not a valid string representation of a long value.

        return theConverter.parseLong( lexicalXSDLong );
    
public static javax.xml.namespace.QNameparseQName(java.lang.String lexicalXSDQName, javax.xml.namespace.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

param
lexicalXSDQName A string containing lexical representation of xsd:QName.
param
nsc A namespace context for interpreting a prefix within a QName.
return
A QName value represented by the string argument.
throws
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.

        return theConverter.parseQName( lexicalXSDQName, nsc );
    
public static shortparseShort(java.lang.String lexicalXSDShort)

Converts the string argument into a short value.

param
lexicalXSDShort A string containing lexical representation of xsd:short.
return
A short value represented by the string argument.
throws
NumberFormatException lexicalXSDShort is not a valid string representation of a short value.

 
        return theConverter.parseShort( lexicalXSDShort );
    
public static java.lang.StringparseString(java.lang.String lexicalXSDString)

Convert the lexical XSD string argument into a String value.

param
lexicalXSDString A string containing a lexical representation of xsd:string.
return
A String value represented by the string argument.

        return theConverter.parseString( lexicalXSDString );
    
public static java.util.CalendarparseTime(java.lang.String lexicalXSDTime)

Converts the string argument into a Calendar value.

param
lexicalXSDTime A string containing lexical representation of xsd:time.
return
A Calendar value represented by the string argument.
throws
IllegalArgumentException if string parameter does not conform to lexical value space defined in XML Schema Part 2: Datatypes for xsd:Time.

        return theConverter.parseTime( lexicalXSDTime ); 
    
public static longparseUnsignedInt(java.lang.String lexicalXSDUnsignedInt)

Converts the string argument into a long value.

param
lexicalXSDUnsignedInt A string containing lexical representation of xsd:unsignedInt.
return
A long value represented by the string argument.
throws
NumberFormatException if string parameter can not be parsed into a long value.

        return theConverter.parseUnsignedInt( lexicalXSDUnsignedInt );
    
public static intparseUnsignedShort(java.lang.String lexicalXSDUnsignedShort)

Converts the string argument into an int value.

param
lexicalXSDUnsignedShort A string containing lexical representation of xsd:unsignedShort.
return
An int value represented by the string argument.
throws
NumberFormatException if string parameter can not be parsed into an int value.

        return theConverter.parseUnsignedShort( lexicalXSDUnsignedShort );
    
public static java.lang.StringprintAnySimpleType(java.lang.String val)

Converts a string value into a string.

param
val A string value
return
A string containing a lexical representation of xsd:AnySimpleType

        return theConverter.printAnySimpleType( val );
    
public static java.lang.StringprintBase64Binary(byte[] val)

Converts an array of bytes into a string.

param
val An array of bytes
return
A string containing a lexical representation of xsd:base64Binary
throws
IllegalArgumentException if val is null.

        return theConverter.printBase64Binary( val );
    
public static java.lang.StringprintBoolean(boolean val)

Converts a boolean value into a string.

param
val A boolean value
return
A string containing a lexical representation of xsd:boolean

        return theConverter.printBoolean( val );
    
public static java.lang.StringprintByte(byte val)

Converts a byte value into a string.

param
val A byte value
return
A string containing a lexical representation of xsd:byte

        return theConverter.printByte( val );
    
public static java.lang.StringprintDate(java.util.Calendar val)

Converts a Calendar value into a string.

param
val A Calendar value
return
A string containing a lexical representation of xsd:date
throws
IllegalArgumentException if val is null.

        return theConverter.printDate( val );
    
public static java.lang.StringprintDateTime(java.util.Calendar val)

Converts a Calendar value into a string.

param
val A Calendar value
return
A string containing a lexical representation of xsd:dateTime
throws
IllegalArgumentException if val is null.

        return theConverter.printDateTime( val );
    
public static java.lang.StringprintDecimal(java.math.BigDecimal val)

Converts a BigDecimal value into a string.

param
val A BigDecimal value
return
A string containing a lexical representation of xsd:decimal
throws
IllegalArgumentException val is null.

        return theConverter.printDecimal( val );
    
public static java.lang.StringprintDouble(double val)

Converts a double value into a string.

param
val A double value
return
A string containing a lexical representation of xsd:double

        return theConverter.printDouble( val );
    
public static java.lang.StringprintFloat(float val)

Converts a float value into a string.

param
val A float value
return
A string containing a lexical representation of xsd:float

        return theConverter.printFloat( val );
    
public static java.lang.StringprintHexBinary(byte[] val)

Converts an array of bytes into a string.

param
val An array of bytes
return
A string containing a lexical representation of xsd:hexBinary
throws
IllegalArgumentException if val is null.

        return theConverter.printHexBinary( val );
    
public static java.lang.StringprintInt(int val)

Converts an int value into a string.

param
val An int value
return
A string containing a lexical representation of xsd:int

        return theConverter.printInt( val );
    
public static java.lang.StringprintInteger(java.math.BigInteger val)

Converts a BigInteger value into a string.

param
val A BigInteger value
return
A string containing a lexical representation of xsd:integer
throws
IllegalArgumentException val is null.

        return theConverter.printInteger( val );
    
public static java.lang.StringprintLong(long val)

Converts A long value into a string.

param
val A long value
return
A string containing a lexical representation of xsd:long

        return theConverter.printLong( val );
    
public static java.lang.StringprintQName(javax.xml.namespace.QName val, javax.xml.namespace.NamespaceContext nsc)

Converts a QName instance into a string.

param
val A QName value
param
nsc A namespace context for interpreting a prefix within a QName.
return
A string containing a lexical representation of QName
throws
IllegalArgumentException if val is null or if nsc is non-null or nsc.getPrefix(nsprefixFromVal) is null.

        return theConverter.printQName( val, nsc );
    
public static java.lang.StringprintShort(short val)

Converts a short value into a string.

param
val A short value
return
A string containing a lexical representation of xsd:short

        return theConverter.printShort( val );
    
public static java.lang.StringprintString(java.lang.String val)

Converts the string argument into a string.

param
val A string value.
return
A string containing a lexical representation of xsd:string.

        return theConverter.printString( val );
    
public static java.lang.StringprintTime(java.util.Calendar val)

Converts a Calendar value into a string.

param
val A Calendar value
return
A string containing a lexical representation of xsd:time
throws
IllegalArgumentException if val is null.

        return theConverter.printTime( val );
    
public static java.lang.StringprintUnsignedInt(long val)

Converts a long value into a string.

param
val A long value
return
A string containing a lexical representation of xsd:unsignedInt

        return theConverter.printUnsignedInt( val );
    
public static java.lang.StringprintUnsignedShort(int val)

Converts an int value into a string.

param
val An int value
return
A string containing a lexical representation of xsd:unsignedShort

        return theConverter.printUnsignedShort( val );
    
public static voidsetDatatypeConverter(javax.xml.bind.DatatypeConverterInterface converter)
This method is for JAXB provider use only.

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.

param
converter an instance of a class that implements the DatatypeConverterInterface class - this parameter must not be null.
throws
IllegalArgumentException if the parameter is null

        if( converter == null ) {
            throw new IllegalArgumentException( 
                Messages.format( Messages.CONVERTER_MUST_NOT_BE_NULL ) );
        } else if( theConverter == null ) {
            theConverter = converter;
        }