Package org.dellroad.stuff.jibx
Class ParseUtil
java.lang.Object
org.dellroad.stuff.jibx.ParseUtil
JiBX parsing utility methods. These methods can be used as JiBX value serializer/deserializer methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
deserializeBoolean
(String string) Boolean parser that allows "yes" and "no" as well as the usual "true", "false", "0", "1".static boolean
deserializeBooleanStrictly
(String string) Deserialize a boolean strictly, only allowing the valuestrue
orfalse
.static byte[]
deserializeByteArray
(String string) Deserialize a byte array usingByteArrayEncoder
.static byte[]
deserializeByteArrayWithColons
(String string) Deserialize a byte array using MAC address notation (colon-separated).static Date
deserializeDate
(String date) Deserialize aDate
in the format supported byDateEncoder
.static Date
deserializeDate
(String string, String format) Deserialize aDate
.static Inet4Address
deserializeInet4Address
(String string) Deserialize anInet4Address
.static int[]
deserializeIntArray
(String string) Deserialize an array of integers separated by commas and/or whitespace.static String
deserializeMatching
(String regex, String string) JiBXString
deserializer support method that verifies that the input string matches the given regular expression.static Pattern
deserializePattern
(String string) Deserialize aPattern
.static <T> T
deserializeReference
(String string, Class<T> type) Deserialize an object by reference.static SimpleDateFormat
deserializeSimpleDateFormat
(String string) Deserialize aSimpleDateFormat
.static String
deserializeString
(String string) Deserialize aString
, allowing arbitrary characters via backslash escapes.static long
deserializeTimeInterval
(String string) Derialize a millisecond-granularity time interval, e.g., "30s", "1d12h", "0.250s", etc.static TimeZone
deserializeTimeZone
(String string) Deserialize aTimeZone
.static URI
deserializeURI
(String string) Deserialize anURI
.static UUID
deserializeUUID
(String string) Deserialize aUUID
.static Date
deserializeXSDDateTime
(String date) Deserialize aDate
in XSD dateTime format.static String
JiBXString
deserializer that normalizes a string as is required by thexsd:token
XSD type.static String
serializeByteArray
(byte[] array) Serialize a byte array usingByteArrayEncoder
.static String
serializeByteArrayWithColons
(byte[] array) Serialize a byte array using MAC address notation (colon-separated).static String
serializeDate
(Date date) Serialize aDate
in the format supported byDateEncoder
.static String
serializeDate
(Date date, String format) Serialize aDate
.static String
Serialize anInet4Address
.static String
serializeIntArray
(int[] array) Serialize an array of integers.static String
serializeReference
(Object obj) Serialize an object by reference.static String
Serialize aSimpleDateFormat
.static String
serializeString
(String string) Serialize aString
.static String
serializeTimeInterval
(long value) Serialize a millisecond-granularity time interval, e.g., "30s", "1d12h", "0.250s", etc.static String
serializeTimeZone
(TimeZone timeZone) Serialize aTimeZone
.static String
serializeXSDDateTime
(Date date) Serialize aDate
to XSD dateTime format.
-
Method Details
-
deserializeString
Deserialize aString
, allowing arbitrary characters via backslash escapes. The string will be decoded byStringEncoder.decode(java.lang.String)
.- Parameters:
string
- XML encoding created byserializeString(java.lang.String)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeString
Serialize aString
. The string will be encoded byStringEncoder.encode(java.lang.String, boolean)
.- Parameters:
string
- value to encode for XML- Returns:
- encoded value
- See Also:
-
deserializeTimeZone
Deserialize aTimeZone
.- Parameters:
string
- XML encoding created byserializeTimeZone(java.util.TimeZone)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeTimeZone
Serialize aTimeZone
.- Parameters:
timeZone
- value to encode for XML- Returns:
- encoded value
- See Also:
-
deserializeURI
Deserialize anURI
.Note: there is no need for a custom serializer, as
URI.toString()
already does the right thing.- Parameters:
string
- XML encoding created byURI.toString()
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails
-
deserializeReference
Deserialize an object by reference.Invoke this method from your own custom deserializer to produce an result of the correct type.
The object must have been unmarshalled already and had its ID registered via
IdMapper.setId(java.lang.Object, java.lang.String)
.- Type Parameters:
T
- expected type- Parameters:
string
- XML encoding created byserializeReference(java.lang.Object)
type
- expected type- Returns:
- decoded value
- Throws:
IllegalArgumentException
- ifstring
cannot be parsedJiBXParseException
- if the reference has not been registered to any object yet (e.g., forward reference)JiBXParseException
- if the referenced object is not an instance oftype
- See Also:
-
serializeReference
Serialize an object by reference.The object must have been marshalled already and had its ID assigned via
IdMapper.getId(java.lang.Object)
.- Parameters:
obj
- object to encode by reference for XML- Returns:
- encoded reference
- Throws:
IllegalArgumentException
- if the object has not been registered yet (e.g., forward reference)- See Also:
-
deserializeUUID
Deserialize aUUID
.Note: there is no need for a custom serializer, as
UUID.toString()
already does the right thing.- Parameters:
string
- XML encoding created byUUID.toString()
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails
-
deserializeTimeInterval
Derialize a millisecond-granularity time interval, e.g., "30s", "1d12h", "0.250s", etc.- Parameters:
string
- XML encoding created byserializeTimeInterval(long)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeTimeInterval
Serialize a millisecond-granularity time interval, e.g., "30s", "1d12h", "0.250s", etc.- Parameters:
value
- millisecond value to encode- Returns:
- encoded value
- See Also:
-
deserializeByteArray
Deserialize a byte array usingByteArrayEncoder
.- Parameters:
string
- XML encoding created byserializeByteArray(byte[])
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeByteArray
Serialize a byte array usingByteArrayEncoder
.- Parameters:
array
- array to encode- Returns:
- encoded array
- See Also:
-
deserializeByteArrayWithColons
Deserialize a byte array using MAC address notation (colon-separated). Each byte must have two digits.- Parameters:
string
- XML encoding created byserializeByteArrayWithColons(byte[])
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeByteArrayWithColons
Serialize a byte array using MAC address notation (colon-separated). Each byte will have two digits.- Parameters:
array
- array to encode- Returns:
- encoded array
- See Also:
-
deserializeInet4Address
Deserialize anInet4Address
. No DNS name resolution of any kind is performed.- Parameters:
string
- XML encoding created byserializeInet4Address(java.net.Inet4Address)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeInet4Address
Serialize anInet4Address
.- Parameters:
addr
- address to encode- Returns:
- encoded address
- See Also:
-
deserializeSimpleDateFormat
Deserialize aSimpleDateFormat
.- Parameters:
string
- XML encoding created byserializeSimpleDateFormat(java.text.SimpleDateFormat)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeSimpleDateFormat
Serialize aSimpleDateFormat
.- Parameters:
format
- date format to encode- Returns:
- encoded format
- See Also:
-
deserializeDate
Deserialize aDate
. This method can be used as a deserialize support method.- Parameters:
string
- XML encoding created byserializeDate(Date, String)
format
- format forSimpleDateFormat
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeDate
Serialize aDate
. This method can be used as a serialize support method.- Parameters:
date
- date to serializeformat
- format forSimpleDateFormat
.- Returns:
- encoded date
- Throws:
JiBXParseException
- ifformat
is invalid- See Also:
-
deserializeDate
Deserialize aDate
in the format supported byDateEncoder
.- Parameters:
date
- XML encoding created byserializeDate(Date)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeDate
Serialize aDate
in the format supported byDateEncoder
.- Parameters:
date
- date to encode- Returns:
- encoded date
- See Also:
-
deserializeXSDDateTime
Deserialize aDate
in XSD dateTime format.- Parameters:
date
- XML encoding created byserializeXSDDateTime(java.util.Date)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeXSDDateTime
Serialize aDate
to XSD dateTime format.- Parameters:
date
- date to encode- Returns:
- encoded date
- See Also:
-
deserializePattern
Deserialize aPattern
.Note: there is no need for a custom serializer, as
Pattern.toString()
already does the right thing.- Parameters:
string
- XML encoding created byPattern.toString()
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails
-
normalize
JiBXString
deserializer that normalizes a string as is required by thexsd:token
XSD type. This removes leading and trailing whitespace, and collapses all interior whitespace down to a single space character.- Parameters:
string
- string to normalize- Returns:
- normalized string
- Throws:
NullPointerException
- ifstring
is null
-
deserializeMatching
JiBXString
deserializer support method that verifies that the input string matches the given regular expression. This method can be invoked by custom deserializers that supply the regular expression to it.- Parameters:
regex
- regular expression pattern which input must matchstring
- input string- Returns:
- decoded value
- Throws:
NullPointerException
- ifstring
ofregex
is nullJiBXParseException
- ifstring
does not matchregex
PatternSyntaxException
- ifregex
is not a valid regular expression
-
deserializeBoolean
Boolean parser that allows "yes" and "no" as well as the usual "true", "false", "0", "1". Comparisons are case-insensitive.- Parameters:
string
- XML encoding of boolean value- Returns:
- decoded value
- Throws:
JiBXParseException
- if the value is not recognizable as a boolean- See Also:
-
deserializeBooleanStrictly
Deserialize a boolean strictly, only allowing the valuestrue
orfalse
.- Parameters:
string
- XML encoding created byBoolean.toString(boolean)
- Returns:
- decoded value
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
deserializeIntArray
Deserialize an array of integers separated by commas and/or whitespace.- Parameters:
string
- XML encoding created byserializeIntArray(int[])
- Returns:
- decoded integer array
- Throws:
JiBXParseException
- if the parse fails- See Also:
-
serializeIntArray
Serialize an array of integers. Example: "1, 2, 3".- Parameters:
array
- integer array- Returns:
- string encoding of
array
- See Also:
-