- All Implemented Interfaces:
DatatypeConverterInterface
DatatypeConverterInterface
.
When client applications specify the use of the static print/parse
methods in DatatypeConverter
, it will delegate
to this class.
This class is responsible for whitespace normalization.
- Since:
- JAXB 2.1
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final DatatypeFactory
private static final byte[]
private static final char[]
private static final char[]
private static final byte
static final DatatypeConverterInterface
To avoid re-creating instances, we cache one instance. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]
_parseBase64Binary
(String text) static Boolean
_parseBoolean
(CharSequence literal) static byte
_parseByte
(CharSequence literal) static GregorianCalendar
static BigDecimal
_parseDecimal
(CharSequence content) static double
_parseDouble
(CharSequence _val) static float
_parseFloat
(CharSequence _val) static int
Faster but less robustString->int
conversion.static BigInteger
static long
static QName
_parseQName
(CharSequence text, NamespaceContext nsc) static short
static String
_printBase64Binary
(byte[] input) static String
_printBase64Binary
(byte[] input, int offset, int len) static int
_printBase64Binary
(byte[] input, int offset, int len, byte[] out, int ptr) Encodes a byte array into another byte array by first doing base64 encoding then encoding the result in ASCII.static int
_printBase64Binary
(byte[] input, int offset, int len, char[] buf, int ptr) Encodes a byte array into a char array by doing base64 encoding.static String
_printBoolean
(boolean val) static String
_printByte
(byte val) static String
_printDate
(Calendar val) static String
_printDateTime
(Calendar val) static String
_printDecimal
(BigDecimal val) static String
_printDouble
(double v) static String
_printFloat
(float v) static String
_printInt
(int val) static String
_printInteger
(BigInteger val) static String
_printLong
(long val) static String
_printQName
(QName val, NamespaceContext nsc) static String
_printShort
(short val) static char
encode
(int i) static byte
encodeByte
(int i) private static int
guessLength
(String text) computes the length of binary data speculatively.private static int
hexToBin
(char ch) private static byte[]
private static char[]
static String
Just return the string passed as a parameter but installs an instance of this class as the DatatypeConverter implementation.private static boolean
isDigitOrPeriodOrSign
(char ch) parseAnySimpleType
(String lexicalXSDAnySimpleType) Return a string containing the lexical representation of the simple type.byte[]
parseBase64Binary
(String lexicalXSDBase64Binary) Converts the string argument into an array of bytes.boolean
parseBoolean
(String lexicalXSDBoolean) Converts the string argument into a boolean value.byte
Converts the string argument into a byte value.Converts the string argument into a Calendar value.parseDateTime
(String lexicalXSDDateTime) Converts the string argument into a Calendar value.parseDecimal
(String content) Converts the string argument into a BigDecimal value.double
parseDouble
(String lexicalXSDDouble) Converts the string argument into a double value.float
parseFloat
(String lexicalXSDFloat) Converts the string argument into a float value.byte[]
Converts the string argument into an array of bytes.int
Convert the string argument into an int value.parseInteger
(String lexicalXSDInteger) Convert the string argument into a BigInteger value.long
Converts the string argument into a long value.parseQName
(String lexicalXSDQName, NamespaceContext nsc) Converts the string argument into a QName value.short
parseShort
(String lexicalXSDShort) Converts the string argument into a short value.parseString
(String lexicalXSDString) Convert the string argument into a string.Converts the string argument into a Calendar value.long
parseUnsignedInt
(String lexicalXSDUnsignedInt) Converts the string argument into a long value.int
parseUnsignedShort
(String lexicalXSDUnsignedShort) Converts the string argument into an int value.printAnySimpleType
(String val) Converts a string value into a string.printBase64Binary
(byte[] val) Converts an array of bytes into a string.printBoolean
(boolean val) Converts a boolean value into a string.printByte
(byte val) Converts a byte value into a string.Converts a Calendar value into a string.printDateTime
(Calendar val) Converts a Calendar value into a string.printDecimal
(BigDecimal val) Converts a BigDecimal value into a string.printDouble
(double v) Converts a double value into a string.printFloat
(float v) Converts a float value into a string.printHexBinary
(byte[] data) Converts an array of bytes into a string.printInt
(int val) Converts an int value into a string.printInteger
(BigInteger val) Converts a BigInteger value into a string.printLong
(long val) Converts a long value into a string.printQName
(QName val, NamespaceContext nsc) Converts a QName instance into a string.printShort
(short val) Converts a short value into a string.printString
(String val) Converts the string argument into a string.Converts a Calendar value into a string.printUnsignedInt
(long val) Converts a long value into a string.printUnsignedShort
(int val) Converts an int value into a string.private static CharSequence
-
Field Details
-
theInstance
To avoid re-creating instances, we cache one instance. -
hexCode
private static final char[] hexCode -
decodeMap
private static final byte[] decodeMap -
PADDING
private static final byte PADDING- See Also:
-
encodeMap
private static final char[] encodeMap -
datatypeFactory
-
-
Constructor Details
-
DatatypeConverterImpl
protected DatatypeConverterImpl()
-
-
Method Details
-
parseString
Description copied from interface:DatatypeConverterInterface
Convert the string argument into a string.- Specified by:
parseString
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDString
- A lexical representation of the XML Schema datatype xsd:string- Returns:
- A string that is the same as the input string.
-
parseInteger
Description copied from interface:DatatypeConverterInterface
Convert the string argument into a BigInteger value.- Specified by:
parseInteger
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDInteger
- A string containing a lexical representation of xsd:integer.- Returns:
- A BigInteger value represented by the string argument.
-
_parseInteger
-
printInteger
Description copied from interface:DatatypeConverterInterface
Converts a BigInteger value into a string.- Specified by:
printInteger
in interfaceDatatypeConverterInterface
- Parameters:
val
- A BigInteger value- Returns:
- A string containing a lexical representation of xsd:integer
-
_printInteger
-
parseInt
Description copied from interface:DatatypeConverterInterface
Convert the string argument into an int value.- Specified by:
parseInt
in interfaceDatatypeConverterInterface
- Parameters:
s
- A string containing a lexical representation of xsd:int.- Returns:
- An int value represented byte the string argument.
-
_parseInt
Faster but less robustString->int
conversion.Note that:
- XML Schema allows '+', but
Integer.valueOf(String)
is not. - XML Schema allows leading and trailing (but not in-between) whitespaces.
Integer.valueOf(String)
doesn't allow any.
- XML Schema allows '+', but
-
parseLong
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a long value.- Specified by:
parseLong
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSLong
- A string containing lexical representation of xsd:long.- Returns:
- A long value represented by the string argument.
-
_parseLong
-
parseShort
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a short value.- Specified by:
parseShort
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDShort
- A string containing lexical representation of xsd:short.- Returns:
- A short value represented by the string argument.
-
_parseShort
-
printShort
Description copied from interface:DatatypeConverterInterface
Converts a short value into a string.- Specified by:
printShort
in interfaceDatatypeConverterInterface
- Parameters:
val
- A short value- Returns:
- A string containing a lexical representation of xsd:short
-
_printShort
-
parseDecimal
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a BigDecimal value.- Specified by:
parseDecimal
in interfaceDatatypeConverterInterface
- Parameters:
content
- A string containing lexical representation of xsd:decimal.- Returns:
- A BigDecimal value represented by the string argument.
-
_parseDecimal
-
parseFloat
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a float value.- Specified by:
parseFloat
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDFloat
- A string containing lexical representation of xsd:float.- Returns:
- A float value represented by the string argument.
-
_parseFloat
-
printFloat
Description copied from interface:DatatypeConverterInterface
Converts a float value into a string.- Specified by:
printFloat
in interfaceDatatypeConverterInterface
- Parameters:
v
- A float value- Returns:
- A string containing a lexical representation of xsd:float
-
_printFloat
-
parseDouble
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a double value.- Specified by:
parseDouble
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDDouble
- A string containing lexical representation of xsd:double.- Returns:
- A double value represented by the string argument.
-
_parseDouble
-
parseBoolean
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a boolean value.- Specified by:
parseBoolean
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDBoolean
- A string containing lexical representation of xsd:boolean.- Returns:
- A boolean value represented by the string argument.
-
_parseBoolean
-
printBoolean
Description copied from interface:DatatypeConverterInterface
Converts a boolean value into a string.- Specified by:
printBoolean
in interfaceDatatypeConverterInterface
- Parameters:
val
- A boolean value- Returns:
- A string containing a lexical representation of xsd:boolean
-
_printBoolean
-
parseByte
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a byte value.- Specified by:
parseByte
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDByte
- A string containing lexical representation of xsd:byte.- Returns:
- A byte value represented by the string argument.
-
_parseByte
-
printByte
Description copied from interface:DatatypeConverterInterface
Converts a byte value into a string.- Specified by:
printByte
in interfaceDatatypeConverterInterface
- Parameters:
val
- A byte value- Returns:
- A string containing a lexical representation of xsd:byte
-
_printByte
-
parseQName
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a QName value.String parameter
lexicalXSDQname
must conform to lexical value space specifed at XML Schema Part 2:Datatypes specification:QNames- Specified by:
parseQName
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDQName
- A string containing lexical representation of xsd:QName.nsc
- A namespace context for interpreting a prefix within a QName.- Returns:
- A QName value represented by the string argument.
-
_parseQName
- Returns:
- null if fails to convert.
-
parseDateTime
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a Calendar value.- Specified by:
parseDateTime
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDDateTime
- A string containing lexical representation of xsd:datetime.- Returns:
- A Calendar object represented by the string argument.
-
_parseDateTime
-
printDateTime
Description copied from interface:DatatypeConverterInterface
Converts a Calendar value into a string.- Specified by:
printDateTime
in interfaceDatatypeConverterInterface
- Parameters:
val
- A Calendar value- Returns:
- A string containing a lexical representation of xsd:dateTime
-
_printDateTime
-
parseBase64Binary
Description copied from interface:DatatypeConverterInterface
Converts the string argument into an array of bytes.- Specified by:
parseBase64Binary
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDBase64Binary
- A string containing lexical representation of xsd:base64Binary.- Returns:
- An array of bytes represented by the string argument.
-
parseHexBinary
Description copied from interface:DatatypeConverterInterface
Converts the string argument into an array of bytes.- Specified by:
parseHexBinary
in interfaceDatatypeConverterInterface
- Parameters:
s
- A string containing lexical representation of xsd:hexBinary.- Returns:
- An array of bytes represented by the string argument.
-
hexToBin
private static int hexToBin(char ch) -
printHexBinary
Description copied from interface:DatatypeConverterInterface
Converts an array of bytes into a string.- Specified by:
printHexBinary
in interfaceDatatypeConverterInterface
- Parameters:
data
- an array of bytes- Returns:
- A string containing a lexical representation of xsd:hexBinary
-
parseUnsignedInt
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a long value.- Specified by:
parseUnsignedInt
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDUnsignedInt
- A string containing lexical representation of xsd:unsignedInt.- Returns:
- A long value represented by the string argument.
-
printUnsignedInt
Description copied from interface:DatatypeConverterInterface
Converts a long value into a string.- Specified by:
printUnsignedInt
in interfaceDatatypeConverterInterface
- Parameters:
val
- A long value- Returns:
- A string containing a lexical representation of xsd:unsignedInt
-
parseUnsignedShort
Description copied from interface:DatatypeConverterInterface
Converts the string argument into an int value.- Specified by:
parseUnsignedShort
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDUnsignedShort
- A string containing lexical representation of xsd:unsignedShort.- Returns:
- An int value represented by the string argument.
-
parseTime
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a Calendar value.- Specified by:
parseTime
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDTime
- A string containing lexical representation of xsd:Time.- Returns:
- A Calendar value represented by the string argument.
-
printTime
Description copied from interface:DatatypeConverterInterface
Converts a Calendar value into a string.- Specified by:
printTime
in interfaceDatatypeConverterInterface
- Parameters:
val
- A Calendar value- Returns:
- A string containing a lexical representation of xsd:time
-
parseDate
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a Calendar value.- Specified by:
parseDate
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDDate
- A string containing lexical representation of xsd:Date.- Returns:
- A Calendar value represented by the string argument.
-
printDate
Description copied from interface:DatatypeConverterInterface
Converts a Calendar value into a string.- Specified by:
printDate
in interfaceDatatypeConverterInterface
- Parameters:
val
- A Calendar value- Returns:
- A string containing a lexical representation of xsd:date
-
_printDate
-
parseAnySimpleType
Description copied from interface:DatatypeConverterInterface
Return a string containing the lexical representation of the simple type.- Specified by:
parseAnySimpleType
in interfaceDatatypeConverterInterface
- Parameters:
lexicalXSDAnySimpleType
- A string containing lexical representation of the simple type.- Returns:
- A string containing the lexical representation of the simple type.
-
printString
Description copied from interface:DatatypeConverterInterface
Converts the string argument into a string.- Specified by:
printString
in interfaceDatatypeConverterInterface
- Parameters:
val
- A string value.- Returns:
- A string containing a lexical representation of xsd:string
-
printInt
Description copied from interface:DatatypeConverterInterface
Converts an int value into a string.- Specified by:
printInt
in interfaceDatatypeConverterInterface
- Parameters:
val
- An int value- Returns:
- A string containing a lexical representation of xsd:int
-
_printInt
-
printLong
Description copied from interface:DatatypeConverterInterface
Converts a long value into a string.- Specified by:
printLong
in interfaceDatatypeConverterInterface
- Parameters:
val
- A long value- Returns:
- A string containing a lexical representation of xsd:long
-
_printLong
-
printDecimal
Description copied from interface:DatatypeConverterInterface
Converts a BigDecimal value into a string.- Specified by:
printDecimal
in interfaceDatatypeConverterInterface
- Parameters:
val
- A BigDecimal value- Returns:
- A string containing a lexical representation of xsd:decimal
-
_printDecimal
-
printDouble
Description copied from interface:DatatypeConverterInterface
Converts a double value into a string.- Specified by:
printDouble
in interfaceDatatypeConverterInterface
- Parameters:
v
- A double value- Returns:
- A string containing a lexical representation of xsd:double
-
_printDouble
-
printQName
Description copied from interface:DatatypeConverterInterface
Converts a QName instance into a string.- Specified by:
printQName
in interfaceDatatypeConverterInterface
- Parameters:
val
- A QName valuensc
- A namespace context for interpreting a prefix within a QName.- Returns:
- A string containing a lexical representation of QName
-
_printQName
-
printBase64Binary
Description copied from interface:DatatypeConverterInterface
Converts an array of bytes into a string.- Specified by:
printBase64Binary
in interfaceDatatypeConverterInterface
- Parameters:
val
- an array of bytes- Returns:
- A string containing a lexical representation of xsd:base64Binary
-
printUnsignedShort
Description copied from interface:DatatypeConverterInterface
Converts an int value into a string.- Specified by:
printUnsignedShort
in interfaceDatatypeConverterInterface
- Parameters:
val
- An int value- Returns:
- A string containing a lexical representation of xsd:unsignedShort
-
printAnySimpleType
Description copied from interface:DatatypeConverterInterface
Converts a string value into a string.- Specified by:
printAnySimpleType
in interfaceDatatypeConverterInterface
- Parameters:
val
- A string value- Returns:
- A string containing a lexical representation of xsd:AnySimpleType
-
installHook
Just return the string passed as a parameter but installs an instance of this class as the DatatypeConverter implementation. Used from static fixed value initializers. -
initDecodeMap
private static byte[] initDecodeMap() -
guessLength
computes the length of binary data speculatively.Our requirement is to create byte[] of the exact length to store the binary data. If we do this in a straight-forward way, it takes two passes over the data. Experiments show that this is a non-trivial overhead (35% or so is spent on the first pass in calculating the length.)
So the approach here is that we compute the length speculatively, without looking at the whole contents. The obtained speculative value is never less than the actual length of the binary data, but it may be bigger. So if the speculation goes wrong, we'll pay the cost of reallocation and buffer copying.
If the base64 text is tightly packed with no indentation nor illegal char (like what most web services produce), then the speculation of this method will be correct, so we get the performance benefit.
-
_parseBase64Binary
- Parameters:
text
- base64Binary data is likely to be long, and decoding requires each character to be accessed twice (once for counting length, another for decoding.)A benchmark showed that taking
String
is faster, presumably because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast)
-
initEncodeMap
private static char[] initEncodeMap() -
encode
public static char encode(int i) -
encodeByte
public static byte encodeByte(int i) -
_printBase64Binary
-
_printBase64Binary
-
_printBase64Binary
public static int _printBase64Binary(byte[] input, int offset, int len, char[] buf, int ptr) Encodes a byte array into a char array by doing base64 encoding.The caller must supply a big enough buffer.
- Returns:
- the value of
ptr+((len+2)/3)*4
, which is the new offset in the output buffer where the further bytes should be placed.
-
_printBase64Binary
public static int _printBase64Binary(byte[] input, int offset, int len, byte[] out, int ptr) Encodes a byte array into another byte array by first doing base64 encoding then encoding the result in ASCII.The caller must supply a big enough buffer.
- Returns:
- the value of
ptr+((len+2)/3)*4
, which is the new offset in the output buffer where the further bytes should be placed.
-
removeOptionalPlus
-
isDigitOrPeriodOrSign
private static boolean isDigitOrPeriodOrSign(char ch)
-